1 19 20 package org.openide.util.lookup; 21 22 import org.openide.util.*; 23 24 import java.util.*; 25 import junit.framework.*; 26 import org.netbeans.junit.*; 27 28 30 public class AbstractLookupMemoryTest extends NbTestCase { 31 public AbstractLookupMemoryTest(java.lang.String testName) { 32 super(testName); 33 } 34 35 public static void main(java.lang.String [] args) { 36 junit.textui.TestRunner.run(new NbTestSuite(AbstractLookupMemoryTest.class)); 37 } 38 39 public void testEmptySize () { 40 AbstractLookup instanceLookup = new AbstractLookup (); 41 assertSize ("Empty lookup should be small", 16, instanceLookup); 42 43 InstanceContent ic = new InstanceContent (); 44 instanceLookup = new AbstractLookup (ic); 45 assertSize ("Lookup with InstanceContent should be small as well", 16, instanceLookup); 46 } 47 48 public void testPairSize () { 49 AbstractLookup.Pair pair = new EmptyPair (); 50 assertSize ("Pair occupies only 16 bytes", 16, pair); 51 } 52 53 public void testPairWithOnePointerSize () { 54 AbstractLookup.Pair pair = new OneItemPair (); 55 assertSize ("Pair occupies only 16 bytes", 16, pair); 56 } 57 58 public void testLookupWithPairs () { 59 Lookup.Template t = new Lookup.Template (Object .class); 60 class L implements org.openide.util.LookupListener { 61 public int cnt; 62 public void resultChanged (org.openide.util.LookupEvent ev) { 63 cnt++; 64 } 65 } 66 L listener = new L (); 67 L listener2 = new L (); 68 69 Object [] ignore = { 70 new EmptyPair (), 71 new EmptyPair (), 72 new EmptyPair (), 73 new EmptyPair (), 74 t, 75 Utilities.activeReferenceQueue(), 76 listener, 77 listener2, 78 new Integer (11) }; 80 81 AbstractLookup.Content c = new AbstractLookup.Content (); 82 AbstractLookup l = new AbstractLookup (c, (Integer )ignore[ignore.length - 1]); 83 84 c.addPair ((EmptyPair)ignore[0]); 85 assertSize ("Should be really small (not counting the pair sizes)", Collections.singleton (l), 56, ignore); 86 87 c.addPair ((EmptyPair)ignore[1]); 88 assertSize ("Is bigger I guess (not counting the pair sizes)", Collections.singleton (l), 56, ignore); 89 90 c.setPairs((Collection)Arrays.asList (ignore).subList (0, 3)); 91 assertSize ("Even bigger (not counting the pair sizes)", Collections.singleton (l), 64, ignore); 92 93 c.setPairs((Collection)Arrays.asList (ignore).subList (0, 4)); 94 assertSize ("Now not that much(not counting the pair sizes)", Collections.singleton (l), 64, ignore); 95 96 Lookup.Result res = l.lookup (t); 97 98 assertSize ("After creating a result", Collections.singleton (l), 120, ignore); 99 100 res.addLookupListener (listener); 101 102 assertSize ("And attaching one listener", Collections.singleton (l), 120, ignore); 103 104 res.addLookupListener (listener2); 105 assertSize ("Second listener makes the situation much worse", Collections.singleton (l), 200, ignore); 106 res.removeLookupListener(listener2); 107 assertSize ("But removing it returns us back to original size", Collections.singleton (l), 120, ignore); 108 109 110 assertEquals ("Current for pairs are in", res.allItems ().size (), 4); assertSize ("and making the listener to work", Collections.singleton (l), 120, ignore); 112 113 c.removePair ((EmptyPair)ignore[0]); 114 assertEquals ("A changes has been delivered", 1, listener.cnt); 115 } 116 117 118 private static class EmptyPair extends AbstractLookup.Pair { 119 protected boolean creatorOf(Object obj) { return false; } 120 public String getDisplayName() { return ""; } 121 public String getId() { return ""; } 122 public Object getInstance() { return null; } 123 public Class getType() { return Object .class; } 124 protected boolean instanceOf(Class c) { return c == getType (); } 125 } 127 128 private static class OneItemPair extends EmptyPair { 129 private Object pointer; 130 } 131 } 132 | Popular Tags |