2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.editor.mimelookup.impl;
44
import java.util.Collection;
45
import org.netbeans.api.editor.mimelookup.MimePath;
46
import org.netbeans.junit.NbTestCase;
47
import org.openide.util.Lookup;
48
import org.openide.util.LookupEvent;
49
import org.openide.util.LookupListener;
55
public class SwitchLookupTest extends NbTestCase {
57
/** Creates a new instance of FolderPathLookupTest */
58
public SwitchLookupTest(String name) {
62
protected @Override void setUp() throws Exception {
64
// Set up the default lookup, repository, etc.
65
EditorTestLookup.setLookup(new String[0], getWorkDir(), new Object[] {},
66
getClass().getClassLoader(),
71
protected @Override void tearDown() {
75
public void testSimple() throws Exception {
76
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
77
TestUtilities.sleepForWhile();
79
// Creating lookup for an existing mime path
80
Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
81
Collection instances = lookup.lookupAll(DummySetting.class);
83
assertEquals("Wrong number of instances", 1, instances.size());
84
assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass());
86
// Now create lookup over a non-existing mime path
87
lookup = new SwitchLookup(MimePath.parse("text/xml"));
88
instances = lookup.lookupAll(Object.class);
90
assertEquals("Wrong number of instances", 0, instances.size());
93
public void testAddingMimePath() throws Exception {
94
// Create lookup over a non-existing mime path
95
Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
96
Lookup.Result result = lookup.lookupResult(DummySetting.class);
99
result.addLookupListener(listener);
100
Collection instances = result.allInstances();
102
assertEquals("There should be no change events", 0, listener.resultChangedCnt);
103
assertEquals("Wrong number of instances", 0, instances.size());
105
// Create the mime path folders and add some instance
106
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
107
TestUtilities.sleepForWhile();
109
// Lookup the instances again
110
instances = lookup.lookupAll(DummySetting.class);
112
assertEquals("Wrong number of change events", 1, listener.resultChangedCnt);
113
assertEquals("Wrong number of instances", 1, instances.size());
114
assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass());
117
public void testRemovingMimePath() throws Exception {
118
// Create the mime path folders and add some instance
119
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
120
TestUtilities.sleepForWhile();
122
// Create lookup over an existing mime path
123
Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
124
Lookup.Result result = lookup.lookupResult(DummySetting.class);
125
L listener = new L();
127
result.addLookupListener(listener);
128
Collection instances = result.allInstances();
130
assertEquals("There should be no change events", 0, listener.resultChangedCnt);
131
assertEquals("Wrong number of instances", 1, instances.size());
132
assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass());
134
// Delete the mime path folder
135
TestUtilities.deleteFile(getWorkDir(), "Editors/text/x-jsp/text");
136
TestUtilities.sleepForWhile();
138
// Lookup the instances again
139
instances = lookup.lookupAll(DummySetting.class);
141
assertEquals("Wrong number of change events", 1, listener.resultChangedCnt);
142
assertEquals("Wrong number of instances", 0, instances.size());
145
// test hierarchy - instances in lower levels are not visible in higher levels,
146
// but instances from higher levels are visible in lower levels
148
public void testHierarchyInheritance() throws Exception {
149
// Create the mime path folders and add some instance
150
TestUtilities.createFile(getWorkDir(), "Editors/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
151
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/");
152
TestUtilities.sleepForWhile();
155
Lookup javaLookup = new SwitchLookup(MimePath.parse("text/x-java"));
156
Collection javaInstances = javaLookup.lookupAll(DummySetting.class);
157
assertEquals("Wrong number of instances", 1, javaInstances.size());
158
assertEquals("Wrong instance", DummySettingImpl.class, javaInstances.iterator().next().getClass());
162
Lookup jspJavaLookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
163
Collection jspJavaInstances = jspJavaLookup.lookupAll(DummySetting.class);
164
assertEquals("Wrong number of instances", 1, jspJavaInstances.size());
165
assertEquals("Wrong instance", DummySettingImpl.class, jspJavaInstances.iterator().next().getClass());
169
public void testHierarchyRootInheritance() throws Exception {
170
// Create the mime path folders and add some instance
171
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/");
172
TestUtilities.createFile(getWorkDir(), "Editors/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
173
TestUtilities.sleepForWhile();
176
Lookup lookup = new SwitchLookup(MimePath.parse(""));
177
Collection instances = lookup.lookupAll(DummySetting.class);
178
assertEquals("Wrong number of instances", 1, instances.size());
179
assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass());
183
Lookup jspLookup = new SwitchLookup(MimePath.parse("text/x-jsp"));
184
Collection jspInstances = jspLookup.lookupAll(DummySetting.class);
185
assertEquals("Wrong number of instances", 1, jspInstances.size());
186
assertEquals("Wrong instance", DummySettingImpl.class, jspInstances.iterator().next().getClass());
190
Lookup javaLookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
191
Collection javaInstances = javaLookup.lookupAll(DummySetting.class);
192
assertEquals("Wrong number of instances", 1, javaInstances.size());
193
assertEquals("Wrong instance", DummySettingImpl.class, javaInstances.iterator().next().getClass());
197
public void testHierarchyLeaks() throws Exception {
198
// Create the mime path folders and add some instance
199
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/");
200
TestUtilities.createFile(getWorkDir(), "Editors/text/x-jsp/text/x-java/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
201
TestUtilities.createFile(getWorkDir(), "Editors/text/x-java/");
202
TestUtilities.sleepForWhile();
205
Lookup jspLookup = new SwitchLookup(MimePath.parse("text/x-jsp"));
206
Collection jspInstances = jspLookup.lookupAll(DummySetting.class);
207
assertEquals("Wrong number of instances", 0, jspInstances.size());
211
Lookup javaLookup = new SwitchLookup(MimePath.parse("text/x-java"));
212
Collection javaInstances = javaLookup.lookupAll(DummySetting.class);
213
assertEquals("Wrong number of instances", 0, javaInstances.size());
217
Lookup jspJavaLookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
218
Collection jspJavaInstances = jspJavaLookup.lookupAll(DummySetting.class);
219
assertEquals("Wrong number of instances", 1, jspJavaInstances.size());
220
assertEquals("Wrong instance", DummySettingImpl.class, jspJavaInstances.iterator().next().getClass());
224
Lookup javaJspLookup = new SwitchLookup(MimePath.parse("text/x-java/text/x-jsp"));
225
Collection javaJspInstances = javaJspLookup.lookupAll(DummySetting.class);
226
assertEquals("Wrong number of instances", 0, javaJspInstances.size());
230
// test that FolderPathLookups are shared and discarded when they are not needed anymore
232
// test that instances of a class with a Class2LayerFolder provider are really read from the proper folder
234
public void testReadFromSpecialFolders() throws Exception {
235
TestUtilities.createFile(getWorkDir(), "Editors/text/x-java/DummyFolder/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
236
TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
237
TestUtilities.sleepForWhile();
239
Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
240
Collection instances = lookup.lookupAll(DummySetting.class);
242
assertEquals("Wrong number of instances", 1, instances.size());
243
assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass());
246
// test that adding/removing a Class2LayerFolder provider updates the lookup for its class
248
public void testChangeInMappers() throws Exception {
249
TestUtilities.createFile(getWorkDir(), "Editors/text/x-java/DummyFolder/org-netbeans-modules-editor-mimelookup-impl-DummySettingImpl.instance");
250
TestUtilities.sleepForWhile();
252
Lookup lookup = new SwitchLookup(MimePath.parse("text/x-jsp/text/x-java"));
253
Lookup.Result result = lookup.lookupResult(DummySetting.class);
254
L listener = new L();
256
result.addLookupListener(listener);
257
Collection instances = result.allInstances();
259
assertEquals("Wrong number of change events", 0, listener.resultChangedCnt);
260
assertEquals("Wrong number of instances", 0, instances.size());
263
TestUtilities.createFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
264
TestUtilities.sleepForWhile();
266
instances = result.allInstances();
267
assertEquals("Wrong number of change events", 1, listener.resultChangedCnt);
268
assertEquals("Wrong number of instances", 1, instances.size());
269
assertEquals("Wrong instance", DummySettingImpl.class, instances.iterator().next().getClass());
271
// Reset the listener
272
listener.resultChangedCnt = 0;
275
TestUtilities.deleteFile(getWorkDir(), "Services/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
276
TestUtilities.sleepForWhile();
278
assertEquals("Wrong number of change events", 1, listener.resultChangedCnt);
279
instances = result.allInstances();
280
assertEquals("Wrong number of instances", 0, instances.size());
283
private static final class L implements LookupListener {
284
public int resultChangedCnt = 0;
285
public void resultChanged(LookupEvent ev) {