KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.List JavaDoc;
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 /**
32  *
33  * @author vita
34  */

35 public class SwitchLookupTest extends NbTestCase {
36
37     /** Creates a new instance of FolderPathLookupTest */
38     public SwitchLookupTest(String JavaDoc name) {
39         super(name);
40     }
41
42     protected void setUp() throws java.lang.Exception JavaDoc {
43         // Set up the default lookup, repository, etc.
44
EditorTestLookup.setLookup(new String JavaDoc[0], getWorkDir(), new Object JavaDoc[] {},
45             getClass().getClassLoader(),
46             null
47         );
48     }
49     
50     protected void tearDown() {
51         TestUtilities.gc();
52     }
53     
54     public void testSimple() throws Exception JavaDoc {
55         TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
56         TestUtilities.sleepForWhile();
57
58         // Creating lookup for an existing mime path
59
Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
60         Collection JavaDoc 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         // Now create lookup over a non-existing mime path
66
lookup = new SwitchLookup(MimePath.parse("text/xml"));
67         instances = lookup.lookupAll(Object JavaDoc.class);
68         
69         assertEquals("Wrong number of instances", 0, instances.size());
70     }
71
72     public void testAddingMimePath() throws Exception JavaDoc {
73         // Create lookup over a non-existing mime path
74
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 JavaDoc 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         // Create the mime path folders and add some instance
85
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
86         TestUtilities.sleepForWhile();
87
88         // Lookup the instances again
89
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 JavaDoc {
97         // Create the mime path folders and add some instance
98
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
99         TestUtilities.sleepForWhile();
100
101         // Create lookup over an existing mime path
102
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 JavaDoc 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         // Delete the mime path folder
114
TestUtilities.deleteFile(getWorkDir(), "Editors/text/x-jsp/text");
115         TestUtilities.sleepForWhile();
116
117         // Lookup the instances again
118
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     // test hierarchy - instances in lower levels are not visible in higher levels,
125
// but instances from higher levels are visible in lower levels
126

127     public void testHierarchyInheritance() throws Exception JavaDoc {
128         // Create the mime path folders and add some instance
129
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 JavaDoc 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 JavaDoc 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 JavaDoc {
149         // Create the mime path folders and add some instance
150
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc {
177         // Create the mime path folders and add some instance
178
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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc javaJspInstances = javaJspLookup.lookupAll(DummySetting.class);
205             assertEquals("Wrong number of instances", 0, javaJspInstances.size());
206         }
207     }
208     
209     // test that FolderPathLookups are shared and discarded when they are not needed anymore
210

211     // test that instances of a class with a Class2LayerFolder provider are really read from the proper folder
212

213     public void testReadFromSpecialFolders() throws Exception JavaDoc {
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 JavaDoc 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     // test that adding/removing a Class2LayerFolder provider updates the lookup for its class
226

227     public void testChangeInMappers() throws Exception JavaDoc {
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 JavaDoc 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         // Add the mapper
242
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         // Reset the listener
251
listener.resultChangedCnt = 0;
252         
253         // Remove the mapper
254
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     // Test mime path -> path[] conversion
263

264     public void testNoMapper() {
265         MimePath mimePath = MimePath.parse("text/x-jsp/text/x-java/text/x-javadoc");
266         List JavaDoc paths = SwitchLookup.computePaths(mimePath, null, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath());
267         checkPaths(
268             Arrays.asList(new String JavaDoc [] {
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 JavaDoc paths = SwitchLookup.computePaths(mimePath, null, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath());
282         checkPaths(
283             Arrays.asList(new String JavaDoc [] {
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 JavaDoc paths = SwitchLookup.computePaths(mimePath, null, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath());
300         checkPaths(
301             Arrays.asList(new String JavaDoc [] {
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 JavaDoc paths = SwitchLookup.computePaths(mimePath, null, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath());
317         checkPaths(
318             Arrays.asList(new String JavaDoc [] {
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 JavaDoc {
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 JavaDoc paths = SwitchLookup.computePaths(mimePath, SwitchLookup.ROOT_FOLDER, ClassInfoStorage.getInstance().getInfo(DummySetting.class.getName()).getExtraPath());
339         checkPaths(
340             Arrays.asList(new String JavaDoc [] {
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 JavaDoc 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 JavaDoc expectedPaths, List JavaDoc paths) {
361 // for(Iterator i = expectedPaths.iterator(); i.hasNext(); ) {
362
// System.out.println("Expected: " + i.next());
363
// }
364
// for(Iterator i = paths.iterator(); i.hasNext(); ) {
365
// System.out.println("Current: " + i.next());
366
// }
367
//
368
assertEquals("Wrong number of paths", expectedPaths.size(), paths.size());
369         
370         for (int i = 0; i < expectedPaths.size(); i++) {
371             String JavaDoc expectedPath = (String JavaDoc) expectedPaths.get(i);
372             String JavaDoc path = (String JavaDoc) 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