1 19 20 package org.netbeans.modules.editor.mimelookup.impl; 21 22 import java.util.Collection ; 23 import org.netbeans.junit.NbTestCase; 24 import org.netbeans.spi.editor.mimelookup.Class2LayerFolder; 25 import org.openide.util.Lookup; 26 import org.openide.util.LookupEvent; 27 import org.openide.util.LookupListener; 28 29 33 public class FolderPathLookupTest extends NbTestCase { 34 35 36 public FolderPathLookupTest(String name) { 37 super(name); 38 } 39 40 protected void setUp() throws java.lang.Exception { 41 EditorTestLookup.setLookup(new String [0], getWorkDir(), new Object [] {}, 43 getClass().getClassLoader(), 44 null 45 ); 46 } 47 48 protected void tearDown() { 49 TestUtilities.gc(); 50 } 51 52 public void testSimple() throws Exception { 53 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance"); 54 55 Lookup lookup = new FolderPathLookup(new String [] { "Tmp/A/B/C/D" }); 57 Collection instances = lookup.lookupAll(Class2LayerFolder.class); 58 59 assertEquals("Wrong number of instances", 1, instances.size()); 60 assertEquals("Wrong instance", DummyClass2LayerFolder.class, instances.iterator().next().getClass()); 61 62 lookup = new FolderPathLookup(new String [] { "Tmp/X/Y/Z" }); 64 instances = lookup.lookupAll(Object .class); 65 66 assertEquals("Wrong number of instances", 0, instances.size()); 67 } 68 69 public void testAddingFolders() throws Exception { 70 Lookup lookup = new FolderPathLookup(new String [] { "Tmp/A/B/C/D" }); 72 Collection instances = lookup.lookupAll(Class2LayerFolder.class); 73 74 assertEquals("Wrong number of instances", 0, instances.size()); 75 76 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance"); 78 79 instances = lookup.lookupAll(Class2LayerFolder.class); 80 assertEquals("Wrong number of instances", 1, instances.size()); 81 assertEquals("Wrong instance", DummyClass2LayerFolder.class, instances.iterator().next().getClass()); 82 } 83 84 public void testRemovingFolders() throws Exception { 85 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance"); 86 87 Lookup lookup = new FolderPathLookup(new String [] { "Tmp/A/B/C/D" }); 89 Collection instances = lookup.lookupAll(Class2LayerFolder.class); 90 91 assertEquals("Wrong number of instances", 1, instances.size()); 92 assertEquals("Wrong instance", DummyClass2LayerFolder.class, instances.iterator().next().getClass()); 93 94 TestUtilities.deleteFile(getWorkDir(), "Tmp"); 96 97 instances = lookup.lookupAll(Class2LayerFolder.class); 98 assertEquals("Wrong number of instances", 0, instances.size()); 99 } 100 101 public void testChangeEvents() throws Exception { 102 Lookup.Result lr = new FolderPathLookup(new String [] { "Tmp/A/B/C/D" }).lookupResult(Class2LayerFolder.class); 103 L listener = new L(); 104 lr.addLookupListener(listener); 105 106 Collection instances = lr.allInstances(); 107 assertEquals("Wrong number of instances", 0, instances.size()); 108 109 TestUtilities.createFile(getWorkDir(), "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance"); 111 112 assertEquals("Wrong number of events", 1, listener.resultChangedCnt); 113 114 instances = lr.allInstances(); 115 assertEquals("Wrong number of instances", 1, instances.size()); 116 assertEquals("Wrong instance", DummyClass2LayerFolder.class, instances.iterator().next().getClass()); 117 118 listener.resultChangedCnt = 0; 120 121 TestUtilities.deleteFile(getWorkDir(), "Tmp"); 123 124 assertEquals("Wrong number of events", 1, listener.resultChangedCnt); 125 126 instances = lr.allInstances(); 127 assertEquals("Wrong number of instances", 0, instances.size()); 128 } 129 130 private static final class L implements LookupListener { 131 public int resultChangedCnt = 0; 132 public void resultChanged(LookupEvent ev) { 133 resultChangedCnt++; 134 } 135 } 136 } 137 | Popular Tags |