KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > MimeLookupTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.mimelookup.impl;
21
22 import java.io.IOException JavaDoc;
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 /**
33  * Testing basic functionality of MimeLookup
34  *
35  * @author Martin Roskanin
36  */

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 JavaDoc testName) {
44         super(testName);
45     }
46     
47     protected void setUp() throws java.lang.Exception JavaDoc {
48         String JavaDoc fsstruct [] = new String JavaDoc [] {
49             "Editors/text/xml/text/html/java-lang-StringBuffer.instance", //NOI18N
50
"Editors/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", //NOI18N
51
"Editors/text/x-java/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance", //NOI18N
52
"Editors/text/html/text/xml/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", //NOI18N
53
"Editors/text/html/text/xml/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance", //NOI18N
54
"Editors/text/xml/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectInstantiation.instance", //NOI18N
55
"Editors/text/xml/text/jsp/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", //NOI18N
56
// testing "compound mime types like application/x-ant+xml"
57
"Editors/application/dtd/text/x-java/java-lang-String.instance", //NOI18N
58
"Editors/application/dtd/testLookup/org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance", //NOI18N
59
"Editors/application/x-ant+dtd/java-lang-StringBuffer.instance", //NOI18N
60
"Editors/application/x-ant+dtd/text/x-java/testLookupTwo/org-netbeans-modules-editor-mimelookup-impl-TestLookupObjectTwo.instance", //NOI18N
61
};
62
63         EditorTestLookup.setLookup(fsstruct, getWorkDir(), new Object JavaDoc[] {},
64                    getClass().getClassLoader());
65     }
66
67     private void createFile(String JavaDoc file) throws IOException JavaDoc{
68         TestUtilities.createFile(getWorkDir(), file); //NOI18N
69
}
70     
71     private void checkResultChange(final int count) throws IOException JavaDoc{
72         // wait for firing event
73
TestUtilities.waitMaxMilisForValue(WAIT_TIME_FIRING, new TestUtilities.ValueResolver(){
74             public Object JavaDoc getValue(){
75                 return Boolean.FALSE;
76             }
77         }, Boolean.TRUE);
78         assertTrue(("resultChangedCount is:"+resultChangedCount[0]+" instead of "+count), resultChangedCount[0] == count);
79     }
80     
81     /**
82      * Looking up the class that has registered subfolder via Class2LayerFolder
83      */

