1 19 20 package org.openide.util.lookup; 21 22 import java.io.Serializable ; 23 import org.openide.util.*; 24 25 import java.util.*; 26 import junit.framework.*; 27 import org.netbeans.junit.*; 28 29 31 public class LookupsProxyTest extends AbstractLookupBaseHid 32 implements AbstractLookupBaseHid.Impl { 33 public LookupsProxyTest(java.lang.String testName) { 34 super(testName, null); 35 } 36 37 public static void main(java.lang.String [] args) { 38 junit.textui.TestRunner.run(new NbTestSuite (LookupsProxyTest.class)); 39 } 40 41 46 public Lookup createLookup (final Lookup lookup) { 47 return org.openide.util.lookup.Lookups.proxy ( 48 new Lookup.Provider () { 49 public Lookup getLookup () { 50 return lookup; 51 } 52 } 53 ); 54 } 55 56 public Lookup createInstancesLookup (InstanceContent ic) { 57 return new AbstractLookup (ic); 58 } 59 60 public void clearCaches () { 61 } 62 63 64 65 68 public void testProxyListener () { 69 Changer ch = new Changer (Lookup.EMPTY); 70 71 Lookup lookup = Lookups.proxy(ch); 72 Lookup.Result res = lookup.lookup (new Lookup.Template (Object .class)); 73 74 LL ll = new LL (); 75 res.addLookupListener (ll); 76 Collection allRes = res.allInstances (); 77 78 ch.setLookup (new AbstractLookup (new InstanceContent ())); lookup.lookup (Object .class); 81 assertEquals("Replacing an empty by empty does not generate an event", 0, ll.getCount()); 82 83 InstanceContent content = new InstanceContent (); 84 AbstractLookup del = new AbstractLookup (content); 85 content.add (this); 86 ch.setLookup (del); 87 lookup.lookup (Object .class); 88 89 if (ll.getCount () != 1) { 90 fail ("Changing lookups with different content generates an event"); 91 } 92 93 ch.setLookup (del); 94 lookup.lookup (Object .class); 95 96 if (ll.getCount () != 0) { 97 fail ("Not changing the lookups does not generate any event"); 98 } 99 } 100 101 102 public void testListeningAndQueryingByTwoListenersInstancesSetLookups() { 103 doListeningAndQueryingByTwoListenersSetLookups(0, 1, false); 104 } 105 public void testListeningAndQueryingByTwoListenersClassesSetLookups() { 106 doListeningAndQueryingByTwoListenersSetLookups(1, 1, false); 107 } 108 public void testListeningAndQueryingByTwoListenersItemsSetLookups() { 109 doListeningAndQueryingByTwoListenersSetLookups(2, 1, false); 110 } 111 112 public void testListeningAndQueryingByTwoListenersInstancesSetLookups2() { 113 doListeningAndQueryingByTwoListenersSetLookups(0, 2, false); 114 } 115 public void testListeningAndQueryingByTwoListenersClassesSetLookups2() { 116 doListeningAndQueryingByTwoListenersSetLookups(1, 2, false); 117 } 118 public void testListeningAndQueryingByTwoListenersItemsSetLookups2() { 119 doListeningAndQueryingByTwoListenersSetLookups(2, 2, false); 120 } 121 122 public void testListeningAndQueryingByTwoListenersInstancesSetLookupsWithProxy() { 123 doListeningAndQueryingByTwoListenersSetLookups(0, 1, true); 124 } 125 public void testListeningAndQueryingByTwoListenersClassesSetLookupsWithProxy() { 126 doListeningAndQueryingByTwoListenersSetLookups(1, 1, true); 127 } 128 public void testListeningAndQueryingByTwoListenersItemsSetLookupsWithProxy() { 129 doListeningAndQueryingByTwoListenersSetLookups(2, 1, true); 130 } 131 132 public void testListeningAndQueryingByTwoListenersInstancesSetLookups2WithProxy() { 133 doListeningAndQueryingByTwoListenersSetLookups(0, 2, true); 134 } 135 public void testListeningAndQueryingByTwoListenersClassesSetLookups2WithProxy() { 136 doListeningAndQueryingByTwoListenersSetLookups(1, 2, true); 137 } 138 public void testListeningAndQueryingByTwoListenersItemsSetLookups2WithProxy() { 139 doListeningAndQueryingByTwoListenersSetLookups(2, 2, true); 140 } 141 142 153 154 private void doListeningAndQueryingByTwoListenersSetLookups(final int type, int depth, boolean cacheOnTop) { 155 Changer orig = new Changer(Lookup.EMPTY); 156 Lookup on = Lookups.proxy(orig); 157 Lookup first = on; 158 159 while (--depth > 0) { 160 Changer next = new Changer(on); 161 on = Lookups.proxy(next); 162 } 163 164 165 final Lookup lookup = cacheOnTop ? new ProxyLookup(new Lookup[] { on }) : on; 166 167 class L implements LookupListener { 168 Lookup.Result integer = lookup.lookup(new Lookup.Template(Integer .class)); 169 Lookup.Result number = lookup.lookup(new Lookup.Template(Number .class)); 170 Lookup.Result serial = lookup.lookup(new Lookup.Template(Serializable .class)); 171 172 { 173 integer.addLookupListener(this); 174 number.addLookupListener(this); 175 serial.addLookupListener(this); 176 } 177 178 int round; 179 180 public void resultChanged(LookupEvent ev) { 181 Collection c1 = get(type, integer); 182 Collection c2 = get(type, number); 183 Collection c3 = get(type, serial); 184 185 assertEquals("round " + round + " c1 vs. c2", c1, c2); 186 assertEquals("round " + round + " c1 vs. c3", c1, c3); 187 assertEquals("round " + round + " c2 vs. c3", c2, c3); 188 189 round++; 190 } 191 192 private Collection get(int type, Lookup.Result res) { 193 Collection c; 194 switch(type) { 195 case 0: c = res.allInstances(); break; 196 case 1: c = res.allClasses(); break; 197 case 2: c = res.allItems(); break; 198 default: c = null; fail("Type: " + type); break; 199 } 200 201 assertNotNull(c); 202 return new ArrayList(c); 203 } 204 } 205 206 L listener = new L(); 207 listener.resultChanged(null); 208 ArrayList arr = new ArrayList(); 209 for(int i = 0; i < 100; i++) { 210 arr.add(new Integer (i)); 211 212 orig.lookup = Lookups.fixed(arr.toArray()); 213 first.lookup((Class )null); 215 } 216 217 assertEquals("3x100+1 checks", 301, listener.round); 218 } 219 220 private static final class Changer implements Lookup.Provider { 221 private Lookup lookup; 222 223 public Changer (Lookup lookup) { 224 setLookup (lookup); 225 } 226 227 public void setLookup (Lookup lookup) { 228 this.lookup = lookup; 229 } 230 231 public Lookup getLookup() { 232 return lookup; 233 } 234 } 235 } 236 | Popular Tags |