openide.util/test/unit/src/org/openide/util/lookup/LookupsProxyTest.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.io.Serializable;
    45 
    46 import java.util.*;
    47 import org.netbeans.junit.*;
    48 import org.openide.util.Lookup;
    49 import org.openide.util.LookupEvent;
    50 import org.openide.util.LookupListener;
    51 
    52 /** Runs all NbLookupTest tests on ProxyLookup and adds few additional.
    53  */
    54 @SuppressWarnings("unchecked") // XXX ought to be corrected, just a lot of them
    55 public class LookupsProxyTest extends AbstractLookupBaseHid
    56 implements AbstractLookupBaseHid.Impl {
    57     public LookupsProxyTest(java.lang.String testName) {
    58         super(testName, null);
    59     }
    60 
    61     public static void main(java.lang.String[] args) {
    62         junit.textui.TestRunner.run(new NbTestSuite (LookupsProxyTest.class));
    63     }
    64     
    65     /** Creates an lookup for given lookup. This class just returns 
    66      * the object passed in, but subclasses can be different.
    67      * @param lookup in lookup
    68      * @return a lookup to use
    69      */
    70     public Lookup createLookup (final Lookup lookup) {
    71         return org.openide.util.lookup.Lookups.proxy (
    72             new Lookup.Provider () {
    73                 public Lookup getLookup () {
    74                     return lookup;
    75                 }
    76             }
    77         );
    78     }
    79     
    80     public Lookup createInstancesLookup (InstanceContent ic) {
    81         return new AbstractLookup (ic);
    82     }
    83 
    84     public void clearCaches () {
    85     }    
    86     
    87     
    88    
    89     /** Check whether setLookups method does not fire when there is no
    90      * change in the lookups.
    91      */
    92     public void testProxyListener () {
    93         Changer ch = new Changer (Lookup.EMPTY);
    94         
    95         Lookup lookup = Lookups.proxy(ch);
    96         Lookup.Result res = lookup.lookup (new Lookup.Template (Object.class));
    97         
    98         LL ll = new LL ();
    99         res.addLookupListener (ll);
   100         Collection allRes = res.allInstances ();
   101 
   102         ch.setLookup (new AbstractLookup (new InstanceContent ())); // another empty lookup
   103         lookup.lookup (Object.class); // does the refresh
   104         
   105         assertEquals("Replacing an empty by empty does not generate an event", 0, ll.getCount());
   106         
   107         InstanceContent content = new InstanceContent ();
   108         AbstractLookup del = new AbstractLookup (content);
   109         content.add (this);
   110         ch.setLookup (del);
   111         lookup.lookup (Object.class);
   112         
   113         if (ll.getCount () != 1) {
   114             fail ("Changing lookups with different content generates an event");
   115         }
   116         
   117         ch.setLookup (del);
   118         lookup.lookup (Object.class);
   119         
   120         if (ll.getCount () != 0) {
   121            fail ("Not changing the lookups does not generate any event");
   122         }
   123     }
   124 
   125     
   126     public void testListeningAndQueryingByTwoListenersInstancesSetLookups() {
   127         doListeningAndQueryingByTwoListenersSetLookups(0, 1, false);
   128     }
   129     public void testListeningAndQueryingByTwoListenersClassesSetLookups() {
   130         doListeningAndQueryingByTwoListenersSetLookups(1, 1, false);        
   131     }
   132     public void testListeningAndQueryingByTwoListenersItemsSetLookups() {
   133         doListeningAndQueryingByTwoListenersSetLookups(2, 1, false);
   134     }
   135     
   136     public void testListeningAndQueryingByTwoListenersInstancesSetLookups2() {
   137         doListeningAndQueryingByTwoListenersSetLookups(0, 2, false);
   138     }
   139     public void testListeningAndQueryingByTwoListenersClassesSetLookups2() {
   140         doListeningAndQueryingByTwoListenersSetLookups(1, 2, false);        
   141     }
   142     public void testListeningAndQueryingByTwoListenersItemsSetLookups2() {
   143         doListeningAndQueryingByTwoListenersSetLookups(2, 2, false);
   144     }
   145 
   146     public void testListeningAndQueryingByTwoListenersInstancesSetLookupsWithProxy() {
   147         doListeningAndQueryingByTwoListenersSetLookups(0, 1, true);
   148     }
   149     public void testListeningAndQueryingByTwoListenersClassesSetLookupsWithProxy() {
   150         doListeningAndQueryingByTwoListenersSetLookups(1, 1, true);        
   151     }
   152     public void testListeningAndQueryingByTwoListenersItemsSetLookupsWithProxy() {
   153         doListeningAndQueryingByTwoListenersSetLookups(2, 1, true);
   154     }
   155     
   156     public void testListeningAndQueryingByTwoListenersInstancesSetLookups2WithProxy() {
   157         doListeningAndQueryingByTwoListenersSetLookups(0, 2, true);
   158     }
   159     public void testListeningAndQueryingByTwoListenersClassesSetLookups2WithProxy() {
   160         doListeningAndQueryingByTwoListenersSetLookups(1, 2, true);        
   161     }
   162     public void testListeningAndQueryingByTwoListenersItemsSetLookups2WithProxy() {
   163         doListeningAndQueryingByTwoListenersSetLookups(2, 2, true);
   164     }
   165     
   166     /* XXX: these are pretty slow, seems there is a performance problem 2^22
   167     public void testListeningAndQueryingByTwoListenersInstancesSetLookups22() {
   168         doListeningAndQueryingByTwoListenersSetLookups(0, 22);
   169     }
   170     public void testListeningAndQueryingByTwoListenersClassesSetLookups22() {
   171         doListeningAndQueryingByTwoListenersSetLookups(1, 22);        
   172     }
   173     public void testListeningAndQueryingByTwoListenersItemsSetLookups22() {
   174         doListeningAndQueryingByTwoListenersSetLookups(2, 22);
   175     }
   176      */
   177     
   178     private void doListeningAndQueryingByTwoListenersSetLookups(final int type, int depth, boolean cacheOnTop) {
   179         Changer orig = new Changer(Lookup.EMPTY);
   180         Lookup on = Lookups.proxy(orig);
   181         Lookup first = on;
   182         
   183         while (--depth > 0) {
   184             Changer next = new Changer(on);
   185             on = Lookups.proxy(next);
   186         }
   187         
   188         
   189         final Lookup lookup = cacheOnTop ? new ProxyLookup(new Lookup[] { on }) : on;
   190         
   191         class L implements LookupListener {
   192             Lookup.Result integer = lookup.lookup(new Lookup.Template(Integer.class));
   193             Lookup.Result number = lookup.lookup(new Lookup.Template(Number.class));
   194             Lookup.Result serial = lookup.lookup(new Lookup.Template(Serializable.class));
   195             
   196             {
   197                 integer.addLookupListener(this);
   198                 number.addLookupListener(this);
   199                 serial.addLookupListener(this);
   200             }
   201             
   202             int round;
   203             
   204             public void resultChanged(LookupEvent ev) {
   205                 Collection c1 = get(type, integer);
   206                 Collection c2 = get(type, number);
   207                 Collection c3 = get(type, serial);
   208                 
   209                 assertEquals("round " + round + " c1 vs. c2", c1, c2);
   210                 assertEquals("round " + round + " c1 vs. c3", c1, c3);
   211                 assertEquals("round " + round + " c2 vs. c3", c2, c3);
   212                 
   213                 round++;
   214             }            
   215 
   216             private Collection get(int type, Lookup.Result res) {
   217                 Collection c;
   218                 switch(type) {
   219                     case 0: c = res.allInstances(); break;
   220                     case 1: c = res.allClasses(); break;
   221                     case 2: c = res.allItems(); break;
   222                     default: c = null; fail("Type: " + type); break;
   223                 }
   224                 
   225                 assertNotNull(c);
   226                 return new ArrayList(c);
   227             }
   228         }
   229         
   230         L listener = new L();
   231         listener.resultChanged(null);
   232         ArrayList arr = new ArrayList();
   233         for(int i = 0; i < 100; i++) {
   234             arr.add(new Integer(i));
   235             
   236             orig.lookup = Lookups.fixed(arr.toArray());
   237             // do the refresh
   238             first.lookup((Class)null);
   239         }
   240         
   241         assertEquals("3x100+1 checks", 301, listener.round);
   242     }
   243     
   244     
   245     public void testRefreshWithoutAllInstances103300 () {
   246         Changer ch = new Changer (Lookup.EMPTY);
   247         
   248         Lookup lookup = Lookups.proxy(ch);
   249 
   250         ch.setLookup (new AbstractLookup (new InstanceContent ())); // another empty lookup
   251         assertNull("Nothing there", lookup.lookup (Object.class)); // does the refresh
   252         
   253         InstanceContent content = new InstanceContent ();
   254         AbstractLookup del = new AbstractLookup (content);
   255         content.add (this);
   256         ch.setLookup (del);
   257         assertEquals("Can see me", this, lookup.lookup (Object.class));
   258         
   259         ch.setLookup (del);
   260         assertEquals("Still can see me", this, lookup.lookup (Object.class));
   261 
   262         assertEquals("I am visible", this, lookup.lookup(LookupsProxyTest.class));
   263     }
   264 
   265 
   266     private static final class Changer implements Lookup.Provider {
   267         private Lookup lookup;
   268         
   269         public Changer (Lookup lookup) {
   270             setLookup (lookup);
   271         }
   272         
   273         public void setLookup (Lookup lookup) {
   274             this.lookup = lookup;
   275         }
   276         
   277         public Lookup getLookup() {
   278             return lookup;
   279         }
   280     }
   281 
   282 }