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 29 31 public class ExcludingLookupTest extends AbstractLookupBaseHid 32 implements AbstractLookupBaseHid.Impl { 33 public ExcludingLookupTest(java.lang.String testName) { 34 super(testName, null); 35 } 36 37 public Lookup createLookup (final Lookup lookup) { 38 return Lookups.exclude (lookup, new Class [0]); 39 } 40 41 public Lookup createInstancesLookup (InstanceContent ic) { 42 return new AbstractLookup (ic); 43 } 44 45 public void clearCaches () { 46 } 47 48 public void testWeCanRemoveInteger () throws Exception { 49 doBasicFilteringTest (Integer .class, Integer .class, 0); 50 } 51 52 public void testWeCanRemoveIntegersEvenByAskingForRemoveOfAllNumbers () throws Exception { 53 doBasicFilteringTest (Number .class, Integer .class, 0); 54 } 55 public void testFunWithInterfaces () throws Exception { 56 doBasicFilteringTest (java.io.Serializable .class, Integer .class, 0); 57 } 58 59 public void testWeCanGetInstanceOfSerializableEvenItIsExcludedIfWeAskForClassNotExtendingIt () throws Exception { 60 Lookup lookup = Lookups.exclude (this.instanceLookup, new Class [] { java.io.Serializable .class }); 61 Lookup.Template t = new Lookup.Template (Object .class); 62 Lookup.Result res = lookup.lookup (t); 63 64 LL ll = new LL (); 65 res.addLookupListener (ll); 66 assertEquals ("Nothing is there", 0, res.allItems ().size ()); 67 68 Object inst = new Integer (3); 69 ic.add (inst); 70 71 assertEquals ("Not Filtered out", inst, lookup.lookup (Object .class)); 72 assertEquals ("Not Filtered out2", inst, lookup.lookupItem (t).getInstance ()); 73 assertEquals ("One is there - 2", 1, res.allItems ().size ()); 74 assertEquals ("One is there - 2a", 1, res.allInstances ().size ()); 75 assertEquals ("One is there - 2b", 1, res.allClasses ().size ()); 76 assertEquals ("Right # of events", 1, ll.getCount ()); 77 78 ic.remove (inst); 79 assertEquals ("Filtered out3", null, lookup.lookupItem (t)); 80 assertEquals ("Nothing is there - 3", 0, res.allItems ().size ()); 81 assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ()); 82 assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ()); 83 assertEquals ("Of course it is not there", null, lookup.lookup (Object .class)); 84 assertEquals ("Right # of events", 1, ll.getCount ()); 85 } 86 87 public void testIntegersQueriedThruObject () throws Exception { 88 doBasicFilteringTest (Number .class, Object .class, 1); 89 } 90 91 private void doBasicFilteringTest (Class theFilter, Class theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception { 92 Lookup lookup = Lookups.exclude (this.instanceLookup, new Class [] { theFilter }); 93 Lookup.Template t = new Lookup.Template (theQuery); 94 Lookup.Result res = lookup.lookup (t); 95 96 LL ll = new LL (); 97 res.addLookupListener (ll); 98 assertEquals ("Nothing is there", 0, res.allItems ().size ()); 99 100 Object inst = new Integer (3); 101 ic.add (inst); 102 103 assertEquals ("Filtered out", null, lookup.lookup (theQuery)); 104 assertEquals ("Filtered out2", null, lookup.lookupItem (t)); 105 assertEquals ("Nothing is there - 2", 0, res.allItems ().size ()); 106 assertEquals ("Nothing is there - 2a", 0, res.allInstances ().size ()); 107 assertEquals ("Nothing is there - 2b", 0, res.allClasses ().size ()); 108 assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ()); 109 110 ic.remove (inst); 111 assertEquals ("Filtered out3", null, lookup.lookupItem (t)); 112 assertEquals ("Nothing is there - 3", 0, res.allItems ().size ()); 113 assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ()); 114 assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ()); 115 assertEquals ("Of course it is not there", null, lookup.lookup (theQuery)); 116 assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ()); 117 118 } 119 120 public void testSizeOfTheLookup () throws Exception { 121 Class exclude = String .class; 122 123 Lookup lookup = Lookups.exclude (this.instanceLookup, new Class [] { exclude }); 124 125 assertSize ("Should be pretty lightweight", Collections.singleton (lookup), 16, 126 new Object [] { this.instanceLookup, exclude }); 127 } 128 public void testSizeOfTheLookupForMultipleFiltersIsHigher () throws Exception { 129 Class exclude = String .class; 130 Class exclude2 = Integer .class; 131 Class [] arr = new Class [] { exclude, exclude2 }; 132 133 Lookup lookup = Lookups.exclude (this.instanceLookup, arr); 134 135 assertSize ("Is fatter", Collections.singleton (lookup), 40, 136 new Object [] { this.instanceLookup, exclude, exclude2 }); 137 assertSize ("But only due to the array", Collections.singleton (lookup), 16, 138 new Object [] { this.instanceLookup, exclude, exclude2, arr }); 139 } 140 141 public void testFilteringOfSomething () throws Exception { 142 doFilteringOfSomething (Runnable .class, java.io.Serializable .class, 1); 143 } 144 145 private void doFilteringOfSomething (Class theFilter, Class theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception { 146 Lookup lookup = Lookups.exclude (this.instanceLookup, new Class [] { theFilter }); 147 Lookup.Template t = new Lookup.Template (theQuery); 148 Lookup.Result res = lookup.lookup (t); 149 150 LL ll = new LL (); 151 res.addLookupListener (ll); 152 assertEquals ("Nothing is there", 0, res.allItems ().size ()); 153 154 Object inst = new Integer (3); 155 ic.add (inst); 156 157 assertEquals ("Accepted", inst, lookup.lookup (theQuery)); 158 assertNotNull ("Accepted too", lookup.lookupItem (t)); 159 assertEquals ("One is there - 2", 1, res.allItems ().size ()); 160 assertEquals ("One is there - 2a", 1, res.allInstances ().size ()); 161 assertEquals ("One is there - 2b", 1, res.allClasses ().size ()); 162 assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ()); 163 164 Object inst2 = new Thread (); ic.add (inst2); 166 assertEquals ("Accepted - 2", inst, lookup.lookup (theQuery)); 167 assertNotNull ("Accepted too -2", lookup.lookupItem (t)); 168 assertEquals ("One is there - 3", 1, res.allItems ().size ()); 169 assertEquals ("One is there - 3a", 1, res.allInstances ().size ()); 170 assertEquals ("One is there - 3b", 1, res.allClasses ().size ()); 171 assertEquals ("Right # of events", 0, ll.getCount ()); 172 173 174 ic.remove (inst); 175 assertEquals ("Filtered out3", null, lookup.lookupItem (t)); 176 assertEquals ("Nothing is there - 3", 0, res.allItems ().size ()); 177 assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ()); 178 assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ()); 179 assertEquals ("Of course it is not there", null, lookup.lookup (theQuery)); 180 assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ()); 181 } 182 183 public void testTheBehaviourAsRequestedByDavidAndDescribedByJesse () throws Exception { 184 class C implements Runnable , java.io.Serializable { 185 public void run () {} 186 } 187 Object c = new C(); 188 Lookup l1 = Lookups.singleton(c); 189 Lookup l2 = Lookups.exclude(l1, new Class [] {Runnable .class}); 190 assertNull(l2.lookup(Runnable .class)); 191 assertEquals(c, l2.lookup(java.io.Serializable .class)); 192 } 193 194 public void testTheBehaviourAsRequestedByDavidAndDescribedByJesseWithUsageOfResult () throws Exception { 195 class C implements Runnable , java.io.Serializable { 196 public void run () {} 197 } 198 Object c = new C(); 199 Lookup l1 = Lookups.singleton(c); 200 Lookup l2 = Lookups.exclude(l1, new Class [] {Runnable .class}); 201 202 Lookup.Result run = l2.lookup (new Lookup.Template (Runnable .class)); 203 Lookup.Result ser = l2.lookup (new Lookup.Template (java.io.Serializable .class)); 204 205 assertEquals ("Runnables filtered out", 0, run.allItems ().size ()); 206 assertEquals ("One serialiazble", 1, ser.allItems ().size ()); 207 assertEquals ("And it is c", c, ser.allInstances ().iterator ().next ()); 208 } 209 } 210 | Popular Tags |