openide.util/test/unit/src/org/openide/util/lookup/ExcludingLookupTest.java
author jtulach@netbeans.org
Wed Sep 26 20:53:05 2007 +0000 (2 years ago)
changeset 56821 4883eaeda744
parent 3250597904746058b
child 127822f7563d9f91ce
permissions -rw-r--r--
I love free software very very very very very very very very very very much
jtulach@21440
     1
/*
jtulach@56821
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@32505
     3
 *
jtulach@56821
     4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
jtulach@32505
     5
 *
jtulach@56821
     6
 * The contents of this file are subject to the terms of either the GNU
jtulach@56821
     7
 * General Public License Version 2 only ("GPL") or the Common
jtulach@56821
     8
 * Development and Distribution License("CDDL") (collectively, the
jtulach@56821
     9
 * "License"). You may not use this file except in compliance with the
jtulach@56821
    10
 * License. You can obtain a copy of the License at
jtulach@56821
    11
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@56821
    12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@56821
    13
 * specific language governing permissions and limitations under the
jtulach@56821
    14
 * License.  When distributing the software, include this License Header
jtulach@56821
    15
 * Notice in each file and include the License file at
jtulach@56821
    16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
jtulach@56821
    17
 * particular file as subject to the "Classpath" exception as provided
jtulach@56821
    18
 * by Sun in the GPL Version 2 section of the License file that
jtulach@56821
    19
 * accompanied this code. If applicable, add the following below the
jtulach@56821
    20
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@56821
    21
 * your own identifying information:
jtulach@32505
    22
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@32505
    23
 *
jtulach@56821
    24
 * Contributor(s):
jtulach@56821
    25
 *
jtulach@32505
    26
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@32505
    27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
jtulach@21440
    28
 * Microsystems, Inc. All Rights Reserved.
jtulach@56821
    29
 *
jtulach@56821
    30
 * If you wish your version of this file to be governed by only the CDDL
jtulach@56821
    31
 * or only the GPL Version 2, indicate your decision by adding
jtulach@56821
    32
 * "[Contributor] elects to include this software in this distribution
jtulach@56821
    33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@56821
    34
 * single choice of license, a recipient has the option to distribute
jtulach@56821
    35
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@56821
    36
 * to extend the choice of license to its licensees as provided above.
jtulach@56821
    37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@56821
    38
 * Version 2 license, then the option applies only if the new code is
jtulach@56821
    39
 * made subject to such option by the copyright holder.
jtulach@21440
    40
 */
jtulach@21440
    41
jtulach@21440
    42
package org.openide.util.lookup;
jtulach@21440
    43
jtulach@21440
    44
import org.openide.util.*;
jtulach@21440
    45
jtulach@21440
    46
import java.lang.ref.WeakReference;
jtulach@21440
    47
import java.util.*;
jtulach@21440
    48
import junit.framework.*;
jtulach@21440
    49
import org.netbeans.junit.*;
jtulach@21440
    50
jtulach@21440
    51
/** Runs all NbLookupTest tests on ProxyLookup and adds few additional.
jtulach@21440
    52
 */
jtulach@21440
    53
public class ExcludingLookupTest extends AbstractLookupBaseHid
jtulach@21440
    54
