Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.openide.util.lookup; |
43 |
|
44 |
import java.io.ByteArrayOutputStream; |
45 |
import java.lang.annotation.Retention; |
46 |
import java.lang.annotation.RetentionPolicy; |
47 |
import java.net.URL; |
48 |
import java.net.URLClassLoader; |
49 |
import java.util.concurrent.Callable; |
50 |
import org.netbeans.junit.NbTestCase; |
51 |
import org.openide.util.Lookup; |
52 |
import org.openide.util.test.AnnotationProcessorTestUtils; |
53 |
|
54 |
/** |
55 |
* |
56 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
57 |
*/ |
58 |
public class NamedServiceDefinitionTest extends NbTestCase { |
59 |
|
60 |
public NamedServiceDefinitionTest(String n) { |
61 |
super(n); |
62 |
} |
63 |
|
64 |
@Override |
65 |
protected void setUp() throws Exception { |
66 |
clearWorkDir(); |
67 |
} |
68 |
|
69 |
public void testNamedDefinition() throws Exception { |
70 |
System.setProperty("executed", "false"); |
71 |
String content = "import org.openide.util.lookup.NamedServiceDefinitionTest.RunTestReg;\n" |
72 |
+ "@RunTestReg(position=10,when=\"now\")\n" |
73 |
+ "public class Test implements Runnable {\n" |
74 |
+ " public void run() { System.setProperty(\"executed\", \"true\"); }\n" |
75 |
+ "}\n"; |
76 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Test", content); |
77 |
assertTrue("Compiles OK", |
78 |
AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, System.err) |
79 |
); |
80 |
|
81 |
URLClassLoader l = new URLClassLoader(new URL[] { getWorkDir().toURI().toURL() }, NamedServiceDefinitionTest.class.getClassLoader()); |
82 |
Lookup lkp = Lookups.metaInfServices(l, "META-INF/namedservices/runtest/now/below/"); |
83 |
for (Runnable r : lkp.lookupAll(Runnable.class)) { |
84 |
r.run(); |
85 |
} |
86 |
assertEquals("Our runnable was executed", "true", System.getProperty("executed")); |
87 |
} |
88 |
|
89 |
public void testNamedDefinitionWithArray() throws Exception { |
90 |
System.setProperty("executed", "false"); |
91 |
String content = "import org.openide.util.lookup.NamedServiceDefinitionTest.RunTestArray;\n" |
92 |
+ "@RunTestArray(position=10,array={\"now\", \"then\" })\n" |
93 |
+ "public class Test implements Runnable {\n" |
94 |
+ " public void run() { System.setProperty(\"executed\", \"true\"); }\n" |
95 |
+ "}\n"; |
96 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Test", content); |
97 |
assertTrue("Compiles OK", |
98 |
AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, System.err) |
99 |
); |
100 |
|
101 |
URLClassLoader l = new URLClassLoader(new URL[] { getWorkDir().toURI().toURL() }, NamedServiceDefinitionTest.class.getClassLoader()); |
102 |
Lookup lkp = Lookups.metaInfServices(l, "META-INF/namedservices/runtest/now/"); |
103 |
for (Runnable r : lkp.lookupAll(Runnable.class)) { |
104 |
r.run(); |
105 |
} |
106 |
assertEquals("Our runnable was executed", "true", System.getProperty("executed")); |
107 |
System.setProperty("executed", "false"); |
108 |
Lookup lkp2 = Lookups.metaInfServices(l, "META-INF/namedservices/runtest/then/"); |
109 |
for (Runnable r : lkp2.lookupAll(Runnable.class)) { |
110 |
r.run(); |
111 |
} |
112 |
assertEquals("Our runnable was executed again", "true", System.getProperty("executed")); |
113 |
} |
114 |
|
115 |
public void testDoesNotImplementInterfaces() throws Exception { |
116 |
System.setProperty("executed", "false"); |
117 |
String content = "import org.openide.util.lookup.NamedServiceDefinitionTest.RunTestReg;\n" |
118 |
+ "@RunTestReg(position=10,when=\"now\")\n" |
119 |
+ "public class Test {\n" |
120 |
+ " public void run() { System.setProperty(\"executed\", \"true\"); }\n" |
121 |
+ "}\n"; |
122 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Test", content); |
123 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
124 |
assertFalse("Compilation fails", |
125 |
AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, os) |
126 |
); |
127 |
String err = new String(os.toByteArray(), "UTF-8"); |
128 |
if (err.indexOf("java.lang.Runnable") == -1) { |
129 |
fail("The error messages should say something about interface Runnable\n" + err); |
130 |
} |
131 |
if (err.indexOf("Callable") == -1) { |
132 |
fail("The error messages should say something about interface Callable\n" + err); |
133 |
} |
134 |
} |
135 |
|
136 |
public void testMissingPathAttribute() throws Exception { |
137 |
String content = "import org.openide.util.lookup.NamedServiceDefinition;\n" |
138 |
+ "@NamedServiceDefinition(path=\"runtest/@when()/below\",serviceType=Runnable.class)\n" |
139 |
+ "public @interface Test {\n" |
140 |
+ " String noWhenAttributeHere();" |
141 |
+ "}\n"; |
142 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Test", content); |
143 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
144 |
assertFalse("Compilation fails", |
145 |
AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, os) |
146 |
); |
147 |
String err = new String(os.toByteArray(), "UTF-8"); |
148 |
if (err.indexOf("@when()") == -1) { |
149 |
fail("The error messages should say something about missing @when\n" + err); |
150 |
} |
151 |
} |
152 |
|
153 |
public void testNonStringPathAttribute() throws Exception { |
154 |
String content = "import org.openide.util.lookup.NamedServiceDefinition;\n" |
155 |
+ "@NamedServiceDefinition(path=\"runtest/@when()/below\",serviceType=Runnable.class)\n" |
156 |
+ "public @interface Test {\n" |
157 |
+ " int when();" |
158 |
+ "}\n"; |
159 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Test", content); |
160 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
161 |
assertFalse("Compilation fails", |
162 |
AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, os) |
163 |
); |
164 |
String err = new String(os.toByteArray(), "UTF-8"); |
165 |
if (err.indexOf("@when()") == -1) { |
166 |
fail("The error messages should say something about missing @when\n" + err); |
167 |
} |
168 |
} |
169 |
|
170 |
public void testNonExistentPositionAttribute() throws Exception { |
171 |
String content = "import org.openide.util.lookup.NamedServiceDefinition;\n" |
172 |
+ "@NamedServiceDefinition(path=\"fixed\",serviceType=Runnable.class,position=\"where\")\n" |
173 |
+ "public @interface Test {\n" |
174 |
+ " int when();" |
175 |
+ "}\n"; |
176 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Test", content); |
177 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
178 |
assertFalse("Compilation fails", |
179 |
AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, os) |
180 |
); |
181 |
String err = new String(os.toByteArray(), "UTF-8"); |
182 |
if (err.indexOf("where") == -1) { |
183 |
fail("The error messages should say something about missing where\n" + err); |
184 |
} |
185 |
} |
186 |
public void testNonIntegerPositionAttribute() throws Exception { |
187 |
String content = "import org.openide.util.lookup.NamedServiceDefinition;\n" |
188 |
+ "@NamedServiceDefinition(path=\"fixed\",serviceType=Runnable.class,position=\"where\")\n" |
189 |
+ "public @interface Test {\n" |
190 |
+ " Class<?> where();" |
191 |
+ "}\n"; |
192 |
AnnotationProcessorTestUtils.makeSource(getWorkDir(), "x.Test", content); |
193 |
ByteArrayOutputStream os = new ByteArrayOutputStream(); |
194 |
assertFalse("Compilation fails", |
195 |
AnnotationProcessorTestUtils.runJavac(getWorkDir(), null, getWorkDir(), null, os) |
196 |
); |
197 |
String err = new String(os.toByteArray(), "UTF-8"); |
198 |
if (err.indexOf("where") == -1) { |
199 |
fail("The error messages should say something about missing where\n" + err); |
200 |
} |
201 |
} |
202 |
|
203 |
@NamedServiceDefinition( |
204 |
path="runtest/@when()/below", |
205 |
serviceType={ Runnable.class, Callable.class } |
206 |
) |
207 |
@Retention(RetentionPolicy.SOURCE) |
208 |
public static @interface RunTestReg { |
209 |
public int position(); |
210 |
public String when(); |
211 |
} |
212 |
@NamedServiceDefinition( |
213 |
path="runtest/@array()", |
214 |
serviceType={ Runnable.class, Callable.class } |
215 |
) |
216 |
@Retention(RetentionPolicy.SOURCE) |
217 |
public static @interface RunTestArray { |
218 |
public int position(); |
219 |
public String[] array(); |
220 |
} |
221 |
} |