lookup/src/test/java/org/netbeans/modules/openide/util/ServiceProviderProcessorTest.java
1.1 --- a/lookup/src/test/java/org/netbeans/modules/openide/util/ServiceProviderProcessorTest.java Wed Jan 27 17:46:23 2010 -0500
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,180 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * If you wish your version of this file to be governed by only the CDDL
1.28 - * or only the GPL Version 2, indicate your decision by adding
1.29 - * "[Contributor] elects to include this software in this distribution
1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.31 - * single choice of license, a recipient has the option to distribute
1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
1.33 - * to extend the choice of license to its licensees as provided above.
1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.35 - * Version 2 license, then the option applies only if the new code is
1.36 - * made subject to such option by the copyright holder.
1.37 - *
1.38 - * Contributor(s):
1.39 - *
1.40 - * Portions Copyrighted 2008 Sun Microsystems, Inc.
1.41 - */
1.42 -
1.43 -package org.netbeans.modules.openide.util;
1.44 -
1.45 -import java.io.ByteArrayOutputStream;
1.46 -import java.io.File;
1.47 -import java.util.ArrayList;
1.48 -import java.util.Arrays;
1.49 -import java.util.Collections;
1.50 -import java.util.Comparator;
1.51 -import java.util.List;
1.52 -import org.netbeans.junit.NbTestCase;
1.53 -import org.openide.util.Lookup;
1.54 -import org.openide.util.lookup.Lookups;
1.55 -import org.openide.util.lookup.ServiceProvider;
1.56 -import org.openide.util.lookup.ServiceProviders;
1.57 -import org.openide.util.test.AnnotationProcessorTestUtils;
1.58 -
1.59 -public class ServiceProviderProcessorTest extends NbTestCase {
1.60 -
1.61 - public ServiceProviderProcessorTest(String n) {
1.62 - super(n);
1.63 - }
1.64 -
1.65 - private static List<Class<?>> classesOf(Iterable<?> objects) {
1.66 - List<Class<?>> cs = new ArrayList<Class<?>>();
1.67 - for (Object o : objects) {
1.68 - cs.add(o.getClass());
1.69 - }
1.70 - return cs;
1.71 - }
1.72 -
1.73 - private static List<Class<?>> classesOfLookup(Class<?> xface) {
1.74 - return classesOf(Lookup.getDefault().lookupAll(xface));
1.75 - }
1.76 -
1.77 - private static List<Class<?>> sortClassList(List<Class<?>> classes) {
1.78 - List<Class<?>> sorted = new ArrayList<Class<?>>(classes);
1.79 - Collections.sort(sorted, new Comparator<Class<?>>() {
1.80 - public int compare(Class<?> c1, Class<?> c2) {
1.81 - return c1.getName().compareTo(c2.getName());
1.82 - }
1.83 - });
1.84 - return sorted;
1.85 - }
1.86 -
1.87 - public void testBasicUsage() throws Exception {
1.88 - assertEquals(Collections.singletonList(Implementation.class), classesOfLookup(Interface.class));
1.89 - }
1.90 - public interface Interface {}
1.91 - @ServiceProvider(service=Interface.class)
1.92 - public static class Implementation implements Interface {}
1.93 -
1.94 - public void testPosition() throws Exception {
1.95 - assertEquals(Arrays.<Class<?>>asList(OrderedImpl3.class, OrderedImpl2.class, OrderedImpl1.class), classesOfLookup(OrderedInterface.class));
1.96 - }
1.97 - public interface OrderedInterface {}
1.98 - @ServiceProvider(service=OrderedInterface.class)
1.99 - public static class OrderedImpl1 implements OrderedInterface {}
1.100 - @ServiceProvider(service=OrderedInterface.class, position=200)
1.101 - public static class OrderedImpl2 implements OrderedInterface {}
1.102 - @ServiceProvider(service=OrderedInterface.class, position=100)
1.103 - public static class OrderedImpl3 implements OrderedInterface {}
1.104 -
1.105 - public void testPath() throws Exception {
1.106 - assertEquals(Collections.singletonList(PathImplementation.class), classesOf(Lookups.forPath("some/path").lookupAll(Interface.class)));
1.107 - }
1.108 - @ServiceProvider(service=Interface.class, path="some/path")
1.109 - public static class PathImplementation implements Interface {}
1.110 -
1.111 - public void testSupersedes() throws Exception {
1.112 - assertEquals(Arrays.<Class<?>>asList(Overrider.class, Unrelated.class), sortClassList(classesOfLookup(CancellableInterface.class)));
1.113 - }
1.114 - public interface CancellableInterface {}
1.115 - @ServiceProvider(service=CancellableInterface.class)
1.116 - public static class Overridden implements CancellableInterface {}
1.117 - @ServiceProvider(service=CancellableInterface.class, supersedes="org.netbeans.modules.openide.util.ServiceProviderProcessorTest$Overridden")
1.118 - public static class Overrider implements CancellableInterface {}
1.119 - @ServiceProvider(service=CancellableInterface.class)
1.120 - public static class Unrelated implements CancellableInterface {}
1.121 -
1.122 - public void testMultipleRegistrations() throws Exception {
1.123 - assertEquals(Collections.singletonList(Multitasking.class), classesOfLookup(Interface1.class));
1.124 - assertEquals(Collections.singletonList(Multitasking.class), classesOfLookup(Interface2.class));
1.125 - }
1.126 - public interface Interface1 {}
1.127 - public interface Interface2 {}
1.128 - @ServiceProviders({@ServiceProvider(service=Interface1.class), @ServiceProvider(service=Interface2.class)})
1.129 - public static class Multitasking implements Interface1, Interface2 {}
1.130 -
1.131 - public void testErrorReporting() throws Exception {
1.132 - clearWorkDir();
1.133 - File src = new File(getWorkDir(), "src");
1.134 - File dest = new File(getWorkDir(), "classes");
1.135 - String xfaceName = Interface.class.getCanonicalName();
1.136 -
1.137 - AnnotationProcessorTestUtils.makeSource(src, "p.C1",
1.138 - "@org.openide.util.lookup.ServiceProvider(service=" + xfaceName + ".class)",
1.139 - "public class C1 implements " + xfaceName + " {}");
1.140 - ByteArrayOutputStream baos = new ByteArrayOutputStream();
1.141 - assertTrue(AnnotationProcessorTestUtils.runJavac(src, "C1", dest, null, baos));
1.142 -
1.143 - AnnotationProcessorTestUtils.makeSource(src, "p.C2",
1.144 - "@org.openide.util.lookup.ServiceProvider(service=" + xfaceName + ".class)",
1.145 - "class C2 implements " + xfaceName + " {}");
1.146 - baos = new ByteArrayOutputStream();
1.147 - assertFalse(AnnotationProcessorTestUtils.runJavac(src, "C2", dest, null, baos));
1.148 - assertTrue(baos.toString(), baos.toString().contains("public"));
1.149 -
1.150 - AnnotationProcessorTestUtils.makeSource(src, "p.C3",
1.151 - "@org.openide.util.lookup.ServiceProvider(service=" + xfaceName + ".class)",
1.152 - "public class C3 implements " + xfaceName + " {",
1.153 - "public C3(boolean x) {}",
1.154 - "}");
1.155 - baos = new ByteArrayOutputStream();
1.156 - assertFalse(AnnotationProcessorTestUtils.runJavac(src, "C3", dest, null, baos));
1.157 - assertTrue(baos.toString(), baos.toString().contains("constructor"));
1.158 -
1.159 - AnnotationProcessorTestUtils.makeSource(src, "p.C4",
1.160 - "@org.openide.util.lookup.ServiceProvider(service=" + xfaceName + ".class)",
1.161 - "public class C4 implements " + xfaceName + " {",
1.162 - "C4() {}",
1.163 - "}");
1.164 - baos = new ByteArrayOutputStream();
1.165 - assertFalse(AnnotationProcessorTestUtils.runJavac(src, "C4", dest, null, baos));
1.166 - assertTrue(baos.toString(), baos.toString().contains("constructor"));
1.167 -
1.168 - AnnotationProcessorTestUtils.makeSource(src, "p.C5",
1.169 - "@org.openide.util.lookup.ServiceProvider(service=" + xfaceName + ".class)",
1.170 - "public abstract class C5 implements " + xfaceName + " {}");
1.171 - baos = new ByteArrayOutputStream();
1.172 - assertFalse(AnnotationProcessorTestUtils.runJavac(src, "C5", dest, null, baos));
1.173 - assertTrue(baos.toString(), baos.toString().contains("abstract"));
1.174 -
1.175 - AnnotationProcessorTestUtils.makeSource(src, "p.C6",
1.176 - "@org.openide.util.lookup.ServiceProvider(service=" + xfaceName + ".class)",
1.177 - "public class C6 {}");
1.178 - baos = new ByteArrayOutputStream();
1.179 - assertFalse(AnnotationProcessorTestUtils.runJavac(src, "C6", dest, null, baos));
1.180 - assertTrue(baos.toString(), baos.toString().contains("assignable"));
1.181 - }
1.182 -
1.183 -}