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.spi.project.support;
043:
044: import java.beans.PropertyChangeListener;
045: import java.util.Collections;
046: import java.util.HashMap;
047: import java.util.HashSet;
048: import javax.swing.Icon;
049: import javax.swing.JButton;
050: import javax.swing.JCheckBox;
051: import javax.swing.JComboBox;
052: import javax.swing.JRadioButton;
053: import javax.swing.JTextArea;
054: import javax.swing.JTextField;
055: import javax.swing.event.ChangeListener;
056: import org.netbeans.api.project.SourceGroup;
057: import org.netbeans.api.project.Sources;
058: import org.netbeans.junit.NbTestCase;
059: import org.netbeans.spi.project.LookupMerger;
060: import org.netbeans.spi.project.LookupProvider;
061: import org.openide.filesystems.FileObject;
062: import org.openide.util.Lookup;
063: import org.openide.util.lookup.AbstractLookup;
064: import org.openide.util.lookup.InstanceContent;
065: import org.openide.util.lookup.Lookups;
066: import org.openide.util.test.MockChangeListener;
067:
068: /**
069: * @author mkleint
070: */
071: public class LookupProviderSupportTest extends NbTestCase {
072:
073: public LookupProviderSupportTest(String testName) {
074: super (testName);
075: }
076:
077: /**
078: * Test of createCompositeLookup method, of class org.netbeans.spi.project.support.LookupProviderSupport.
079: */
080: public void testCreateCompositeLookup() {
081: LookupMergerImpl merger = new LookupMergerImpl();
082: Lookup base = Lookups.fixed(new JButton(), new JComboBox(),
083: merger);
084: LookupProviderImpl pro1 = new LookupProviderImpl();
085: LookupProviderImpl pro2 = new LookupProviderImpl();
086: LookupProviderImpl pro3 = new LookupProviderImpl();
087:
088: InstanceContent provInst = new InstanceContent();
089: Lookup providers = new AbstractLookup(provInst);
090: provInst.add(pro1);
091: provInst.add(pro2);
092:
093: pro1.ic.add(new JTextField());
094: pro2.ic.add(new JTextArea());
095:
096: LookupProviderSupport.DelegatingLookupImpl del = new LookupProviderSupport.DelegatingLookupImpl(
097: base, providers, "<irrelevant>");
098:
099: assertNotNull(del.lookup(JTextArea.class));
100: assertNotNull(del.lookup(JComboBox.class));
101:
102: // test merger..
103: JButton butt = del.lookup(JButton.class);
104: assertNotNull(butt);
105: assertEquals("CORRECT", butt.getText());
106: assertEquals(1, del.lookupAll(JButton.class).size());
107: assertEquals(1, merger.expectedCount);
108:
109: pro3.ic.add(new JButton());
110: pro3.ic.add(new JRadioButton());
111: provInst.add(pro3);
112: assertNotNull(del.lookup(JRadioButton.class));
113:
114: // test merger..
115: butt = del.lookup(JButton.class);
116: assertNotNull(butt);
117: assertEquals("CORRECT", butt.getText());
118: assertEquals(1, del.lookupAll(JButton.class).size());
119: assertEquals(2, merger.expectedCount);
120:
121: pro1.ic.add(new JButton());
122:
123: // test merger..
124: butt = del.lookup(JButton.class);
125: assertNotNull(butt);
126: assertEquals("CORRECT", butt.getText());
127: assertEquals(1, del.lookupAll(JButton.class).size());
128: assertEquals(3, merger.expectedCount);
129:
130: }
131:
132: private SourcesImpl createImpl(String id) {
133: SourcesImpl impl0 = new SourcesImpl();
134: SourceGroupImpl grp0 = new SourceGroupImpl();
135: grp0.name = id;
136: impl0.grpMap.put("java", new SourceGroup[] { grp0 });
137: return impl0;
138: }
139:
140: public void testSourcesMerger() {
141: SourcesImpl impl0 = createImpl("group0");
142: SourcesImpl impl1 = createImpl("group1");
143: SourcesImpl impl2 = createImpl("group2");
144: SourcesImpl impl3 = createImpl("group3");
145:
146: Lookup base = Lookups.fixed(impl0, LookupProviderSupport
147: .createSourcesMerger());
148: LookupProviderImpl2 pro1 = new LookupProviderImpl2();
149: LookupProviderImpl2 pro2 = new LookupProviderImpl2();
150: LookupProviderImpl2 pro3 = new LookupProviderImpl2();
151:
152: InstanceContent provInst = new InstanceContent();
153: Lookup providers = new AbstractLookup(provInst);
154: provInst.add(pro1);
155: provInst.add(pro2);
156:
157: pro1.ic.add(impl1);
158: pro2.ic.add(impl2);
159: pro3.ic.add(impl3);
160:
161: LookupProviderSupport.DelegatingLookupImpl del = new LookupProviderSupport.DelegatingLookupImpl(
162: base, providers, "<irrelevant>");
163:
164: Sources srcs = del.lookup(Sources.class);
165: assertNotNull(srcs);
166: SourceGroup[] grps = srcs.getSourceGroups("java");
167: assertEquals(3, grps.length);
168:
169: //now let's add another module to the bunch and see if the new SG appears
170: provInst.add(pro3);
171:
172: srcs = del.lookup(Sources.class);
173: assertNotNull(srcs);
174: grps = srcs.getSourceGroups("java");
175: assertEquals(4, grps.length);
176:
177: //now let's remove another module to the bunch and see if the SG disappears
178: provInst.remove(pro2);
179:
180: srcs = del.lookup(Sources.class);
181: assertNotNull(srcs);
182: grps = srcs.getSourceGroups("java");
183: assertEquals(3, grps.length);
184:
185: //lets remove one and listen for changes...
186: srcs = del.lookup(Sources.class);
187: MockChangeListener ch = new MockChangeListener();
188: srcs.addChangeListener(ch);
189: provInst.remove(pro1);
190:
191: ch.assertEvent();
192: grps = srcs.getSourceGroups("java");
193: assertEquals(2, grps.length);
194:
195: provInst.add(pro2);
196:
197: ch.assertEvent();
198: grps = srcs.getSourceGroups("java");
199: assertEquals(3, grps.length);
200:
201: }
202:
203: public void testNonexistentPath() throws Exception {
204: // #87544: don't choke on a nonexistent path! Just leave it empty.
205: Lookup l = LookupProviderSupport.createCompositeLookup(
206: Lookup.EMPTY, "nowhere");
207: assertEquals(Collections.<Object> emptySet(),
208: new HashSet<Object>(l.lookupAll(Object.class)));
209: }
210:
211: private class LookupProviderImpl implements LookupProvider {
212: InstanceContent ic = new InstanceContent();
213: boolean wasAlreadyCalled = false;
214:
215: public Lookup createAdditionalLookup(Lookup baseContext) {
216: assertNotNull(baseContext.lookup(JButton.class));
217: assertNull(baseContext.lookup(JCheckBox.class));
218: assertFalse(wasAlreadyCalled);
219: wasAlreadyCalled = true;
220: return new AbstractLookup(ic);
221: }
222: }
223:
224: private class LookupProviderImpl2 implements LookupProvider {
225: InstanceContent ic = new InstanceContent();
226: AbstractLookup l;
227:
228: public Lookup createAdditionalLookup(Lookup baseContext) {
229: if (l == null) {
230: l = new AbstractLookup(ic);
231: }
232: return l;
233: }
234: }
235:
236: private class LookupMergerImpl implements LookupMerger<JButton> {
237:
238: int expectedCount;
239:
240: public Class<JButton> getMergeableClass() {
241: return JButton.class;
242: }
243:
244: public JButton merge(Lookup lookup) {
245: expectedCount = lookup.lookupAll(JButton.class).size();
246: return new JButton("CORRECT");
247: }
248:
249: }
250:
251: private static class SourcesImpl implements Sources {
252: public HashMap<String, SourceGroup[]> grpMap = new HashMap<String, SourceGroup[]>();
253:
254: public SourceGroup[] getSourceGroups(String type) {
255: return grpMap.get(type);
256: }
257:
258: public void addChangeListener(ChangeListener listener) {
259: }
260:
261: public void removeChangeListener(ChangeListener listener) {
262: }
263: }
264:
265: private static class SourceGroupImpl implements SourceGroup {
266:
267: String name;
268:
269: String displayName;
270:
271: public FileObject getRootFolder() {
272: return null;
273: }
274:
275: public String getName() {
276: return name;
277: }
278:
279: public String getDisplayName() {
280: return displayName;
281: }
282:
283: public Icon getIcon(boolean opened) {
284: return null;
285: }
286:
287: public boolean contains(FileObject file)
288: throws IllegalArgumentException {
289: return false;
290: }
291:
292: public void addPropertyChangeListener(
293: PropertyChangeListener listener) {
294: }
295:
296: public void removePropertyChangeListener(
297: PropertyChangeListener listener) {
298: }
299: }
300:
301: }
|