implements AbstractLookupBaseHid.Impl {
jtulach@21440
    55
    public ExcludingLookupTest(java.lang.String testName) {
jtulach@21440
    56
        super(testName, null);
jtulach@21440
    57
    }
jtulach@32505
    58
jtulach@21440
    59
    public Lookup createLookup (final Lookup lookup) {
jtulach@21440
    60
        return Lookups.exclude (lookup, new Class[0]);
jtulach@21440
    61
    }
jtulach@21440
    62
    
jtulach@21440
    63
    public Lookup createInstancesLookup (InstanceContent ic) {
jtulach@21440
    64
        return new AbstractLookup (ic);
jtulach@21440
    65
    }
jtulach@21440
    66
jtulach@21440
    67
    public void clearCaches () {
jtulach@21440
    68
    }    
jtulach@21440
    69
    
jtulach@21440
    70
    public void testWeCanRemoveInteger () throws Exception {
jtulach@21440
    71
        doBasicFilteringTest (Integer.class, Integer.class, 0);
jtulach@21440
    72
    }
jtulach@21440
    73
    
jtulach@21440
    74
    public void testWeCanRemoveIntegersEvenByAskingForRemoveOfAllNumbers () throws Exception {
jtulach@21440
    75
        doBasicFilteringTest (Number.class, Integer.class, 0);
jtulach@21440
    76
    }
jtulach@21440
    77
    public void testFunWithInterfaces () throws Exception {
jtulach@21440
    78
        doBasicFilteringTest (java.io.Serializable.class, Integer.class, 0);
jtulach@21440
    79
    }
jtulach@21440
    80
    
jtulach@21440
    81
    public void testWeCanGetInstanceOfSerializableEvenItIsExcludedIfWeAskForClassNotExtendingIt () throws Exception {
jtulach@21440
    82
        Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { java.io.Serializable.class });
jtulach@21440
    83
        Lookup.Template t = new Lookup.Template (Object.class);
jtulach@21440
    84
        Lookup.Result res = lookup.lookup (t);
jtulach@21440
    85
        
jtulach@21440
    86
        LL ll = new LL ();
jtulach@21440
    87
        res.addLookupListener (ll);
jtulach@21440
    88
        assertEquals ("Nothing is there", 0, res.allItems ().size ());
jtulach@21440
    89
        
jtulach@21440
    90
        Object inst = new Integer (3);
jtulach@21440
    91
        ic.add (inst);
jtulach@21440
    92
        
jtulach@21440
    93
        assertEquals ("Not Filtered out", inst, lookup.lookup (Object.class));
jtulach@21440
    94
        assertEquals ("Not Filtered out2", inst, lookup.lookupItem (t).getInstance ());
jtulach@21440
    95
        assertEquals ("One is there - 2", 1, res.allItems ().size ());
jtulach@21440
    96
        assertEquals ("One is there - 2a", 1, res.allInstances ().size ());
jtulach@21440
    97
        assertEquals ("One is there - 2b", 1, res.allClasses ().size ());
jtulach@21440
    98
        assertEquals ("Right # of events", 1, ll.getCount ());
jtulach@21440
    99
        
jtulach@21440
   100
        ic.remove (inst);
jtulach@21440
   101
        assertEquals ("Filtered out3", null, lookup.lookupItem (t));
jtulach@21440
   102
        assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
jtulach@21440
   103
        assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
jtulach@21440
   104
        assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
jtulach@21440
   105
        assertEquals ("Of course it is not there", null, lookup.lookup (Object.class));
jtulach@21440
   106
        assertEquals ("Right # of events", 1, ll.getCount ());
jtulach@21440
   107
    }
jtulach@21440
   108
    
jtulach@21440
   109
    public void testIntegersQueriedThruObject () throws Exception {
jtulach@21440
   110
        doBasicFilteringTest (Number.class, Object.class, 1);
jtulach@21440
   111
    }
jtulach@21440
   112
    
