1.1 --- a/lookup/src/test/java/org/openide/util/lookup/AbstractLookupMemoryTest.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,158 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * Contributor(s):
1.28 - *
1.29 - * The Original Software is NetBeans. The Initial Developer of the Original
1.30 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
1.31 - * Microsystems, Inc. All Rights Reserved.
1.32 - *
1.33 - * If you wish your version of this file to be governed by only the CDDL
1.34 - * or only the GPL Version 2, indicate your decision by adding
1.35 - * "[Contributor] elects to include this software in this distribution
1.36 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.37 - * single choice of license, a recipient has the option to distribute
1.38 - * your version of this file under either the CDDL, the GPL Version 2 or
1.39 - * to extend the choice of license to its licensees as provided above.
1.40 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.41 - * Version 2 license, then the option applies only if the new code is
1.42 - * made subject to such option by the copyright holder.
1.43 - */
1.44 -
1.45 -package org.openide.util.lookup;
1.46 -
1.47 -import java.util.*;
1.48 -import org.netbeans.junit.*;
1.49 -import org.netbeans.modules.openide.util.ActiveQueue;
1.50 -import org.openide.util.Lookup;
1.51 -
1.52 -/** Testing memory consumption of various AbstractLookup aspects.
1.53 - */
1.54 -public class AbstractLookupMemoryTest extends NbTestCase {
1.55 - public AbstractLookupMemoryTest(java.lang.String testName) {
1.56 - super(testName);
1.57 - }
1.58 -
1.59 - public static void main(java.lang.String[] args) {
1.60 - junit.textui.TestRunner.run(new NbTestSuite(AbstractLookupMemoryTest.class));
1.61 - }
1.62 -
1.63 - public void testEmptySize () {
1.64 - AbstractLookup instanceLookup = new AbstractLookup ();
1.65 - assertSize ("Empty lookup should be small", 16, instanceLookup);
1.66 -
1.67 - InstanceContent ic = new InstanceContent ();
1.68 - instanceLookup = new AbstractLookup (ic);
1.69 - assertSize ("Lookup with InstanceContent should be small as well", 16, instanceLookup);
1.70 - }
1.71 -
1.72 - public void testPairSize () {
1.73 - AbstractLookup.Pair pair = new EmptyPair ();
1.74 - assertSize ("Pair occupies only 16 bytes", 16, pair);
1.75 - }
1.76 -
1.77 - public void testPairWithOnePointerSize () {
1.78 - AbstractLookup.Pair pair = new OneItemPair ();
1.79 - assertSize ("Pair occupies only 16 bytes", 16, pair);
1.80 - }
1.81 -
1.82 - public void testLookupWithPairs () {
1.83 - Lookup.Template<Object> t = new Lookup.Template<Object>(Object.class);
1.84 - class L implements org.openide.util.LookupListener {
1.85 - public int cnt;
1.86 - public void resultChanged (org.openide.util.LookupEvent ev) {
1.87 - cnt++;
1.88 - }
1.89 - }
1.90 - L listener = new L ();
1.91 - L listener2 = new L ();
1.92 -
1.93 - EmptyPair[] pairs = {
1.94 - new EmptyPair(),
1.95 - new EmptyPair(),
1.96 - new EmptyPair(),
1.97 - new EmptyPair(),
1.98 - };
1.99 - Object[] ignore = {
1.100 - pairs[0],
1.101 - pairs[1],
1.102 - pairs[2],
1.103 - pairs[3],
1.104 - t,
1.105 - ActiveQueue.queue(),
1.106 - listener,
1.107 - listener2,
1.108 - new Integer (11) // trashhold is shared
1.109 - };
1.110 -
1.111 - AbstractLookup.Content c = new AbstractLookup.Content ();
1.112 - AbstractLookup l = new AbstractLookup (c, (Integer)ignore[ignore.length - 1]);
1.113 -
1.114 - c.addPair ((EmptyPair)ignore[0]);
1.115 - assertSize ("Should be really small (not counting the pair sizes)", Collections.singleton (l), 56, ignore);
1.116 -
1.117 - c.addPair ((EmptyPair)ignore[1]);
1.118 - assertSize ("Is bigger I guess (not counting the pair sizes)", Collections.singleton (l), 56, ignore);
1.119 -
1.120 - c.setPairs(Arrays.asList(pairs).subList(0, 3));
1.121 - assertSize ("Even bigger (not counting the pair sizes)", Collections.singleton (l), 64, ignore);
1.122 -
1.123 - c.setPairs(Arrays.asList(pairs).subList(0, 4));
1.124 - assertSize ("Now not that much(not counting the pair sizes)", Collections.singleton (l), 64, ignore);
1.125 -
1.126 - Lookup.Result res = l.lookup (t);
1.127 -
1.128 - assertSize ("After creating a result", Collections.singleton (l), 120, ignore);
1.129 -
1.130 - res.addLookupListener (listener);
1.131 -
1.132 - assertSize ("And attaching one listener", Collections.singleton (l), 120, ignore);
1.133 -
1.134 - res.addLookupListener (listener2);
1.135 - assertSize ("Second listener makes the situation much worse", Collections.singleton (l), 200, ignore);
1.136 - res.removeLookupListener(listener2);
1.137 - assertSize ("But removing it returns us back to original size", Collections.singleton (l), 120, ignore);
1.138 -
1.139 -
1.140 - assertEquals ("Current for pairs are in", res.allItems ().size (), 4); // also activates the listener
1.141 - assertSize ("and making the listener to work", Collections.singleton (l), 120, ignore);
1.142 -
1.143 - c.removePair ((EmptyPair)ignore[0]);
1.144 - assertEquals ("A changes has been delivered", 1, listener.cnt);
1.145 - }
1.146 -
1.147 - /** Simple pair with no data */
1.148 - private static class EmptyPair extends AbstractLookup.Pair {
1.149 - protected boolean creatorOf(Object obj) { return false; }
1.150 - public String getDisplayName() { return ""; }
1.151 - public String getId() { return ""; }
1.152 - public Object getInstance() { return null; }
1.153 - public Class getType() { return Object.class; }
1.154 - protected boolean instanceOf(Class c) { return c == getType (); }
1.155 - } // end of EmptyPair
1.156 -
1.157 - /** Pair with one item (like InstanceContent.Pair) */
1.158 - private static class OneItemPair extends EmptyPair {
1.159 - private Object pointer;
1.160 - }
1.161 -}