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.junit.NbTestCase; 25 import org.openide.util.Lookup; 26 import org.openide.util.Lookup.Result; 27 import org.openide.util.Lookup.Template; 28 import org.openide.util.LookupEvent; 29 import org.openide.util.LookupListener; 30 31 37 public class Depr_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 Depr_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 68 private void createFile(String file) throws IOException { 69 TestUtilities.createFile(getWorkDir(), file); } 71 72 private void checkResultChange(final int count) throws IOException { 73 TestUtilities.waitMaxMilisForValue(WAIT_TIME_FIRING, new TestUtilities.ValueResolver(){ 75 public Object getValue(){ 76 return Boolean.FALSE; 77 } 78 }, Boolean.TRUE); 79 assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of "+count), resultChangedCount[0] == count); 80 } 81 82 85 public void testRegisteredClassLookup() throws IOException { 86 87 MimeLookup lookup; 88 89 lookup = MimeLookup.getMimeLookup("text/jsp"); checkLookupObject(lookup, TestLookupObject.class, true); 91 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 92 93 lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"); checkLookupObject(lookup, TestLookupObject.class, false); 95 checkLookupObject(lookup ,TestLookupObjectTwo.class, true); 96 97 lookup = MimeLookup.getMimeLookup("text/html").childLookup("text/xml"); checkLookupObject(lookup, TestLookupObject.class, true); 99 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 100 101 lookup = MimeLookup.getMimeLookup("text/xml").childLookup("text/jsp").childLookup("text/html"); checkLookupObject(lookup, TestLookupObject.class, true); 104 lookup = MimeLookup.getMimeLookup("text/xml"); 105 checkLookupObject(lookup, TestLookupObject.class, false); 106 lookup = MimeLookup.getMimeLookup("text/xml").childLookup("text/jsp"); 107 checkLookupObject(lookup, TestLookupObject.class, true); 108 109 TestUtilities.deleteFile(getWorkDir(), "Editors/text/x-java/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 111 lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"); checkLookupObject(lookup, TestLookupObject.class, false); 113 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 114 115 lookup = MimeLookup.getMimeLookup("text/jsp"); checkLookupObject(lookup, TestLookupObject.class, true); 117 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 118 119 lookup = MimeLookup.getMimeLookup("text/html").childLookup("text/xml"); checkLookupObject(lookup, TestLookupObject.class, true); 121 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 122 123 createFile("Editors/text/x-java/text/x-properties/testLookupTwo/" + 125 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 126 127 lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/x-properties"); checkLookupObject(lookup, TestLookupObject.class, false); 129 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 130 131 createFile("Editors/text/x-java/text/x-properties/testLookup/" + 132 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 133 checkLookupObject(lookup, TestLookupObject.class, true); 134 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 135 136 TestUtilities.deleteFile(getWorkDir(), 138 "Editors/text/x-java/text/x-properties/testLookupTwo/" + 139 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 140 checkLookupObject(lookup, TestLookupObject.class, true); 141 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 142 143 TestUtilities.deleteFile(getWorkDir(), 145 "Editors/text/x-java/text/x-properties/testLookup/" + 146 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 147 checkLookupObject(lookup, TestLookupObject.class, false); 148 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 149 150 createFile("Editors/text/x-java/text/x-properties/testLookupTwo/" + 152 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 153 154 lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/x-properties"); checkLookupObject(lookup, TestLookupObject.class, false); 156 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 157 158 TestUtilities.deleteFile(getWorkDir(), 160 "Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 161 162 TestUtilities.deleteFile(getWorkDir(), 163 "Editors/text/x-java/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 164 165 TestUtilities.deleteFile(getWorkDir(), 166 "Editors/text/html/text/xml/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 167 168 TestUtilities.deleteFile(getWorkDir(), 169 "Editors/text/html/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 170 171 lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/xml"); checkLookupObject(lookup, TestLookupObject.class, false); 173 checkLookupObject(lookup ,TestLookupObjectTwo.class, false); 174 175 lookup = MimeLookup.getMimeLookup("text/jsp"); checkLookupObject(lookup, TestLookupObject.class, false); 177 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 178 179 lookup = MimeLookup.getMimeLookup("text/html").childLookup("text/xml"); checkLookupObject(lookup, TestLookupObject.class, false); 181 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 182 183 lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/x-properties"); checkLookupObject(lookup, TestLookupObject.class, false); 185 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 186 187 lookup = MimeLookup.getMimeLookup("text/html").childLookup("text/xml"); createFile("Editors/text/html/text/xml/testLookup/" + 191 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 192 193 checkLookupObject(lookup, TestLookupObject.class, true); 194 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 195 196 createFile("Editors/text/html/text/xml/testLookupTwo/" + 197 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 198 checkLookupObject(lookup, TestLookupObject.class, true); 199 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 200 202 203 resultChangedCount[0] = 0; 206 lookup = MimeLookup.getMimeLookup("text/dtd").childLookup("text/xml"); Result result = lookup.lookup(new Template(TestLookupObject.class)); 208 result.allInstances(); LookupListener listener = new LookupListener(){ 210 public void resultChanged(LookupEvent ev){ 211 resultChangedCount[0]++; 212 } 213 }; 214 result.addLookupListener(listener); 215 216 createFile("Editors/text/dtd/text/xml/testLookup/" + "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 220 checkResultChange(1); 221 result.removeLookupListener(listener); 222 resultChangedCount[0] = 0; 223 225 226 227 228 resultChangedCount[0] = 0; 231 lookup = MimeLookup.getMimeLookup("image/jpeg"); result = lookup.lookup(new Template(TestLookupObjectTwo.class)); 235 result.allInstances(); 236 result.addLookupListener(listener); 237 checkResultChange(0); 238 239 createFile("Editors/image/jpeg/testLookup/" + 241 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 242 243 checkResultChange(0); 244 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 245 checkLookupObject(lookup, TestLookupObject.class, true); 246 result.removeLookupListener(listener); 247 resultChangedCount[0] = 0; 248 250 251 lookup = MimeLookup.getMimeLookup("application/x-ant+dtd"); checkLookupObject(lookup, TestLookupObject.class, true); 260 checkLookupObject(lookup, StringBuffer .class, true); 261 checkLookupObject(lookup, String .class, false); 262 checkLookupObject(lookup, TestLookupObjectTwo.class, false); 263 264 lookup = MimeLookup.getMimeLookup("application/x-ant+dtd").childLookup("text/x-java"); checkLookupObject(lookup, TestLookupObject.class, true); checkLookupObject(lookup, StringBuffer .class, true); checkLookupObject(lookup, String .class, true); 268 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 269 271 272 lookup = MimeLookup.getMimeLookup("image/x-ant+dtd").childLookup("text/x-java"); checkLookupObject(lookup ,TestLookupObjectTwo.class, false); 277 createFile("Editors/image/x-ant+dtd/text/x-java/testLookupTwo/" + 278 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 279 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 280 282 283 284 resultChangedCount[0] = 0; 287 lookup = MimeLookup.getMimeLookup("audio/x-ant+dtd").childLookup("text/x-java"); result = lookup.lookup(new Template(TestLookupObjectTwo.class)); 289 result.allInstances(); 290 result.addLookupListener(listener); 291 checkResultChange(0); 292 293 createFile("Editors/audio/x-ant+dtd/text/x-java/testLookupTwo/" + 295 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 296 297 checkResultChange(1); 298 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 299 checkLookupObject(lookup, TestLookupObject.class, false); 300 result.removeLookupListener(listener); 301 resultChangedCount[0] = 0; 302 304 305 306 resultChangedCount[0] = 0; 310 lookup = MimeLookup.getMimeLookup("audiox/mp3+dtd"); result = lookup.lookup(new Template(TestLookupObject.class)); 312 result.allInstances(); 313 result.addLookupListener(listener); 314 checkResultChange(0); 315 316 createFile("Editors/audiox/dtd/testLookupTwo/" + 318 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 319 320 checkResultChange(0); 321 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 322 checkLookupObject(lookup, TestLookupObject.class, false); 323 324 createFile("Editors/audiox/dtd/testLookup/" + 327 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 328 329 checkResultChange(1); 330 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 331 checkLookupObject(lookup, TestLookupObject.class, true); 332 result.removeLookupListener(listener); 333 resultChangedCount[0] = 0; 334 336 resultChangedCount[0] = 0; 340 lookup = MimeLookup.getMimeLookup("audioy/mp3+dtd").childLookup("audio/wav"); result = lookup.lookup(new Template(TestLookupObject.class)); 342 result.allInstances(); 343 result.addLookupListener(listener); 344 checkResultChange(0); 345 346 createFile("Editors/audioy/dtd/audio/wav/testLookupTwo/" + 348 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 349 350 checkResultChange(0); 351 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 352 checkLookupObject(lookup, TestLookupObject.class, false); 353 354 createFile("Editors/audioy/dtd/audio/wav/testLookup/" + 357 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 358 359 checkResultChange(1); 360 checkLookupObject(lookup, TestLookupObjectTwo.class, true); 361 checkLookupObject(lookup, TestLookupObject.class, true); 362 result.removeLookupListener(listener); 363 resultChangedCount[0] = 0; 364 366 resultChangedCount[0] = 0; 370 lookup = MimeLookup.getMimeLookup("audioy/mp3+dtd").childLookup("audio/wav"); result = lookup.lookup(new Template(IllegalStateException .class)); 372 result.allInstances(); 373 result.addLookupListener(listener); 374 checkResultChange(0); 375 376 createFile("Editors/audioy/dtd/" + 378 "java-lang-InstantiationException.instance"); 379 380 checkResultChange(0); 381 checkLookupObject(lookup, IllegalStateException .class, false); 382 checkLookupObject(lookup, InstantiationException .class, true); 383 384 createFile("Editors/audioy/dtd/" + 387 "java-lang-IllegalStateException.instance"); 388 389 checkResultChange(1); 390 checkLookupObject(lookup, IllegalStateException .class, true); 391 checkLookupObject(lookup, InstantiationException .class, true); 392 result.removeLookupListener(listener); 393 resultChangedCount[0] = 0; 394 396 resultChangedCount[0] = 0; 400 lookup = MimeLookup.getMimeLookup("audioz/mp3"); result = lookup.lookup(new Template(IllegalStateException .class)); 402 result.allInstances(); 403 result.addLookupListener(listener); 404 checkResultChange(0); 405 406 createFile("Editors/audioz/mp3/" + 408 "java-lang-InstantiationException.instance"); 409 410 checkResultChange(0); 411 checkLookupObject(lookup, IllegalStateException .class, false); 412 checkLookupObject(lookup, InstantiationException .class, true); 413 414 createFile("Editors/audioz/mp3/" + 417 "java-lang-IllegalStateException.instance"); 418 419 checkResultChange(1); 420 checkLookupObject(lookup, IllegalStateException .class, true); 421 checkLookupObject(lookup, InstantiationException .class, true); 422 result.removeLookupListener(listener); 423 resultChangedCount[0] = 0; 424 426 427 } 428 429 435 public void testLookupFolderRecursivity(){ 436 MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); 440 checkLookupObject(lookup, StringBuffer .class, false); 441 } 442 443 444 448 public void testNotRegisteredClassLookup() throws IOException { 449 MimeLookup lookup = MimeLookup.getMimeLookup("text/xml").childLookup("text/html"); checkLookupObject(lookup, StringBuffer .class, true); 451 TestUtilities.deleteFile(getWorkDir(), 452 "Editors/text/xml/text/html/java-lang-StringBuffer.instance"); 453 checkLookupObject(lookup, StringBuffer .class, false); 454 } 455 456 457 461 public void testLazyLookupObjectInstantiation() throws IOException { 462 MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); checkLookupObject(lookup, StringBuffer .class, false); 465 } 469 470 473 public void testRegisteredTemplatesLookup() throws IOException { 474 createFile("Editors/text/x-java/text/x-properties/testLookupTwo/" + 475 "org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance"); 476 477 MimeLookup lookup = MimeLookup.getMimeLookup("text/x-java").childLookup("text/x-properties"); checkLookupTemplate(lookup, TestLookupObject.class, 0); 479 checkLookupTemplate(lookup, TestLookupObjectTwo.class, 1); 480 481 lookup = MimeLookup.getMimeLookup("text/xml").childLookup("text/jsp"); checkLookupTemplate(lookup, TestLookupObject.class, 1); 483 checkLookupTemplate(lookup, TestLookupObjectTwo.class, 0); 484 485 TestUtilities.deleteFile(getWorkDir(), 487 "Editors/text/xml/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 488 TestUtilities.deleteFile(getWorkDir(), 489 "Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); 490 checkLookupTemplate(lookup, TestLookupObject.class, 0); 491 checkLookupTemplate(lookup, TestLookupObjectTwo.class, 0); 492 } 493 494 495 private void checkLookupObject(final MimeLookup lookup, final Class clazz, final boolean shouldBePresent){ 496 TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){ 497 public Object getValue(){ 498 Object obj = lookup.lookup(clazz); 499 boolean bool = (shouldBePresent) ? obj != null : obj == null; 500 return Boolean.valueOf(bool); 501 } 502 }, Boolean.TRUE); 503 Object obj = lookup.lookup(clazz); 504 if (shouldBePresent){ 505 assertTrue("Object should be present in the lookup",obj!=null); 506 } else { 507 assertTrue("Object should NOT be present in the lookup",obj==null); 508 } 509 } 510 511 private void checkLookupTemplate(final MimeLookup lookup, final Class clazz, final int instCount){ 512 TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){ 513 public Object getValue(){ 514 Lookup.Result result = lookup.lookup(new Lookup.Template(clazz)); 515 boolean bool = result.allInstances().size() == instCount; 516 return Boolean.valueOf(bool); 517 } 518 }, Boolean.TRUE); 519 Lookup.Result result = lookup.lookup(new Lookup.Template(clazz)); 520 int size = result.allInstances().size(); 521 boolean bool = (size == instCount); 522 assertTrue("Number of instances doesn't match. Found:"+size+". Should be presented:"+instCount+".", bool); 523 } 524 525 } 526 | Popular Tags |