1 19 20 package org.netbeans.modules.editor.mimelookup.impl; 21 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.List ; 25 import org.netbeans.api.editor.mimelookup.MimePath; 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.util.Lookup; 28 import org.openide.util.LookupEvent; 29 import org.openide.util.LookupListener; 30 31 35 public class SwitchLookupTest extends NbTestCase { 36 37 38 public SwitchLookupTest(String name) { 39 super(name); 40 } 41 42 protected void setUp() throws java.lang.Exception { 43 EditorTestLookup.setLookup(new String [0], getWorkDir(), new Object [] {}, 45 getClass().getClassLoader(), 46 null 47 ); 48 } 49 50 protected void tearDown() { 51 TestUtilities.gc(); 52 } 53 54 public void testSimple() throws Exception { 55 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance"); 56 TestUtilities.sleepForWhile(); 57 58 Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java")); 60 Collection instances = lookup.lookupAll(DummySetting.class); 61 62 assertEquals("Wrong number of instances", 1, instances.size()); 63 assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass()); 64 65 lookup = new SwitchLookup(MimePath.parse("text/xml")); 67 instances = lookup.lookupAll(Object .class); 68 69 assertEquals("Wrong number of instances", 0, instances.size()); 70 } 71 72 public void testAddingMimePath() throws Exception { 73 Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java")); 75 Lookup.Result result = lookup.lookupResult(DummySetting.class); 76 L listener = new L(); 77 78 result.addLookupListener(listener); 79 Collection instances = result.allInstances(); 80 81 assertEquals("There should be no change events", 0, listener.resultChangedCnt); 82 assertEquals("Wrong number of instances", 0, instances.size()); 83 84 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance"); 86 TestUtilities.sleepForWhile(); 87 88 instances = lookup.lookupAll(DummySetting.class); 90 91 assertEquals("Wrong number of change events", 1, listener.resultChangedCnt); 92 assertEquals("Wrong number of instances", 1, instances.size()); 93 assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass()); 94 } 95 96 public void testRemovingMimePath() throws Exception { 97 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance"); 99 TestUtilities.sleepForWhile(); 100 101 Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java")); 103 Lookup.Result result = lookup.lookupResult(DummySetting.class); 104 L listener = new L(); 105 106 result.addLookupListener(listener); 107 Collection instances = result.allInstances(); 108 109 assertEquals("There should be no change events", 0, listener.resultChangedCnt); 110 assertEquals("Wrong number of instances", 1, instances.size()); 111 assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass()); 112 113 TestUtilities.deleteFile(getWorkDir(), "Editors/text/x-jsp/text"); 115 TestUtilities.sleepForWhile(); 116 117 instances = lookup.lookupAll(DummySetting.class); 119 120 assertEquals("Wrong number of change events", 1, listener.resultChangedCnt); 121 assertEquals("Wrong number of instances", 0, instances.size()); 122 } 123 124 127 public void testHierarchyInheritance() throws Exception { 128 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance"); 130 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/"); 131 TestUtilities.sleepForWhile(); 132 133 { 134 Lookup jspLookup = new SwitchLookup(MimePath.parse("text/x-jsp")); 135 Collection jspInstances = jspLookup.lookupAll(DummySetting.class); 136 assertEquals("Wrong number of instances", 1, jspInstances.size()); 137 assertEquals("Wrong instance", DummySettingImpl.class, jspInstances.iterator().next().getClass()); 138 } 139 140 { 141 Lookup javaLookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java")); 142 Collection javaInstances = javaLookup.lookupAll(DummySetting.class); 143 assertEquals("Wrong number of instances", 1, javaInstances.size()); 144 assertEquals("Wrong instance", DummySettingImpl.class, javaInstances.iterator().next().getClass()); 145 } 146 } 147 148 public void testHierarchyRootInheritance() throws Exception { 149 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/"); 151 TestUtilities.createFile(getWorkDir(), "Editors/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance"); 152 TestUtilities.sleepForWhile(); 153 154 { 155 Lookup lookup = new SwitchLookup(MimePath.parse("")); 156 Collection instances = lookup.lookupAll(DummySetting.class); 157 assertEquals("Wrong number of instances", 1, instances.size()); 158 assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass()); 159 } 160 161 { 162 Lookup jspLookup = new SwitchLookup(MimePath.parse("text/x-jsp")); 163 Collection jspInstances = jspLookup.lookupAll(DummySetting.class); 164 assertEquals("Wrong number of instances", 1, jspInstances.size()); 165 assertEquals("Wrong instance", DummySettingImpl.class, jspInstances.iterator().next().getClass()); 166 } 167 168 { 169 Lookup javaLookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java")); 170 Collection javaInstances = javaLookup.lookupAll(DummySetting.class); 171 assertEquals("Wrong number of instances", 1, javaInstances.size()); 172 assertEquals("Wrong instance", DummySettingImpl.class, javaInstances.iterator().next().getClass()); 173 } 174 } 175 176 public void testHierarchyLeaks() throws Exception { 177 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/"); 179 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance"); 180 TestUtilities.createFile(getWorkDir(), "Editors/text/x-java/"); 181 TestUtilities.sleepForWhile(); 182 183 { 184 Lookup jspLookup = new SwitchLookup(MimePath.parse("text/x-jsp")); 185 Collection jspInstances = jspLookup.lookupAll(DummySetting.class); 186 assertEquals("Wrong number of instances", 0, jspInstances.size()); 187 } 188 189 { 190 Lookup javaLookup = new SwitchLookup(MimePath.parse("text/x-java")); 191 Collection javaInstances = javaLookup.lookupAll(DummySetting.class); 192 assertEquals("Wrong number of instances", 0, javaInstances.size()); 193 } 194 195 { 196 Lookup jspJavaLookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java")); 197 Collection jspJavaInstances = jspJavaLookup.lookupAll(DummySetting.class); 198 assertEquals("Wrong number of instances", 1, jspJavaInstances.size()); 199 assertEquals("Wrong instance", DummySettingImpl.class, jspJavaInstances.iterator().next().getClass()); 200 } 201 202 { 203 Lookup javaJspLookup = new SwitchLookup(MimePath.parse("text/x-java/text/x-jsp")); 204 Collection javaJspInstances = javaJspLookup.lookupAll(DummySetting.class); 205 assertEquals("Wrong number of instances", 0, javaJspInstances.size()); 206 } 207 } 208 209 211 213 public void testReadFromSpecialFolders() throws Exception { 214 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/DummyFolder/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance"); 215 TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance"); 216 TestUtilities.sleepForWhile(); 217 218 Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java")); 219 Collection instances = lookup.lookupAll(DummySetting.class); 220 221 assertEquals("Wrong number of instances", 1, instances.size()); 222 assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass()); 223 } 224 225 227 public void testChangeInMappers() throws Exception { 228 TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/DummyFolder/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance"); 229 TestUtilities.sleepForWhile(); 230 231 Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java")); 232 Lookup.Result result = lookup.lookupResult(DummySetting.class); 233 L listener = new L(); 234 235 result.addLookupListener(listener); 236 Collection instances = result.allInstances(); 237 238 assertEquals("Wrong number of change events", 0, listener.resultChangedCnt); 239 assertEquals("Wrong number of instances", 0, instances.size()); 240 241 TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance"); 243 TestUtilities.sleepForWhile(); 244 245 instances = result.allInstances(); 246 assertEquals("Wrong number of change events", 1, listener.resultChangedCnt); 247 assertEquals("Wrong number of instances", 1, instances.size()); 248 assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass()); 249 250 listener.resultChangedCnt = 0; 252 253 TestUtilities.deleteFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance"); 255 TestUtilities.sleepForWhile(); 256 257 assertEquals("Wrong number of change events", 1, listener.resultChangedCnt); 258 instances = result.allInstances(); 259 assertEquals("Wrong number of instances", 0, instances.size()); 260 } 261 262 264 public void testNoMapper() { 265 MimePath mimePath = MimePath.parse("text/x-jsp/text/x-java/text/x-javadoc"); 266 List paths = SwitchLookup.computePaths(mimePath, null, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath()); 267 checkPaths( 268 Arrays.asList(new String [] { 269 "text/x-jsp/text/x-java/text/x-javadoc", 270 "text/x-javadoc", 271 "text/x-jsp/text/x-java", 272 "text/x-jsp", 273 "" 274 }), 275 paths 276 ); 277 } 278 279 public void testNoMapperCompoundMimeType1() { 280 MimePath mimePath = MimePath.parse("text/x-ant+xml/text/x-java/text/x-javadoc"); 281 List paths = SwitchLookup.computePaths(mimePath, null, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath()); 282 checkPaths( 283 Arrays.asList(new String [] { 284 "text/x-ant+xml/text/x-java/text/x-javadoc", 285 "text/x-javadoc", 286 "text/x-ant+xml/text/x-java", 287 "text/x-ant+xml", 288 "text/xml/text/x-java/text/x-javadoc", 289 "text/xml/text/x-java", 290 "text/xml", 291 "" 292 }), 293 paths 294 ); 295 } 296 297 public void testNoMapperCompoundMimeType2() { 298 MimePath mimePath = MimePath.parse("text/x-ant+xml/text/x-ant+xml"); 299 List paths = SwitchLookup.computePaths(mimePath, null, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath()); 300 checkPaths( 301 Arrays.asList(new String [] { 302 "text/x-ant+xml/text/x-ant+xml", 303 "text/x-ant+xml", 304 "text/xml", 305 "text/x-ant+xml/text/xml", 306 "text/xml/text/x-ant+xml", 307 "text/xml/text/xml", 308 "" 309 }), 310 paths 311 ); 312 } 313 314 public void testNoMapperCompoundMimeType3() { 315 MimePath mimePath = MimePath.parse("text/x-ant+xml/text/x-java/text/x-ant+xml"); 316 List paths = SwitchLookup.computePaths(mimePath, null, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath()); 317 checkPaths( 318 Arrays.asList(new String [] { 319 "text/x-ant+xml/text/x-java/text/x-ant+xml", 320 "text/x-ant+xml", 321 "text/xml", 322 "text/x-ant+xml/text/x-java/text/xml", 323 "text/x-ant+xml/text/x-java", 324 "text/xml/text/x-java/text/x-ant+xml", 325 "text/xml/text/x-java/text/xml", 326 "text/xml/text/x-java", 327 "" 328 }), 329 paths 330 ); 331 } 332 333 public void testDummyMapper() throws Exception { 334 TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance"); 335 TestUtilities.sleepForWhile(); 336 337 MimePath mimePath = MimePath.parse("text/x-jsp/text/x-java/text/x-javadoc"); 338 List paths = SwitchLookup.computePaths(mimePath, SwitchLookup.ROOT_FOLDER, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath()); 339 checkPaths( 340 Arrays.asList(new String [] { 341 SwitchLookup.ROOT_FOLDER + "/text/x-jsp/text/x-java/text/x-javadoc/DummyFolder", 342 SwitchLookup.ROOT_FOLDER + "/text/x-javadoc/DummyFolder", 343 SwitchLookup.ROOT_FOLDER + "/text/x-jsp/text/x-java/DummyFolder", 344 SwitchLookup.ROOT_FOLDER + "/text/x-jsp/DummyFolder", 345 SwitchLookup.ROOT_FOLDER + "/DummyFolder" 346 }), 347 paths 348 ); 349 } 350 351 public void testGetGenericPartOfCompoundMimeType() { 352 String generic = SwitchLookup.getGenericPartOfCompoundMimeType("text/x-ant+xml"); 353 assertNotNull("Didn't detect compound mime type", generic); 354 assertEquals("Wrong generic part", "text/xml", generic); 355 356 generic = SwitchLookup.getGenericPartOfCompoundMimeType("text/c++"); 357 assertNull("text/c++ is not a compound mime type", generic); 358 } 359 360 private void checkPaths(List expectedPaths, List paths) { 361 assertEquals("Wrong number of paths", expectedPaths.size(), paths.size()); 369 370 for (int i = 0; i < expectedPaths.size(); i++) { 371 String expectedPath = (String ) expectedPaths.get(i); 372 String path = (String ) paths.get(i); 373 assertEquals("Invalid path", expectedPath, path); 374 } 375 } 376 377 private static final class L implements LookupListener { 378 public int resultChangedCnt = 0; 379 public void resultChanged(LookupEvent ev) { 380 resultChangedCnt++; 381 } 382 } 383 } 384 | Popular Tags |