View Javadoc
1   package wjhk.jupload2.context;
2   
3   import java.awt.Cursor;
4   import java.awt.Frame;
5   import java.text.MessageFormat;
6   import java.util.Date;
7   import java.util.Properties;
8   import java.util.Vector;
9   
10  import javax.swing.JWindow;
11  import javax.swing.RootPaneContainer;
12  
13  import org.junit.Assert;
14  import org.junit.Before;
15  import org.junit.Test;
16  
17  import wjhk.jupload2.exception.JUploadException;
18  import wjhk.jupload2.gui.JUploadPanel;
19  import wjhk.jupload2.policies.UploadPolicy;
20  import wjhk.jupload2.testhelpers.UploadPolicyTestHelper;
21  import wjhk.jupload2.upload.AbstractJUploadTestHelper;
22  
23  /**
24   * A test to trap calls to jsHandlers, and check what DefaultJUploadContext
25   * actually does.
26   * 
27   * @author etienne_sf
28   */
29  class JavascriptHandlerTestHelper extends JavascriptHandler {
30  	String lastCommandReceived = null;
31  
32  	public JavascriptHandlerTestHelper(UploadPolicy uploadPolicy,
33  			JUploadPanel theJUploadPanel) {
34  		super(uploadPolicy, theJUploadPanel);
35  	}
36  
37  	public synchronized String doCommand(String command) {
38  		this.lastCommandReceived = command;
39  		return RETURN_STARTED;
40  	}
41  }
42  
43  /**
44   * @author etienne_sf
45   */
46  public class DefaultJUploadContextTest extends AbstractJUploadTestHelper {
47  
48  	Frame frame = new Frame();
49  
50  	RootPaneContainer rootPaneContainer = new JWindow();
51  
52  	DefaultJUploadContext defaultJUploadContext = null;
53  
54  	boolean unloadHasBeenExecuted = false;
55  
56  	/** */
57  	@Before
58  	public void setUp() {
59  		this.defaultJUploadContext = new DefaultJUploadContext();
60  		this.defaultJUploadContext.uploadPolicy = this.uploadPolicy;
61  		((UploadPolicyTestHelper) this.uploadPolicy).uploadContext = this.juploadContext;
62  		this.defaultJUploadContext.init(this.frame, this.rootPaneContainer);
63  
64  		this.juploadContext = this.defaultJUploadContext;
65  	}
66  
67  	/** Test of attributes default values */
68  	@Test
69  	public void testAttributeCreation() {
70  		Assert.assertNotNull("svn Properties",
71  				this.defaultJUploadContext.svnProperties);
72  		Assert.assertNotNull("svn Properties (jupload.version)",
73  				this.defaultJUploadContext.svnProperties
74  						.getProperty("jupload.version"));
75  		Assert.assertNotNull("svn Properties (jupload.svn.revision)",
76  				this.defaultJUploadContext.svnProperties
77  						.getProperty("jupload.svn.revision"));
78  		Assert.assertNotNull("svn Properties (jupload.buildTimestamp)",
79  				this.defaultJUploadContext.svnProperties
80  						.getProperty("jupload.buildTimestamp"));
81  
82  		Assert.assertNotNull("unloadCallbacks",
83  				this.defaultJUploadContext.unloadCallbacks);
84  	}
85  
86  	/** */
87  	@Test
88  	public void testInit() {
89  		Assert.assertNotNull("uploadPolicy must be set",
90  				this.defaultJUploadContext.uploadPolicy);
91  		Assert
92  				.assertEquals(
93  						"Check upload policy (mainly to be sure that next tests will work: there must be only one uploadPolicy in the system)",
94  						this.uploadPolicy,
95  						this.defaultJUploadContext.uploadPolicy);
96  		Assert.assertNotNull("logWindow must be set",
97  				this.defaultJUploadContext.logWindow);
98  		Assert.assertNotNull("jUploadPanel must be set",
99  				this.defaultJUploadContext.jUploadPanel);
100 		Assert.assertEquals("rootPaneContainer",
101 				this.defaultJUploadContext.jUploadPanel, this.rootPaneContainer
102 						.getContentPane());
103 		Assert.assertNotNull("jsHandler must be set",
104 				this.defaultJUploadContext.jsHandler);
105 		Assert.assertTrue("jsHandler must be alive",
106 				this.defaultJUploadContext.jsHandler.isAlive());
107 		Assert.assertTrue("The thread must be a daemon",
108 				this.defaultJUploadContext.jsHandler.isDaemon());
109 
110 		Assert
111 				.assertEquals(
112 						"unloadCallbacks must contain one item: for the current context",
113 						1, this.defaultJUploadContext.unloadCallbacks.size());
114 		Assert
115 				.assertEquals(
116 						"unloadCallbacks must contain one item: for the current context",
117 						this.defaultJUploadContext,
118 						this.defaultJUploadContext.unloadCallbacks.get(0)
119 								.getObject());
120 	}
121 
122 	/**
123 	 * @throws InterruptedException
124 	 */
125 	@Test
126 	public void testUnload() throws InterruptedException {
127 		// Initial condition: the jsHandler thread is alive. Tested in
128 		// testInit().
129 		JavascriptHandler jsHandler = this.defaultJUploadContext.jsHandler;
130 		this.defaultJUploadContext.unload();
131 		Assert.assertNull("jsHandler must be cleared",
132 				this.defaultJUploadContext.jsHandler);
133 		Thread.sleep(100);
134 		Assert.assertFalse("jsHandler must be stopped", jsHandler.isAlive());
135 	}
136 
137 	/**
138 	 * @throws JUploadException
139 	 */
140 	@Test
141 	public void testGetters() throws JUploadException {
142 		Assert.assertEquals("Last modified is taken from svnProperties",
143 				this.defaultJUploadContext.svnProperties
144 						.getProperty("jupload.lastSrcDirModificationDate"),
145 				this.juploadContext.getLastModified());
146 		Date date = new Date(Long
147 				.parseLong(this.defaultJUploadContext.svnProperties
148 						.getProperty("jupload.buildTimestamp")));
149 		Assert.assertEquals("buildDate is taken from svnProperties",
150 				MessageFormat.format("{0,date,medium}", date),
151 				this.juploadContext.getBuildDate());
152 
153 		Assert
154 				.assertEquals(
155 						"buildNumber is taken from svnProperties (currently: no build number)",
156 						"-1", Integer.toString(this.juploadContext
157 								.getBuildNumber()));
158 
159 		Assert.assertEquals("logWindow getter",
160 				this.defaultJUploadContext.logWindow, this.juploadContext
161 						.getLogWindow());
162 		Assert.assertEquals("jUploadPanel getter",
163 				this.defaultJUploadContext.jUploadPanel, this.juploadContext
164 						.getUploadPanel());
165 		Assert.assertEquals("uploadPolicy getter",
166 				this.defaultJUploadContext.uploadPolicy, this.juploadContext
167 						.getUploadPolicy());
168 
169 		Assert.assertEquals("frame getter", this.defaultJUploadContext.frame,
170 				this.juploadContext.getFrame());
171 		try {
172 			this.juploadContext.getCursor();
173 			Assert.fail("cursor getter should throw an exception");
174 		} catch (UnsupportedOperationException e) {
175 			// Success!
176 		}
177 
178 		String dummy = "dummy parameter name";
179 		try {
180 			String def = "0";
181 			this.juploadContext.getParameter(dummy, def);
182 			Assert.fail("getParameter(String, int) should throw an exception");
183 		} catch (UnsupportedOperationException e) {
184 			// Success!
185 		}
186 		try {
187 			int def = 0;
188 			this.juploadContext.getParameter(dummy, def);
189 			Assert.fail("getParameter(String, int) should throw an exception");
190 		} catch (UnsupportedOperationException e) {
191 			// Success!
192 		}
193 		try {
194 			float def = 0;
195 			this.juploadContext.getParameter(dummy, def);
196 			Assert.fail("getParameter(String, int) should throw an exception");
197 		} catch (UnsupportedOperationException e) {
198 			// Success!
199 		}
200 		try {
201 			long def = 0;
202 			this.juploadContext.getParameter(dummy, def);
203 			Assert.fail("getParameter(String, int) should throw an exception");
204 		} catch (UnsupportedOperationException e) {
205 			// Success!
206 		}
207 		try {
208 			boolean def = true;
209 			this.juploadContext.getParameter(dummy, def);
210 			Assert.fail("getParameter(String, int) should throw an exception");
211 		} catch (UnsupportedOperationException e) {
212 			// Success!
213 		}
214 	}
215 
216 	/** */
217 	@Test
218 	public void testGetMimeType() {
219 		Assert.assertEquals("txt file", "text/plain", this.juploadContext
220 				.getMimeType("txt"));
221 		Assert.assertEquals("jepg file", "image/jpeg", this.juploadContext
222 				.getMimeType("jpeg"));
223 		Assert.assertEquals("jpg file", "image/jpeg", this.juploadContext
224 				.getMimeType("jpg"));
225 		Assert
226 				.assertEquals(
227 						"docx file",
228 						"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
229 						this.juploadContext.getMimeType("docx"));
230 	}
231 
232 	/**
233 	 * @throws JUploadException
234 	 * @throws InterruptedException
235 	 */
236 	@Test
237 	public void testSetProperty() throws JUploadException, InterruptedException {
238 		// We change the value to times, to be sure that it's a success (not
239 		// just luck that the previous value was the same we are testing)
240 		this.juploadContext.setProperty(UploadPolicy.PROP_NB_FILES_PER_REQUEST,
241 				"3");
242 		// The setProperty is called by invokeLater. So we wait a little, so
243 		// that the setter is actually executed.
244 		Thread.sleep(100);
245 
246 		Assert.assertEquals("Check the value set (nbFilesPerRequest)", 3,
247 				this.uploadPolicy.getNbFilesPerRequest());
248 		this.juploadContext.setProperty(UploadPolicy.PROP_NB_FILES_PER_REQUEST,
249 				"4");
250 		// The setProperty is called by invokeLater. So we wait a little, so
251 		// that the setter is actually executed.
252 		Thread.sleep(100);
253 		Assert.assertEquals("Check the value set (nbFilesPerRequest)", 4,
254 				this.uploadPolicy.getNbFilesPerRequest());
255 	}
256 
257 	/** */
258 	@Test
259 	public void testStartUpload() {
260 		JavascriptHandlerTestHelper jsHandlerTestHelper = new JavascriptHandlerTestHelper(
261 				this.uploadPolicy, this.uploadPolicy.getContext()
262 						.getUploadPanel());
263 		this.defaultJUploadContext.jsHandler = jsHandlerTestHelper;
264 		Assert.assertNull("No command at initialization",
265 				jsHandlerTestHelper.lastCommandReceived);
266 		this.juploadContext.startUpload();
267 		Assert.assertEquals("The correct command after at the end of the test",
268 				JavascriptHandler.COMMAND_START_UPLOAD,
269 				jsHandlerTestHelper.lastCommandReceived);
270 	}
271 
272 	/** */
273 	@Test
274 	public void testDisplayErr() {
275 		String msg = "An error message";
276 		this.defaultJUploadContext.displayErr(msg);
277 		Assert.assertEquals("Check message", msg,
278 				((UploadPolicyTestHelper) this.uploadPolicy).lastMsgReceived);
279 	}
280 
281 	/** */
282 	@Test
283 	public void testDisplayInfo() {
284 		String msg = "An information message";
285 		this.defaultJUploadContext.displayInfo(msg);
286 		Assert.assertEquals("Check message", msg,
287 				((UploadPolicyTestHelper) this.uploadPolicy).lastMsgReceived);
288 	}
289 
290 	/** */
291 	@Test
292 	public void testDisplayWarn() {
293 		String msg = "A warning message";
294 		this.defaultJUploadContext.displayWarn(msg);
295 		Assert.assertTrue("Check message",
296 				((UploadPolicyTestHelper) this.uploadPolicy).lastMsgReceived
297 						.endsWith(msg));
298 	}
299 
300 	/** */
301 	@Test
302 	public void testDisplayDebug() {
303 		String msg = "A debug message";
304 		this.defaultJUploadContext.displayDebug(msg, -1);
305 		Assert.assertEquals("Check message", msg,
306 				((UploadPolicyTestHelper) this.uploadPolicy).lastMsgReceived);
307 	}
308 
309 	/** */
310 	@Test
311 	public void testGetSvnProperties() {
312 		Properties p = DefaultJUploadContext.getSvnProperties();
313 		Assert.assertEquals("Check buildDate", "1111111111111", p
314 				.getProperty("jupload.buildTimestamp"));
315 		Assert.assertEquals("Check buildNumber", "A version", p
316 				.getProperty("jupload.version"));
317 		Assert.assertEquals("Check revision", "A revision number", p
318 				.getProperty("jupload.svn.revision"));
319 		Assert.assertEquals("Check lastSrcDirModificationDate",
320 				"A source modification date", p
321 						.getProperty("jupload.lastSrcDirModificationDate"));
322 	}
323 
324 	/**
325 	 * This method is the entry point given to the
326 	 * {@link JUploadContext#registerUnload(Object, String)} method. It is not a
327 	 * JUnit test.
328 	 */
329 	public void anUnloadMethod() {
330 		this.unloadHasBeenExecuted = true;
331 	}
332 
333 	/** */
334 	@Test
335 	public void testRegisterUnload() {
336 		Assert
337 				.assertEquals(
338 						"DefaultUploadContexwt should have registered one callback when starting the test",
339 						1, this.defaultJUploadContext.unloadCallbacks.size());
340 		this.juploadContext.registerUnload(this, "anUnloadMethod");
341 		Assert.assertEquals("One callback after the test", 2,
342 				this.defaultJUploadContext.unloadCallbacks.size());
343 		Assert.assertEquals("Checking callback object", this,
344 				this.defaultJUploadContext.unloadCallbacks.get(0).getObject());
345 		Assert.assertEquals("Checking callback method", "anUnloadMethod",
346 				this.defaultJUploadContext.unloadCallbacks.get(0).getMethod());
347 	}
348 
349 	/** */
350 	@Test
351 	public void testRunUnload() {
352 		this.juploadContext.registerUnload(this, "anUnloadMethod");
353 		Assert.assertFalse("Unload has not been executed yet",
354 				this.unloadHasBeenExecuted);
355 		this.juploadContext.runUnload();
356 		Assert.assertTrue("Unload has been executed",
357 				this.unloadHasBeenExecuted);
358 	}
359 
360 	/** */
361 	@Test
362 	public void testDisplayDebugParameterValue() {
363 		String key = "A key";
364 		String value = "A value";
365 		String dummyValue = "no real value";
366 		((UploadPolicyTestHelper) this.uploadPolicy).lastMsgReceived = dummyValue;
367 
368 		// With debugLevel set to 79, there should be no output.
369 		System.setProperty(UploadPolicy.PROP_DEBUG_LEVEL, "79");
370 		this.defaultJUploadContext.displayDebugParameterValue(key, value);
371 		Assert.assertEquals("Check message", dummyValue,
372 				((UploadPolicyTestHelper) this.uploadPolicy).lastMsgReceived);
373 
374 		// With debugLevel set to 80, there should be an output.
375 		System.setProperty(UploadPolicy.PROP_DEBUG_LEVEL, "80");
376 		this.defaultJUploadContext.displayDebugParameterValue(key, value);
377 		Assert.assertEquals("Check message", "Parameter '" + key
378 				+ "' loaded. Value: " + value,
379 				((UploadPolicyTestHelper) this.uploadPolicy).lastMsgReceived);
380 	}
381 
382 	/** */
383 	@Test
384 	public void testParseInt() {
385 		Assert.assertEquals("A valid number", 12345, this.defaultJUploadContext
386 				.parseInt("12345", -1));
387 		Assert.assertEquals("An invalid number", -234,
388 				this.defaultJUploadContext.parseInt(" QSDQ ", -234));
389 	}
390 
391 	/** */
392 	@Test
393 	public void testParseFloat() {
394 		Assert.assertEquals("A valid number", 12345.6789,
395 				this.defaultJUploadContext.parseFloat(" 12345.6789 ", -1),
396 				0.001);
397 		Assert.assertEquals("An invalid number", -456.11235,
398 				this.defaultJUploadContext.parseFloat(" ZAEZA ",
399 						(float) -456.11235), 0.0001);
400 	}
401 
402 	/** */
403 	@Test
404 	public void testParseLong() {
405 		Assert
406 				.assertEquals("A valid number", 9223372036854775807L,
407 						this.defaultJUploadContext.parseLong(
408 								"9223372036854775807", -1));
409 		Assert.assertEquals("An invalid number", -9223372036854775808L,
410 				this.defaultJUploadContext.parseLong(" QSDQ ",
411 						-9223372036854775808L));
412 	}
413 
414 	/** */
415 	@Test
416 	public void testParseBoolean() {
417 		Assert.assertEquals("A valid boolean", true, this.defaultJUploadContext
418 				.parseBoolean("true", false));
419 		Assert.assertEquals("A valid boolean", false,
420 				this.defaultJUploadContext.parseBoolean("false", true));
421 		Assert.assertEquals("A valid boolean", false,
422 				this.defaultJUploadContext.parseBoolean("FAlSE", true));
423 		Assert.assertEquals("An invalid boolean", false,
424 				this.defaultJUploadContext.parseBoolean("QSDQ", false));
425 		Assert.assertEquals("An invalid boolean", true,
426 				this.defaultJUploadContext.parseBoolean("QSDQ", true));
427 	}
428 
429 	/** */
430 	@Test(expected = UnsupportedOperationException.class)
431 	public void testSetWaitCursor() {
432 		this.juploadContext.setWaitCursor();
433 	}
434 
435 	/** */
436 	@Test(expected = UnsupportedOperationException.class)
437 	public void testDisplayURL() {
438 		this.defaultJUploadContext.displayURL("http://jupload.sourceforge.net",
439 				true);
440 	}
441 
442 	/**
443 	 * @throws JUploadException
444 	 */
445 	@Test(expected = UnsupportedOperationException.class)
446 	public void testNormalizeURL() throws JUploadException {
447 		this.defaultJUploadContext
448 				.normalizeURL("http://jupload.sourceforge.net");
449 	}
450 
451 	/** */
452 	@Test(expected = UnsupportedOperationException.class)
453 	public void testReadCookieFromNavigator() {
454 		this.defaultJUploadContext
455 				.readCookieFromNavigator(new Vector<String>());
456 	}
457 
458 	/** */
459 	@Test(expected = UnsupportedOperationException.class)
460 	public void testReadUserAgentFromNavigator() {
461 		this.defaultJUploadContext
462 				.readUserAgentFromNavigator(new Vector<String>());
463 	}
464 
465 	/** */
466 	@Test(expected = UnsupportedOperationException.class)
467 	public void testSetCursor() {
468 		this.defaultJUploadContext.setCursor(new Cursor(Cursor.WAIT_CURSOR));
469 	}
470 
471 	/** */
472 	@Test(expected = UnsupportedOperationException.class)
473 	public void testShowStatus() {
474 		this.defaultJUploadContext.showStatus("status");
475 	}
476 }