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.ArrayList ; 25 import java.util.Set ; 26 import org.netbeans.junit.NbTestCase; 27 28 32 public class ClassInfoStorageTest extends NbTestCase { 33 34 35 public ClassInfoStorageTest(String name) { 36 super(name); 37 } 38 39 protected void setUp() throws java.lang.Exception { 40 EditorTestLookup.setLookup(new String [0], getWorkDir(), new Object [] {}, 42 getClass().getClassLoader(), 43 null 44 ); 45 } 46 47 public void testNoMapper() { 48 ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()); 49 assertTrue("There should be no mapper registered", isEmpty(info)); 50 } 51 52 public void testDummyMapper() throws Exception { 53 TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance"); 54 TestUtilities.sleepForWhile(); 55 56 ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()); 57 assertNotNull("The mapper not found", info); 58 assertEquals("Wrang mapper class", DummySetting.class.getName(), info.getClassName()); 59 assertEquals("Wrong wrapper extra path", "DummyFolder", info.getExtraPath()); 60 assertEquals("Wrong instance provider class", DummyInstanceProvider.class.getName(), info.getInstanceProviderClass()); 61 assertNotNull("Instance provider should not be null", info.getInstanceProvider()); 62 assertTrue("Wrong instance provider", info.getInstanceProvider() instanceof DummyInstanceProvider); 63 } 64 65 public void testAddingMapper() throws Exception { 66 ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()); 67 assertTrue("There should be no mapper registered", isEmpty(info)); 68 69 L listener = new L(); 70 ClassInfoStorage.getInstance().addPropertyChangeListener(listener); 71 try { 72 TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance"); 74 TestUtilities.sleepForWhile(); 75 76 assertEquals("Wrong number of change events", 1, listener.changeEventsCnt); 77 assertNotNull("Invalid change event", listener.events.get(0)); 78 assertEquals("Wrong change event", 79 ClassInfoStorage.PROP_CLASS_INFO_ADDED, ((PropertyChangeEvent )listener.events.get(0)).getPropertyName()); 80 81 Set value = (Set ) ((PropertyChangeEvent )listener.events.get(0)).getNewValue(); 82 assertEquals("Invalid number of class in the change event", 1, value.size()); 83 assertTrue("Wrong change event value", value.contains(DummySetting.class.getName())); 84 } finally { 85 ClassInfoStorage.getInstance().removePropertyChangeListener(listener); 86 } 87 88 info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()); 89 checkInfo(info, "DummyFolder", DummySetting.class, DummyInstanceProvider.class); 90 } 91 92 public void testRemovingMapper() throws Exception { 93 TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance"); 94 TestUtilities.sleepForWhile(); 95 96 ClassInfoStorage.Info info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()); 97 checkInfo(info, "DummyFolder", DummySetting.class, DummyInstanceProvider.class); 98 99 L listener = new L(); 100 ClassInfoStorage.getInstance().addPropertyChangeListener(listener); 101 try { 102 TestUtilities.deleteFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolderWithInstanceProvider.instance"); 104 TestUtilities.sleepForWhile(); 105 106 assertEquals("Wrong number of change events", 1, listener.changeEventsCnt); 107 assertNotNull("Invalid change event", listener.events.get(0)); 108 assertEquals("Wrong change event", 109 ClassInfoStorage.PROP_CLASS_INFO_REMOVED, ((PropertyChangeEvent )listener.events.get(0)).getPropertyName()); 110 111 Set value = (Set ) ((PropertyChangeEvent )listener.events.get(0)).getNewValue(); 112 assertEquals("Invalid number of class in the change event", 1, value.size()); 113 assertTrue("Wrong change event value", value.contains(DummySetting.class.getName())); 114 } finally { 115 ClassInfoStorage.getInstance().removePropertyChangeListener(listener); 116 } 117 118 info = ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()); 119 assertTrue("There should be no mapper registered", isEmpty(info)); 120 } 121 122 private void checkInfo(ClassInfoStorage.Info info, String extraPath, Class infoClass, Class instanceProviderClass) { 123 assertNotNull("The mapper not found", info); 124 assertEquals("Wrang mapper class", infoClass.getName(), info.getClassName()); 125 assertEquals("Wrong wrapper extra path", extraPath, info.getExtraPath()); 126 assertEquals("Wrong instance provider class", instanceProviderClass.getName(), info.getInstanceProviderClass()); 127 assertNotNull("Instance provider should not be null", info.getInstanceProvider()); 128 assertEquals("Wrong instance provider", instanceProviderClass, info.getInstanceProvider().getClass()); 129 } 130 131 private boolean isEmpty(ClassInfoStorage.Info info) { 132 return info.getExtraPath().length() == 0 && 133 info.getInstanceProviderClass() == null && 134 info.getInstanceProvider() == null; 135 } 136 137 private static class L implements PropertyChangeListener { 138 139 public int changeEventsCnt = 0; 140 public ArrayList events = new ArrayList (); 141 142 public void propertyChange(PropertyChangeEvent evt) { 143 changeEventsCnt++; 144 events.add(evt); 145 } 146 147 public void reset() { 148 changeEventsCnt = 0; 149 events.clear(); 150 } 151 } 152 } 153 | Popular Tags |