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