001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.editor.mimelookup.impl;
043:
044: import java.net.URL;
045: import java.util.Collection;
046: import org.netbeans.junit.NbTestCase;
047: import org.netbeans.spi.editor.mimelookup.Class2LayerFolder;
048: import org.openide.filesystems.FileObject;
049: import org.openide.util.Lookup;
050: import org.openide.util.LookupEvent;
051: import org.openide.util.LookupListener;
052:
053: /**
054: *
055: * @author vita
056: */
057: public class FolderPathLookupTest extends NbTestCase {
058:
059: /** Creates a new instance of FolderPathLookupTest */
060: public FolderPathLookupTest(String name) {
061: super (name);
062: }
063:
064: protected void setUp() throws java.lang.Exception {
065: clearWorkDir();
066: // Set up the default lookup, repository, etc.
067: EditorTestLookup.setLookup(new String[0], getWorkDir(),
068: new Object[] {}, getClass().getClassLoader(), null);
069: }
070:
071: protected void tearDown() {
072: TestUtilities.gc();
073: }
074:
075: public void testSimple() throws Exception {
076: TestUtilities
077: .createFile(
078: getWorkDir(),
079: "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
080:
081: // Create lookup over an existing folder
082: Lookup lookup = new FolderPathLookup(
083: new String[] { "Tmp/A/B/C/D" });
084: Collection instances = lookup
085: .lookupAll(Class2LayerFolder.class);
086:
087: assertEquals("Wrong number of instances", 1, instances.size());
088: assertEquals("Wrong instance", DummyClass2LayerFolder.class,
089: instances.iterator().next().getClass());
090:
091: // Now create lookup over a non-existing folder
092: lookup = new FolderPathLookup(new String[] { "Tmp/X/Y/Z" });
093: instances = lookup.lookupAll(Object.class);
094:
095: assertEquals("Wrong number of instances", 0, instances.size());
096: }
097:
098: public void testAddingFolders() throws Exception {
099: // Create lookup over a non-existing folder
100: Lookup lookup = new FolderPathLookup(
101: new String[] { "Tmp/A/B/C/D" });
102: Collection instances = lookup
103: .lookupAll(Class2LayerFolder.class);
104:
105: assertEquals("Wrong number of instances", 0, instances.size());
106:
107: // Create the folder and the instance
108: TestUtilities
109: .createFile(
110: getWorkDir(),
111: "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
112:
113: instances = lookup.lookupAll(Class2LayerFolder.class);
114: assertEquals("Wrong number of instances", 1, instances.size());
115: assertEquals("Wrong instance", DummyClass2LayerFolder.class,
116: instances.iterator().next().getClass());
117: }
118:
119: public void testRemovingFolders() throws Exception {
120: TestUtilities
121: .createFile(
122: getWorkDir(),
123: "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
124:
125: // Create lookup over an existing folder
126: Lookup lookup = new FolderPathLookup(
127: new String[] { "Tmp/A/B/C/D" });
128: Collection instances = lookup
129: .lookupAll(Class2LayerFolder.class);
130:
131: assertEquals("Wrong number of instances", 1, instances.size());
132: assertEquals("Wrong instance", DummyClass2LayerFolder.class,
133: instances.iterator().next().getClass());
134:
135: // Delete the folders
136: TestUtilities.deleteFile(getWorkDir(), "Tmp");
137:
138: instances = lookup.lookupAll(Class2LayerFolder.class);
139: assertEquals("Wrong number of instances", 0, instances.size());
140: }
141:
142: public void testChangeEvents() throws Exception {
143: Lookup.Result lr = new FolderPathLookup(
144: new String[] { "Tmp/A/B/C/D" })
145: .lookupResult(Class2LayerFolder.class);
146: L listener = new L();
147: lr.addLookupListener(listener);
148:
149: Collection instances = lr.allInstances();
150: assertEquals("Wrong number of instances", 0, instances.size());
151:
152: // Create the folder and the instance
153: TestUtilities
154: .createFile(
155: getWorkDir(),
156: "Tmp/A/B/C/D/org-netbeans-modules-editor-mimelookup-impl-DummyClass2LayerFolder.instance");
157:
158: assertEquals("Wrong number of events", 1,
159: listener.resultChangedCnt);
160:
161: instances = lr.allInstances();
162: assertEquals("Wrong number of instances", 1, instances.size());
163: assertEquals("Wrong instance", DummyClass2LayerFolder.class,
164: instances.iterator().next().getClass());
165:
166: // Reset the listener
167: listener.resultChangedCnt = 0;
168:
169: // Delete the folders
170: TestUtilities.deleteFile(getWorkDir(), "Tmp");
171:
172: assertEquals("Wrong number of events", 1,
173: listener.resultChangedCnt);
174:
175: instances = lr.allInstances();
176: assertEquals("Wrong number of instances", 0, instances.size());
177: }
178:
179: // IZ #104705
180: public void testInstaceOf() throws Exception {
181: EditorTestLookup.setLookup(new URL[] { getClass().getResource(
182: "test-layer.xml") }, getWorkDir(), new Object[] {},
183: getClass().getClassLoader());
184:
185: Lookup lookup = new FolderPathLookup(
186: new String[] { "Tmp/PathFolderLookupTest/testInstanceOf" });
187:
188: // Check IfaceA instances, it should not pick up the one with
189: // instanceOf == ...$IfaceB
190: Collection instances = lookup.lookupAll(IfaceA.class);
191: assertEquals("Wrong number of IfaceA instances", 1, instances
192: .size());
193:
194: Object instance = instances.iterator().next();
195: assertTrue("Wrong instance", instance instanceof ImplAB);
196: assertEquals(
197: "Wrong instance file",
198: "Tmp/PathFolderLookupTest/testInstanceOf/ifaceA-impl.instance",
199: ((ImplAB) instance).fileObject.getPath());
200: }
201:
202: public static Object createIfacesImpl(FileObject fo) {
203: return new ImplAB(fo);
204: }
205:
206: public static interface IfaceA {
207: void methodA();
208: }
209:
210: public static interface IfaceB {
211: void methodB();
212: }
213:
214: private static final class ImplAB implements IfaceA, IfaceB {
215: public FileObject fileObject;
216:
217: public ImplAB(FileObject fo) {
218: this .fileObject = fo;
219: }
220:
221: public void methodA() {
222: }
223:
224: public void methodB() {
225: }
226: }
227:
228: private static final class L implements LookupListener {
229: public int resultChangedCnt = 0;
230:
231: public void resultChanged(LookupEvent ev) {
232: resultChangedCnt++;
233: }
234: }
235: }
|