KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > lookup > AbstractLookupMemoryTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /** Testing memory consumption of various AbstractLookup aspects.
29  */

30 public class AbstractLookupMemoryTest extends NbTestCase {
31     public AbstractLookupMemoryTest(java.lang.String JavaDoc testName) {
32         super(testName);
33     }
34
35     public static void main(java.lang.String JavaDoc[] 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 JavaDoc.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 JavaDoc[] 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 JavaDoc (11) // trashhold is shared
79
};
80         
81         AbstractLookup.Content c = new AbstractLookup.Content ();
82         AbstractLookup l = new AbstractLookup (c, (Integer JavaDoc)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); // also activates the listener
111
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     /** Simple pair with no data */
118     private static class EmptyPair extends AbstractLookup.Pair {
119         protected boolean creatorOf(Object JavaDoc obj) { return false; }
120         public String JavaDoc getDisplayName() { return ""; }
121         public String JavaDoc getId() { return ""; }
122         public Object JavaDoc getInstance() { return null; }
123         public Class JavaDoc getType() { return Object JavaDoc.class; }
124         protected boolean instanceOf(Class JavaDoc c) { return c == getType (); }
125     } // end of EmptyPair
126

127     /** Pair with one item (like InstanceContent.Pair) */
128     private static class OneItemPair extends EmptyPair {
129         private Object JavaDoc pointer;
130     }
131 }
132
Popular Tags