84     public void testRegisteredClassLookup() throws IOException JavaDoc{
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");//NOI18N
91
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         //test inheritance from underlaying mime types
101
mp = MimePath.parse("text/xml/text/jsp/text/html"); //NOI18N
102
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         //simulating module uninstallation and removal of mime lookup file from xml layer
111
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"); //NOI18N
113
lookup = MimeLookup.getLookup(mp);
114         checkLookupObject(lookup, TestLookupObject.class, false);
115         checkLookupObject(lookup, TestLookupObjectTwo.class, false);
116         
117         lookup = MimeLookup.getMimeLookup("text/jsp");//NOI18N
118
checkLookupObject(lookup, TestLookupObject.class, true);
119         checkLookupObject(lookup, TestLookupObjectTwo.class, false);
120         
121         mp = MimePath.parse("text/html/text/xml"); //NOI18N
122
lookup = MimeLookup.getLookup(mp);
123         checkLookupObject(lookup, TestLookupObject.class, true);
124         checkLookupObject(lookup, TestLookupObjectTwo.class, true);
125         
126         //simulate module installation, new file will be added
127
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"); //NOI18N
131
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         //uninstall ObjectTwo
141
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         //uninstall ObjectOne
148
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         //simulate module installation, new file will be added
155
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"); //NOI18N
159
lookup = MimeLookup.getLookup(mp);
160         checkLookupObject(lookup, TestLookupObject.class, false);
161         checkLookupObject(lookup, TestLookupObjectTwo.class, true);
162
163         //delete all
164
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"); //NOI18N
177
lookup = MimeLookup.getLookup(mp);
178         checkLookupObject(lookup, TestLookupObject.class, false);
179         checkLookupObject(lookup ,TestLookupObjectTwo.class, false);
180         
181         lookup = MimeLookup.getMimeLookup("text/jsp");//NOI18N
182
checkLookupObject(lookup, TestLookupObject.class, false);
183         checkLookupObject(lookup, TestLookupObjectTwo.class, false);
184         
185         mp = MimePath.get(MimePath.get("text/html"), "text/xml"); //NOI18N
186
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"); //NOI18N
192
lookup = MimeLookup.getLookup(mp);
193         
194         checkLookupObject(lookup, TestLookupObject.class, false);
195         checkLookupObject(lookup, TestLookupObjectTwo.class, true);
196
197         //----------------------------------------------------------------------
198
//simulate module installation, new file will be added
199
mp = MimePath.parse("text/html/text/xml"); //NOI18N
200
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         //----------------------------------------------------------------------
212

213         
214         //----------------------------------------------------------------------
215
// Register listener on a Result of lookup on unexisted object
216
resultChangedCount[0] = 0;
217         mp = MimePath.parse("text/dtd/text/xml"); //NOI18N
218
lookup = MimeLookup.getLookup(mp);
219         Result result = lookup.lookup(new Template(TestLookupObject.class));
220         result.allInstances(); // remove this line if issue #60010 is fixed
221
LookupListener listener = new LookupListener(){
222             public void resultChanged(LookupEvent ev){
223                 resultChangedCount[0]++;
224             }
225         };
226         result.addLookupListener(listener);
227         
228         //simulate module installation, new file will be added
229
createFile("Editors/text/dtd/text/xml/testLookup/" + //NOI18N
230
"org-netbeans-modules-editor-mimelookup-impl-TestLookupObject.instance"); //NOI18N
231

232         checkResultChange(1);
233         result.removeLookupListener(listener);
234         resultChangedCount[0] = 0;
235         //----------------------------------------------------------------------
236

237         
238
239         
240         //----------------------------------------------------------------------
241
// result *NOT* firing on standard mime type testing
242
resultChangedCount[0] = 0;
243         lookup = MimeLookup.getMimeLookup("image/jpeg"); //NOI18N
244
// try to lookup TestLookupObjectTwo, while TestLookupObject will be installed.
245
// firing should not happen
246
result = lookup.lookup(new Template(TestLookupObjectTwo.class));
247         result.allInstances();
248         result.addLookupListener(listener);
249         checkResultChange(0);
250
251         //simulate module installation, new file will be added
252
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         //----------------------------------------------------------------------
261

262         
263         //----------------------------------------------------------------------
264
//
265
// testing "compound" mime types like application/x-ant+dtd
266
// "Editors/application/dtd/text/x-java/java-lang-String.instance", //NOI18N
267
// "Editors/application/dtd/testLookup/org-netbeans-modules-editor-mimelookup-TestLookupObject.instance", //NOI18N
268
// "Editors/application/x-ant+dtd/java-lang-StringBuffer.instance", //NOI18N
269
// "Editors/application/x-ant+dtd/text/x-java/testLookupTwo/org-netbeans-modules-editor-mimelookup-TestLookupObjectTwo.instance", //NOI18N
270
lookup = MimeLookup.getMimeLookup("application/x-ant+dtd"); //NOI18N
271
checkLookupObject(lookup, TestLookupObject.class, true);
272         checkLookupObject(lookup, StringBuffer JavaDoc.class, true);
273         checkLookupObject(lookup, String JavaDoc.class, false);
274         checkLookupObject(lookup, TestLookupObjectTwo.class, false);
275         
276         mp = MimePath.parse("application/x-ant+dtd/text/x-java"); //NOI18N
277
lookup = MimeLookup.getLookup(mp);
278         checkLookupObject(lookup, TestLookupObject.class, true); //it is inherited from parent
279
checkLookupObject(lookup, StringBuffer JavaDoc.class, true); //it is inherited from parent
280
checkLookupObject(lookup, String JavaDoc.class, true);
281         checkLookupObject(lookup, TestLookupObjectTwo.class, true);
282         //----------------------------------------------------------------------
283

284         
285         //----------------------------------------------------------------------
286
// Test lookup of "compound" mime types if the object is not installed yet
287
// "Editors/image/x-ant+dtd/text/x-java/testLookupTwo/org-netbeans-modules-editor-mimelookup-TestLookupObjectTwo.instance", //NOI18N
288
mp = MimePath.parse("image/x-ant+dtd/text/x-java"); //NOI18N
289
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         //----------------------------------------------------------------------
295

296
297         
298         //----------------------------------------------------------------------
299
// result firing on "compound" mime type testing
300
resultChangedCount[0] = 0;
301         mp = MimePath.parse("audio/x-ant+dtd/text/x-java"); //NOI18N
302
lookup = MimeLookup.getLookup(mp);
303         result = lookup.lookup(new Template(TestLookupObjectTwo.class));
304         result.allInstances();
305         result.addLookupListener(listener);
306         checkResultChange(0);
307
308         //simulate module installation, new file will be added
309
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         //----------------------------------------------------------------------
318

319          
320
321         //----------------------------------------------------------------------
322
// result *NOT* firing and firing on "compound" mime type testing where object is
323
// installed in inherited folder
324
resultChangedCount[0] = 0;
325         lookup = MimeLookup.getMimeLookup("audiox/mp3+dtd"); //NOI18N
326
result = lookup.lookup(new Template(TestLookupObject.class));
327         result.allInstances();
328         result.addLookupListener(listener);
329         checkResultChange(0);
330
331         //simulate module installation, new file will be added
332
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         // now install TestLookupObject, firing should happen
340
//simulate module installation, new file will be added
341
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         //----------------------------------------------------------------------
350

351         //----------------------------------------------------------------------
352
// result *NOT* firing and firing on "compound" *SUB* mime type testing where object is
353
// installed in inherited folder
354
resultChangedCount[0] = 0;
355         mp = MimePath.parse("audioy/mp3+dtd/audio/wav"); //NOI18N
356
lookup = MimeLookup.getLookup(mp);
357         result = lookup.lookup(new Template(TestLookupObject.class));
358         result.allInstances();
359         result.addLookupListener(listener);
360         checkResultChange(0);
361
362         //simulate module installation, new file will be added
363
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         // now install TestLookupObject, firing should happen
371
//simulate module installation, new file will be added
372
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         //----------------------------------------------------------------------
381

382         //----------------------------------------------------------------------
383
// result *NOT* firing and firing on "compound" *SUB* mime type testing where object is
384
// installed in inherited mime type folder
385
resultChangedCount[0] = 0;
386         mp = MimePath.get(MimePath.get("audioy/mp3+dtd"), "audio/wav"); //NOI18N
387
lookup = MimeLookup.getLookup(mp);
388         result = lookup.lookup(new Template(IllegalStateException JavaDoc.class));
389         result.allInstances();
390         result.addLookupListener(listener);
391         checkResultChange(0);
392
393         //simulate module installation, new file will be added
394
createFile("Editors/audioy/dtd/" +
395                 "java-lang-InstantiationException.instance");
396         
397         checkResultChange(0);
398         checkLookupObject(lookup, IllegalStateException JavaDoc.class, false);
399         checkLookupObject(lookup, InstantiationException JavaDoc.class, true);
400         
401         // now install TestLookupObject, firing should happen
402
//simulate module installation, new file will be added
403
createFile("Editors/audioy/dtd/" +
404                 "java-lang-IllegalStateException.instance");
405
406         checkResultChange(1);
407         checkLookupObject(lookup, IllegalStateException JavaDoc.class, true);
408         checkLookupObject(lookup, InstantiationException JavaDoc.class, true);
409         result.removeLookupListener(listener);
410         resultChangedCount[0] = 0;
411         //----------------------------------------------------------------------
412

413         //----------------------------------------------------------------------
414
// result *NOT* firing and firing on mime type testing where object is
415
// installed in mime type folder
416
resultChangedCount[0] = 0;
417         lookup = MimeLookup.getMimeLookup("audioz/mp3"); //NOI18N
418
result = lookup.lookup(new Template(IllegalStateException JavaDoc.class));
419         result.allInstances();
420         result.addLookupListener(listener);
421         checkResultChange(0);
422
423         //simulate module installation, new file will be added
424
createFile("Editors/audioz/mp3/" +
425                 "java-lang-InstantiationException.instance");
426         
427         checkResultChange(0);
428         checkLookupObject(lookup, IllegalStateException JavaDoc.class, false);
429         checkLookupObject(lookup, InstantiationException JavaDoc.class, true);
430         
431         // now install TestLookupObject, firing should happen
432
//simulate module installation, new file will be added
433
createFile("Editors/audioz/mp3/" +
434                 "java-lang-IllegalStateException.instance");
435
436         checkResultChange(1);
437         checkLookupObject(lookup, IllegalStateException JavaDoc.class, true);
438         checkLookupObject(lookup, InstantiationException JavaDoc.class, true);
439         result.removeLookupListener(listener);
440         resultChangedCount[0] = 0;
441         //----------------------------------------------------------------------
442

443         
444     }
445
446     /**
447      * FolderLookup behaves recursively by default. It is not ideal as for MimeLookup,
448      * that should operate only on the mime-type-folder
449      * see issue #58991
450      * Testing if the MimeLookup is not recursive
451      */

