Warnings.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
30 * If you wish your version of this file to be governed by only the CDDL
31 * or only the GPL Version 2, indicate your decision by adding
32 * "[Contributor] elects to include this software in this distribution
33 * under the [CDDL or GPL Version 2] license." If you do not indicate a
34 * single choice of license, a recipient has the option to distribute
35 * your version of this file under either the CDDL, the GPL Version 2 or
36 * to extend the choice of license to its licensees as provided above.
37 * However, if you add GPL Version 2 code and therefore, elected the GPL
38 * Version 2 license, then the option applies only if the new code is
39 * made subject to such option by the copyright holder.
42 package org.openide.util.lookup;
45 import org.netbeans.junit.*;
46 import org.netbeans.modules.openide.util.ActiveQueue;
47 import org.openide.util.Lookup;
49 /** Testing memory consumption of various AbstractLookup aspects.
51 public class AbstractLookupMemoryTest extends NbTestCase {
52 public AbstractLookupMemoryTest(java.lang.String testName) {
56 public static void main(java.lang.String[] args) {
57 junit.textui.TestRunner.run(new NbTestSuite(AbstractLookupMemoryTest.class));
60 public void testEmptySize () {
61 AbstractLookup instanceLookup = new AbstractLookup ();
62 assertSize ("Empty lookup should be small", 16, instanceLookup);
64 InstanceContent ic = new InstanceContent ();
65 instanceLookup = new AbstractLookup (ic);
66 assertSize ("Lookup with InstanceContent should be small as well", 16, instanceLookup);
69 public void testPairSize () {
70 AbstractLookup.Pair pair = new EmptyPair ();
71 assertSize ("Pair occupies only 16 bytes", 16, pair);
74 public void testPairWithOnePointerSize () {
75 AbstractLookup.Pair pair = new OneItemPair ();
76 assertSize ("Pair occupies only 16 bytes", 16, pair);
79 public void testLookupWithPairs () {
80 Lookup.Template<Object> t = new Lookup.Template<Object>(Object.class);
81 class L implements org.openide.util.LookupListener {
83 public void resultChanged (org.openide.util.LookupEvent ev) {
87 L listener = new L ();
88 L listener2 = new L ();
105 new Integer (11) // trashhold is shared
108 AbstractLookup.Content c = new AbstractLookup.Content ();
109 AbstractLookup l = new AbstractLookup (c, (Integer)ignore[ignore.length - 1]);
111 c.addPair ((EmptyPair)ignore[0]);
112 assertSize ("Should be really small (not counting the pair sizes)", Collections.singleton (l), 56, ignore);
114 c.addPair ((EmptyPair)ignore[1]);
115 assertSize ("Is bigger I guess (not counting the pair sizes)", Collections.singleton (l), 56, ignore);
117 c.setPairs(Arrays.asList(pairs).subList(0, 3));
118 assertSize ("Even bigger (not counting the pair sizes)", Collections.singleton (l), 64, ignore);
120 c.setPairs(Arrays.asList(pairs).subList(0, 4));
121 assertSize ("Now not that much(not counting the pair sizes)", Collections.singleton (l), 64, ignore);
123 Lookup.Result res = l.lookup (t);
125 assertSize ("After creating a result", Collections.singleton (l), 120, ignore);
127 res.addLookupListener (listener);
129 assertSize ("And attaching one listener", Collections.singleton (l), 120, ignore);
131 res.addLookupListener (listener2);
132 assertSize ("Second listener makes the situation much worse", Collections.singleton (l), 200, ignore);
133 res.removeLookupListener(listener2);
134 assertSize ("But removing it returns us back to original size", Collections.singleton (l), 120, ignore);
137 assertEquals ("Current for pairs are in", res.allItems ().size (), 4); // also activates the listener
138 assertSize ("and making the listener to work", Collections.singleton (l), 120, ignore);
140 c.removePair ((EmptyPair)ignore[0]);
141 assertEquals ("A changes has been delivered", 1, listener.cnt);
144 /** Simple pair with no data */
145 private static class EmptyPair extends AbstractLookup.Pair {
146 protected boolean creatorOf(Object obj) { return false; }
147 public String getDisplayName() { return ""; }
148 public String getId() { return ""; }
149 public Object getInstance() { return null; }
150 public Class getType() { return Object.class; }
151 protected boolean instanceOf(Class c) { return c == getType (); }
152 } // end of EmptyPair
154 /** Pair with one item (like InstanceContent.Pair) */
155 private static class OneItemPair extends EmptyPair {
156 private Object pointer;