Merge with trunk, on Jun 24, 2008 simpletests_before_merge_with_main
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 24 Jun 2008 20:25:26 +0200
changeset 4156f575d394b5f
parent 410 4a4889f55f7e
parent 414 8de20b457675
child 420 f7e8169560f1
Merge with trunk, on Jun 24, 2008
     1.1 --- a/openide.util/src/org/openide/util/io/ReaderInputStream.java	Sun Jun 22 23:34:38 2008 +0200
     1.2 +++ b/openide.util/src/org/openide/util/io/ReaderInputStream.java	Tue Jun 24 20:25:26 2008 +0200
     1.3 @@ -98,6 +98,10 @@
     1.4      }
     1.5  
     1.6      public int read(byte[] b, int off, int len) throws IOException {
     1.7 +        if (len == 0) {
     1.8 +            return 0;
     1.9 +        }
    1.10 +
    1.11          int c = read();
    1.12  
    1.13          if (c == -1) {
     2.1 --- a/openide.util/src/org/openide/util/lookup/Lookups.java	Sun Jun 22 23:34:38 2008 +0200
     2.2 +++ b/openide.util/src/org/openide/util/lookup/Lookups.java	Tue Jun 24 20:25:26 2008 +0200
     2.3 @@ -1,7 +1,7 @@
     2.4  /*
     2.5   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6   *
     2.7 - * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
     2.8 + * Copyright 1997-2008 Sun Microsystems, Inc. All rights reserved.
     2.9   *
    2.10   * The contents of this file are subject to the terms of either the GNU
    2.11   * General Public License Version 2 only ("GPL") or the Common
    2.12 @@ -24,7 +24,7 @@
    2.13   * Contributor(s):
    2.14   *
    2.15   * The Original Software is NetBeans. The Initial Developer of the Original
    2.16 - * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
    2.17 + * Software is Sun Microsystems, Inc. Portions Copyright 1997-2008 Sun
    2.18   * Microsystems, Inc. All Rights Reserved.
    2.19   *
    2.20   * If you wish your version of this file to be governed by only the CDDL
    2.21 @@ -42,7 +42,6 @@
    2.22  package org.openide.util.lookup;
    2.23  
    2.24  import java.util.Arrays;
    2.25 -import java.util.Collections;
    2.26  import org.netbeans.modules.openide.util.NamedServicesProvider;
    2.27  import org.openide.util.Lookup;
    2.28  
    2.29 @@ -72,10 +71,7 @@
    2.30              throw new NullPointerException();
    2.31          }
    2.32  
    2.33 -        // performance of the resulting lookup might be further
    2.34 -        // improved by providing specialized singleton result (and lookup)
    2.35 -        // instead of using SimpleResult
    2.36 -        return new SimpleLookup(Collections.singleton(objectToLookup));
    2.37 +        return new SingletonLookup(objectToLookup);
    2.38      }
    2.39  
    2.40      /**
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/openide.util/src/org/openide/util/lookup/SingletonLookup.java	Tue Jun 24 20:25:26 2008 +0200
     3.3 @@ -0,0 +1,172 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + * 
     3.7 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
     3.8 + * 
     3.9 + * The contents of this file are subject to the terms of either the GNU
    3.10 + * General Public License Version 2 only ("GPL") or the Common
    3.11 + * Development and Distribution License("CDDL") (collectively, the
    3.12 + * "License"). You may not use this file except in compliance with the
    3.13 + * License. You can obtain a copy of the License at
    3.14 + * http://www.netbeans.org/cddl-gplv2.html
    3.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.16 + * specific language governing permissions and limitations under the
    3.17 + * License.  When distributing the software, include this License Header
    3.18 + * Notice in each file and include the License file at
    3.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    3.20 + * particular file as subject to the "Classpath" exception as provided
    3.21 + * by Sun in the GPL Version 2 section of the License file that
    3.22 + * accompanied this code. If applicable, add the following below the
    3.23 + * License Header, with the fields enclosed by brackets [] replaced by
    3.24 + * your own identifying information:
    3.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.26 + * 
    3.27 + * If you wish your version of this file to be governed by only the CDDL
    3.28 + * or only the GPL Version 2, indicate your decision by adding
    3.29 + * "[Contributor] elects to include this software in this distribution
    3.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.31 + * single choice of license, a recipient has the option to distribute
    3.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.33 + * to extend the choice of license to its licensees as provided above.
    3.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.35 + * Version 2 license, then the option applies only if the new code is
    3.36 + * made subject to such option by the copyright holder.
    3.37 + * 
    3.38 + * Contributor(s):
    3.39 + * 
    3.40 + * Portions Copyrighted 2008 Sun Microsystems, Inc.
    3.41 + */
    3.42 +
    3.43 +package org.openide.util.lookup;
    3.44 +
    3.45 +import java.util.Collection;
    3.46 +import java.util.Collections;
    3.47 +import java.util.Set;
    3.48 +import org.openide.util.Lookup;
    3.49 +import org.openide.util.LookupListener;
    3.50 +
    3.51 +/**
    3.52 + * Unmodifiable lookup that contains just one fixed object.
    3.53 + *
    3.54 + * @author Marian Petras
    3.55 + */
    3.56 +class SingletonLookup extends Lookup {
    3.57 +
    3.58 +    private final Object objectToLookup;
    3.59 +    private final String id;
    3.60 +
    3.61 +    SingletonLookup(Object objectToLookup) {
    3.62 +        this(objectToLookup, null);
    3.63 +    }
    3.64 +
    3.65 +    SingletonLookup(Object objectToLookup, String id) {
    3.66 +        if (objectToLookup == null) {
    3.67 +            throw new IllegalArgumentException("null");                 //NOI18N
    3.68 +        }
    3.69 +
    3.70 +        this.objectToLookup = objectToLookup;
    3.71 +        this.id = id;
    3.72 +    }
    3.73 +
    3.74 +    @Override
    3.75 +    public <T> T lookup(Class<T> clazz) {
    3.76 +        if (clazz == null) {
    3.77 +            throw new IllegalArgumentException("null");                 //NOI18N
    3.78 +        }
    3.79 +
    3.80 +        return (clazz.isInstance(objectToLookup))
    3.81 +               ? clazz.cast(objectToLookup)
    3.82 +               : null;
    3.83 +    }
    3.84 +
    3.85 +    @Override
    3.86 +    public <T> Result<T> lookup(Template<T> template) {
    3.87 +        if (template == null) {
    3.88 +            throw new IllegalArgumentException("null");                 //NOI18N
    3.89 +        }
    3.90 +
    3.91 +        Lookup.Item<T> item = lookupItem(template);
    3.92 +        if (item != null) {
    3.93 +            return new SingletonResult<T>(item);
    3.94 +        } else {
    3.95 +            return Lookup.EMPTY.lookup(template);
    3.96 +        }
    3.97 +    }
    3.98 +
    3.99 +    @Override
   3.100 +    public <T> Collection<? extends T> lookupAll(Class<T> clazz) {
   3.101 +        if (clazz == null) {
   3.102 +            throw new IllegalArgumentException("null");                 //NOI18N
   3.103 +        }
   3.104 +
   3.105 +        return (clazz.isInstance(objectToLookup))
   3.106 +               ? Collections.singletonList(clazz.cast(objectToLookup))
   3.107 +               : Collections.<T>emptyList();
   3.108 +    }
   3.109 +
   3.110 +    @Override
   3.111 +    public <T> Item<T> lookupItem(Template<T> template) {
   3.112 +        if (template == null) {
   3.113 +            throw new IllegalArgumentException("null");                 //NOI18N
   3.114 +        }
   3.115 +
   3.116 +        String templateId = template.getId();
   3.117 +        if ((templateId != null) && !templateId.equals(id)) {
   3.118 +            return null;
   3.119 +        }
   3.120 +
   3.121 +        Object templateInst = template.getInstance();
   3.122 +        if ((templateInst != null) && (objectToLookup != templateInst)) {
   3.123 +            return null;
   3.124 +        }
   3.125 +
   3.126 +        Class<T> clazz = template.getType();
   3.127 +        if ((clazz != null) && !clazz.isInstance(objectToLookup)) {
   3.128 +            return null;
   3.129 +        }
   3.130 +
   3.131 +        Lookup.Item<T> item;
   3.132 +        if (clazz != null) {
   3.133 +            item = Lookups.lookupItem(clazz.cast(objectToLookup), id);
   3.134 +        } else {
   3.135 +            item = Lookups.lookupItem((T) objectToLookup, id);
   3.136 +        }
   3.137 +        return item;
   3.138 +    }
   3.139 +
   3.140 +    static class SingletonResult<T> extends Lookup.Result<T> {
   3.141 +
   3.142 +        private final Lookup.Item<T> item;
   3.143 +
   3.144 +        SingletonResult(Lookup.Item<T> item) {
   3.145 +            this.item = item;
   3.146 +        }
   3.147 +
   3.148 +        @Override
   3.149 +        public void addLookupListener(LookupListener l) {
   3.150 +            // this result never changes - no need to register a listener
   3.151 +        }
   3.152 +
   3.153 +        @Override
   3.154 +        public void removeLookupListener(LookupListener l) {
   3.155 +            // this result never changes - no need to register a listener
   3.156 +        }
   3.157 +
   3.158 +        @Override
   3.159 +        public Set<Class<? extends T>> allClasses() {
   3.160 +            return Collections.<Class<? extends T>>singleton(item.getType());
   3.161 +        }
   3.162 +
   3.163 +        @Override
   3.164 +        public Collection<? extends Item<T>> allItems() {
   3.165 +            return Collections.singletonList(item);
   3.166 +        }
   3.167 +
   3.168 +        @Override
   3.169 +        public Collection<? extends T> allInstances() {
   3.170 +            return Collections.singletonList(item.getInstance());
   3.171 +        }
   3.172 +
   3.173 +    }
   3.174 +
   3.175 +}
   3.176 \ No newline at end of file
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/openide.util/test/unit/src/org/openide/util/io/ReaderInputStreamTest.java	Tue Jun 24 20:25:26 2008 +0200
     4.3 @@ -0,0 +1,55 @@
     4.4 +/*
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
     4.8 + *
     4.9 + * The contents of this file are subject to the terms of either the GNU
    4.10 + * General Public License Version 2 only ("GPL") or the Common
    4.11 + * Development and Distribution License("CDDL") (collectively, the
    4.12 + * "License"). You may not use this file except in compliance with the
    4.13 + * License. You can obtain a copy of the License at
    4.14 + * http://www.netbeans.org/cddl-gplv2.html
    4.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    4.16 + * specific language governing permissions and limitations under the
    4.17 + * License.  When distributing the software, include this License Header
    4.18 + * Notice in each file and include the License file at
    4.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    4.20 + * particular file as subject to the "Classpath" exception as provided
    4.21 + * by Sun in the GPL Version 2 section of the License file that
    4.22 + * accompanied this code. If applicable, add the following below the
    4.23 + * License Header, with the fields enclosed by brackets [] replaced by
    4.24 + * your own identifying information:
    4.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    4.26 + *
    4.27 + * If you wish your version of this file to be governed by only the CDDL
    4.28 + * or only the GPL Version 2, indicate your decision by adding
    4.29 + * "[Contributor] elects to include this software in this distribution
    4.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    4.31 + * single choice of license, a recipient has the option to distribute
    4.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    4.33 + * to extend the choice of license to its licensees as provided above.
    4.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    4.35 + * Version 2 license, then the option applies only if the new code is
    4.36 + * made subject to such option by the copyright holder.
    4.37 + *
    4.38 + * Contributor(s):
    4.39 + *
    4.40 + * Portions Copyrighted 2008 Sun Microsystems, Inc.
    4.41 + */
    4.42 +
    4.43 +package org.openide.util.io;
    4.44 +
    4.45 +import java.io.StringReader;
    4.46 +import org.netbeans.junit.NbTestCase;
    4.47 +
    4.48 +public class ReaderInputStreamTest extends NbTestCase {
    4.49 +
    4.50 +    public ReaderInputStreamTest(String name) {
    4.51 +        super(name);
    4.52 +    }
    4.53 +
    4.54 +    public void testZeroLengthRead() throws Exception { // #137881
    4.55 +        assertEquals(0, new ReaderInputStream(new StringReader(("abc"))).read(new byte[256], 0, 0));
    4.56 +    }
    4.57 +
    4.58 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/openide.util/test/unit/src/org/openide/util/lookup/SingletonLookupTest.java	Tue Jun 24 20:25:26 2008 +0200
     5.3 @@ -0,0 +1,113 @@
     5.4 +/*
     5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 + * 
     5.7 + * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
     5.8 + * 
     5.9 + * The contents of this file are subject to the terms of either the GNU
    5.10 + * General Public License Version 2 only ("GPL") or the Common
    5.11 + * Development and Distribution License("CDDL") (collectively, the
    5.12 + * "License"). You may not use this file except in compliance with the
    5.13 + * License. You can obtain a copy of the License at
    5.14 + * http://www.netbeans.org/cddl-gplv2.html
    5.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    5.16 + * specific language governing permissions and limitations under the
    5.17 + * License.  When distributing the software, include this License Header
    5.18 + * Notice in each file and include the License file at
    5.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    5.20 + * particular file as subject to the "Classpath" exception as provided
    5.21 + * by Sun in the GPL Version 2 section of the License file that
    5.22 + * accompanied this code. If applicable, add the following below the
    5.23 + * License Header, with the fields enclosed by brackets [] replaced by
    5.24 + * your own identifying information:
    5.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    5.26 + * 
    5.27 + * If you wish your version of this file to be governed by only the CDDL
    5.28 + * or only the GPL Version 2, indicate your decision by adding
    5.29 + * "[Contributor] elects to include this software in this distribution
    5.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    5.31 + * single choice of license, a recipient has the option to distribute
    5.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    5.33 + * to extend the choice of license to its licensees as provided above.
    5.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    5.35 + * Version 2 license, then the option applies only if the new code is
    5.36 + * made subject to such option by the copyright holder.
    5.37 + * 
    5.38 + * Contributor(s):
    5.39 + * 
    5.40 + * Portions Copyrighted 2008 Sun Microsystems, Inc.
    5.41 + */
    5.42 +
    5.43 +package org.openide.util.lookup;
    5.44 +
    5.45 +import java.util.Collection;
    5.46 +import org.netbeans.junit.NbTestCase;
    5.47 +import org.openide.util.Lookup;
    5.48 +
    5.49 +/**
    5.50 + * Contains tests of class {@code SingletonLookup}.
    5.51 + *
    5.52 + * @author  Marian Petras
    5.53 + */
    5.54 +public class SingletonLookupTest extends NbTestCase {
    5.55 +    
    5.56 +    public SingletonLookupTest(String testName) {
    5.57 +        super(testName);
    5.58 +    }
    5.59 +
    5.60 +    public void testBasics() {
    5.61 +        Object orig = new Object();
    5.62 +        Lookup p1 = new SingletonLookup(orig);
    5.63 +        Object obj = p1.lookup(Object.class);
    5.64 +        assertTrue(obj == orig);
    5.65 +        assertNull(p1.lookup(String.class)); 
    5.66 +        assertTrue(orig == p1.lookup(Object.class)); // 2nd time, still the same?
    5.67 +        //
    5.68 +        Lookup p2 = new SingletonLookup("test");
    5.69 +        assertNotNull(p2.lookup(Object.class));
    5.70 +        assertNotNull(p2.lookup(String.class));
    5.71 +        assertNotNull(p2.lookup(java.io.Serializable.class));
    5.72 +    }
    5.73 +
    5.74 +    public void testId() {
    5.75 +        Object orig = new Object();
    5.76 +        Collection allInstances;
    5.77 +
    5.78 +        Lookup l = new SingletonLookup(orig, "id");
    5.79 +
    5.80 +        allInstances = l.lookup(new Lookup.Template<Object>(Object.class, null, null)).allInstances();
    5.81 +        assertNotNull(allInstances);
    5.82 +        assertFalse(allInstances.isEmpty());
    5.83 +        assertEquals(1, allInstances.size());
    5.84 +        assertTrue(allInstances.iterator().next() == orig);
    5.85 +
    5.86 +        allInstances = l.lookup(new Lookup.Template<Object>(Object.class, "id", null)).allInstances();
    5.87 +        assertNotNull(allInstances);
    5.88 +        assertFalse(allInstances.isEmpty());
    5.89 +        assertEquals(1, allInstances.size());
    5.90 +        assertTrue(allInstances.iterator().next() == orig);
    5.91 +
    5.92 +        allInstances = l.lookup(new Lookup.Template<Object>(Object.class, "not", null)).allInstances();
    5.93 +        assertNotNull(allInstances);
    5.94 +        assertTrue(allInstances.isEmpty());
    5.95 +
    5.96 +        allInstances = l.lookup(new Lookup.Template<String>(String.class, null, null)).allInstances();
    5.97 +        assertNotNull(allInstances);
    5.98 +        assertTrue(allInstances.isEmpty());
    5.99 +
   5.100 +        allInstances = l.lookup(new Lookup.Template<String>(String.class, "id", null)).allInstances();
   5.101 +        assertNotNull(allInstances);
   5.102 +        assertTrue(allInstances.isEmpty());
   5.103 +
   5.104 +        allInstances = l.lookup(new Lookup.Template<String>(String.class, "not", null)).allInstances();
   5.105 +        assertNotNull(allInstances);
   5.106 +        assertTrue(allInstances.isEmpty());
   5.107 +    }
   5.108 +
   5.109 +    public void testSize() {
   5.110 +        final Object obj = new Object();
   5.111 +        assertSize("The singleton lookup instance should be small",
   5.112 +                   24,
   5.113 +                   new SingletonLookup(obj));
   5.114 +    }
   5.115 +
   5.116 +}