1 19 20 package org.netbeans.modules.editor.mimelookup.impl; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.Repository; 29 30 34 public class CompoundFolderChildrenTest extends NbTestCase { 35 36 37 public CompoundFolderChildrenTest(String name) { 38 super(name); 39 } 40 41 protected void setUp() throws java.lang.Exception { 42 EditorTestLookup.setLookup( 44 new String [] { 45 "Tmp/" 46 }, 47 getWorkDir(), new Object [] {}, 48 getClass().getClassLoader(), 49 null 50 ); 51 } 52 53 55 public void testCollecting() throws Exception { 56 String fileName1 = "file-on-layer-1.instance"; 57 String fileName2 = "file-on-layer-2.instance"; 58 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName1); 59 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/" + fileName2); 60 61 CompoundFolderChildren cfch = new CompoundFolderChildren(new String [] { "Tmp/A/B/C/D", "Tmp/A/B" }, false); 62 List files = cfch.getChildren(); 63 64 assertEquals("Wrong number of files", 2, files.size()); 65 assertNotNull("Files do not contain " + fileName1, findFileByName(files, fileName1)); 66 assertNotNull("Files do not contain " + fileName2, findFileByName(files, fileName2)); 67 68 cfch = new CompoundFolderChildren(new String [] { "Tmp/X/Y/Z" }); 69 files = cfch.getChildren(); 70 71 assertEquals("Wrong number of files", 0, files.size()); 72 } 73 74 76 public void testHidingSameFilesOnLowerLayers() throws Exception { 77 String fileName = "some-file.instance"; 78 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName); 79 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/" + fileName); 80 81 CompoundFolderChildren cfch = new CompoundFolderChildren(new String [] { "Tmp/A/B/C/D", "Tmp/A/B" }, false); 82 List files = cfch.getChildren(); 83 84 assertEquals("Wrong number of files", 1, files.size()); 85 assertEquals("Wrong layerA file", fileName, ((FileObject) files.get(0)).getNameExt()); 86 } 87 88 90 122 public void testFilesHiddenByAttribute() throws Exception { 123 String fileName1 = "file-on-layer-A.instance"; 124 String fileName2 = "file-on-layer-B.instance"; 125 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName1); 126 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/" + fileName2); 127 TestUtilities.createFile(getWorkDir(), "Tmp/A/" + fileName2); 128 129 FileObject f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/" + fileName2); 131 assertNotNull("The hidden file on the second layer does not exist", f); 132 133 f.setAttribute("hidden", Boolean.TRUE); 136 137 f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/" + fileName2); 138 assertNotNull("The original file on the third layer that should be hidden does not exist", f); 139 140 CompoundFolderChildren cfch = new CompoundFolderChildren(new String [] { "Tmp/A/B/C/D", "Tmp/A/B", "Tmp/A" }, false); 142 List files = cfch.getChildren(); 143 144 assertEquals("Wrong number of files", 1, files.size()); 145 assertEquals("Wrong layerA file", fileName1, ((FileObject) files.get(0)).getNameExt()); 146 } 147 148 150 public void testSorting() throws Exception { 151 String fileName1 = "file-1.instance"; 153 String fileName2 = "file-2.instance"; 154 String fileName3 = "file-3.instance"; 155 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName1); 156 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/" + fileName2); 157 TestUtilities.createFile(getWorkDir(), "Tmp/A/" + fileName3); 158 159 FileObject layer1 = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/C/D"); 161 FileObject layer2 = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B"); 162 FileObject layer3 = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A"); 163 164 layer1.setAttribute("file-3.instance/file-1.instance", Boolean.TRUE); 165 layer2.setAttribute("file-2.instance/file-3.instance", Boolean.TRUE); 166 167 CompoundFolderChildren cfch = new CompoundFolderChildren(new String [] { "Tmp/A/B/C/D", "Tmp/A/B", "Tmp/A" }, false); 169 List files = cfch.getChildren(); 170 171 assertEquals("Wrong number of files", 3, files.size()); 172 assertEquals("Wrong first file", fileName2, ((FileObject) files.get(0)).getNameExt()); 173 assertEquals("Wrong second file", fileName3, ((FileObject) files.get(1)).getNameExt()); 174 assertEquals("Wrong third file", fileName1, ((FileObject) files.get(2)).getNameExt()); 175 } 176 177 179 private FileObject findFileByName(List files, String nameExt) { 180 for (Iterator i = files.iterator(); i.hasNext(); ) { 181 FileObject f = (FileObject) i.next(); 182 if (nameExt.equals(f.getNameExt())) { 183 return f; 184 } 185 } 186 return null; 187 } 188 189 private static class L implements PropertyChangeListener { 190 public int changeEventsCnt = 0; 191 public PropertyChangeEvent lastEvent = null; 192 193 public void propertyChange(PropertyChangeEvent evt) { 194 changeEventsCnt++; 195 lastEvent = evt; 196 } 197 198 public void reset() { 199 changeEventsCnt = 0; 200 lastEvent = null; 201 } 202 } } 204 | Popular Tags |