1 19 20 package org.openide.util.lookup; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import java.util.Collection ; 25 import java.util.List ; 26 import java.util.Set ; 27 import org.netbeans.junit.NbTestCase; 28 import org.openide.util.Lookup; 29 30 34 public class SimpleLookupTest extends NbTestCase { 35 36 public SimpleLookupTest(String testName) { 37 super(testName); 38 } 39 40 43 public void testSingleton() { 44 Object orig = new Object (); 46 Lookup p1 = Lookups.singleton(orig); 47 Object obj = p1.lookup(Object .class); 48 assertTrue(obj == orig); 49 assertNull(p1.lookup(String .class)); 50 assertTrue(orig == p1.lookup(Object .class)); Lookup p2 = Lookups.singleton("test"); 53 assertNotNull(p2.lookup(Object .class)); 54 assertNotNull(p2.lookup(String .class)); 55 assertNotNull(p2.lookup(java.io.Serializable .class)); 56 } 57 58 61 public void testFixed() { 62 Object [] orig = new Object [] { new Object (), new Object () }; 64 Lookup p1 = Lookups.fixed(orig); 65 Object obj = p1.lookup(Object .class); 66 assertTrue(obj == orig[0] || obj == orig[1]); 67 assertNull(p1.lookup(String .class)); 68 String [] s = new String [] { "test1", "test2" }; 70 Lookup p2 = Lookups.fixed(s); 71 Object obj2 = p2.lookup(Object .class); 72 assertNotNull(obj2); 73 if (obj2 != s[0] && obj2 != s[1]) { 74 fail("Returned objects are not the originals"); 75 } 76 assertNotNull(p2.lookup(String .class)); 77 assertNotNull(p2.lookup(java.io.Serializable .class)); 78 Lookup.Template<String > t = new Lookup.Template<String >(String .class); 79 Lookup.Result<String > r = p2.lookup(t); 80 Collection <? extends String > all = r.allInstances(); 81 assertTrue(all.size() == 2); 82 for (String o : all) { 83 assertTrue("allInstances contains wrong objects", o == s[0] || o == s[1]); 84 } 85 86 try { 87 Lookups.fixed(new Object [] {null}); 88 fail("No nulls are allowed"); 89 } catch (NullPointerException ex) { 90 } 92 } 93 94 97 public void testConverting() { 98 String [] orig = new String [] { TestConvertor.TEST1, TestConvertor.TEST2 }; 100 TestConvertor convertor = new TestConvertor(); 101 Lookup p1 = Lookups.fixed(orig, convertor); 102 assertNull("Converting from String to Integer - it should not find String in result", p1.lookup(String .class)); 103 assertNotNull(p1.lookup(Integer .class)); 104 assertNotNull(p1.lookup(Integer .class)); 105 assertTrue("Convertor should be called only once.", convertor.getNumberOfConvertCalls() == 1); 106 Lookup.Template<Integer > t = new Lookup.Template<Integer >(Integer .class); 107 Lookup.Result<Integer > r = p1.lookup(t); 108 Collection <? extends Integer > all = r.allInstances(); 109 assertTrue(all.size() == 2); 110 for (int i : all) { 111 assertTrue("allInstances contains wrong objects", i == TestConvertor.t1 || i == TestConvertor.t2); 112 } 113 } 114 115 private static class TestConvertor implements InstanceContent.Convertor<String ,Integer > { 116 static final String TEST1 = "test1"; 117 static final int t1 = 1; 118 static final String TEST2 = "test2"; 119 static final int t2 = 2; 120 121 private int numberOfConvertCalls = 0; 122 123 public Integer convert(String obj) { 124 numberOfConvertCalls++; 125 if (obj.equals(TEST1)) { 126 return t1; 127 } 128 if (obj.equals(TEST2)) { 129 return t2; 130 } 131 throw new IllegalArgumentException (); 132 } 133 134 public String displayName(String obj) { 135 return obj; 136 } 137 138 public String id(String obj) { 139 if (obj.equals(TEST1)) { 140 return TEST1; 141 } 142 if (obj.equals(TEST2)) { 143 return TEST2; 144 } 145 return null; 146 } 147 148 public Class <? extends Integer > type(String obj) { 149 return Integer .class; 150 } 151 152 int getNumberOfConvertCalls() { 153 return numberOfConvertCalls; 154 } 155 } 156 157 public void testLookupItem() { 158 SomeInst inst = new SomeInst(); 159 Lookup.Item item = Lookups.lookupItem(inst, "XYZ"); 160 161 assertTrue("Wrong instance", item.getInstance() == inst); 162 assertTrue("Wrong instance class", item.getType() == inst.getClass()); 163 assertEquals("Wrong id", "XYZ", item.getId()); 164 165 item = Lookups.lookupItem(inst, null); 166 assertNotNull("Id must never be null", item.getId()); 167 } 168 169 public void testLookupItemEquals() { 170 SomeInst instA = new SomeInst(); 171 SomeInst instB = new SomeInst(); 172 Lookup.Item itemA = Lookups.lookupItem(instA, null); 173 Lookup.Item itemB = Lookups.lookupItem(instB, null); 174 175 assertTrue("Lookup items shouldn't be equal", !itemA.equals(itemB) && !itemB.equals(itemA)); 176 177 itemA = Lookups.lookupItem(instA, null); 178 itemB = Lookups.lookupItem(instA, null); 180 assertTrue("Lookup items should be equal", itemA.equals(itemB) && itemB.equals(itemA)); 181 assertTrue("Lookup items hashcode should be same", itemA.hashCode() == itemB.hashCode()); 182 183 itemA = Lookups.lookupItem(new String ("VOKURKA"), null); 184 itemB = Lookups.lookupItem(new String ("VOKURKA"), null); 185 186 assertTrue("Lookup items shouldn't be equal (2)", !itemA.equals(itemB) && !itemB.equals(itemA)); 187 } 188 189 public void testAllClassesIssue42399 () throws Exception { 190 Object [] arr = { "Ahoj", new Object () }; 191 192 Lookup l = Lookups.fixed (arr); 193 194 Set <Class <? extends Object >> s = l.lookup(new Lookup.Template<Object >(Object .class)).allClasses(); 195 196 assertEquals ("Two there", 2, s.size ()); 197 assertTrue ("Contains Object.class", s.contains (Object .class)); 198 assertTrue ("Contains string", s.contains (String .class)); 199 200 } 201 202 public void testLookupItemEarlyInitializationProblem() { 203 InstanceContent ic = new InstanceContent(); 204 AbstractLookup al = new AbstractLookup(ic); 205 LI item = new LI(); 206 List <AbstractLookup.Pair> pairs1 = new ArrayList <AbstractLookup.Pair>(); 207 List <AbstractLookup.Pair> pairs2 = new ArrayList <AbstractLookup.Pair>(); 208 209 assertEquals("Item's instance shouldn't be requested", 0, item.cnt); 210 211 pairs1.add(new ItemPair<Object >(Lookups.<Object >lookupItem(new SomeInst(), null))); 212 pairs1.add(new ItemPair<Object >(item)); 213 pairs1.add(new ItemPair<Object >(Lookups.lookupItem(new Object (), null))); 214 215 pairs2.add(new ItemPair<Object >(item)); 216 pairs2.add(new ItemPair<Object >(Lookups.lookupItem(new Object (), null))); 217 218 ic.setPairs(pairs1); 219 ic.setPairs(pairs2); 220 221 assertEquals("Item's instance shouldn't be requested when added to lookup", 0, item.cnt); 222 223 LI item2 = al.lookup(LI.class); 224 assertEquals("Item's instance should be requested", 1, item.cnt); 225 } 226 227 public void testConvenienceMethods() throws Exception { 228 Lookup l = Lookups.fixed(new Object [] {1, "hello", 2, "goodbye"}); 230 Collection <? extends Integer > ints = l.lookupAll(Integer .class); 231 assertEquals(Arrays.asList(new Integer [] {1, 2}), new ArrayList <Integer >(ints)); 232 Lookup.Result<Integer > r = l.lookupResult(Integer .class); 233 ints = r.allInstances(); 234 assertEquals(Arrays.asList(new Integer [] {1, 2}), new ArrayList <Integer >(ints)); 235 } 236 237 private static class SomeInst { } 238 239 private static class LI extends Lookup.Item<Object > { 240 241 public long cnt = 0; 242 243 public String getDisplayName() { 244 return getId(); 245 } 246 247 public String getId() { 248 return getClass() + "@" + hashCode(); 249 } 250 251 public Object getInstance() { 252 cnt++; 253 return this; 254 } 255 256 public Class <? extends Object > getType() { 257 return getClass(); 258 } 259 } 261 private static class ItemPair<T> extends AbstractLookup.Pair<T> { 262 263 private AbstractLookup.Item<T> item; 264 265 public ItemPair(Lookup.Item<T> i) { 266 this.item = i; 267 } 268 269 protected boolean creatorOf(Object obj) { 270 return item.getInstance() == obj; 271 } 272 273 public String getDisplayName() { 274 return item.getDisplayName (); 275 } 276 277 public String getId() { 278 return item.getId (); 279 } 280 281 public T getInstance() { 282 return item.getInstance (); 283 } 284 285 public Class <? extends T> getType() { 286 return item.getType (); 287 } 288 289 protected boolean instanceOf(Class <?> c) { 290 return c.isAssignableFrom(getType()); 291 } 292 293 public boolean equals (Object o) { 294 if (o instanceof ItemPair) { 295 ItemPair p = (ItemPair)o; 296 return item.equals (p.item); 297 } 298 return false; 299 } 300 301 public int hashCode () { 302 return item.hashCode (); 303 } 304 } } 306 | Popular Tags |