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.List ; 25 import org.netbeans.junit.NbTestCase; 26 import org.openide.filesystems.FileObject; 27 import org.openide.filesystems.Repository; 28 29 33 public class FolderChildrenTest extends NbTestCase { 34 35 36 public FolderChildrenTest(String name) { 37 super(name); 38 } 39 40 protected void setUp() throws java.lang.Exception { 41 EditorTestLookup.setLookup( 43 new String [] { 44 "Tmp/" 45 }, 46 getWorkDir(), new Object [] {}, 47 getClass().getClassLoader(), 48 null 49 ); 50 } 51 52 public void testSimple() throws Exception { 53 String fileName = "org-netbeans-modules-editor-mimelookup-DummyClass2LayerFolder.instance"; 54 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName); 55 56 FolderChildren fch = new FolderChildren("Tmp/A/B/C/D"); 57 List files = fch.getChildren(); 58 59 assertEquals("Wrong number of files", 1, files.size()); 60 assertEquals("Wrong file", fileName, ((FileObject) files.get(0)).getNameExt()); 61 62 fch = new FolderChildren("Tmp/X/Y/Z"); 63 files = fch.getChildren(); 64 65 assertEquals("Wrong number of files", 0, files.size()); 66 } 67 68 public void testAddingFolders() throws Exception { 69 String fileName = "org-netbeans-modules-editor-mimelookup-DummyClass2LayerFolder.instance"; 70 FolderChildren fch = new FolderChildren("Tmp/A/B/C/D"); 71 List files = fch.getChildren(); 72 73 assertEquals("Wrong number of files", 0, files.size()); 74 75 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName); 76 77 files = fch.getChildren(); 78 assertEquals("Wrong number of files", 1, files.size()); 79 assertEquals("Wrong file", fileName, ((FileObject) files.get(0)).getNameExt()); 80 } 81 82 public void testRemovingFolders() throws Exception { 83 String fileName = "org-netbeans-modules-editor-mimelookup-DummyClass2LayerFolder.instance"; 84 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName); 85 86 FolderChildren fch = new FolderChildren("Tmp/A/B/C/D"); 87 List files = fch.getChildren(); 88 89 assertEquals("Wrong number of files", 1, files.size()); 90 assertEquals("Wrong file", fileName, ((FileObject) files.get(0)).getNameExt()); 91 92 TestUtilities.deleteFile(getWorkDir(), "Tmp/A/"); 93 94 files = fch.getChildren(); 95 assertEquals("Wrong number of files", 0, files.size()); 96 } 97 98 public void testMultipleAddRemove() throws Exception { 99 for (int i = 0; i < 7; i++) { 100 testAddingFolders(); 101 testRemovingFolders(); 102 } 103 } 104 105 public void testChangeEvents() throws Exception { 106 String fileName = "org-netbeans-modules-editor-mimelookup-DummyClass2LayerFolder.instance"; 107 FolderChildren fch = new FolderChildren("Tmp/A/B/C/D"); 108 L listener = new L(); 109 fch.addPropertyChangeListener(listener); 110 111 List files = fch.getChildren(); 112 assertEquals("Wrong number of events", 0, listener.changeEventsCnt); 113 assertEquals("Wrong number of files", 0, files.size()); 114 115 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/" + fileName); 116 117 assertEquals("Wrong number of events", 1, listener.changeEventsCnt); 118 assertEquals("Wrong event", FolderChildren.PROP_CHILDREN, listener.lastEvent.getPropertyName()); 119 files = fch.getChildren(); 120 assertEquals("Wrong number of files", 1, files.size()); 121 assertEquals("Wrong file", fileName, ((FileObject) files.get(0)).getNameExt()); 122 123 listener.reset(); 124 125 TestUtilities.deleteFile(getWorkDir(), "Tmp/A/"); 126 127 assertEquals("Wrong number of events", 1, listener.changeEventsCnt); 128 assertEquals("Wrong event", FolderChildren.PROP_CHILDREN, listener.lastEvent.getPropertyName()); 129 files = fch.getChildren(); 130 assertEquals("Wrong number of files", 0, files.size()); 131 } 132 133 public void testEventsWithMultipleChanges() throws Exception { 134 for (int i = 0; i < 11; i++) { 135 testChangeEvents(); 136 } 137 } 138 139 public void testEmptyFolder() throws Exception { 140 FolderChildren fch = new FolderChildren("Tmp/A/B/C/D"); 141 L listener = new L(); 142 fch.addPropertyChangeListener(listener); 143 144 List files = fch.getChildren(); 145 assertEquals("Wrong number of events", 0, listener.changeEventsCnt); 146 assertEquals("Wrong number of files", 0, files.size()); 147 148 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/"); 149 150 assertEquals("Wrong number of events", 0, listener.changeEventsCnt); 151 files = fch.getChildren(); 152 assertEquals("Wrong number of files", 0, files.size()); 153 154 listener.reset(); 155 156 TestUtilities.deleteFile(getWorkDir(), "Tmp/A/"); 157 158 assertEquals("Wrong number of events", 0, listener.changeEventsCnt); 159 files = fch.getChildren(); 160 assertEquals("Wrong number of files", 0, files.size()); 161 } 162 163 public void testAttributeChanges() throws Exception { 164 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/"); 165 FolderChildren fch = new FolderChildren("Tmp/A/B/C/D"); 166 L listener = new L(); 167 fch.addPropertyChangeListener(listener); 168 169 List files = fch.getChildren(); 170 assertEquals("Wrong number of events", 0, listener.changeEventsCnt); 171 assertEquals("Wrong number of files", 0, files.size()); 172 173 FileObject f = Repository.getDefault().getDefaultFileSystem().findResource("Tmp/A/B/C/D"); 174 assertNotNull("Can't find the folder", f); 175 176 f.setAttribute("attrName", "attrValue"); 177 178 assertEquals("Wrong number of events", 1, listener.changeEventsCnt); 179 assertEquals("Wrong event", FolderChildren.PROP_ATTRIBUTES, listener.lastEvent.getPropertyName()); 180 files = fch.getChildren(); 181 assertEquals("Wrong number of files", 0, files.size()); 182 183 listener.reset(); 184 185 f.setAttribute("attrName", null); 186 187 assertEquals("Wrong number of events", 1, listener.changeEventsCnt); 188 assertEquals("Wrong event", FolderChildren.PROP_ATTRIBUTES, listener.lastEvent.getPropertyName()); 189 files = fch.getChildren(); 190 assertEquals("Wrong number of files", 0, files.size()); 191 } 192 193 private static class L implements PropertyChangeListener { 194 public int changeEventsCnt = 0; 195 public PropertyChangeEvent lastEvent = null; 196 197 public void propertyChange(PropertyChangeEvent evt) { 198 changeEventsCnt++; 199 lastEvent = evt; 200 } 201 202 public void reset() { 203 changeEventsCnt = 0; 204 lastEvent = null; 205 } 206 } } 208 | Popular Tags |