View Javadoc
1   package wjhk.jupload2.policies;
2   
3   import java.io.File;
4   import java.io.UnsupportedEncodingException;
5   import java.net.URLEncoder;
6   import java.nio.charset.UnsupportedCharsetException;
7   import java.util.Locale;
8   import java.util.Properties;
9   
10  import javax.swing.JOptionPane;
11  import javax.swing.UIManager;
12  
13  import org.junit.Assert;
14  import org.junit.Before;
15  import org.junit.Test;
16  
17  import static org.mockito.BDDMockito.*;
18  
19  import wjhk.jupload2.JUploadDaemon;
20  import wjhk.jupload2.exception.JUploadException;
21  import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles;
22  import wjhk.jupload2.exception.JUploadExceptionUploadFailed;
23  import wjhk.jupload2.gui.JUploadPanel;
24  import wjhk.jupload2.testhelpers.FileDataTestHelper;
25  import wjhk.jupload2.testhelpers.JUploadContextTestHelper;
26  import wjhk.jupload2.upload.AbstractJUploadTestHelper;
27  
28  /**
29   * @author etienne_sf
30   */
31  public class DefaultUploadPolicyTest extends AbstractJUploadTestHelper {
32  
33      final static String DEFAULT_UPLOADPOLICY_PROPERTIES = "/tests/policies/default_uploadPolicy.properties";
34  
35      Properties defaultProperties;
36  
37      /**
38       * Creation of JUploadApplet or JUploadExecutable
39       * 
40       * @throws JUploadException
41       */
42      @Before
43      public void initJUpload() throws JUploadException {
44          this.juploadDaemon = new JUploadDaemon();
45          this.juploadContext = new JUploadContextTestHelper(this.juploadPanel);
46  
47          // A try with Mockito. Very nice framework. As the new generates
48          this.uploadPolicy = spy(new DefaultUploadPolicy(this.juploadContext));
49          ((JUploadContextTestHelper) this.juploadContext).uploadPolicy = this.uploadPolicy;
50          doNothing().when(this.uploadPolicy).alertStr(anyString());
51          doNothing().when(this.uploadPolicy).alert(anyString());
52          doNothing().when(this.uploadPolicy).displayErr(anyString());
53  
54      }
55  
56      /**
57       * @throws Exception
58       */
59      @Test
60      public void testSetPostURL() throws Exception {
61          String postURL = "/test.jsp";
62  
63          ((DefaultUploadPolicy) this.uploadPolicy).setPostURL(postURL);
64          String url = this.uploadPolicy.getPostURL();
65          Assert.assertNotNull("The postURL is mandatory", url);
66          Assert.assertTrue("postURL set to '" + postURL + "'", url.endsWith(postURL));
67  
68          postURL = "ftp://127.0.0.1/pub";
69          initJUpload();
70          ((DefaultUploadPolicy) this.uploadPolicy).setPostURL(postURL);
71          url = this.uploadPolicy.getPostURL();
72          Assert.assertTrue("postURL set to '" + postURL + "'", url.endsWith(postURL));
73      }
74  
75      /**
76       * @throws Exception
77       */
78      @Test
79      public void testSetCurrentBrowsingDirectoryString() throws Exception {
80          String dir = AbstractJUploadTestHelper.getTestFilesRootPath();
81  
82          ((DefaultUploadPolicy) this.uploadPolicy).setCurrentBrowsingDirectory(dir);
83  
84          Assert.assertEquals("current browsing directory set to '" + dir + "'", dir, this.uploadPolicy
85                  .getCurrentBrowsingDirectory().getAbsolutePath() + File.separator);
86      }
87  
88      /**
89       * @throws Exception
90       */
91      @Test
92      public void testSetCurrentBrowsingDirectoryFile() throws Exception {
93          // We need an existing folder.
94          File tmpFile = File.createTempFile("prefix", "suffix");
95          File dir = tmpFile.getParentFile();
96  
97          ((DefaultUploadPolicy) this.uploadPolicy).setCurrentBrowsingDirectory(dir);
98  
99          Assert.assertEquals("current browsing directory set to '" + dir.getAbsolutePath() + "'", dir.getAbsolutePath(),
100                 this.uploadPolicy.getCurrentBrowsingDirectory().getAbsolutePath());
101     }
102 
103     /**
104      * @throws Exception
105      */
106     @Test
107     public void testSetAfterUploadTarget_Null() throws Exception {
108         ((DefaultUploadPolicy) this.uploadPolicy).setAfterUploadURL(null);
109         Assert.assertNull("AfterUploadURL (null)", this.uploadPolicy.getAfterUploadURL());
110     }
111 
112     /**
113      * @throws Exception
114      */
115     @Test
116     public void testSetAfterUploadTarget_RelativeURL() throws Exception {
117         String afterUploadURL = "/after/upload/url.jsp";
118         ((DefaultUploadPolicy) this.uploadPolicy).setAfterUploadURL(afterUploadURL);
119         Assert.assertEquals("AfterUploadURL (URL)", afterUploadURL, this.uploadPolicy.getAfterUploadURL());
120     }
121 
122     /**
123      * @throws Exception
124      */
125     @Test
126     public void testSetAfterUploadTarget_javascript() throws Exception {
127         String afterUploadURL = "javascript:afterUpload()";
128         ((DefaultUploadPolicy) this.uploadPolicy).setAfterUploadURL(afterUploadURL);
129         Assert.assertEquals("AfterUploadURL (javascript)", afterUploadURL, this.uploadPolicy.getAfterUploadURL());
130     }
131 
132     /**
133      * 
134      */
135     @Test
136     public void testSetAllowHttpPersistent() {
137         boolean allowHttpPersistent = true;
138         ((DefaultUploadPolicy) this.uploadPolicy).setAllowHttpPersistent(allowHttpPersistent);
139         Assert.assertEquals("allowHttpPersistent (true)", allowHttpPersistent,
140                 this.uploadPolicy.getAllowHttpPersistent());
141         allowHttpPersistent = false;
142         ((DefaultUploadPolicy) this.uploadPolicy).setAllowHttpPersistent(allowHttpPersistent);
143         Assert.assertEquals("allowHttpPersistent (false)", allowHttpPersistent,
144                 this.uploadPolicy.getAllowHttpPersistent());
145     }
146 
147     /** */
148     @Test
149     public void testSetAllowedFileExtensions() {
150         String allowedFileExtensions = "txt/jpg";
151         String allowedFileExtensionsCorrected = "/" + allowedFileExtensions + "/";
152         ((DefaultUploadPolicy) this.uploadPolicy).setAllowedFileExtensions(allowedFileExtensions);
153         Assert.assertEquals("allowedFileExtensions (" + allowedFileExtensions + ")", allowedFileExtensionsCorrected,
154                 this.uploadPolicy.getAllowedFileExtensions());
155         allowedFileExtensions = null;
156         ((DefaultUploadPolicy) this.uploadPolicy).setAllowedFileExtensions(allowedFileExtensions);
157         Assert.assertEquals("allowedFileExtensions (null)", allowedFileExtensions,
158                 this.uploadPolicy.getAllowedFileExtensions());
159     }
160 
161     /** */
162     @Test
163     public void testSetFileFilterName() {
164         String allowedFileExtensions = "txt/jpg";
165         String fileFilterName = "A name for the file filter";
166 
167         ((DefaultUploadPolicy) this.uploadPolicy).setAllowedFileExtensions(allowedFileExtensions);
168         Assert.assertFalse("fileFilterName not set (" + allowedFileExtensions + ")",
169                 fileFilterName.equals(this.uploadPolicy.getFileFilterName()));
170 
171         // Let's set our fileFilterName
172         ((DefaultUploadPolicy) this.uploadPolicy).setFileFilterName(fileFilterName);
173         Assert.assertTrue("getFileFilterName(): fileFilterName not set (" + allowedFileExtensions + ")",
174                 this.uploadPolicy.getFileFilterName().equals(fileFilterName));
175         Assert.assertTrue("fileFilterGetDescription(): fileFilterName not set (" + allowedFileExtensions + ")",
176                 this.uploadPolicy.fileFilterGetDescription().equals(fileFilterName));
177 
178         // If allowedFileExtensions is not provided, there should be no
179         // fileFilterName
180         ((DefaultUploadPolicy) this.uploadPolicy).setAllowedFileExtensions(null);
181         Assert.assertNull("fileFilterGetDescription() when allowedFileExtensions is null",
182                 this.uploadPolicy.fileFilterGetDescription());
183         ((DefaultUploadPolicy) this.uploadPolicy).setAllowedFileExtensions("");
184         Assert.assertNull("fileFilterGetDescription() when allowedFileExtensions is an empty String",
185                 this.uploadPolicy.fileFilterGetDescription());
186     }
187 
188     /** */
189     @Test
190     public void testGetContext() {
191         Assert.assertEquals("getContext", this.juploadContext, this.uploadPolicy.getContext());
192     }
193 
194     /** */
195     @Test
196     public void testSetDebutLevel() {
197         int debugLevel = 47;
198         this.uploadPolicy.setDebugLevel(debugLevel);
199         Assert.assertEquals("debugLevel (" + debugLevel + ")", debugLevel, this.uploadPolicy.getDebugLevel());
200     }
201 
202     /** */
203     @Test
204     public void testSetFileChooserIconFromFileContent() {
205         int fileChooserIconFromFileContent = -1;
206         ((DefaultUploadPolicy) this.uploadPolicy).setFileChooserIconFromFileContent(fileChooserIconFromFileContent);
207         Assert.assertEquals("fileChooserIconFromFileContent", fileChooserIconFromFileContent,
208                 this.uploadPolicy.getFileChooserIconFromFileContent());
209 
210         fileChooserIconFromFileContent = 0;
211         ((DefaultUploadPolicy) this.uploadPolicy).setFileChooserIconFromFileContent(fileChooserIconFromFileContent);
212         Assert.assertEquals("fileChooserIconFromFileContent", fileChooserIconFromFileContent,
213                 this.uploadPolicy.getFileChooserIconFromFileContent());
214 
215         fileChooserIconFromFileContent = 1;
216         ((DefaultUploadPolicy) this.uploadPolicy).setFileChooserIconFromFileContent(fileChooserIconFromFileContent);
217         Assert.assertEquals("fileChooserIconFromFileContent", fileChooserIconFromFileContent,
218                 this.uploadPolicy.getFileChooserIconFromFileContent());
219 
220         fileChooserIconFromFileContent = 2;
221         try {
222             // This should throw an IllegalValueException
223             ((DefaultUploadPolicy) this.uploadPolicy).setFileChooserIconFromFileContent(fileChooserIconFromFileContent);
224             Assert.fail("fileChooserIconFromFileContent = 2   should throw an exception");
225         } catch (java.lang.IllegalArgumentException e) {
226             // We're happy if we go here !
227         }
228     }
229 
230     /** */
231     @Test
232     public void testSetFileChooserIconSize() {
233         int fileChooserIconSize = 100;
234         ((DefaultUploadPolicy) this.uploadPolicy).setFileChooserIconSize(fileChooserIconSize);
235         Assert.assertEquals("fileChooserIconSize", fileChooserIconSize, this.uploadPolicy.getFileChooserIconSize());
236 
237         fileChooserIconSize = 0;
238         try {
239             // This should throw an IllegalValueException
240             ((DefaultUploadPolicy) this.uploadPolicy).setFileChooserIconSize(fileChooserIconSize);
241             Assert.fail("fileChooserIconSize <= 0   should throw an exception");
242         } catch (java.lang.IllegalArgumentException e) {
243             // We're happy if we go here !
244         }
245     }
246 
247     /** */
248     @Test
249     public void testSetLangNull() {
250         String lang = null;
251         ((DefaultUploadPolicy) this.uploadPolicy).setLang(lang);
252 
253         // The choosen translaton is available through the
254         // UploadPolicy.getLocalizedString() method
255         String languageSet = this.uploadPolicy.getLocalizedString("language");
256 
257         ((DefaultUploadPolicy) this.uploadPolicy).setLang(Locale.getDefault().getLanguage());
258         String languageDefault = this.uploadPolicy.getLocalizedString("language");
259         Assert.assertEquals("lang=null => default", languageDefault, languageSet);
260     }
261 
262     /** */
263     @Test
264     public void testSetLangFr() {
265         String lang = "fr";
266         ((DefaultUploadPolicy) this.uploadPolicy).setLang(lang);
267 
268         // The choosen translaton is available through the
269         // UploadPolicy.getLocalizedString() method
270         Assert.assertEquals("lang=fr", "French", this.uploadPolicy.getLocalizedString("language"));
271     }
272 
273     /** */
274     @Test
275     public void testSetLookAndFeel() {
276         String lookAndFeel = null;
277         ((DefaultUploadPolicy) this.uploadPolicy).setLookAndFeel(lookAndFeel);
278         // The default look and feel is the java one.
279         Assert.assertEquals("lookAndFeel", "The Java(tm) Look and Feel", UIManager.getLookAndFeel().getDescription());
280 
281         lookAndFeel = "java";
282         ((DefaultUploadPolicy) this.uploadPolicy).setLookAndFeel(lookAndFeel);
283         Assert.assertEquals("lookAndFeel", lookAndFeel, ((DefaultUploadPolicy) this.uploadPolicy).getLookAndFeel());
284         Assert.assertEquals("lookAndFeel", "The Java(tm) Look and Feel", UIManager.getLookAndFeel().getDescription());
285 
286         lookAndFeel = "system";
287         ((DefaultUploadPolicy) this.uploadPolicy).setLookAndFeel(lookAndFeel);
288         Assert.assertEquals("lookAndFeel", lookAndFeel, ((DefaultUploadPolicy) this.uploadPolicy).getLookAndFeel());
289         if (System.getProperty("os.name").contains("Windows")) {
290             Assert.assertEquals("lookAndFeel", "The Microsoft Windows Look and Feel", UIManager.getLookAndFeel()
291                     .getDescription());
292         } else if (System.getProperty("os.name").contains("Mac")) {
293             Assert.assertEquals("lookAndFeel", "Aqua Look and Feel for Mac OS X", UIManager.getLookAndFeel()
294                     .getDescription());
295         } else if (System.getProperty("os.name").contains("Linux")) {
296             // No check there, as it depends on the current installation
297         } else {
298             Assert.assertEquals("lookAndFeel", "The Java(tm) Look and Feel", UIManager.getLookAndFeel()
299                     .getDescription());
300         }
301 
302         lookAndFeel = "a non existing look and feel";
303         ((DefaultUploadPolicy) this.uploadPolicy).setLookAndFeel(lookAndFeel);
304         Assert.assertEquals("lookAndFeel", "java", ((DefaultUploadPolicy) this.uploadPolicy).getLookAndFeel());
305         Assert.assertEquals("lookAndFeel", "The Java(tm) Look and Feel", UIManager.getLookAndFeel().getDescription());
306     }
307 
308     /** */
309     @Test
310     public void testSetMaxChunkSize() {
311         long maxChunkSize = 0;
312         ((DefaultUploadPolicy) this.uploadPolicy).setMaxChunkSize(maxChunkSize);
313         Assert.assertEquals("maxChunkSize: forced to Long.MAX_VALUE when <=0", Long.MAX_VALUE,
314                 this.uploadPolicy.getMaxChunkSize());
315 
316         maxChunkSize = 1;
317         ((DefaultUploadPolicy) this.uploadPolicy).setMaxChunkSize(maxChunkSize);
318         Assert.assertEquals("maxChunkSize", maxChunkSize, this.uploadPolicy.getMaxChunkSize());
319     }
320 
321     /** */
322     @Test
323     public void testSetMaxFileSize() {
324         long maxFileSize = 0;
325         ((DefaultUploadPolicy) this.uploadPolicy).setMaxFileSize(maxFileSize);
326         Assert.assertEquals("maxFileSize: forced to Long.MAX_VALUE when <=0", Long.MAX_VALUE,
327                 this.uploadPolicy.getMaxFileSize());
328 
329         maxFileSize = 1;
330         ((DefaultUploadPolicy) this.uploadPolicy).setMaxFileSize(maxFileSize);
331         Assert.assertEquals("maxFileSize", maxFileSize, this.uploadPolicy.getMaxFileSize());
332     }
333 
334     /**
335      * @throws JUploadException
336      */
337     @Test
338     public void testSetNbFilesPerRequest() throws JUploadException {
339         int nbFilesPerRequest = 0;
340         ((DefaultUploadPolicy) this.uploadPolicy).setNbFilesPerRequest(nbFilesPerRequest);
341         Assert.assertEquals("nbFilesPerRequest: forced to Long.MAX_VALUE when <=0", Integer.MAX_VALUE,
342                 this.uploadPolicy.getNbFilesPerRequest());
343 
344         nbFilesPerRequest = 1;
345         ((DefaultUploadPolicy) this.uploadPolicy).setNbFilesPerRequest(nbFilesPerRequest);
346         Assert.assertEquals("nbFilesPerRequest", nbFilesPerRequest, this.uploadPolicy.getNbFilesPerRequest());
347 
348         nbFilesPerRequest = 2;
349         try {
350             ((DefaultUploadPolicy) this.uploadPolicy).httpUploadParameterType = UploadPolicy.HTTPUPLOADPARAMETERTYPE_ONE_FILE;
351             ((DefaultUploadPolicy) this.uploadPolicy).setNbFilesPerRequest(nbFilesPerRequest);
352             Assert.fail("nbFilesPerRequest=2 is not allowed with HTTPUPLOADPARAMETERTYPE_ONE_FILE");
353         } catch (JUploadException e) {
354             // Success! ;-)
355         }
356     }
357 
358     /**  */
359     @Test
360     public void testSetFilenameEncoding() {
361         String filenameEncoding = "test";
362         try {
363             ((DefaultUploadPolicy) this.uploadPolicy).setFilenameEncoding(filenameEncoding);
364             Assert.fail("The filenameEncoding '" + filenameEncoding + "' should throw a UnsupportedCharsetException");
365         } catch (UnsupportedCharsetException e) {
366             // success !
367         }
368 
369         filenameEncoding = "UTF-8";
370         ((DefaultUploadPolicy) this.uploadPolicy).setFilenameEncoding(filenameEncoding);
371         Assert.assertEquals("filenameEncoding", filenameEncoding, this.uploadPolicy.getFilenameEncoding());
372     }
373 
374     /**  */
375     @Test
376     public void testSetFtpCreateDirectoryStructure() {
377         boolean ftpCreateDirectoryStructure = true;
378         ((DefaultUploadPolicy) this.uploadPolicy).setFtpCreateDirectoryStructure(ftpCreateDirectoryStructure);
379         Assert.assertEquals("ftpCreateDirectoryStructure", ftpCreateDirectoryStructure,
380                 this.uploadPolicy.getFtpCreateDirectoryStructure());
381 
382         ftpCreateDirectoryStructure = false;
383         ((DefaultUploadPolicy) this.uploadPolicy).setFtpCreateDirectoryStructure(ftpCreateDirectoryStructure);
384         Assert.assertEquals("ftpCreateDirectoryStructure", ftpCreateDirectoryStructure,
385                 this.uploadPolicy.getFtpCreateDirectoryStructure());
386     }
387 
388     /**  */
389     @Test
390     public void testSetFtpTransfertBinary() {
391         boolean ftpTransfertBinary = true;
392         ((DefaultUploadPolicy) this.uploadPolicy).setFtpTransfertBinary(ftpTransfertBinary);
393         Assert.assertEquals("ftpTransfertBinary", ftpTransfertBinary, this.uploadPolicy.getFtpTransfertBinary());
394 
395         ftpTransfertBinary = false;
396         ((DefaultUploadPolicy) this.uploadPolicy).setFtpTransfertBinary(ftpTransfertBinary);
397         Assert.assertEquals("ftpTransfertBinary", ftpTransfertBinary, this.uploadPolicy.getFtpTransfertBinary());
398     }
399 
400     /**  */
401     @Test
402     public void testSetFtpTransfertPassive() {
403         boolean ftpTransfertPassive = true;
404         ((DefaultUploadPolicy) this.uploadPolicy).setFtpTransfertPassive(ftpTransfertPassive);
405         Assert.assertEquals("ftpTransfertPassive", ftpTransfertPassive, this.uploadPolicy.getFtpTransfertPassive());
406 
407         ftpTransfertPassive = false;
408         ((DefaultUploadPolicy) this.uploadPolicy).setFtpTransfertPassive(ftpTransfertPassive);
409         Assert.assertEquals("ftpTransfertPassive", ftpTransfertPassive, this.uploadPolicy.getFtpTransfertPassive());
410     }
411 
412     /**  */
413     @Test
414     public void testSetReadCookieFromNavigator() {
415         boolean readCookieFromNavigator = true;
416         ((DefaultUploadPolicy) this.uploadPolicy).setReadCookieFromNavigator(readCookieFromNavigator);
417         Assert.assertEquals("readCookieFromNavigator", readCookieFromNavigator,
418                 this.uploadPolicy.getReadCookieFromNavigator());
419 
420         readCookieFromNavigator = false;
421         ((DefaultUploadPolicy) this.uploadPolicy).setReadCookieFromNavigator(readCookieFromNavigator);
422         Assert.assertEquals("readCookieFromNavigator", readCookieFromNavigator,
423                 this.uploadPolicy.getReadCookieFromNavigator());
424     }
425 
426     /**  */
427     @Test
428     public void testSetReadUserAgentFromNavigator() {
429         boolean readCookieFromNavigator = true;
430         ((DefaultUploadPolicy) this.uploadPolicy).setReadUserAgentFromNavigator(readCookieFromNavigator);
431         Assert.assertEquals("readCookieFromNavigator", readCookieFromNavigator,
432                 this.uploadPolicy.getReadUserAgentFromNavigator());
433 
434         readCookieFromNavigator = false;
435         ((DefaultUploadPolicy) this.uploadPolicy).setReadUserAgentFromNavigator(readCookieFromNavigator);
436         Assert.assertEquals("readCookieFromNavigator", readCookieFromNavigator,
437                 this.uploadPolicy.getReadUserAgentFromNavigator());
438     }
439 
440     /** */
441     @Test
442     public void testSetRetryMaxNumberOf() {
443         int retryMaxNumberOf = 0;
444         ((DefaultUploadPolicy) this.uploadPolicy).setRetryMaxNumberOf(retryMaxNumberOf);
445         Assert.assertEquals("RetryMaxNumberOf", retryMaxNumberOf, this.uploadPolicy.getRetryMaxNumberOf());
446 
447         retryMaxNumberOf = 1;
448         ((DefaultUploadPolicy) this.uploadPolicy).setRetryMaxNumberOf(retryMaxNumberOf);
449         Assert.assertEquals("RetryMaxNumberOf", retryMaxNumberOf, this.uploadPolicy.getRetryMaxNumberOf());
450 
451         retryMaxNumberOf = -1;
452         try {
453             ((DefaultUploadPolicy) this.uploadPolicy).setRetryMaxNumberOf(retryMaxNumberOf);
454             Assert.fail("With retryMaxNumberOf set to -1, we should have an IllegalArgumentException");
455         } catch (IllegalArgumentException e) {
456             // Success !
457         }
458     }
459 
460     /** */
461     @Test
462     public void testSetRetryNbSecondsBetween() {
463         int retryNbSecondsBetween = 0;
464         ((DefaultUploadPolicy) this.uploadPolicy).setRetryNbSecondsBetween(retryNbSecondsBetween);
465         Assert.assertEquals("RetryNbSecondsBetween", retryNbSecondsBetween,
466                 this.uploadPolicy.getRetryNbSecondsBetween());
467 
468         retryNbSecondsBetween = 1;
469         ((DefaultUploadPolicy) this.uploadPolicy).setRetryNbSecondsBetween(retryNbSecondsBetween);
470         Assert.assertEquals("RetryNbSecondsBetween", retryNbSecondsBetween,
471                 this.uploadPolicy.getRetryNbSecondsBetween());
472 
473         retryNbSecondsBetween = -1;
474         try {
475             ((DefaultUploadPolicy) this.uploadPolicy).setRetryNbSecondsBetween(retryNbSecondsBetween);
476             Assert.fail("With retryNbSecondsBetween set to -1, we should have an IllegalArgumentException");
477         } catch (IllegalArgumentException e) {
478             // Success !
479         }
480     }
481 
482     /** */
483     @Test
484     public void testSetServerProtocol() {
485         String serverProtocol = "HTTP/1.2"; // It's a new one : ;-)
486         ((DefaultUploadPolicy) this.uploadPolicy).setServerProtocol(serverProtocol);
487         Assert.assertEquals("ServerProtocol", serverProtocol, this.uploadPolicy.getServerProtocol());
488     }
489 
490     /**
491      * @throws JUploadException
492      */
493     @Test
494     public void testCheckUploadSuccess_failure() throws JUploadException {
495         int status = 59;
496         String msg = "Hum, hum";
497         String body = "We're in the case\r\n of an unknown failure!\r\n";
498 
499         try {
500             this.uploadPolicy.checkUploadSuccess(status, msg, body);
501             Assert.fail("Status value " + status + " should raise a JUploadExceptionUploadFailed");
502         } catch (JUploadExceptionUploadFailed e1) {
503             // Success!
504         }
505 
506         // Let's check a response without error or success string
507         // Special case (always success)
508         status = 100;
509         try {
510             Assert.assertTrue(this.uploadPolicy.checkUploadSuccess(status, msg, body));
511         } catch (JUploadExceptionUploadFailed e1) {
512             Assert.fail("Expected always success for http status 100");
513         }
514 
515         // Let's check a response without error or success string
516         status = 200;
517         try {
518             this.uploadPolicy.checkUploadSuccess(status, msg, body);
519             Assert.fail("Response without success nor error string should raise a JUploadExceptionUploadFailed");
520         } catch (JUploadExceptionUploadFailed e1) {
521             // Success!
522         }
523 
524         // Let's check a response without error or success string
525         String errMsg = "this is an error!";
526         body = "We're in the case\r\n of a known failure!\r\nERROR: " + errMsg + "\r\nbla bla bla";
527         try {
528             this.uploadPolicy.checkUploadSuccess(status, msg, body);
529             Assert.fail("Response with an error message should raise a JUploadExceptionUploadFailed");
530         } catch (JUploadExceptionUploadFailed e1) {
531             // Success!
532             Assert.assertEquals("Check of the error message retrieval", errMsg, e1.getMessage());
533         }
534 
535         // Let's change the stringUploadError value
536         ((DefaultUploadPolicy) this.uploadPolicy).setStringUploadError("^OUPS: (.*)$");
537         body = "We're in the case\r\n of a known failure!\r\nOUPS: " + errMsg + "\r\nbla bla bla";
538         try {
539             this.uploadPolicy.checkUploadSuccess(status, msg, body);
540             Assert.fail("Response with an error message should raise a JUploadExceptionUploadFailed");
541         } catch (JUploadExceptionUploadFailed e1) {
542             // Success!
543             Assert.assertEquals("Check of the error message retrieval", errMsg, e1.getMessage());
544         }
545     }
546 
547     /**
548      * @throws JUploadException
549      */
550     @Test
551     public void testCheckUploadSuccess_warning() throws JUploadException {
552         int status = 200;
553         String msg = "Hum, hum";
554         String warnMsg = "this is an warning!";
555         String body = "We're in the case\r\n of success then warning!\r\nSUCCESS\r\nWARNING: " + warnMsg
556                 + "\r\nbla bla bla";
557         ((DefaultUploadPolicy) this.uploadPolicy).setStringUploadWarning(UploadPolicy.DEFAULT_STRING_UPLOAD_WARNING);
558         Assert.assertTrue("The upload should be a success", this.uploadPolicy.checkUploadSuccess(status, msg, body));
559         verify(this.uploadPolicy).displayWarn(warnMsg);
560         /*
561          * Assert .assertEquals( "Check of the warning message retrieval", warnMsg,
562          * ((DefaultUploadPolicy_WithoutAlertBox) this.uploadPolicy).lastAlertMessage);
563          */
564 
565         // Let's change the stringUploadError value
566         ((DefaultUploadPolicy) this.uploadPolicy).setStringUploadWarning("OUPS: (.*)$");
567         warnMsg = "this is another warning!";
568         body = "We're in the case\r\n of warning then success!\r\nOUPS: " + warnMsg + "\r\nbla bla bla\r\nSUCCESS";
569         Assert.assertTrue("The upload should be a success", this.uploadPolicy.checkUploadSuccess(status, msg, body));
570         verify(this.uploadPolicy).displayWarn(warnMsg);
571         /*
572          * Assert .assertEquals( "Check of the warning message retrieval", warnMsg,
573          * ((DefaultUploadPolicy_WithoutAlertBox) this.uploadPolicy).lastAlertMessage);
574          */
575     }
576 
577     /**
578      * @throws JUploadException
579      */
580     @Test
581     public void testCheckUploadSuccess_success() throws JUploadException {
582         int status = 100;
583         String msg = "Hum, hum";
584         String body = "We're in the case\r\n of a complete success!\r\nSUCCESS\r\nbla bla bla";
585 
586         Assert.assertTrue("The upload should be a success", this.uploadPolicy.checkUploadSuccess(status, msg, body));
587         verify(this.uploadPolicy, never()).displayWarn(anyString());
588     }
589 
590     /**
591      * @throws JUploadException
592      */
593     @Test
594     public void testAfterUpload() throws JUploadException {
595 
596         // A specific class, to check the call of displayURL.
597         class JUploadContextCheckDisplayURL extends JUploadContextTestHelper {
598 
599             boolean displayURLHasBeenCalled = false;
600 
601             public JUploadContextCheckDisplayURL(UploadPolicy uploadPolicy, JUploadPanel juploadPanel) {
602                 super(uploadPolicy, juploadPanel);
603             }
604 
605             public void displayURL(String url, boolean success) {
606                 this.displayURLHasBeenCalled = true;
607             }
608         }
609 
610         // Let's use our specific class.
611         this.juploadContext = new JUploadContextCheckDisplayURL(this.uploadPolicy, this.juploadPanel);
612         ((DefaultUploadPolicy) this.uploadPolicy).juploadContext = this.juploadContext;
613 
614         String serverOutput = "Some output";
615         Exception e = null;
616         ((DefaultUploadPolicy) this.uploadPolicy).setAfterUploadURL(null);
617         this.uploadPolicy.afterUpload(e, serverOutput);
618         Assert.assertFalse("setAfterUploadURL should not have been called",
619                 ((JUploadContextCheckDisplayURL) this.juploadContext).displayURLHasBeenCalled);
620 
621         String url = "An URL";
622         ((DefaultUploadPolicy) this.uploadPolicy).setAfterUploadURL(url);
623         this.uploadPolicy.afterUpload(e, serverOutput);
624         Assert.assertTrue("setAfterUploadURL should  have been called",
625                 ((JUploadContextCheckDisplayURL) this.juploadContext).displayURLHasBeenCalled);
626     }
627 
628     /**
629      * @throws JUploadException
630      */
631     @Test
632     public void testCreateFileData() throws JUploadException {
633         doReturn(JOptionPane.CANCEL_OPTION).when(this.uploadPolicy).confirmDialogStr(anyString(), anyInt());
634         doReturn(false).when(this.uploadPolicy).fileFilterAccept((File) anyObject());
635         File file = new File("/aTestFile");
636         try {
637             this.uploadPolicy.createFileData(file);
638             Assert.fail("A JUploadExceptionStopAddingFiles should have been thrown");
639         } catch (JUploadExceptionStopAddingFiles e) {
640             // We're happy here ! Test is a success
641         }
642 
643         // Not allowed file. The user may chooses Ok to the confirmDialog
644         doReturn(JOptionPane.OK_OPTION).when(this.uploadPolicy).confirmDialogStr(anyString(), anyInt());
645         Assert.assertNull("There should be no creation of file data, if the file is not accepted",
646                 this.uploadPolicy.createFileData(file));
647 
648         // ((DefaultUploadPolicy_WithoutAlertBox_FileFilterAccept) this.uploadPolicy).acceptFile = true;
649         doReturn(true).when(this.uploadPolicy).fileFilterAccept((File) anyObject());
650         Assert.assertNull("There should be a creation of file data, if the file is accepted",
651                 this.uploadPolicy.createFileData(file));
652     }
653 
654     /**
655      * @throws JUploadException
656      * @throws UnsupportedEncodingException
657      */
658     @Test
659     public void testGetEncodedFilename() throws JUploadException, UnsupportedEncodingException {
660         String encoding = null;
661         String filename = "A_file_name_with_accents_\u00f8\u00e5";
662         ((DefaultUploadPolicy) this.uploadPolicy).setFilenameEncoding(encoding);
663         Assert.assertEquals("No encoding for encoding=" + encoding, filename,
664                 ((DefaultUploadPolicy) this.uploadPolicy).getEncodedFilename(filename));
665 
666         encoding = "";
667         ((DefaultUploadPolicy) this.uploadPolicy).setFilenameEncoding(encoding);
668         Assert.assertEquals("No encoding for encoding=" + encoding, filename,
669                 ((DefaultUploadPolicy) this.uploadPolicy).getEncodedFilename(filename));
670 
671         encoding = "UTF-8";
672         ((DefaultUploadPolicy) this.uploadPolicy).setFilenameEncoding(encoding);
673         Assert.assertEquals("Encoding for encoding=" + encoding, URLEncoder.encode(filename, encoding),
674                 ((DefaultUploadPolicy) this.uploadPolicy).getEncodedFilename(filename));
675     }
676 
677     /**
678      * @throws JUploadException
679      */
680     @Test
681     public void testGetUploadName() throws JUploadException {
682         int fileNumber = 59;
683         FileDataTestHelper fileData = new FileDataTestHelper(fileNumber, uploadPolicy);
684         String uploadParameterName = "aName";
685         int index = 8;
686         int nbFilesPerRequest = 4;
687 
688         // Let's check that it works with a forced value (different from the
689         // default one)
690         ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterName(uploadParameterName);
691         ((DefaultUploadPolicy) this.uploadPolicy).setNbFilesPerRequest(nbFilesPerRequest);
692 
693         ((DefaultUploadPolicy) this.uploadPolicy)
694                 .setHttpUploadParameterType(UploadPolicy.HTTPUPLOADPARAMETERTYPE_ARRAY);
695         Assert.assertEquals("Array mode", uploadParameterName + "[]", this.uploadPolicy.getUploadName(fileData, index));
696 
697         ((DefaultUploadPolicy) this.uploadPolicy)
698                 .setHttpUploadParameterType(UploadPolicy.HTTPUPLOADPARAMETERTYPE_ITERATION);
699         Assert.assertEquals("Array mode", uploadParameterName + index, this.uploadPolicy.getUploadName(fileData, index));
700 
701         try {
702             ((DefaultUploadPolicy) this.uploadPolicy)
703                     .setHttpUploadParameterType(UploadPolicy.HTTPUPLOADPARAMETERTYPE_ONE_FILE);
704             this.uploadPolicy.getUploadName(fileData, index);
705             Assert.fail(UploadPolicy.HTTPUPLOADPARAMETERTYPE_ONE_FILE + " is not compatible with nbFilesPerRequest="
706                     + nbFilesPerRequest);
707         } catch (JUploadException e) {
708             // Success !
709         }
710 
711         nbFilesPerRequest = 1;
712         ((DefaultUploadPolicy) this.uploadPolicy).setNbFilesPerRequest(nbFilesPerRequest);
713         ((DefaultUploadPolicy) this.uploadPolicy)
714                 .setHttpUploadParameterType(UploadPolicy.HTTPUPLOADPARAMETERTYPE_ONE_FILE);
715         Assert.assertEquals("One file mode", uploadParameterName, this.uploadPolicy.getUploadName(fileData, index));
716     }
717 
718     /**
719      * @throws JUploadException
720      */
721     @Test
722     public void testSetHttpUploadParameterName() throws JUploadException {
723         String httpUploadParameterName = "AName";
724         ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterName(httpUploadParameterName);
725         Assert.assertEquals("Standard name", httpUploadParameterName, this.uploadPolicy.getHttpUploadParameterName());
726 
727         httpUploadParameterName = null;
728         try {
729             ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterName(httpUploadParameterName);
730             Assert.fail("null is not a valid value for httpUploadParameterName");
731         } catch (JUploadException e) {
732             // Success !
733         }
734 
735         httpUploadParameterName = "";
736         try {
737             ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterName(httpUploadParameterName);
738             Assert.fail("\"\" is not a valid value for httpUploadParameterName");
739         } catch (JUploadException e) {
740             // Success !
741         }
742 
743         httpUploadParameterName = "\u00f8\u00e5#";
744         try {
745             ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterName(httpUploadParameterName);
746             Assert.fail(httpUploadParameterName + " is not a valid value for httpUploadParameterName");
747         } catch (JUploadException e) {
748             // Success !
749         }
750     }
751 
752     /**
753      * @throws JUploadException
754      */
755     @Test
756     public void testSetHttpUploadParameterType() throws JUploadException {
757         String httpUploadParameterType = "Not a valid one!";
758         try {
759             ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterType(httpUploadParameterType);
760             Assert.fail(httpUploadParameterType + " is not a valid one for httpUploadParameterType");
761         } catch (JUploadException e) {
762             // Success!
763         }
764 
765         httpUploadParameterType = null;
766         try {
767             ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterType(httpUploadParameterType);
768             Assert.fail("null is not a valid one for httpUploadParameterType");
769         } catch (JUploadException e) {
770             // Success!
771         }
772 
773         httpUploadParameterType = UploadPolicy.HTTPUPLOADPARAMETERTYPE_ARRAY;
774         ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterType(httpUploadParameterType);
775         Assert.assertEquals("null is not a valid value for httpUploadParameterType", httpUploadParameterType,
776                 this.uploadPolicy.getHttpUploadParameterType());
777 
778         httpUploadParameterType = UploadPolicy.HTTPUPLOADPARAMETERTYPE_ITERATION;
779         ((DefaultUploadPolicy) this.uploadPolicy).setHttpUploadParameterType(httpUploadParameterType);
780         Assert.assertEquals("null is not a valid value for httpUploadParameterType", httpUploadParameterType,
781                 this.uploadPolicy.getHttpUploadParameterType());
782     }
783 }