452     public void testLookupFolderRecursivity(){
453         //StringBuffer.instance is declared
454
// in "Editors/text/xml/text/html/java-lang-StringBuffer.instance"
455
// it shouldn't be found in text/xml parent folder
456
MimeLookup lookup = MimeLookup.getMimeLookup("text/xml");
457         checkLookupObject(lookup, StringBuffer JavaDoc.class, false);
458     }
459
460     
461     /**
462      * Looking up the class that has NOT registered subfolder via Class2LayerFolder.
463      * It should be found appropriate mime-type specific folder
464      */

465     public void testNotRegisteredClassLookup() throws IOException JavaDoc {
466         MimePath mp = MimePath.get(MimePath.get("text/xml"), "text/html"); //NOI18N
467
Lookup lookup = MimeLookup.getLookup(mp);
468         checkLookupObject(lookup, StringBuffer JavaDoc.class, true);
469         TestUtilities.deleteFile(getWorkDir(),
470                 "Editors/text/xml/text/html/java-lang-StringBuffer.instance");
471         checkLookupObject(lookup, StringBuffer JavaDoc.class, false);
472     }
473
474
475     /**
476      * Checking wheather the initialization of MimeLookup for appropriate
477      * mime type will not instantiate lookup object
478      */

479     public void testLazyLookupObjectInstantiation() throws IOException JavaDoc{
480         MimeLookup lookup = MimeLookup.getMimeLookup("text/xml"); //NOI18N
481
// lookup for some object in the mime lookup
482
checkLookupObject(lookup, StringBuffer JavaDoc.class, false);
483         // calling
484
// checkLookupObject(lookup, TestLookupObjectInstantiation.class, false);
485
// should fail
486
}
487     
488     /**
489      * Lookuping the template that has registered subfolder via Class2LayerFolder
490      */

491     public void testRegisteredTemplatesLookup() throws IOException JavaDoc{
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"); //NOI18N
496
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"); //NOI18N
501
lookup = MimeLookup.getLookup(mp);
502         checkLookupTemplate(lookup, TestLookupObject.class, 1);
503         checkLookupTemplate(lookup, TestLookupObjectTwo.class, 0);
504
505         // testing issue #58941
506
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 JavaDoc clazz, final boolean shouldBePresent){
516         TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){
517             public Object JavaDoc getValue(){
518                 Object JavaDoc obj = lookup.lookup(clazz);
519                 boolean bool = (shouldBePresent) ? obj != null : obj == null;
520                 return Boolean.valueOf(bool);
521             }
522         }, Boolean.TRUE);
523         Object JavaDoc 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 JavaDoc clazz, final int instCount){
532         TestUtilities.waitMaxMilisForValue(WAIT_TIME, new TestUtilities.ValueResolver(){
533             public Object JavaDoc 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