1 19 20 package org.netbeans.spi.project.support; 21 22 import java.beans.PropertyChangeListener ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.List ; 28 import javax.swing.Icon ; 29 import javax.swing.JButton ; 30 import javax.swing.JCheckBox ; 31 import javax.swing.JComboBox ; 32 import javax.swing.JRadioButton ; 33 import javax.swing.JTextArea ; 34 import javax.swing.JTextField ; 35 import javax.swing.event.ChangeEvent ; 36 import javax.swing.event.ChangeListener ; 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 51 public class LookupProviderSupportTest extends NbTestCase { 52 53 public LookupProviderSupportTest(String testName) { 54 super(testName); 55 } 56 57 60 public void testCreateCompositeLookup() { 61 LookupMergerImpl merger = new LookupMergerImpl(); 62 Lookup base = Lookups.fixed(new JButton (), new JComboBox (), 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 ()); 73 pro2.ic.add(new JTextArea ()); 74 75 LookupProviderSupport.DelegatingLookupImpl del = new LookupProviderSupport.DelegatingLookupImpl(base, providers); 76 77 assertNotNull(del.lookup(JTextArea .class)); 78 assertNotNull(del.lookup(JComboBox .class)); 79 80 JButton butt = del.lookup(JButton .class); 82 assertNotNull(butt); 83 assertEquals("CORRECT", butt.getText()); 84 assertEquals(1, del.lookup(new Lookup.Template(JButton .class)).allInstances().size()); 85 assertEquals(1, merger.expectedCount); 86 87 pro3.ic.add(new JButton ()); 88 pro3.ic.add(new JRadioButton ()); 89 provInst.add(pro3); 90 assertNotNull(del.lookup(JRadioButton .class)); 91 92 butt = del.lookup(JButton .class); 94 assertNotNull(butt); 95 assertEquals("CORRECT", butt.getText()); 96 assertEquals(1, del.lookup(new Lookup.Template(JButton .class)).allInstances().size()); 97 assertEquals(2, merger.expectedCount); 98 99 pro1.ic.add(new JButton ()); 100 101 butt = del.lookup(JButton .class); 103 assertNotNull(butt); 104 assertEquals("CORRECT", butt.getText()); 105 assertEquals(1, del.lookup(new Lookup.Template(JButton .class)).allInstances().size()); 106 assertEquals(3, merger.expectedCount); 107 108 } 109 110 private SourcesImpl createImpl(String 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 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 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 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 { 181 Lookup l = LookupProviderSupport.createCompositeLookup(Lookup.EMPTY, "nowhere"); 183 assertEquals(Collections.<Object >emptySet(), new HashSet <Object >(l.lookupAll(Object .class))); 184 } 185 186 private class ChangeListenerImpl implements ChangeListener { 187 boolean pinged = false; 188 public void stateChanged(ChangeEvent 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 .class)); 198 assertNull(baseContext.lookup(JCheckBox .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 > { 217 218 int expectedCount; 219 220 public Class <JButton > getMergeableClass() { 221 return JButton .class; 222 } 223 224 public JButton merge(Lookup lookup) { 225 Lookup.Result res = lookup.lookup(new Lookup.Template(JButton .class)); 226 int size = res.allInstances().size(); 227 expectedCount = size; 228 return new JButton ("CORRECT"); 229 } 230 231 } 232 233 private static class SourcesImpl implements Sources { 234 public HashMap <String , SourceGroup[]> grpMap = new HashMap <String , SourceGroup[]>(); 235 private List <ChangeListener > listeners = new ArrayList <ChangeListener >(); 236 237 238 public SourceGroup[] getSourceGroups(String type) { 239 return grpMap.get(type); 240 } 241 242 public void addChangeListener(ChangeListener listener) { 243 listeners.add(listener); 244 } 245 246 public void removeChangeListener(ChangeListener listener) { 247 listeners.remove(listener); 248 } 249 250 public void fireChange() { 251 for (ChangeListener listener : listeners) { 252 listener.stateChanged(new ChangeEvent (this)); 253 } 254 } 255 } 256 257 private static class SourceGroupImpl implements SourceGroup { 258 259 String name; 260 261 String displayName; 262 public FileObject getRootFolder() { 263 return null; 264 } 265 266 public String getName() { 267 return name; 268 } 269 270 public String getDisplayName() { 271 return displayName; 272 } 273 274 public Icon getIcon(boolean opened) { 275 return null; 276 } 277 278 public boolean contains(FileObject file) throws IllegalArgumentException { 279 return false; 280 } 281 282 public void addPropertyChangeListener(PropertyChangeListener listener) { 283 } 284 285 public void removePropertyChangeListener(PropertyChangeListener listener) { 286 } 287 } 288 289 } 290 | Popular Tags |