KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > LookupProviderSupportTest


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.spi.project.support;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.Icon JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.JCheckBox JavaDoc;
31 import javax.swing.JComboBox JavaDoc;
32 import javax.swing.JRadioButton JavaDoc;
33 import javax.swing.JTextArea JavaDoc;
34 import javax.swing.JTextField JavaDoc;
35 import javax.swing.event.ChangeEvent JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37 import org.netbeans.api.project.SourceGroup;
38 import org.netbeans.api.project.Sources;
39 import org.netbeans.junit.NbTestCase;
40 import org.netbeans.spi.project.LookupMerger;
41 import org.netbeans.spi.project.LookupProvider;
42 import org.openide.filesystems.FileObject;
43 import org.openide.util.Lookup;
44 import org.openide.util.lookup.AbstractLookup;
45 import org.openide.util.lookup.InstanceContent;
46 import org.openide.util.lookup.Lookups;
47
48 /**
49  * @author mkleint
50  */

51 public class LookupProviderSupportTest extends NbTestCase {
52     
53     public LookupProviderSupportTest(String JavaDoc testName) {
54         super(testName);
55     }
56
57     /**
58      * Test of createCompositeLookup method, of class org.netbeans.spi.project.support.LookupProviderSupport.
59      */

60     public void testCreateCompositeLookup() {
61         LookupMergerImpl merger = new LookupMergerImpl();
62         Lookup base = Lookups.fixed(new JButton JavaDoc(), new JComboBox JavaDoc(), merger);
63         LookupProviderImpl pro1 = new LookupProviderImpl();
64         LookupProviderImpl pro2 = new LookupProviderImpl();
65         LookupProviderImpl pro3 = new LookupProviderImpl();
66         
67         InstanceContent provInst = new InstanceContent();
68         Lookup providers = new AbstractLookup(provInst);
69         provInst.add(pro1);
70         provInst.add(pro2);
71         
72         pro1.ic.add(new JTextField JavaDoc());
73         pro2.ic.add(new JTextArea JavaDoc());
74         
75         LookupProviderSupport.DelegatingLookupImpl del = new LookupProviderSupport.DelegatingLookupImpl(base, providers);
76         
77         assertNotNull(del.lookup(JTextArea JavaDoc.class));
78         assertNotNull(del.lookup(JComboBox JavaDoc.class));
79         
80         // test merger..
81
JButton JavaDoc butt = del.lookup(JButton JavaDoc.class);
82         assertNotNull(butt);
83         assertEquals("CORRECT", butt.getText());
84         assertEquals(1, del.lookup(new Lookup.Template(JButton JavaDoc.class)).allInstances().size());
85         assertEquals(1, merger.expectedCount);
86         
87         pro3.ic.add(new JButton JavaDoc());
88         pro3.ic.add(new JRadioButton JavaDoc());
89         provInst.add(pro3);
90         assertNotNull(del.lookup(JRadioButton JavaDoc.class));
91                 
92         // test merger..
93
butt = del.lookup(JButton JavaDoc.class);
94         assertNotNull(butt);
95         assertEquals("CORRECT", butt.getText());
96         assertEquals(1, del.lookup(new Lookup.Template(JButton JavaDoc.class)).allInstances().size());
97         assertEquals(2, merger.expectedCount);
98         
99         pro1.ic.add(new JButton JavaDoc());
100         
101         // test merger..
102
butt = del.lookup(JButton JavaDoc.class);
103         assertNotNull(butt);
104         assertEquals("CORRECT", butt.getText());
105         assertEquals(1, del.lookup(new Lookup.Template(JButton JavaDoc.class)).allInstances().size());
106         assertEquals(3, merger.expectedCount);
107         
108     }
109     
110     private SourcesImpl createImpl(String JavaDoc id) {
111         SourcesImpl impl0 = new SourcesImpl();
112         SourceGroupImpl grp0 = new SourceGroupImpl();
113         grp0.name = id;
114         impl0.grpMap.put("java", new SourceGroup[] {grp0});
115         return impl0;
116     }
117     
118     public void testSourcesMerger() {
119         SourcesImpl impl0 = createImpl("group0");
120         SourcesImpl impl1 = createImpl("group1");
121         SourcesImpl impl2 = createImpl("group2");
122         SourcesImpl impl3 = createImpl("group3");
123         
124         Lookup base = Lookups.fixed(impl0, LookupProviderSupport.createSourcesMerger());
125         LookupProviderImpl2 pro1 = new LookupProviderImpl2();
126         LookupProviderImpl2 pro2 = new LookupProviderImpl2();
127         LookupProviderImpl2 pro3 = new LookupProviderImpl2();
128         
129         InstanceContent provInst = new InstanceContent();
130         Lookup providers = new AbstractLookup(provInst);
131         provInst.add(pro1);
132         provInst.add(pro2);
133         
134         pro1.ic.add(impl1);
135         pro2.ic.add(impl2);
136         pro3.ic.add(impl3);
137         
138         LookupProviderSupport.DelegatingLookupImpl del = new LookupProviderSupport.DelegatingLookupImpl(base, providers);
139         
140         Sources srcs = del.lookup(Sources.class);
141         assertNotNull(srcs);
142         SourceGroup[] grps = srcs.getSourceGroups("java");
143         assertEquals(3, grps.length);
144         
145         //now let's add another module to the bunch and see if the new SG appears
146
provInst.add(pro3);
147         
148         srcs = del.lookup(Sources.class);
149         assertNotNull(srcs);
150         grps = srcs.getSourceGroups("java");
151         assertEquals(4, grps.length);
152         
153         //now let's remove another module to the bunch and see if the SG disappears
154
provInst.remove(pro2);
155         
156         srcs = del.lookup(Sources.class);
157         assertNotNull(srcs);
158         grps = srcs.getSourceGroups("java");
159         assertEquals(3, grps.length);
160         
161         //lets remove one and listen for changes...
162
srcs = del.lookup(Sources.class);
163         ChangeListenerImpl ch = new ChangeListenerImpl();
164         srcs.addChangeListener(ch);
165         provInst.remove(pro1);
166         
167         assertTrue(ch.pinged);
168         grps = srcs.getSourceGroups("java");
169         assertEquals(2, grps.length);
170         
171         ch.pinged = false;
172         provInst.add(pro2);
173         
174         assertTrue(ch.pinged);
175         grps = srcs.getSourceGroups("java");
176         assertEquals(3, grps.length);
177         
178     }
179     
180     public void testNonexistentPath() throws Exception JavaDoc {
181         // #87544: don't choke on a nonexistent path! Just leave it empty.
182
Lookup l = LookupProviderSupport.createCompositeLookup(Lookup.EMPTY, "nowhere");
183         assertEquals(Collections.<Object JavaDoc>emptySet(), new HashSet JavaDoc<Object JavaDoc>(l.lookupAll(Object JavaDoc.class)));
184     }
185     
186     private class ChangeListenerImpl implements ChangeListener JavaDoc {
187         boolean pinged = false;
188         public void stateChanged(ChangeEvent JavaDoc e) {
189             pinged = true;
190         }
191     }
192     
193     private class LookupProviderImpl implements LookupProvider {
194         InstanceContent ic = new InstanceContent();
195         boolean wasAlreadyCalled = false;
196         public Lookup createAdditionalLookup(Lookup baseContext) {
197             assertNotNull(baseContext.lookup(JButton JavaDoc.class));
198             assertNull(baseContext.lookup(JCheckBox JavaDoc.class));
199             assertFalse(wasAlreadyCalled);
200             wasAlreadyCalled = true;
201             return new AbstractLookup(ic);
202         }
203     }
204
205     private class LookupProviderImpl2 implements LookupProvider {
206         InstanceContent ic = new InstanceContent();
207         AbstractLookup l;
208         public Lookup createAdditionalLookup(Lookup baseContext) {
209             if (l == null) {
210                 l = new AbstractLookup(ic);
211             }
212             return l;
213         }
214     }
215     
216     private class LookupMergerImpl implements LookupMerger<JButton JavaDoc> {
217         
218         int expectedCount;
219         
220         public Class JavaDoc<JButton JavaDoc> getMergeableClass() {
221             return JButton JavaDoc.class;
222         }
223
224         public JButton JavaDoc merge(Lookup lookup) {
225             Lookup.Result res = lookup.lookup(new Lookup.Template(JButton JavaDoc.class));
226             int size = res.allInstances().size();
227             expectedCount = size;
228             return new JButton JavaDoc("CORRECT");
229         }
230         
231     }
232     
233     private static class SourcesImpl implements Sources {
234         public HashMap JavaDoc<String JavaDoc, SourceGroup[]> grpMap = new HashMap JavaDoc<String JavaDoc, SourceGroup[]>();
235         private List JavaDoc<ChangeListener JavaDoc> listeners = new ArrayList JavaDoc<ChangeListener JavaDoc>();
236         
237         
238         public SourceGroup[] getSourceGroups(String JavaDoc type) {
239             return grpMap.get(type);
240         }
241
242         public void addChangeListener(ChangeListener JavaDoc listener) {
243             listeners.add(listener);
244         }
245
246         public void removeChangeListener(ChangeListener JavaDoc listener) {
247             listeners.remove(listener);
248         }
249         
250         public void fireChange() {
251             for (ChangeListener JavaDoc listener : listeners) {
252                 listener.stateChanged(new ChangeEvent JavaDoc(this));
253             }
254         }
255     }
256     
257     private static class SourceGroupImpl implements SourceGroup {
258
259         String JavaDoc name;
260
261         String JavaDoc displayName;
262         public FileObject getRootFolder() {
263             return null;
264         }
265
266         public String JavaDoc getName() {
267             return name;
268         }
269
270         public String JavaDoc getDisplayName() {
271             return displayName;
272         }
273
274         public Icon JavaDoc getIcon(boolean opened) {
275             return null;
276         }
277
278         public boolean contains(FileObject file) throws IllegalArgumentException JavaDoc {
279             return false;
280         }
281
282         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
283         }
284
285         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
286         }
287     }
288     
289 }
290
Popular Tags