jtulach@21440
   113
    private void doBasicFilteringTest (Class theFilter, Class theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception {
jtulach@21440
   114
        Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { theFilter });
jtulach@21440
   115
        Lookup.Template t = new Lookup.Template (theQuery);
jtulach@21440
   116
        Lookup.Result res = lookup.lookup (t);
jtulach@21440
   117
        
jtulach@21440
   118
        LL ll = new LL ();
jtulach@21440
   119
        res.addLookupListener (ll);
jtulach@21440
   120
        assertEquals ("Nothing is there", 0, res.allItems ().size ());
jtulach@21440
   121
        
jtulach@21440
   122
        Object inst = new Integer (3);
jtulach@21440
   123
        ic.add (inst);
jtulach@21440
   124
        
jtulach@21440
   125
        assertEquals ("Filtered out", null, lookup.lookup (theQuery));
jtulach@21440
   126
        assertEquals ("Filtered out2", null, lookup.lookupItem (t));
jtulach@21440
   127
        assertEquals ("Nothing is there - 2", 0, res.allItems ().size ());
jtulach@21440
   128
        assertEquals ("Nothing is there - 2a", 0, res.allInstances ().size ());
jtulach@21440
   129
        assertEquals ("Nothing is there - 2b", 0, res.allClasses ().size ());
jtulach@21440
   130
        assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
jtulach@21440
   131
        
jtulach@21440
   132
        ic.remove (inst);
jtulach@21440
   133
        assertEquals ("Filtered out3", null, lookup.lookupItem (t));
jtulach@21440
   134
        assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
jtulach@21440
   135
        assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
jtulach@21440
   136
        assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
jtulach@21440
   137
        assertEquals ("Of course it is not there", null, lookup.lookup (theQuery));
jtulach@21440
   138
        assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
jtulach@21440
   139
        
jtulach@21440
   140
    }
jtulach@21440
   141
    
jtulach@21440
   142
    public void testSizeOfTheLookup () throws Exception {
jtulach@21440
   143
        Class exclude = String.class;
jtulach@21440
   144
        
jtulach@21440
   145
        Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { exclude });
jtulach@21440
   146
jtulach@21440
   147
        assertSize ("Should be pretty lightweight", Collections.singleton (lookup), 16, 
jtulach@21440
   148
                new Object[] { this.instanceLookup, exclude });
jtulach@21440
   149
    }
jtulach@21440
   150
    public void testSizeOfTheLookupForMultipleFiltersIsHigher () throws Exception {
jtulach@21440
   151
        Class exclude = String.class;
jtulach@21440
   152
        Class exclude2 = Integer.class;
jtulach@21440
   153
        Class[] arr = new Class[] { exclude, exclude2 };
jtulach@21440
   154
        
jtulach@21440
   155
        Lookup lookup = Lookups.exclude (this.instanceLookup, arr);
jtulach@21440
   156
jtulach@21440
   157
        assertSize ("Is fatter", Collections.singleton (lookup), 40, 
jtulach@21440
   158
                new Object[] { this.instanceLookup, exclude, exclude2 });
jtulach@21440
   159
        assertSize ("But only due to the array", Collections.singleton (lookup), 16, 
jtulach@21440
   160
                new Object[] { this.instanceLookup, exclude, exclude2, arr });
jtulach@21440
   161
    }
jtulach@21440
   162
    
jtulach@21440
   163
    public void testFilteringOfSomething () throws Exception {
jtulach@21440
   164
        doFilteringOfSomething (Runnable.class, java.io.Serializable.class, 1);
jtulach@21440
   165
    }
jtulach@21440
   166
    
