View Javadoc
1   package wjhk.jupload2.gui.filepanel.treeview;
2   
3   import static org.junit.Assert.*;
4   
5   import java.io.File;
6   import java.util.Date;
7   import java.util.List;
8   
9   import org.junit.Before;
10  import org.junit.Test;
11  
12  import wjhk.jupload2.JUploadDaemon;
13  import wjhk.jupload2.exception.JUploadException;
14  import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles;
15  import wjhk.jupload2.exception.JUploadIOException;
16  import wjhk.jupload2.filedata.DefaultFileData;
17  import wjhk.jupload2.testhelpers.FilePanelTestHelper;
18  import wjhk.jupload2.testhelpers.JUploadPanelTestHelper;
19  import wjhk.jupload2.testhelpers.UploadPolicyTestHelper;
20  import wjhk.jupload2.upload.AbstractJUploadTestHelper;
21  import wjhk.jupload2.upload.helper.ByteArrayEncoderHTTP;
22  
23  public class FolderNodeTest extends AbstractJUploadTestHelper {
24  
25      File folderContainingTestResources = null;
26  
27      File root = new File(".");
28  
29      FolderNode folderNode = null;
30  
31      @Before
32      public void setUp() throws Exception {
33          // Set the postURL for the current unit test, according to the local network access.
34          setPostURL();
35  
36          this.juploadDaemon = new JUploadDaemon();
37          this.filePanel = new FilePanelTestHelper(this.filesToUpload);
38          this.juploadPanel = new JUploadPanelTestHelper(this.filePanel);
39          this.uploadPolicy = new UploadPolicyTestHelper(this.juploadPanel);
40  
41          folderContainingTestResources = AbstractJUploadTestHelper.getTestFile(File.separator + "files");
42          assertTrue("file is dir", folderContainingTestResources.isDirectory());
43          folderNode = (FolderNode) this.fileDataTreeViewModel.getTreePathFromFile(folderContainingTestResources, true)
44                  .getLastPathComponent();
45          this.fileDataTreeViewModel.setRoot(folderNode);
46          assertEquals("Check model", this.fileDataTreeViewModel, folderNode.treeModel);
47      }
48  
49      /**
50       * Adds some tests nodes, to have a real case hierarchy, loaded in the tree view.
51       */
52      private void addTestNodesToTree() {
53          FolderNode subfolder = new FolderNode(new File(folderContainingTestResources, "level1"), uploadPolicy,
54                  this.fileDataTreeViewModel, this.filePanelFlatDataModel2);
55          folderNode.addChild(subfolder);
56          subfolder.addChild(new FileDataNode(new DefaultFileData(new File(folderContainingTestResources,
57                  "level1/ATestFile.txt"), uploadPolicy)));
58  
59          folderNode.addChild(new FileDataNode(new DefaultFileData(new File(folderContainingTestResources, "1.txt"),
60                  uploadPolicy)));
61          folderNode.addChild(new FileDataNode(new DefaultFileData(new File(folderContainingTestResources, "2.txt"),
62                  uploadPolicy)));
63          folderNode.addChild(new FileDataNode(new DefaultFileData(new File(folderContainingTestResources, "3.txt"),
64                  uploadPolicy)));
65      }
66  
67      @SuppressWarnings("unchecked")
68      @Test
69      public void testFolderNode_OK() {
70          addTestNodesToTree();
71  
72          assertNotNull("not null", folderNode);
73          assertEquals("getChildren", 4, folderNode.getChildren().size());
74          assertEquals("getFileName", "files", folderNode.getFileName());
75          assertEquals("getFileLength", -1, folderNode.getFileLength());
76          assertEquals("getLastModified", new Date(this.folderContainingTestResources.lastModified()),
77                  folderNode.getLastModified());
78          assertEquals("getDirectory", folderContainingTestResources.getAbsoluteFile().getParent(),
79                  folderNode.getDirectory());
80          assertEquals("canRead", true, folderNode.canRead());
81          assertEquals("getAbsolutePath", folderContainingTestResources.getAbsolutePath(), folderNode.getAbsolutePath());
82          assertEquals("getChildCount", 4, folderNode.getChildCount());
83          assertEquals("getChild", "2.txt", folderNode.getChild(2).getFileName());
84          assertEquals("getChild", "2.txt", folderNode.getChild("2.txt").getFileName());
85          assertEquals("isLeaf", false, folderNode.isLeaf());
86          assertEquals("parent", "test-classes", folderNode.getParent().toString());
87          assertEquals("model", this.fileDataTreeViewModel, folderNode.treeModel);
88  
89          // All children have this as a parent, and the same model.
90          for (TreeFileDataNode tfdn : (List<TreeFileDataNode>) (List<?>) folderNode.getChildren()) {
91              assertEquals("parent for " + tfdn, folderNode, tfdn.getParent());
92              if (tfdn instanceof FileDataNode) {
93                  assertEquals("parent for " + tfdn, folderNode.treeModel, ((FileDataNode) tfdn).treeModel);
94              } else {
95                  assertEquals("parent for " + tfdn, folderNode.treeModel, ((FolderNode) tfdn).treeModel);
96              }
97          }
98  
99      }
100 
101     @Test(expected = IllegalArgumentException.class)
102     public void testFolderNodeFile_KO() {
103         addTestNodesToTree();
104 
105         folderNode = new FolderNode(new File(folderContainingTestResources, "1.txt"), uploadPolicy,
106                 this.fileDataTreeViewModel, this.filePanelFlatDataModel2);
107     }
108 
109     @Test(expected = IllegalAccessError.class)
110     public void testAppendFileProperties() throws JUploadIOException {
111         addTestNodesToTree();
112 
113         folderNode.appendFileProperties(new ByteArrayEncoderHTTP(uploadPolicy), 1);
114     }
115 
116     @Test(expected = IllegalAccessError.class)
117     public void testAppendFileExtension() throws JUploadIOException {
118         addTestNodesToTree();
119 
120         assertEquals("getFileExtension", "txt", folderNode.getFileExtension());
121     }
122 
123     @Test(expected = IllegalAccessError.class)
124     public void testBeforeUpload() throws JUploadException {
125         addTestNodesToTree();
126 
127         folderNode.beforeUpload(null);
128     }
129 
130     @Test(expected = IllegalAccessError.class)
131     public void testGetUploadLength() {
132         addTestNodesToTree();
133 
134         folderNode.getUploadLength();
135     }
136 
137     @Test(expected = IllegalAccessError.class)
138     public void testAfterUpload() {
139         addTestNodesToTree();
140 
141         folderNode.afterUpload();
142     }
143 
144     @Test(expected = IllegalAccessError.class)
145     public void testGetInputStream() throws JUploadException {
146         addTestNodesToTree();
147 
148         folderNode.getInputStream();
149     }
150 
151     @Test
152     public void testGetUploadFlag() {
153         // Preparation
154         addTestNodesToTree();
155         checkUploadFlag("testGetUploadFlag (before)", true, folderNode);
156 
157         // First shot
158         folderNode.setUploadFlag(false);
159         checkUploadFlag("testGetUploadFlag (false)", false, folderNode);
160 
161         // Second shot
162         folderNode.setUploadFlag(true);
163         checkUploadFlag("testGetUploadFlag (true)", true, folderNode);
164     }
165 
166     /**
167      * 
168      */
169     private void checkUploadFlag(String msg, boolean expected, FolderNode node) {
170         assertEquals(msg + " (node) for " + node, expected, node.getUploadFlag());
171         for (TreeFileDataNode tfdn : node.children) {
172             if (tfdn instanceof FolderNode) {
173                 checkUploadFlag(msg + " (" + tfdn + ") ", expected, (FolderNode) tfdn);
174             } else {
175                 assertEquals(msg + " (children) for " + tfdn, expected, tfdn.getUploadFlag());
176             }
177         }// for
178     }
179 
180     @Test(expected = IllegalAccessError.class)
181     public void testGetMD5() throws JUploadException {
182         addTestNodesToTree();
183 
184         folderNode.getMD5();
185     }
186 
187     @Test(expected = IllegalAccessError.class)
188     public void testGetMimeType() {
189         addTestNodesToTree();
190 
191         folderNode.getMimeType();
192     }
193 
194     @Test(expected = IllegalAccessError.class)
195     public void testIsPreparedForUpload() {
196         addTestNodesToTree();
197 
198         folderNode.isPreparedForUpload();
199     }
200 
201     @Test
202     public void testGetChildOrCreateIt() {
203         addTestNodesToTree();
204         assertEquals("getChildCount", 4, folderNode.getChildCount());
205 
206         // Adding a new FileDataNode
207         FileDataNode fdn4 = (FileDataNode) folderNode.addChild(new FileDataNode(new DefaultFileData(new File(
208                 folderContainingTestResources, "4.txt"), uploadPolicy)));
209         assertEquals("getChildCount (fdn4)", 5, folderNode.getChildCount());
210         assertEquals("getFileName (fdn4)", "4.txt", fdn4.getFileName());
211         assertEquals("parent (fdn4)", folderNode, fdn4.getParent());
212         assertEquals("model (fdn4)", this.fileDataTreeViewModel, fdn4.treeModel);
213 
214         // Getting an existing FileDatanode.
215         FileDataNode fdn3 = (FileDataNode) folderNode.addChild(new FileDataNode(new DefaultFileData(new File(
216                 folderContainingTestResources, "3.txt"), uploadPolicy)));
217         assertEquals("getChildCount (fdn3)", 5, folderNode.getChildCount());
218         assertEquals("getFileName (fdn3)", "3.txt", fdn3.getFileName());
219         assertEquals("parent (fdn3)", folderNode, (FolderNode) fdn3.getParent());
220         assertEquals("model (fdn3)", this.fileDataTreeViewModel, fdn3.treeModel);
221     }
222 
223     @Test
224     public void testGetSubfolderOrCreateIt() throws JUploadExceptionStopAddingFiles {
225         addTestNodesToTree();
226         // Adding a new FolderNode
227         FolderNode fn4 = (FolderNode) folderNode.getSubfolderOrCreateIt(new File(folderContainingTestResources,
228                 "level11"));
229         assertEquals("getChildCount (folderNode fn4)", 5, folderNode.getChildCount());
230         assertEquals("getChildCount (fn4)", 0, fn4.getChildCount());
231         assertEquals("getFileName (fn4)", "level11", fn4.getFileName());
232         assertEquals("parent (fdn4)", folderNode, fn4.getParent());
233         assertEquals("model (fdn4)", this.fileDataTreeViewModel, fn4.treeModel);
234 
235         // Adding an existing FolderNode
236         FolderNode fn5 = (FolderNode) folderNode.getSubfolderOrCreateIt(new File(folderContainingTestResources,
237                 "level1"));
238         assertEquals("getChildCount (folderNode fn5)", 5, folderNode.getChildCount());
239         assertEquals("getChildCount (fn5)", 1, fn5.getChildCount());
240         assertEquals("getFileName (fn5)", "level1", fn5.getFileName());
241         assertTrue("parent (fn5)", folderNode == fn5.getParent());
242         assertTrue("model (fn5)", this.fileDataTreeViewModel == fn5.treeModel);
243     }
244 
245     @Test
246     public void testRemove() {
247         addTestNodesToTree();
248         // Checks before
249         assertEquals("before children", 4, folderNode.getChildCount());
250 
251         // Let's remove one child (FileDataNode)
252         FileDataNode fdn1 = (FileDataNode) folderNode.children.get(1);
253         assertEquals("before removing fdn1 (children count)" + fdn1, 4, folderNode.getChildCount());
254         assertEquals("before removing fdn1 (fdn children count)" + fdn1, 0, fdn1.getChildCount());
255         assertEquals("before removing fdn1 (fdn parent)" + fdn1, folderNode, fdn1.parent);
256         assertEquals("before removing fdn1 (fdn model)" + fdn1, folderNode.treeModel, fdn1.treeModel);
257         this.folderNode.removeChild(fdn1);
258         assertEquals("before removing fdn1 (children count)" + fdn1, 3, folderNode.getChildCount());
259         assertEquals("before removing fdn1 (fdn children count)" + fdn1, 0, fdn1.getChildCount());
260         assertEquals("before removing fdn1 (fdn parent)" + fdn1, null, fdn1.parent);
261         assertEquals("before removing fdn1 (fdn model)" + fdn1, null, fdn1.treeModel);
262 
263         // Let's try to remove an item which is not a child
264         FileDataNode fdn2 = new FileDataNode(new DefaultFileData(new File("files/5.txt"), uploadPolicy));
265         try {
266             this.folderNode.removeChild(fdn2);
267             fail("A IllegalArgumentException should have been raised");
268         } catch (IllegalArgumentException e) {
269             // OK !
270         }
271 
272         // Let's try to remove a subfolder
273         // Let's find a subfolder of fdn3. It should also be cleared by the removal of fdn3 (to avoid memory leak)
274         FolderNode fdn3 = null;
275         for (MyTreeNode mtn : folderNode.getChildren()) {
276             if (mtn instanceof FolderNode) {
277                 fdn3 = (FolderNode) mtn;
278                 break;
279             }
280         }
281         assertNotNull("There must be at least on subFolder" + fdn3, fdn3);
282         assertTrue("The subFolder should have at least one child " + fdn3, fdn3.getChildCount() > 0);
283         this.folderNode.removeChild(fdn3);
284         assertEquals("after removing fdn3 (children count)" + fdn3, 2, folderNode.getChildCount());
285         assertEquals("after removing fdn3 (fdn children count)" + fdn3, 0, fdn3.getChildCount());
286         assertEquals("after removing fdn3 (fdn parent)" + fdn3, null, fdn3.parent);
287         assertEquals("after removing fdn3 (fdn model)" + fdn3, null, fdn3.treeModel);
288         assertNotNull("after removing - There must be at least on subFolder", fdn3);
289         assertEquals("after removing - The subFolder should have at least on child", 0, fdn3.getChildCount());
290         assertEquals("after removing fdn3 (subFolder1 parent)" + fdn3, null, fdn3.parent);
291         assertEquals("after removing fdn3 (subFolder1 model)" + fdn3, null, fdn3.treeModel);
292 
293         // Let's remove the two last children
294         this.folderNode.removeChild(folderNode.children.get(0));
295         this.folderNode.removeChild(folderNode.children.get(0));
296         assertEquals("after removing last child (children count)", 0, folderNode.getChildCount());
297         assertEquals("after removing last child (fdn parent)", null, fdn3.parent);
298         assertEquals("after removing last child (fdn model)", null, fdn3.treeModel);
299     }
300 
301     /**
302      * @throws JUploadExceptionStopAddingFiles
303      */
304     @Test
305     public void testAddChildFile() throws JUploadExceptionStopAddingFiles {
306         // Preparation
307         assertEquals("nb children Before", 0, folderNode.getChildCount());
308         File subfolderLevel11 = new File(folderContainingTestResources, "level11");
309         File fsubsubfolderLevel111 = new File(subfolderLevel11, "level111");
310 
311         // Go, go, go
312         TreeFileDataNode folderNodeLevel2 = folderNode.addChild(fsubsubfolderLevel111);
313 
314         // Verification
315         assertEquals("nb children After (1)", 1, folderNode.getChildCount());
316         assertEquals("no children for new node (1)", 0, folderNodeLevel2.getChildCount());
317         assertTrue("new node is FolderNode", folderNodeLevel2 instanceof FolderNode);
318 
319         // Preparation
320         File fsubsubfolderFile11 = new File(subfolderLevel11, "11.txt");
321 
322         // Go, go, go
323         TreeFileDataNode fileNodeFile11 = folderNode.addChild(fsubsubfolderFile11);
324 
325         // Verification
326         assertEquals("nb children After (2)", 2, folderNode.getChildCount());
327         assertEquals("no children for new node (2)", 0, fileNodeFile11.getChildCount());
328         assertTrue("new node is FileDataNode", fileNodeFile11 instanceof FileDataNode);
329     }
330 }