jaroslav@123
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@123
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@123
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@123
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@123
|
42 |
*/
|
jaroslav@362
|
43 |
package org.netbeans.html.boot.impl;
|
jaroslav@123
|
44 |
|
jaroslav@451
|
45 |
import java.io.Closeable;
|
jaroslav@123
|
46 |
import java.lang.reflect.InvocationTargetException;
|
jaroslav@123
|
47 |
import java.lang.reflect.Method;
|
jaroslav@153
|
48 |
import java.lang.reflect.Modifier;
|
jtulach@838
|
49 |
import org.netbeans.html.boot.spi.Fn;
|
jtulach@851
|
50 |
import org.testng.Assert;
|
jaroslav@123
|
51 |
import static org.testng.Assert.*;
|
jaroslav@123
|
52 |
import org.testng.annotations.BeforeMethod;
|
jaroslav@123
|
53 |
import org.testng.annotations.Test;
|
jaroslav@123
|
54 |
|
jaroslav@123
|
55 |
/**
|
jaroslav@123
|
56 |
*
|
jtulach@655
|
57 |
* @author Jaroslav Tulach
|
jaroslav@123
|
58 |
*/
|
jaroslav@123
|
59 |
public class JsClassLoaderBase {
|
jaroslav@123
|
60 |
protected static Class<?> methodClass;
|
jaroslav@123
|
61 |
|
jaroslav@123
|
62 |
public JsClassLoaderBase() {
|
jaroslav@123
|
63 |
}
|
jaroslav@123
|
64 |
|
jaroslav@123
|
65 |
@BeforeMethod
|
jaroslav@123
|
66 |
public void assertClassDefined() {
|
jaroslav@123
|
67 |
assertNotNull(methodClass, "BeforeClass set up code should provide methodClass");
|
jaroslav@123
|
68 |
}
|
jaroslav@123
|
69 |
|
jaroslav@123
|
70 |
@Test public void noParamMethod() throws Throwable {
|
jaroslav@123
|
71 |
Method plus = methodClass.getMethod("fortyTwo");
|
jaroslav@123
|
72 |
try {
|
jtulach@1041
|
73 |
final java.lang.Object val = plus.invoke(null);
|
jaroslav@123
|
74 |
assertTrue(val instanceof Number, "A number returned " + val);
|
jaroslav@123
|
75 |
assertEquals(((Number)val).intValue(), 42);
|
jaroslav@123
|
76 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
77 |
throw ex.getTargetException();
|
jaroslav@123
|
78 |
}
|
jaroslav@123
|
79 |
}
|
jaroslav@123
|
80 |
|
jaroslav@123
|
81 |
@Test public void testExecuteScript() throws Throwable {
|
jaroslav@123
|
82 |
Method plus = methodClass.getMethod("plus", int.class, int.class);
|
jaroslav@123
|
83 |
try {
|
jaroslav@123
|
84 |
assertEquals(plus.invoke(null, 10, 20), 30);
|
jaroslav@123
|
85 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
86 |
throw ex.getTargetException();
|
jaroslav@123
|
87 |
}
|
jaroslav@123
|
88 |
}
|
jaroslav@123
|
89 |
|
jaroslav@123
|
90 |
@Test public void overloadedMethod() throws Throwable {
|
jaroslav@123
|
91 |
Method plus = methodClass.getMethod("plus", int.class);
|
jaroslav@123
|
92 |
try {
|
jaroslav@123
|
93 |
assertEquals(plus.invoke(null, 10), 10);
|
jaroslav@123
|
94 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
95 |
throw ex.getTargetException();
|
jaroslav@123
|
96 |
}
|
jaroslav@123
|
97 |
}
|
jaroslav@123
|
98 |
|
jaroslav@123
|
99 |
@Test public void instanceMethod() throws Throwable {
|
jaroslav@123
|
100 |
Method plus = methodClass.getMethod("plusInst", int.class);
|
jtulach@1041
|
101 |
java.lang.Object inst = methodClass.newInstance();
|
jaroslav@123
|
102 |
try {
|
jaroslav@123
|
103 |
assertEquals(plus.invoke(inst, 10), 10);
|
jaroslav@123
|
104 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
105 |
throw ex.getTargetException();
|
jaroslav@123
|
106 |
}
|
jaroslav@123
|
107 |
}
|
jaroslav@123
|
108 |
|
jaroslav@123
|
109 |
@Test public void staticThis() throws Throwable {
|
jaroslav@123
|
110 |
Method st = methodClass.getMethod("staticThis");
|
jaroslav@123
|
111 |
try {
|
jaroslav@123
|
112 |
assertNull(st.invoke(null));
|
jaroslav@123
|
113 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
114 |
throw ex.getTargetException();
|
jaroslav@123
|
115 |
}
|
jaroslav@123
|
116 |
}
|
jaroslav@123
|
117 |
|
jaroslav@123
|
118 |
@Test public void getThis() throws Throwable {
|
jtulach@1041
|
119 |
java.lang.Object th = methodClass.newInstance();
|
jaroslav@123
|
120 |
Method st = methodClass.getMethod("getThis");
|
jaroslav@123
|
121 |
try {
|
jaroslav@123
|
122 |
assertEquals(st.invoke(th), th);
|
jaroslav@123
|
123 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
124 |
throw ex.getTargetException();
|
jaroslav@123
|
125 |
}
|
jaroslav@123
|
126 |
}
|
jaroslav@123
|
127 |
|
jaroslav@153
|
128 |
@Test public void truth() throws Throwable {
|
jaroslav@153
|
129 |
Method st = methodClass.getMethod("truth");
|
jaroslav@153
|
130 |
assertTrue((st.getModifiers() & Modifier.STATIC) != 0, "Is static");
|
jaroslav@153
|
131 |
assertEquals(st.invoke(null), Boolean.TRUE, "Can return boolean");
|
jaroslav@153
|
132 |
}
|
jaroslav@153
|
133 |
|
jaroslav@160
|
134 |
@Test public void callback() throws Throwable {
|
jaroslav@160
|
135 |
class R implements Runnable {
|
jaroslav@160
|
136 |
int cnt;
|
jaroslav@160
|
137 |
|
jaroslav@160
|
138 |
@Override
|
jaroslav@160
|
139 |
public void run() {
|
jaroslav@160
|
140 |
cnt++;
|
jaroslav@160
|
141 |
}
|
jaroslav@160
|
142 |
}
|
jaroslav@160
|
143 |
R r = new R();
|
jaroslav@160
|
144 |
|
jaroslav@160
|
145 |
Method inc = methodClass.getMethod("callback", Runnable.class);
|
jaroslav@160
|
146 |
inc.invoke(null, r);
|
jaroslav@160
|
147 |
|
jaroslav@160
|
148 |
assertEquals(r.cnt, 1, "Callback happened");
|
jaroslav@160
|
149 |
}
|
jaroslav@160
|
150 |
|
jaroslav@161
|
151 |
@Test public void sumArray() throws Throwable {
|
jaroslav@161
|
152 |
Method st = methodClass.getMethod("sumArr", int[].class);
|
jaroslav@161
|
153 |
assertEquals(st.invoke(null, new int[] { 1, 2, 3 }), 6, "1+2+3 is six");
|
jaroslav@161
|
154 |
}
|
jaroslav@163
|
155 |
|
jaroslav@163
|
156 |
@Test public void javaScriptResource() throws Throwable {
|
jaroslav@163
|
157 |
try {
|
jaroslav@163
|
158 |
Method st = methodClass.getMethod("useExternalMul", int.class, int.class);
|
jaroslav@163
|
159 |
assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
|
jaroslav@163
|
160 |
} catch (InvocationTargetException ex) {
|
jaroslav@163
|
161 |
throw ex.getTargetException();
|
jaroslav@163
|
162 |
}
|
jaroslav@163
|
163 |
}
|
jaroslav@171
|
164 |
|
jaroslav@171
|
165 |
@Test public void callJavaScriptMethodOnOwnClass() throws Throwable {
|
jaroslav@171
|
166 |
try {
|
jtulach@1041
|
167 |
java.lang.Object thiz = methodClass.newInstance();
|
jaroslav@171
|
168 |
Method st = methodClass.getMethod("returnYourSelf", methodClass);
|
jaroslav@171
|
169 |
assertEquals(st.invoke(null, thiz), thiz, "Returns this");
|
jaroslav@171
|
170 |
} catch (InvocationTargetException ex) {
|
jaroslav@171
|
171 |
throw ex.getTargetException();
|
jaroslav@171
|
172 |
}
|
jaroslav@191
|
173 |
}
|
jaroslav@191
|
174 |
|
jaroslav@191
|
175 |
@Test public void callStaticJavaMethod() throws Throwable {
|
jaroslav@191
|
176 |
Method st = methodClass.getMethod("staticCallback", int.class, int.class);
|
jaroslav@191
|
177 |
assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
|
jaroslav@191
|
178 |
}
|
jaroslav@191
|
179 |
|
jaroslav@191
|
180 |
@Test public void callStaticStringParamMethod() throws Throwable {
|
jaroslav@191
|
181 |
Method st = methodClass.getMethod("parseInt", String.class);
|
jaroslav@191
|
182 |
assertEquals(st.invoke(null, "42"), 42, "Meaning of JavaScript?");
|
jaroslav@171
|
183 |
}
|
jtulach@933
|
184 |
|
jtulach@933
|
185 |
@Test public void passEnum() throws Throwable {
|
jtulach@933
|
186 |
Class<?> enmClazz = methodClass.getDeclaredClasses()[0];
|
jtulach@933
|
187 |
assertTrue(Enum.class.isAssignableFrom(enmClazz), "It is an enum: " + enmClazz);
|
jtulach@933
|
188 |
Class<? extends Enum> enmClazz2 = enmClazz.asSubclass(Enum.class);
|
jtulach@933
|
189 |
Method st = methodClass.getMethod("fromEnum", enmClazz);
|
jtulach@933
|
190 |
|
jtulach@1041
|
191 |
java.lang.Object valueB = Enum.valueOf(enmClazz2, "B");
|
jtulach@933
|
192 |
assertEquals(st.invoke(null, valueB), "B", "Converts to string");
|
jtulach@933
|
193 |
}
|
jaroslav@192
|
194 |
|
jaroslav@192
|
195 |
@Test public void firstLong() throws Throwable {
|
jaroslav@192
|
196 |
Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
|
jaroslav@192
|
197 |
assertEquals(st.invoke(null, true, false, 10, 20), 10L, "Take first value");
|
jaroslav@192
|
198 |
}
|
jaroslav@192
|
199 |
|
jaroslav@192
|
200 |
@Test public void secondLong() throws Throwable {
|
jaroslav@192
|
201 |
Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
|
jaroslav@192
|
202 |
assertEquals(st.invoke(null, false, true, 10, 20), 20L, "Take 2nd value");
|
jaroslav@192
|
203 |
}
|
jaroslav@192
|
204 |
|
jaroslav@192
|
205 |
@Test public void bothLong() throws Throwable {
|
jaroslav@192
|
206 |
Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
|
jaroslav@192
|
207 |
assertEquals(st.invoke(null, true, true, 10, 20), 30L, "Take both values");
|
jaroslav@192
|
208 |
}
|
jaroslav@196
|
209 |
|
jaroslav@196
|
210 |
@Test public void recordError() throws Throwable {
|
jtulach@1041
|
211 |
Method st = methodClass.getMethod("recordError", java.lang.Object.class);
|
jaroslav@196
|
212 |
assertEquals(st.invoke(methodClass.newInstance(), "Hello"), "Hello", "The same parameter returned");
|
jaroslav@196
|
213 |
}
|
jaroslav@439
|
214 |
|
jaroslav@451
|
215 |
@Test public void plusOrMul() throws Throwable {
|
jaroslav@451
|
216 |
Method st = methodClass.getMethod("plusOrMul", int.class, int.class);
|
jaroslav@451
|
217 |
assertNotNull(Fn.activePresenter(), "Is there a presenter?");
|
jaroslav@451
|
218 |
Closeable c = Fn.activate(null);
|
jaroslav@451
|
219 |
try {
|
jaroslav@451
|
220 |
assertNull(Fn.activePresenter(), "No presenter now");
|
jaroslav@451
|
221 |
assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
|
jaroslav@451
|
222 |
} finally {
|
jaroslav@451
|
223 |
c.close();
|
jaroslav@451
|
224 |
}
|
jaroslav@451
|
225 |
assertNotNull(Fn.activePresenter(), "Is there a presenter again");
|
jaroslav@451
|
226 |
assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript");
|
jaroslav@451
|
227 |
c = Fn.activate(null);
|
jaroslav@451
|
228 |
try {
|
jaroslav@451
|
229 |
assertNull(Fn.activePresenter(), "No presenter again");
|
jaroslav@451
|
230 |
assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
|
jaroslav@451
|
231 |
} finally {
|
jaroslav@451
|
232 |
c.close();
|
jaroslav@451
|
233 |
}
|
jaroslav@451
|
234 |
assertNotNull(Fn.activePresenter(), "Is there a presenter again");
|
jaroslav@451
|
235 |
assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript again");
|
jaroslav@451
|
236 |
}
|
jaroslav@451
|
237 |
|
jaroslav@439
|
238 |
@Test public void arrayInOut() throws Throwable {
|
jaroslav@439
|
239 |
String[] arr = { "Ahoj" };
|
jtulach@1041
|
240 |
Method st = methodClass.getMethod("arr", java.lang.Object[].class);
|
jtulach@1041
|
241 |
java.lang.Object ret = st.invoke(null, (java.lang.Object) arr);
|
jtulach@1041
|
242 |
assertTrue(ret instanceof java.lang.Object[], "Expecting array: " + ret);
|
jtulach@1041
|
243 |
java.lang.Object[] res = (java.lang.Object[]) ret;
|
jaroslav@439
|
244 |
assertEquals(res.length, 1, "One element");
|
jaroslav@439
|
245 |
assertEquals(res[0], "Ahoj", "The right string");
|
jaroslav@439
|
246 |
}
|
jtulach@851
|
247 |
|
jtulach@851
|
248 |
@Test public void checkTheTypeOfThrownException() throws Throwable {
|
jtulach@851
|
249 |
FnContext.currentPresenter(null);
|
jtulach@851
|
250 |
assertNull(Fn.activePresenter(), "No presenter is activer right now");
|
jtulach@1041
|
251 |
java.lang.Object res = null;
|
jtulach@851
|
252 |
try {
|
jtulach@851
|
253 |
Method st = methodClass.getMethod("plus", int.class, int.class);
|
jtulach@851
|
254 |
try {
|
jtulach@851
|
255 |
res = st.invoke(null, 40, 2);
|
jtulach@852
|
256 |
Assert.fail("Native method should throw IllegalStateException. Was: " + res);
|
jtulach@851
|
257 |
} catch (InvocationTargetException ex) {
|
jtulach@851
|
258 |
throw ex.getTargetException();
|
jtulach@851
|
259 |
}
|
jtulach@851
|
260 |
} catch (IllegalStateException ex) {
|
jtulach@851
|
261 |
assertEquals(ex.getMessage(), "No presenter active. Use BrwsrCtx.execute!");
|
jtulach@851
|
262 |
}
|
jtulach@851
|
263 |
}
|
jtulach@851
|
264 |
}
|