jtulach@21440
   167
    private void doFilteringOfSomething (Class theFilter, Class theQuery, int numberOfExcpectedEventsAfterOneChange) throws Exception {
jtulach@21440
   168
        Lookup lookup = Lookups.exclude (this.instanceLookup, new Class[] { theFilter });
jtulach@21440
   169
        Lookup.Template t = new Lookup.Template (theQuery);
jtulach@21440
   170
        Lookup.Result res = lookup.lookup (t);
jtulach@21440
   171
        
jtulach@21440
   172
        LL ll = new LL ();
jtulach@21440
   173
        res.addLookupListener (ll);
jtulach@21440
   174
        assertEquals ("Nothing is there", 0, res.allItems ().size ());
jtulach@21440
   175
        
jtulach@21440
   176
        Object inst = new Integer (3);
jtulach@21440
   177
        ic.add (inst);
jtulach@21440
   178
        
jtulach@21440
   179
        assertEquals ("Accepted", inst, lookup.lookup (theQuery));
jtulach@21440
   180
        assertNotNull ("Accepted too", lookup.lookupItem (t));
jtulach@21440
   181
        assertEquals ("One is there - 2", 1, res.allItems ().size ());
jtulach@21440
   182
        assertEquals ("One is there - 2a", 1, res.allInstances ().size ());
jtulach@21440
   183
        assertEquals ("One is there - 2b", 1, res.allClasses ().size ());
jtulach@21440
   184
        assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
jtulach@21440
   185
jtulach@21440
   186
        Object inst2 = new Thread (); // implements Runnable
jtulach@21440
   187
        ic.add (inst2);
jtulach@21440
   188
        assertEquals ("Accepted - 2", inst, lookup.lookup (theQuery));
jtulach@21440
   189
        assertNotNull ("Accepted too -2", lookup.lookupItem (t));
jtulach@21440
   190
        assertEquals ("One is there - 3", 1, res.allItems ().size ());
jtulach@21440
   191
        assertEquals ("One is there - 3a", 1, res.allInstances ().size ());
jtulach@21440
   192
        assertEquals ("One is there - 3b", 1, res.allClasses ().size ());
jtulach@21440
   193
        assertEquals ("Right # of events", 0, ll.getCount ());
jtulach@21440
   194
        
jtulach@21440
   195
        
jtulach@21440
   196
        ic.remove (inst);
jtulach@21440
   197
        assertEquals ("Filtered out3", null, lookup.lookupItem (t));
jtulach@21440
   198
        assertEquals ("Nothing is there - 3", 0, res.allItems ().size ());
jtulach@21440
   199
        assertEquals ("Nothing is there - 3a", 0, res.allInstances ().size ());
jtulach@21440
   200
        assertEquals ("Nothing is there - 3b", 0, res.allClasses ().size ());
jtulach@21440
   201
        assertEquals ("Of course it is not there", null, lookup.lookup (theQuery));
jtulach@21440
   202
        assertEquals ("Right # of events", numberOfExcpectedEventsAfterOneChange, ll.getCount ());
jtulach@21440
   203
    }
jtulach@21440
   204
jtulach@21440
   205
    public void testTheBehaviourAsRequestedByDavidAndDescribedByJesse () throws Exception {
jtulach@21440
   206
        class C implements Runnable, java.io.Serializable {
jtulach@21440
   207
            public void run () {}
jtulach@21440
   208
        }
jtulach@21440
   209
        Object c = new C();
jtulach@21440
   210
        Lookup l1 = Lookups.singleton(c);
jtulach@21440
   211
        Lookup l2 = Lookups.exclude(l1, new Class[] {Runnable.class});
jtulach@21440
   212
        assertNull(l2.lookup(Runnable.class));
jtulach@21440
   213
        assertEquals(c, l2.lookup(java.io.Serializable.class));
jtulach@21440
   214
    }
jtulach@21440
   215
    
jtulach@21440
   216
    public void testTheBehaviourAsRequestedByDavidAndDescribedByJesseWithUsageOfResult () throws Exception {
jtulach@21440
   217
        class C implements Runnable, java.io.Serializable {
jtulach@21440
   218
            public void run () {}
jtulach@21440
   219
        }
jtulach@21440
   220
        Object c = new C();
jtulach@21440
   221
        Lookup l1 = Lookups.singleton(c);
jtulach@21440
   222
        Lookup l2 = Lookups.exclude(l1, new Class[] {Runnable.class});
jtulach@21440
   223
        
jtulach@21440
   224
        Lookup.Result run = l2.lookup (new Lookup.Template (Runnable.class));
jtulach@21440
   225
        Lookup.Result ser = l2.lookup (new Lookup.Template (java.io.Serializable.class));
jtulach@21440
   226
        
jtulach@21440
   227
        assertEquals ("Runnables filtered out", 0, run.allItems ().size ());
jtulach@21440
   228
        assertEquals ("One serialiazble", 1, ser.allItems ().size ());
jtulach@21440
   229
        assertEquals ("And it is c", c, ser.allInstances ().iterator ().next ());
jtulach@21440
   230
    }
jtulach@21440
   231
}