openide.util/test/unit/src/org/openide/util/lookup/AbstractLookupMemoryTest.java
author Jesse Glick <jglick@netbeans.org>
Fri, 09 Oct 2009 15:11:13 -0400
changeset 833 0e00857c5827
parent 829 3b2ed3e1f01b
permissions -rw-r--r--
Warnings.
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
     5  *
     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]"
    23  *
    24  * Contributor(s):
    25  *
    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.
    29  *
    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.
    40  */
    41 
    42 package org.openide.util.lookup;
    43 
    44 import java.util.*;
    45 import org.netbeans.junit.*;
    46 import org.netbeans.modules.openide.util.ActiveQueue;
    47 import org.openide.util.Lookup;
    48 
    49 /** Testing memory consumption of various AbstractLookup aspects.
    50  */
    51 public class AbstractLookupMemoryTest extends NbTestCase {
    52     public AbstractLookupMemoryTest(java.lang.String testName) {
    53         super(testName);
    54     }
    55 
    56     public static void main(java.lang.String[] args) {
    57         junit.textui.TestRunner.run(new NbTestSuite(AbstractLookupMemoryTest.class));
    58     }
    59 
    60     public void testEmptySize () {
    61         AbstractLookup instanceLookup = new AbstractLookup ();
    62         assertSize ("Empty lookup should be small", 16, instanceLookup);
    63         
    64         InstanceContent ic = new InstanceContent ();
    65         instanceLookup = new AbstractLookup (ic);
    66         assertSize ("Lookup with InstanceContent should be small as well", 16, instanceLookup);
    67     }
    68 
    69     public void testPairSize () {
    70         AbstractLookup.Pair pair = new EmptyPair ();
    71         assertSize ("Pair occupies only 16 bytes", 16, pair);
    72     }
    73     
    74     public void testPairWithOnePointerSize () {
    75         AbstractLookup.Pair pair = new OneItemPair ();
    76         assertSize ("Pair occupies only 16 bytes", 16, pair);
    77     }
    78     
    79     public void testLookupWithPairs () {
    80         Lookup.Template<Object> t = new Lookup.Template<Object>(Object.class);
    81         class L implements org.openide.util.LookupListener {
    82             public int cnt;
    83             public void resultChanged (org.openide.util.LookupEvent ev) {
    84                 cnt++;
    85             }
    86         }
    87         L listener = new L ();
    88         L listener2 = new L ();
    89 
    90         EmptyPair[] pairs = {
    91             new EmptyPair(),
    92             new EmptyPair(),
    93             new EmptyPair(),
    94             new EmptyPair(),
    95         };
    96         Object[] ignore = {
    97             pairs[0],
    98             pairs[1],
    99             pairs[2],
   100             pairs[3],
   101             t,
   102             ActiveQueue.queue(),
   103             listener,
   104             listener2,
   105             new Integer (11) // trashhold is shared
   106         };
   107         
   108         AbstractLookup.Content c = new AbstractLookup.Content ();
   109         AbstractLookup l = new AbstractLookup (c, (Integer)ignore[ignore.length - 1]);
   110 
   111         c.addPair ((EmptyPair)ignore[0]);
   112         assertSize ("Should be really small (not counting the pair sizes)", Collections.singleton (l), 56, ignore);
   113         
   114         c.addPair ((EmptyPair)ignore[1]);
   115         assertSize ("Is bigger I guess (not counting the pair sizes)", Collections.singleton (l), 56, ignore);
   116         
   117         c.setPairs(Arrays.asList(pairs).subList(0, 3));
   118         assertSize ("Even bigger (not counting the pair sizes)", Collections.singleton (l), 64, ignore);
   119         
   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);
   122         
   123         Lookup.Result res = l.lookup (t);
   124         
   125         assertSize ("After creating a result", Collections.singleton (l), 120, ignore);
   126         
   127         res.addLookupListener (listener);
   128         
   129         assertSize ("And attaching one listener", Collections.singleton (l), 120, ignore);
   130 
   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);
   135         
   136         
   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);
   139         
   140         c.removePair ((EmptyPair)ignore[0]);
   141         assertEquals ("A changes has been delivered", 1, listener.cnt);
   142     }
   143 
   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
   153     
   154     /** Pair with one item (like InstanceContent.Pair) */
   155     private static class OneItemPair extends EmptyPair {
   156         private Object pointer;
   157     }
   158 }