View Javadoc
1   //
2   // $Id$
3   //
4   // jupload - A file upload applet.
5   //
6   // Copyright 2010 The JUpload Team
7   //
8   // Created: 25 fevr. 2010
9   // Creator: etienne_sf
10  // Last modified: $Date$
11  //
12  // This program is free software; you can redistribute it and/or modify
13  // it under the terms of the GNU General Public License as published by
14  // the Free Software Foundation; either version 2 of the License, or
15  // (at your option) any later version.
16  //
17  // This program is distributed in the hope that it will be useful,
18  // but WITHOUT ANY WARRANTY; without even the implied warranty of
19  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  // GNU General Public License for more details.
21  //
22  // You should have received a copy of the GNU General Public License
23  // along with this program; if not, write to the Free Software
24  // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  package wjhk.jupload2.filedata;
26  
27  import static org.junit.Assert.*;
28  
29  import java.io.File;
30  import java.io.IOException;
31  import java.io.InputStream;
32  import java.util.Date;
33  import java.util.Vector;
34  
35  import org.junit.Before;
36  import org.junit.Test;
37  
38  import wjhk.jupload2.exception.JUploadException;
39  import wjhk.jupload2.exception.JUploadIOException;
40  import wjhk.jupload2.gui.filepanel.FilePanel;
41  import wjhk.jupload2.gui.filepanel.treeview.FileDataNode;
42  import wjhk.jupload2.gui.filepanel.treeview.FolderNode;
43  import wjhk.jupload2.gui.filepanel.treeview.RootNode;
44  import wjhk.jupload2.policies.UploadPolicy;
45  import wjhk.jupload2.testhelpers.JUploadContextTestHelper;
46  import wjhk.jupload2.testhelpers.UploadPolicyTestHelper;
47  import wjhk.jupload2.upload.AbstractJUploadTestHelper;
48  import wjhk.jupload2.upload.helper.ByteArrayEncoder;
49  import wjhk.jupload2.upload.helper.ByteArrayEncoderHTTP;
50  
51  /**
52   * @author etienne_sf
53   */
54  public class DefaultFileDataTest extends AbstractJUploadTestHelper {
55  
56      private String rootPathForTestFiles;
57  
58      private DefaultFileData fileData;
59  
60      private File file;
61  
62      private String root;
63  
64      /** */
65      @Before
66      public void setUp() {
67          this.rootPathForTestFiles = AbstractJUploadTestHelper.getTestFilesRootPath();
68          String filePath = this.rootPathForTestFiles + File.separator + "level1" + File.separator + "ATestFile.txt";
69          this.file = new File(filePath);
70  
71          assertTrue("The file must exist (" + filePath + ")", this.file.exists());
72          assertTrue("The root must exist (" + this.rootPathForTestFiles + ")", new File(rootPathForTestFiles).exists());
73          assertTrue("The root must be a valid folder", new File(rootPathForTestFiles).isDirectory());
74          assertTrue("The test file must be readable", this.file.canRead());
75  
76          this.root = this.file.getParentFile().getAbsolutePath();
77          this.fileData = new DefaultFileData(this.file, this.uploadPolicy);
78      }
79  
80      /** */
81      @Test
82      public void testConstructor() {
83          // Check file attribute and getter
84          assertEquals("Check of the file attribute", this.file, this.fileData.file);
85          assertEquals("Check of the file getter", this.file, this.fileData.getFile());
86  
87          // Check fileSize attribute and getter
88          assertEquals("Check of the fileSize attribute", this.file.length(), this.fileData.fileSize);
89          assertEquals("Check of the fileSize getter", this.file.length(), this.fileData.getFileLength());
90  
91          // Check lastModified attribute and getter
92          assertEquals("Check of the lastModified attribute", new Date(this.file.lastModified()),
93                  this.fileData.fileModified);
94          assertEquals("Check of the lastModified getter", new Date(this.file.lastModified()),
95                  this.fileData.getLastModified());
96  
97          // Check mimeType attribute and getter
98          assertEquals("Check of the mimeType attribute", JUploadContextTestHelper.TEST_CASE_MIME_TYPE,
99                  this.fileData.mimeType);
100         assertEquals("Check of the mimeType getter", JUploadContextTestHelper.TEST_CASE_MIME_TYPE,
101                 this.fileData.getMimeType());
102 
103         // Check parent attribute and getter
104         assertEquals("Check of the parent attribute", this.file.getAbsoluteFile().getParent(), this.fileData.fileDir);
105         assertEquals("Check of the parent getter", this.file.getAbsoluteFile().getParent(),
106                 this.fileData.getDirectory());
107 
108         // Check root attribute. Null at construction
109         assertNull("Check of the root attribute", this.fileData.fileRoot);
110 
111         // Check uploadPolicy attribute
112         assertEquals("Check of the uploadPolicy attribute", this.uploadPolicy, fileData.uploadPolicy);
113 
114         // Check preparedForUpload attribute
115         assertEquals("Check of the preparedForUpload attribute", false, this.fileData.preparedForUpload);
116     }
117 
118     /** */
119     @Test(expected = IllegalArgumentException.class)
120     public void testConstructor_KO() {
121         new DefaultFileData(new File(this.rootPathForTestFiles), uploadPolicy);
122     }
123 
124     /**
125      * @throws JUploadException
126      */
127     @Test
128     public void testAppendFileProperties() throws JUploadException {
129         // /////////////////////////////////////////////////////////////////////////////
130         // ////////// A Utility class, to check the properties wich are managed
131         // /////////////////////////////////////////////////////////////////////////////
132         class ByteArrayEncoderHTTP_CheckAppendTextProperty extends ByteArrayEncoderHTTP {
133             String mimeType = null;
134 
135             String pathInfo = null;
136 
137             String relpathinfo = null;
138 
139             String filemodificationdate = null;
140 
141             ByteArrayEncoderHTTP_CheckAppendTextProperty() throws JUploadIOException {
142                 super(DefaultFileDataTest.this.uploadPolicy);
143             }
144 
145             @Override
146             public ByteArrayEncoder appendTextProperty(String name, String value, int index) throws JUploadIOException {
147                 if (name.equals("mimetype")) {
148                     this.mimeType = value;
149                 } else if (name.equals("pathinfo")) {
150                     this.pathInfo = value;
151                 } else if (name.equals("relpathinfo")) {
152                     this.relpathinfo = value;
153                 } else if (name.equals("filemodificationdate")) {
154                     this.filemodificationdate = value;
155                 } else {
156                     throw new java.lang.IllegalArgumentException("Unknown property : " + name);
157                 }
158                 return this;
159             }
160         }
161         // /////////////////////////////////////////////////////////////////////////////
162         ByteArrayEncoderHTTP_CheckAppendTextProperty bae = new ByteArrayEncoderHTTP_CheckAppendTextProperty();
163 
164         int index = 58;
165         this.fileData.beforeUpload(this.root);
166         this.fileData.appendFileProperties(bae, index);
167         assertEquals("Check mimeType value", JUploadContextTestHelper.TEST_CASE_MIME_TYPE, bae.mimeType);
168         assertEquals("Check pathInfo value", this.file.getParentFile().getAbsolutePath(), bae.pathInfo);
169 
170         // The relpathinfo, is the path relative to the file root.
171         assertEquals("The relpathinfo is the part of the file absolute path, wich is after the file root", this.file
172                 .getParentFile().getAbsolutePath(), this.root /* + File.separator */+ bae.relpathinfo);
173         // date here.
174         assertNotNull("Check mimeType value", bae.filemodificationdate);
175     }
176 
177     /**
178      * @throws JUploadException
179      */
180     @Test
181     public void testBeforeUpload() throws JUploadException {
182         // The next call should not throw an exception.
183         this.fileData.beforeUpload(this.root);
184 
185         this.fileData.preparedForUpload = false;
186         ((UploadPolicyTestHelper) this.uploadPolicy).maxFileSize = 5;
187         try {
188             // The next call should throw an exception.
189             this.fileData.beforeUpload(this.root);
190             fail("The file should be too big!");
191         } catch (JUploadException e) {
192             // Success !
193         }
194     }
195 
196     /** */
197     @Test
198     public void testGetUploadLength() {
199         this.fileData.preparedForUpload = false;
200         try {
201             this.fileData.getUploadLength();
202             fail("getUploadLength should raise an exception when the file is not prepared for upload");
203         } catch (IllegalStateException e) {
204             // Success!
205         }
206 
207         this.fileData.preparedForUpload = true;
208         assertEquals("Check upload length", this.file.length(), this.fileData.getUploadLength());
209     }
210 
211     /** */
212     @Test
213     public void testAfterUpload() {
214         this.fileData.preparedForUpload = false;
215         try {
216             this.fileData.afterUpload();
217             fail("getUploadLength should raise an exception when the file is not prepared for upload");
218         } catch (IllegalStateException e) {
219             // Success!
220         }
221 
222         this.fileData.preparedForUpload = true;
223         this.fileData.afterUpload();
224         assertEquals("After afterUpload(), the file is no more prepared", false, this.fileData.preparedForUpload);
225     }
226 
227     /**
228      * @throws JUploadException
229      * @throws IOException
230      */
231     @Test
232     public void testGetInputStream() throws JUploadException, IOException {
233         this.fileData.preparedForUpload = false;
234         try {
235             this.fileData.afterUpload();
236             fail("getUploadLength should raise an exception when the file is not prepared for upload");
237         } catch (IllegalStateException e) {
238             // Success!
239         }
240 
241         this.fileData.preparedForUpload = true;
242         InputStream is = this.fileData.getInputStream();
243         // Success !
244         is.close();
245     }
246 
247     /** */
248     @Test
249     public void testGetFileName() {
250         assertEquals("Check file name", this.file.getName(), this.fileData.getFileName());
251     }
252 
253     /** */
254     @Test
255     public void testGetFileExtension() {
256         int lastPoint = this.file.getName().lastIndexOf(".");
257         assertTrue("We must have a point, to find the extension!", lastPoint >= 0);
258         String extension = this.file.getName().substring(lastPoint + 1);
259         assertEquals("Check file extension", extension, DefaultFileData.getExtension(this.file.getName()));
260     }
261 
262     /** */
263     @Test
264     public void testCanRead() {
265         assertTrue("should be able to read the test file (attribute)", this.fileData.canRead());
266         assertTrue("should be able to read the test file (getter)", this.fileData.canRead.booleanValue());
267 
268         this.fileData = new DefaultFileData(new File("This is not a file"), this.uploadPolicy);
269         assertFalse("should be able to read the test file (attribute)", this.fileData.canRead());
270         assertFalse("should be able to read the test file (getter)", this.fileData.canRead.booleanValue());
271     }
272 
273     /**
274      * @throws JUploadException
275      */
276     @Test
277     public void testGetRelativeDir_flatMode() throws JUploadException {
278         // Preparation
279         this.fileData.fileRoot = "";
280         // The relpathinfo, is the path relative to the file root.
281         String absPath = (this.file.getParentFile().getAbsolutePath() == null) ? "" : this.file.getParentFile()
282                 .getAbsolutePath();
283 
284         // Go, go, go
285         String relativeDir = this.fileData.getRelativeDir();
286 
287         // Verification
288         assertEquals("Relative Dir (1)", absPath, relativeDir);
289         assertEquals("The relpathinfo is the part of the file absolute path, wich is after the file root", absPath,
290                 this.fileData.fileRoot + relativeDir);
291 
292         // ////////////////////////////////////////////////////////////////
293         // Let's choose another file root
294         int fromIndex = 0;
295         for (int i = 0; i < 3; i += 1) {
296             int netSlash = absPath.indexOf('/', fromIndex + 1);
297             fromIndex = (netSlash >= 0) ? netSlash : absPath.indexOf('\\', fromIndex + 1);
298         } // for
299         String rootPath = absPath.substring(0, fromIndex);
300         this.fileData.beforeUpload(rootPath);
301 
302         // Go, go, go
303         relativeDir = this.fileData.getRelativeDir();
304 
305         // Verification
306         assertTrue("The rootPath is a non empty path", rootPath.length() > 5);
307         assertEquals("The relpathinfo is the part of the file absolute path, wich is after the file root", absPath,
308                 this.fileData.fileRoot + relativeDir);
309 
310         // ////////////////////////////////////////////////////////////////
311         // Let's choose another file root (with a trailing slash)
312         String rootPathWithSlash = rootPath + '/';
313         this.fileData.beforeUpload(rootPathWithSlash);
314 
315         // Go, go, go
316         relativeDir = this.fileData.getRelativeDir();
317 
318         // Verification
319         assertTrue("The rootPath is a non empty path", rootPathWithSlash.length() > 5);
320         assertEquals("The relpathinfo is the part of the file absolute path, wich is after the file root", absPath,
321                 this.fileData.fileRoot + relativeDir);
322 
323         // ////////////////////////////////////////////////////////////////
324         // Let's choose another file root (with a trailing anti-slash)
325         String rootPathWithAntiSlash = rootPath + '\\';
326         this.fileData.beforeUpload(rootPathWithAntiSlash);
327 
328         // Go, go, go
329         relativeDir = this.fileData.getRelativeDir();
330 
331         // Verification
332         assertTrue("The rootPath is a non empty path", rootPathWithAntiSlash.length() > 5);
333         assertEquals("The relpathinfo is the part of the file absolute path, wich is after the file root", absPath,
334                 this.fileData.fileRoot + relativeDir);
335     }
336 
337     /**
338      * @throws JUploadException
339      */
340     @Test
341     public void testGetRelativeDir_independentTreeViewMode() throws JUploadException {
342         // Preparation
343         // We go to INDEPENDENT_TREE_VIEW
344         String oldValue = System.getProperty(UploadPolicy.PROP_FILE_LIST_VIEW_MODE);
345         System.setProperty(UploadPolicy.PROP_FILE_LIST_VIEW_MODE,
346                 FilePanel.FileListViewMode.INDEPENDENT_TREE_VIEW.toString());
347 
348         try {
349             // Preparation
350             this.fileData.fileRoot = "";
351             // We need to create a path, to check that every thing is ok.
352             RootNode rootNode = (RootNode) this.fileDataTreeViewModel.getAbsoluteRoot();
353             FolderNode folder = (FolderNode) rootNode.addChild(new FolderNode(getTestFile("files"), this.uploadPolicy,
354                     null, null));
355             FolderNode subfolder = (FolderNode) folder.addChild(new FolderNode(getTestFile("files/level1"),
356                     this.uploadPolicy, null, null));
357             FileDataNode fileDataNode = (FileDataNode) subfolder.addChild(this.fileData);
358             assertEquals("fileDataNode", fileDataNode, this.fileData.treeFileDataNode);
359 
360             // go, go, go
361             String relativeDir = this.fileData.getRelativeDir();
362 
363             // Verification
364             assertEquals("Relative Dir (1)", "files/level1", relativeDir);
365         } finally {
366             // Cleaning of the test environment
367             if (oldValue == null) {
368                 System.clearProperty(UploadPolicy.PROP_FILE_LIST_VIEW_MODE);
369             } else {
370                 System.setProperty(UploadPolicy.PROP_FILE_LIST_VIEW_MODE, oldValue);
371             }
372         }
373     }
374 
375     /** */
376     @Test
377     public void getPreparedForUpload() {
378         this.fileData.preparedForUpload = true;
379         assertTrue("File is prepared", this.fileData.isPreparedForUpload());
380         this.fileData.preparedForUpload = false;
381         assertFalse("File is not prepared", this.fileData.isPreparedForUpload());
382     }
383 
384     /** */
385     @Test
386     public void testGetRoot() {
387         Vector<DefaultFileData> fileArray = new Vector<DefaultFileData>(5);
388 
389         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level3/file.txt"),
390                 uploadPolicy));
391         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level33/file.txt"),
392                 uploadPolicy));
393         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level3/file.txt"),
394                 uploadPolicy));
395         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level33/file.txt"),
396                 uploadPolicy));
397         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level3/file.txt"),
398                 uploadPolicy));
399         String result = DefaultFileData.getRoot(fileArray).getAbsolutePath();
400         String expected = this.rootPathForTestFiles + "level1" + File.separator + "level2";
401         assertTrue("Check getRoot: checking '" + result + "' against '" + expected + "'", result.endsWith(expected));
402 
403         fileArray.clear();
404         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level3/file.txt"),
405                 uploadPolicy));
406         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level33/file.txt"),
407                 uploadPolicy));
408         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level22/level3/file.txt"),
409                 uploadPolicy));
410         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level33/file.txt"),
411                 uploadPolicy));
412         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level33/file.txt"),
413                 uploadPolicy));
414         result = DefaultFileData.getRoot(fileArray).getAbsolutePath();
415         expected = this.rootPathForTestFiles + "level1";
416         assertTrue("Check getRoot: checking '" + result + "' against '" + expected + "'", result.endsWith(expected));
417 
418         fileArray.clear();
419         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level11/level2/level3/file.txt"),
420                 uploadPolicy));
421         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level33/file.txt"),
422                 uploadPolicy));
423         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level3/file.txt"),
424                 uploadPolicy));
425         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1/level2/level3/file.txt"),
426                 uploadPolicy));
427         fileArray.add(new DefaultFileData(new File(this.rootPathForTestFiles + "level1.txt"), uploadPolicy));
428         result = DefaultFileData.getRoot(fileArray).getAbsolutePath() + File.separator;
429         expected = this.rootPathForTestFiles /* + File.separator */;
430         assertTrue("Check getRoot: checking '" + result + "' against '" + expected + "'", result.endsWith(expected));
431 
432     }
433 
434 }