View Javadoc
1   //
2   // $Id$
3   //
4   // jupload - A file upload applet.
5   //
6   // Copyright 2015 The JUpload Team
7   //
8   // Created: 8 févr. 2015
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  
26  package wjhk.jupload2.gui.filepanel;
27  
28  import static org.junit.Assert.*;
29  
30  import java.awt.Point;
31  import java.io.File;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  import org.junit.Before;
36  import org.junit.Ignore;
37  import org.junit.Test;
38  
39  import wjhk.jupload2.filedata.DefaultFileData;
40  import wjhk.jupload2.filedata.FileData;
41  import wjhk.jupload2.gui.filepanel.treeview.FileDataNode;
42  import wjhk.jupload2.gui.filepanel.treeview.FileDataTreeViewModel;
43  import wjhk.jupload2.gui.filepanel.treeview.FolderNode;
44  import wjhk.jupload2.gui.filepanel.treeview.RootNode;
45  import wjhk.jupload2.gui.filepanel.treeview.TreeFileDataNode;
46  import wjhk.jupload2.upload.AbstractJUploadTestHelper;
47  
48  /**
49   * @author etienne_sf
50   */
51  public class FilePanelTableImpTest extends AbstractJUploadTestHelper {
52  
53      FilePanelTableImp filePanelTableImp = null;
54  
55      /**
56       * @throws java.lang.Exception
57       */
58      @Before
59      public void setUp() throws Exception {
60          filePanelTableImp = new FilePanelTableImp(juploadPanel, uploadPolicy);
61          this.fileDataTreeViewModel = (FileDataTreeViewModel) ((FileDataTreeViewModel) this.filePanelTableImp.treeModel);
62          assertEquals("check root",
63                  (TreeFileDataNode) ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot(),
64                  (TreeFileDataNode) this.fileDataTreeViewModel.getRoot());
65  
66          File[] files = new File[2];
67          files[0] = getTestFile("files/level1/level2");
68          files[1] = getTestFile("files/level1/level22");
69          filePanelTableImp.addFiles(files);
70  
71          // For this test, we need both table to be 'visible'. So that they get updated when the file list changes.
72          // filePanelTableImp.flatTable.setVisible(true);
73          // filePanelTableImp.treeTable.setVisible(true);
74      }
75  
76      @Test
77      public void testConstructor() {
78          assertNotNull("flatTable", filePanelTableImp.flatTable);
79          assertNotNull("flatModel", filePanelTableImp.flatModel);
80          assertNotNull("treeModel", ((FileDataTreeViewModel) this.filePanelTableImp.treeModel));
81          assertNotNull("treeTable", filePanelTableImp.treeTable);
82          assertNotNull("root", ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot());
83      }
84  
85      /**
86       * Test method for {@link wjhk.jupload2.gui.filepanel.FilePanelTableImp#addFiles(java.io.File[], java.io.File)}.
87       */
88      @SuppressWarnings("unchecked")
89      @Test
90      public void testAddFiles() {
91          // Check of the original call to addFiles
92          assertEquals("before 1", 3, filePanelTableImp.getFiles().size());
93          assertEquals("before 2", 3, filePanelTableImp.getFilesLength());
94          // The number of children for the absolute root depends on the location of the project on the local file system
95          assertEquals("filePanelTableImp.addFiles (treeModel.getAbsoluteRoot())",
96                  nbSubfoldersForSrcTestResourcesFiles + 8, ((FileDataTreeViewModel) this.filePanelTableImp.treeModel)
97                          .getAbsoluteRoot().getTotalChildCount());
98          // The number of children for the visible root is an absolute numbre (doesn't depend on the location of the
99          // project on the local file system)
100         assertEquals("filePanelTableImp.addFiles (visible root)", 7,
101                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getRoot().getTotalChildCount());
102         TreeFileDataNode tfdn = ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot();
103         assertTrue("tfdn instanceof RootNode", tfdn instanceof RootNode);
104         // The tree root must the good one (not the absolute one)
105         assertEquals("treeModel.getRoot().getFileName()", "level1",
106                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getRoot().getFileName());
107         // We go from RootNode to files/level1
108         for (int i = 0; i <= nbSubfoldersForSrcTestResourcesFiles; i += 1) {
109             assertEquals("One child for " + tfdn.getFileName(), 1, tfdn.getChildCount());
110             // For folder other than the FS root (/ for unix, and C:\ for windows, for instance), we check that the
111             // filenames doesn't contain a file separator
112             if (i > 1) {
113                 assertFalse("No file separator 1 " + tfdn.getFileName(), tfdn.getFileName().contains("/"));
114                 assertFalse("No file separator 2 " + tfdn.getFileName(), tfdn.getFileName().contains("\\"));
115             }
116             tfdn = (TreeFileDataNode) tfdn.getChildren().get(0);
117             assertTrue("FolderNode for " + tfdn.getFileName(), tfdn instanceof FolderNode);
118         }// for
119          // Let's test the specific loading of the use case. Two folders have been added: files/level1/level2 and
120          // files/level1/level22
121 
122         // Check of files/level1
123         assertEquals("Two children for " + tfdn.getFileName(), 2, tfdn.getChildCount());
124         for (FolderNode sub : (List<FolderNode>) (List<?>) tfdn.getChildren()) {
125             assertFalse("No file separator 1 " + sub.getFileName(), sub.getFileName().contains("/"));
126             assertFalse("No file separator 2 " + sub.getFileName(), sub.getFileName().contains("\\"));
127         }
128         // Check of files/level1/level2
129         TreeFileDataNode tfdnLevel2 = (TreeFileDataNode) tfdn.getChildren().get(0);
130         checkTreeFileDataNode(tfdnLevel2, FolderNode.class, "level2", 2, 4);
131         checkTreeFileDataNode((TreeFileDataNode) tfdnLevel2.getChildren().get(0), FolderNode.class, "level3", 1, 1);
132         checkTreeFileDataNode((TreeFileDataNode) tfdnLevel2.getChildren().get(0).getChildren().get(0),
133                 FileDataNode.class, "ATestFile3.txt", 0, 0);
134         checkTreeFileDataNode((TreeFileDataNode) tfdnLevel2.getChildren().get(1), FolderNode.class, "level33", 1, 1);
135         checkTreeFileDataNode((TreeFileDataNode) tfdnLevel2.getChildren().get(1).getChildren().get(0),
136                 FileDataNode.class, "ATestFile33.txt", 0, 0);
137         // Check of files/level1/level22
138         TreeFileDataNode tfdnLevel22 = (TreeFileDataNode) tfdn.getChildren().get(1);
139         checkTreeFileDataNode(tfdnLevel22, FolderNode.class, "level22", 1, 1);
140         checkTreeFileDataNode((TreeFileDataNode) tfdnLevel22.getChildren().get(0), FileDataNode.class,
141                 "ATestFile22.txt", 0, 0);
142 
143         // go, go, go...
144         File[] files = new File[2];
145         files[0] = getTestFile("files/level11");
146         files[1] = getTestFile("files");
147         filePanelTableImp.addFiles(files);
148 
149         // Checks
150         assertEquals("after 1", 18, filePanelTableImp.getFiles().size());
151         assertEquals("after 2", 18, filePanelTableImp.getFilesLength());
152         // The number of children for the absolute root depends on the location of the project on the local file system
153         assertEquals("after 3 (treeModel.getAbsoluteRoot())", nbSubfoldersForSrcTestResourcesFiles + 25,
154                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot().getTotalChildCount());
155         // The number of children for the visible root is an absolute numbre (doesn't depend on the location of the
156         // project on the local file system)
157         assertEquals("after 3 (treeview root)", 25, ((FileDataTreeViewModel) this.filePanelTableImp.treeModel)
158                 .getRoot().getTotalChildCount());
159 
160         // The tree root must the good one (not the absolute one)
161         assertEquals("treeModel.getRoot().getFileName()", "files",
162                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getRoot().getFileName());
163     }
164 
165     private void checkTreeFileDataNode(TreeFileDataNode tfdn, Class<?> clazz, String filename, int childCount,
166             int totalChildCount) {
167         assertTrue("FolderNode for " + tfdn.getFileName(), clazz.isInstance(tfdn));
168         assertEquals("getFileName for " + tfdn.getFileName(), filename, tfdn.getFileName());
169         assertEquals("getChildCount for " + tfdn.getFileName(), childCount, tfdn.getChildCount());
170         assertEquals("getTotalChildCount for " + tfdn.getFileName(), totalChildCount, tfdn.getTotalChildCount());
171     }
172 
173     /**
174      * Test method for {@link wjhk.jupload2.gui.filepanel.FilePanelTableImp#removeSelected()}.
175      */
176     @Test
177     public void testRemoveSelected() {
178         // Preparation
179         assertEquals("before 2", 3, filePanelTableImp.getFilesLength());
180         // check on absolute root
181         assertEquals("before 3", nbSubfoldersForSrcTestResourcesFiles + 8,
182                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot().getTotalChildCount());
183         // check on visible root
184         assertEquals("filePanelTableImp.addFiles (visible root)", 7,
185                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getRoot().getTotalChildCount());
186         filePanelTableImp.flatTable.setRowSelectionInterval(1, 2);
187 
188         // go, go, go...
189         filePanelTableImp.removeSelected();
190 
191         // Checks
192         assertEquals("after 2", 1, filePanelTableImp.getFilesLength());
193         // check on absolute root
194         assertEquals("after 3", nbSubfoldersForSrcTestResourcesFiles + 6,
195                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot().getTotalChildCount());
196         // check on visible root
197         assertEquals("filePanelTableImp.addFiles (visible root)", 5,
198                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getRoot().getTotalChildCount());
199     }
200 
201     /**
202      * Test method for {@link wjhk.jupload2.gui.filepanel.FilePanelTableImp#remove(wjhk.jupload2.filedata.FileData)}.
203      */
204     @Test
205     public void testRemoveFileData() {
206         // Preparation
207         List<FileData> rows = new ArrayList<FileData>(filePanelTableImp.flatModel.getRowCount());
208         for (int i = 0; i < filePanelTableImp.flatModel.getRowCount(); i += 1) {
209             rows.add(filePanelTableImp.flatModel.getFileDataAt(i));
210         }
211         assertEquals("before (flat)", 3, rows.size());
212         assertEquals("before (tree)", nbSubfoldersForSrcTestResourcesFiles + 8,
213                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot().getTotalChildCount());
214         // A useless selection. Should not interfere
215         filePanelTableImp.flatTable.setRowSelectionInterval(1, 2);
216 
217         // go, go, go...
218 
219         // First a useless remove: this file doesn't exist in the hierarchy created by setup().
220         filePanelTableImp.remove(new DefaultFileData(getTestFile("files/level11/11.txt"), uploadPolicy));
221         assertEquals("after useless removal", 3, filePanelTableImp.getFilesLength());
222         assertEquals("after useless removal", nbSubfoldersForSrcTestResourcesFiles + 8,
223                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot().getTotalChildCount());
224 
225         // Then, the real ones
226         filePanelTableImp.remove(rows.get(1));
227         filePanelTableImp.remove(rows.get(2));
228         filePanelTableImp.remove(rows.get(0));
229 
230         // Checks
231         assertEquals("after 2", 0, filePanelTableImp.getFilesLength());
232         assertEquals("after useless removal", nbSubfoldersForSrcTestResourcesFiles + 5,
233                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot().getTotalChildCount());
234     }
235 
236     /**
237      * Test method for {@link wjhk.jupload2.gui.filepanel.FilePanelTableImp#removeAll()}.
238      */
239     @Test
240     public void testRemoveAll() {
241         // Preparation
242         assertEquals("before 1", 3, filePanelTableImp.getFiles().size());
243         assertEquals("before 2", 3, filePanelTableImp.getFilesLength());
244         // check on absolute root
245         assertEquals("before 3", nbSubfoldersForSrcTestResourcesFiles + 8,
246                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot().getTotalChildCount());
247         // check on visible root
248         assertEquals("filePanelTableImp.addFiles (visible root)", 7,
249                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getRoot().getTotalChildCount());
250 
251         // go, go, go...
252         filePanelTableImp.removeAll();
253 
254         // Checks
255         assertEquals("after 1", 0, filePanelTableImp.getFiles().size());
256         assertEquals("after 2", 0, filePanelTableImp.getFilesLength());
257         // check on absolute root
258         assertEquals("before 3", nbSubfoldersForSrcTestResourcesFiles + 5,
259                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getAbsoluteRoot().getTotalChildCount());
260         // check on visible root
261         assertEquals("filePanelTableImp.addFiles (visible root)", 4,
262                 ((FileDataTreeViewModel) this.filePanelTableImp.treeModel).getRoot().getTotalChildCount());
263     }
264 
265     /**
266      * Test method for {@link wjhk.jupload2.gui.filepanel.FilePanelTableImp#clearSelection()}.
267      */
268     @Test
269     public void testClearSelection_FlatView() {
270         // Preparation
271         filePanelTableImp.flatTable.setRowSelectionInterval(1, 2);
272         assertEquals("before 2", 2, filePanelTableImp.flatTable.getSelectedRowCount());
273 
274         // go, go, go...
275         filePanelTableImp.clearSelection();
276 
277         // Checks
278         assertEquals("before 2", 0, filePanelTableImp.flatTable.getSelectedRowCount());
279     }
280 
281     /**
282      * Test method for {@link wjhk.jupload2.gui.filepanel.FilePanelTableImp#clearSelection()}.
283      */
284     @Test
285     @Ignore("Selection is not yet implemented in tree view")
286     public void testClearSelection_TreeView() {
287         fail("not implemented");
288     }
289 
290     /**
291      * Test method for {@link wjhk.jupload2.gui.filepanel.FilePanelTableImp#getFileDataAt(java.awt.Point)}.
292      */
293     @Test
294     public void testGetFileDataAt() {
295         // Preparation
296         Point p = new Point(5, 5);
297 
298         // go, go, go...
299         FileData fd = filePanelTableImp.getFileDataAt(p);
300 
301         // Checks
302         assertEquals("before 2", "ATestFile3.txt", fd.getFileName());
303     }
304 
305 }