1 19 20 package org.openide.util.lookup; 21 22 import org.openide.util.*; 23 24 import java.lang.ref.WeakReference ; 25 import java.util.*; 26 import junit.framework.*; 27 import org.netbeans.junit.*; 28 import java.io.Serializable ; 29 import org.openide.util.io.NbMarshalledObject; 30 31 public class AbstractLookupTest extends AbstractLookupBaseHid implements AbstractLookupBaseHid.Impl { 32 public AbstractLookupTest(java.lang.String testName) { 33 super(testName, null); 34 } 35 36 40 42 public Lookup createInstancesLookup (InstanceContent ic) { 43 return new AbstractLookup (ic, new InheritanceTree ()); 44 } 45 46 51 public Lookup createLookup (Lookup lookup) { 52 return lookup; 53 } 54 55 public void clearCaches () { 56 } 57 58 public static void main(java.lang.String [] args) { 59 junit.textui.TestRunner.run(new NbTestSuite(AbstractLookupTest.class)); 60 } 61 62 static class LkpResultCanBeGargageCollectedAndClearsTheResult extends AbstractLookup { 63 public int cleared; 64 public int dirty; 65 66 synchronized boolean cleanUpResult (Template t) { 67 boolean res = super.cleanUpResult (t); 68 if (res) { 69 cleared++; 70 } else { 71 dirty++; 72 } 73 74 notifyAll (); 75 76 return res; 77 } 78 } 79 public void testResultCanBeGargageCollectedAndClearsTheResult () throws Exception { 80 LkpResultCanBeGargageCollectedAndClearsTheResult lkp = new LkpResultCanBeGargageCollectedAndClearsTheResult (); 81 assertSize ("24 for AbstractLookup, 8 for two ints", 32, lkp); 82 synchronized (lkp) { 83 Lookup.Result res = lkp.lookup (new Lookup.Template (getClass ())); 84 res.allItems(); 85 86 WeakReference ref = new WeakReference (res); 87 res = null; 88 assertGC ("Reference can get cleared", ref); 89 90 while (lkp.cleared == 0 && lkp.dirty == 0) { 92 lkp.wait (); 93 } 94 95 assertEquals ("No dirty cleanups", 0, lkp.dirty); 96 assertEquals ("One final cleanup", 1, lkp.cleared); 97 } 98 100 } 101 102 public void testPairCannotBeUsedInMoreThanOneLookupAtOnce () throws Exception { 103 104 class EmptyPair extends AbstractLookup.Pair { 105 protected boolean creatorOf(Object obj) { return false; } 106 public String getDisplayName() { return "Empty"; } 107 public String getId() { return "empty"; } 108 public Object getInstance() { return null; } 109 public Class getType() { return Object .class; } 110 protected boolean instanceOf(Class c) { return c == getType (); } 111 } 113 AbstractLookup.Content c1 = new AbstractLookup.Content (); 114 AbstractLookup.Content c2 = new AbstractLookup.Content (); 115 AbstractLookup l1 = new AbstractLookup (c1); 116 AbstractLookup l2 = new AbstractLookup (c2); 117 118 EmptyPair empty = new EmptyPair (); 119 c1.addPair (empty); 120 Lookup.Result res = l1.lookup (new Lookup.Template (Object .class)); 121 assertEquals ( 122 "Pair is really found", empty, 123 res.allItems ().iterator().next () 124 ); 125 try { 126 c2.addPair (empty); 127 fail ("It should not be possible to add pair to two lookups"); 128 } catch (IllegalStateException ex) { 129 } 131 assertEquals ( 132 "L2 is still empty", Collections.EMPTY_LIST, 133 new ArrayList (l2.lookup (new Lookup.Template (Object .class)).allItems ()) 134 ); 135 } 136 137 public void testInitializationCanBeDoneFromAnotherThread () { 138 class MyLkp extends AbstractLookup implements Runnable { 139 private InstanceContent ic; 140 private boolean direct; 141 142 public MyLkp (boolean direct) { 143 this (direct, new InstanceContent ()); 144 } 145 146 private MyLkp (boolean direct, InstanceContent ic) { 147 super (ic); 148 this.direct = direct; 149 this.ic = ic; 150 } 151 152 protected void initialize () { 153 if (direct) { 154 run (); 155 } else { 156 RequestProcessor.getDefault().post (this).waitFinished (); 157 } 158 } 159 160 public void run () { 161 ic.add (this); 162 ic.remove (this); 163 ic.set (Collections.nCopies(10, this), null); 164 ic.set (Collections.EMPTY_LIST, null); 165 ic.add (AbstractLookupTest.this); 166 } 167 } 168 169 assertEquals ("The test should be there", this, new MyLkp (true).lookup (Object .class)); 170 assertEquals ("and in async mode as well", this, new MyLkp (false).lookup (Object .class)); 171 } 172 173 public void testBeforeLookupIsCalled () { 174 class BeforeL extends AbstractLookup { 175 public ArrayList list = new ArrayList (); 176 public String toAdd; 177 public InstanceContent ic; 178 179 public BeforeL () { 180 this (new InstanceContent ()); 181 } 182 183 private BeforeL (InstanceContent c) { 184 super (c); 185 this.ic = c; 186 } 187 188 protected void beforeLookup (Template t) { 189 if (toAdd != null) { 190 list.add (0, new SerialPair (toAdd)); 191 setPairs (list); 192 } else { 193 ic.add (new Integer (1)); 194 } 195 } 196 } 197 198 BeforeL lookup = new BeforeL (); 199 200 lookup.toAdd = "First"; 201 assertEquals ("First if found", "First", lookup.lookup (String .class)); 202 203 lookup.toAdd = "2"; 204 assertEquals ("2 is not first", "2", lookup.lookup (String .class)); 205 206 Lookup.Result res = lookup.lookup (new Lookup.Template (Object .class)); 207 for (int i = 3; i < 20; i++) { 208 lookup.toAdd = String.valueOf (i); 209 assertEquals (i + " items are now there", i, res.allInstances ().size ()); 210 } 211 for (int i = 20; i < 35; i++) { 212 lookup.toAdd = String.valueOf (i); 213 assertEquals (i + " items are now there", i, res.allItems ().size ()); 214 } 215 216 assertEquals ("Just strings are there now", 1, res.allClasses ().size ()); 217 lookup.toAdd = null; assertEquals ("Two classes now", 2, res.allClasses ().size ()); 219 } 220 221 public void testInconsistentAfterDeserIssue71744() throws Exception { 222 InheritanceTree inhTree = new InheritanceTree(); 223 224 AbstractLookup al = new AbstractLookup(new AbstractLookup.Content(), inhTree); 225 { 226 227 Collection r = al.lookup(new Lookup.Template(Integer .class)).allInstances(); 228 assertEquals("None", 0, r.size()); 229 } 230 231 ICP item = new ICP(new Integer (10)); 232 al.addPair(item); 233 al.removePair(item); 234 235 NbMarshalledObject mar = new NbMarshalledObject(al); 236 237 AbstractLookup newLookup = (AbstractLookup)mar.get(); 238 239 newLookup.lookup(Number .class); 240 241 242 newLookup.addPair(new ICP(new Long (20))); 243 244 { 245 246 Collection r = newLookup.lookup(new Lookup.Template(Number .class)).allInstances(); 247 assertEquals("one", 1, r.size()); 248 252 } 253 } 254 255 private static final class ICP extends AbstractLookup.Pair { 256 private Number s; 257 258 public ICP (Number s) { 259 this.s = s; 260 } 261 262 263 protected boolean instanceOf(Class c) { 264 return c.isInstance(s); 265 } 266 267 protected boolean creatorOf(Object obj) { 268 return s == obj; 269 } 270 271 public Object getInstance() { 272 return s; 273 } 274 275 public Class getType() { 276 return s.getClass(); 277 } 278 279 public String getId() { 280 return s.toString(); 281 } 282 283 public String getDisplayName() { 284 return getId(); 285 } 286 287 } 288 } 289 | Popular Tags |