1 19 20 package org.netbeans.modules.editor.mimelookup.impl; 21 22 import java.io.IOException ; 23 import org.netbeans.api.editor.mimelookup.MimeLookup; 24 import org.netbeans.api.editor.mimelookup.MimePath; 25 import org.netbeans.junit.NbTestCase; 26 import org.openide.util.Lookup; 27 import org.openide.util.Lookup.Result; 28 import org.openide.util.Lookup.Template; 29 import org.openide.util.LookupEvent; 30 import org.openide.util.LookupListener; 31 32 37 public class MimeLookupTest extends NbTestCase { 38 39 private static final int WAIT_TIME = 5000; 40 private static final int WAIT_TIME_FIRING = 1500; 41 final int resultChangedCount[] = new int[1]; 42 43 public MimeLookupTest(java.lang.String testName) { 44 super(testName); 45 } 46 47 protected void setUp() throws java.lang.Exception { 48 String fsstruct [] = new String [] { 49 "Editors/text/xml/text/html/java-lang-StringBuffer.instance", "Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", "Editors/text/x-java/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance", "Editors/text/html/text/xml/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", "Editors/text/html/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance", "Editors/text/xml/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectInstantiation.instance", "Editors/text/xml/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", "Editors/application/dtd/text/x-java/java-lang-String.instance", "Editors/application/dtd/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", "Editors/application/x-ant+dtd/java-lang-StringBuffer.instance", "Editors/application/x-ant+dtd/text/x-java/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance", }; 62 63 EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object [] {}, 64 getClass().getClassLoader()); 65 } 66 67 private void createFile(String file) throws IOException { 68 TestUtilities.createFile(getWorkDir(), file); } 70 71 private void checkResultChange(final int count) throws IOException { 72 TestUtilities.waitMaxMilisForValue(WAIT_TIME_FIRING, new TestUtilities.ValueResolver(){ 74 public Object getValue(){ 75 return Boolean.FALSE; 76 } 77 }, Boolean.TRUE); 78 assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of "+count), resultChangedCount[0] == count); 79 } 80 81 84 public void testRegisteredClassLookup() throws IOException { 85 MimePath mp = MimePath.parse("text/x-java/text/xml"); 86 Lookup lookup = MimeLookup.getLookup(mp); 87 checkLookupObject(lookup, TestLookupObject.class, false); 88 checkLookupObject(lookup ,TestLookupObjectTwo.class, true); 89 90 lookup = MimeLookup.getMimeLookup("text/jsp"); checkLookupObject(lookup, TestLookupObject.class, true); 92 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 93 94 mp = MimePath.parse("text/html/text/xml"); 95 lookup = MimeLookup.getLookup(mp); 96 97 checkLookupObject(lookup, TestLookupObject.class, true); 98 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 99 100 mp = MimePath.parse("text/xml/text/jsp/text/html"); lookup = MimeLookup.getLookup(mp); 103 checkLookupObject(lookup, TestLookupObject.class, true); 104 lookup = MimeLookup.getMimeLookup("text/xml"); 105 checkLookupObject(lookup, TestLookupObject.class, false); 106 mp = MimePath.parse("text/xml/text/jsp"); 107 lookup = MimeLookup.getLookup(mp); 108 checkLookupObject(lookup, TestLookupObject.class, true); 109 110 TestUtilities.deleteFile(getWorkDir(), "Editors/text/x-java/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 112 mp = MimePath.parse("text/x-java/text/xml"); lookup = MimeLookup.getLookup(mp); 114 checkLookupObject(lookup, TestLookupObject.class, false); 115 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 116 117 lookup = MimeLookup.getMimeLookup("text/jsp"); checkLookupObject(lookup, TestLookupObject.class, true); 119 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 120 121 mp = MimePath.parse("text/html/text/xml"); lookup = MimeLookup.getLookup(mp); 123 checkLookupObject(lookup, TestLookupObject.class, true); 124 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 125 126 createFile("Editors/text/x-java/text/x-properties/testLookupTwo/" + 128 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 129 130 mp = MimePath.get(MimePath.get("text/x-java"), "text/x-properties"); lookup = MimeLookup.getLookup(mp); 132 checkLookupObject(lookup, TestLookupObject.class, false); 133 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 134 135 createFile("Editors/text/x-java/text/x-properties/testLookup/" + 136 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 137 checkLookupObject(lookup, TestLookupObject.class, true); 138 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 139 140 TestUtilities.deleteFile(getWorkDir(), 142 "Editors/text/x-java/text/x-properties/testLookupTwo/" + 143 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 144 checkLookupObject(lookup, TestLookupObject.class, true); 145 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 146 147 TestUtilities.deleteFile(getWorkDir(), 149 "Editors/text/x-java/text/x-properties/testLookup/" + 150 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 151 checkLookupObject(lookup, TestLookupObject.class, false); 152 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 153 154 createFile("Editors/text/x-java/text/x-properties/testLookupTwo/" + 156 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 157 158 mp = MimePath.parse("text/x-java/text/x-properties"); lookup = MimeLookup.getLookup(mp); 160 checkLookupObject(lookup, TestLookupObject.class, false); 161 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 162 163 TestUtilities.deleteFile(getWorkDir(), 165 "Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 166 167 TestUtilities.deleteFile(getWorkDir(), 168 "Editors/text/x-java/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 169 170 TestUtilities.deleteFile(getWorkDir(), 171 "Editors/text/html/text/xml/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 172 173 TestUtilities.deleteFile(getWorkDir(), 174 "Editors/text/html/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 175 176 mp = MimePath.parse("text/x-java/text/xml"); lookup = MimeLookup.getLookup(mp); 178 checkLookupObject(lookup, TestLookupObject.class, false); 179 checkLookupObject(lookup ,TestLookupObjectTwo.class, false); 180 181 lookup = MimeLookup.getMimeLookup("text/jsp"); checkLookupObject(lookup, TestLookupObject.class, false); 183 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 184 185 mp = MimePath.get(MimePath.get("text/html"), "text/xml"); lookup = MimeLookup.getLookup(mp); 187 188 checkLookupObject(lookup, TestLookupObject.class, false); 189 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 190 191 mp = MimePath.parse("text/x-java/text/x-properties"); lookup = MimeLookup.getLookup(mp); 193 194 checkLookupObject(lookup, TestLookupObject.class, false); 195 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 196 197 mp = MimePath.parse("text/html/text/xml"); lookup = MimeLookup.getLookup(mp); 201 createFile("Editors/text/html/text/xml/testLookup/" + 202 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 203 204 checkLookupObject(lookup, TestLookupObject.class, true); 205 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 206 207 createFile("Editors/text/html/text/xml/testLookupTwo/" + 208 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 209 checkLookupObject(lookup, TestLookupObject.class, true); 210 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 211 213 214 resultChangedCount[0] = 0; 217 mp = MimePath.parse("text/dtd/text/xml"); lookup = MimeLookup.getLookup(mp); 219 Result result = lookup.lookup(new Template(TestLookupObject.class)); 220 result.allInstances(); LookupListener listener = new LookupListener(){ 222 public void resultChanged(LookupEvent ev){ 223 resultChangedCount[0]++; 224 } 225 }; 226 result.addLookupListener(listener); 227 228 createFile("Editors/text/dtd/text/xml/testLookup/" + "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 232 checkResultChange(1); 233 result.removeLookupListener(listener); 234 resultChangedCount[0] = 0; 235 237 238 239 240 resultChangedCount[0] = 0; 243 lookup = MimeLookup.getMimeLookup("image/jpeg"); result = lookup.lookup(new Template(TestLookupObjectTwo.class)); 247 result.allInstances(); 248 result.addLookupListener(listener); 249 checkResultChange(0); 250 251 createFile("Editors/image/jpeg/testLookup/" + 253 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 254 255 checkResultChange(0); 256 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 257 checkLookupObject(lookup, TestLookupObject.class, true); 258 result.removeLookupListener(listener); 259 resultChangedCount[0] = 0; 260 262 263 lookup = MimeLookup.getMimeLookup("application/x-ant+dtd"); checkLookupObject(lookup, TestLookupObject.class, true); 272 checkLookupObject(lookup, StringBuffer .class, true); 273 checkLookupObject(lookup, String .class, false); 274 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 275 276 mp = MimePath.parse("application/x-ant+dtd/text/x-java"); lookup = MimeLookup.getLookup(mp); 278 checkLookupObject(lookup, TestLookupObject.class, true); checkLookupObject(lookup, StringBuffer .class, true); checkLookupObject(lookup, String .class, true); 281 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 282 284 285 mp = MimePath.parse("image/x-ant+dtd/text/x-java"); lookup = MimeLookup.getLookup(mp); 290 checkLookupObject(lookup ,TestLookupObjectTwo.class, false); 291 createFile("Editors/image/x-ant+dtd/text/x-java/testLookupTwo/" + 292 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 293 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 294 296 297 298 resultChangedCount[0] = 0; 301 mp = MimePath.parse("audio/x-ant+dtd/text/x-java"); lookup = MimeLookup.getLookup(mp); 303 result = lookup.lookup(new Template(TestLookupObjectTwo.class)); 304 result.allInstances(); 305 result.addLookupListener(listener); 306 checkResultChange(0); 307 308 createFile("Editors/audio/x-ant+dtd/text/x-java/testLookupTwo/" + 310 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 311 312 checkResultChange(1); 313 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 314 checkLookupObject(lookup, TestLookupObject.class, false); 315 result.removeLookupListener(listener); 316 resultChangedCount[0] = 0; 317 319 320 321 resultChangedCount[0] = 0; 325 lookup = MimeLookup.getMimeLookup("audiox/mp3+dtd"); result = lookup.lookup(new Template(TestLookupObject.class)); 327 result.allInstances(); 328 result.addLookupListener(listener); 329 checkResultChange(0); 330 331 createFile("Editors/audiox/dtd/testLookupTwo/" + 333 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 334 335 checkResultChange(0); 336 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 337 checkLookupObject(lookup, TestLookupObject.class, false); 338 339 createFile("Editors/audiox/dtd/testLookup/" + 342 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 343 344 checkResultChange(1); 345 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 346 checkLookupObject(lookup, TestLookupObject.class, true); 347 result.removeLookupListener(listener); 348 resultChangedCount[0] = 0; 349 351 resultChangedCount[0] = 0; 355 mp = MimePath.parse("audioy/mp3+dtd/audio/wav"); lookup = MimeLookup.getLookup(mp); 357 result = lookup.lookup(new Template(TestLookupObject.class)); 358 result.allInstances(); 359 result.addLookupListener(listener); 360 checkResultChange(0); 361 362 createFile("Editors/audioy/dtd/audio/wav/testLookupTwo/" + 364 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 365 366 checkResultChange(0); 367 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 368 checkLookupObject(lookup, TestLookupObject.class, false); 369 370 createFile("Editors/audioy/dtd/audio/wav/testLookup/" + 373 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 374 375 checkResultChange(1); 376 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 377 checkLookupObject(lookup, TestLookupObject.class, true); 378 result.removeLookupListener(listener); 379 resultChangedCount[0] = 0; 380 382 resultChangedCount[0] = 0; 386 mp = MimePath.get(MimePath.get("audioy/mp3+dtd"), "audio/wav"); lookup = MimeLookup.getLookup(mp); 388 result = lookup.lookup(new Template(IllegalStateException .class)); 389 result.allInstances(); 390 result.addLookupListener(listener); 391 checkResultChange(0); 392 393 createFile("Editors/audioy/dtd/" + 395 "java-lang-InstantiationException.instance"); 396 397 checkResultChange(0); 398 checkLookupObject(lookup, IllegalStateException .class, false); 399 checkLookupObject(lookup, InstantiationException .class, true); 400 401 createFile("Editors/audioy/dtd/" + 404 "java-lang-IllegalStateException.instance"); 405 406 checkResultChange(1); 407 checkLookupObject(lookup, IllegalStateException .class, true); 408 checkLookupObject(lookup, InstantiationException .class, true); 409 result.removeLookupListener(listener); 410 resultChangedCount[0] = 0; 411 413 resultChangedCount[0] = 0; 417 lookup = MimeLookup.getMimeLookup("audioz/mp3"); result = lookup.lookup(new Template(IllegalStateException .class)); 419 result.allInstances(); 420 result.addLookupListener(listener); 421 checkResultChange(0); 422 423 createFile("Editors/audioz/mp3/" + 425 "java-lang-InstantiationException.instance"); 426 427 checkResultChange(0); 428 checkLookupObject(lookup, IllegalStateException .class, false); 429 checkLookupObject(lookup, InstantiationException .class, true); 430 431 createFile("Editors/audioz/mp3/" + 434 "java-lang-IllegalStateException.instance"); 435 436 checkResultChange(1); 437 checkLookupObject(lookup, IllegalStateException .class, true); 438 checkLookupObject(lookup, InstantiationException .class, true); 439 result.removeLookupListener(listener); 440 resultChangedCount[0] = 0; 441 443 444 } 445 446 452 public void testLookupFolderRecursivity(){ 453 MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); 457 checkLookupObject(lookup, StringBuffer .class, false); 458 } 459 460 461 465 public void testNotRegisteredClassLookup() throws IOException { 466 MimePath mp = MimePath.get(MimePath.get("text/xml"), "text/html"); Lookup lookup = MimeLookup.getLookup(mp); 468 checkLookupObject(lookup, StringBuffer .class, true); 469 TestUtilities.deleteFile(getWorkDir(), 470 "Editors/text/xml/text/html/java-lang-StringBuffer.instance"); 471 checkLookupObject(lookup, StringBuffer .class, false); 472 } 473 474 475 479 public void testLazyLookupObjectInstantiation() throws IOException { 480 MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); checkLookupObject(lookup, StringBuffer .class, false); 483 } 487 488 491 public void testRegisteredTemplatesLookup() throws IOException { 492 createFile("Editors/text/x-java/text/x-properties/testLookupTwo/" + 493 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 494 495 MimePath mp = MimePath.parse("text/x-java/text/x-properties"); Lookup lookup = MimeLookup.getLookup(mp); 497 checkLookupTemplate(lookup, TestLookupObject.class, 0); 498 checkLookupTemplate(lookup, TestLookupObjectTwo.class, 1); 499 500 mp = MimePath.parse("text/xml/text/jsp"); lookup = MimeLookup.getLookup(mp); 502 checkLookupTemplate(lookup, TestLookupObject.class, 1); 503 checkLookupTemplate(lookup, TestLookupObjectTwo.class, 0); 504 505 TestUtilities.deleteFile(getWorkDir(), 507 "Editors/text/xml/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 508 TestUtilities.deleteFile(getWorkDir(), 509 "Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 510 checkLookupTemplate(lookup, TestLookupObject.class, 0); 511 checkLookupTemplate(lookup, TestLookupObjectTwo.class, 0); 512 } 513 514 515 private void checkLookupObject(final Lookup lookup, final Class clazz, final boolean shouldBePresent){ 516 TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){ 517 public Object getValue(){ 518 Object obj = lookup.lookup(clazz); 519 boolean bool = (shouldBePresent) ? obj != null : obj == null; 520 return Boolean.valueOf(bool); 521 } 522 }, Boolean.TRUE); 523 Object obj = lookup.lookup(clazz); 524 if (shouldBePresent){ 525 assertTrue("Object should be present in the lookup",obj!=null); 526 } else { 527 assertTrue("Object should NOT be present in the lookup",obj==null); 528 } 529 } 530 531 private void checkLookupTemplate(final Lookup lookup, final Class clazz, final int instCount){ 532 TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){ 533 public Object getValue(){ 534 Lookup.Result result = lookup.lookup(new Lookup.Template(clazz)); 535 boolean bool = result.allInstances().size() == instCount; 536 return Boolean.valueOf(bool); 537 } 538 }, Boolean.TRUE); 539 Lookup.Result result = lookup.lookup(new Lookup.Template(clazz)); 540 int size = result.allInstances().size(); 541 boolean bool = (size == instCount); 542 assertTrue("Number of instances doesn't match. Found:"+size+". Should be presented:"+instCount+".", bool); 543 } 544 545 } 546 | Popular Tags |