jaroslav@123
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@123
|
3 |
*
|
jaroslav@358
|
4 |
* Copyright 1997-2010 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@358
|
30 |
* Software is Oracle. Portions Copyright 2013-2013 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@123
|
45 |
import java.lang.reflect.InvocationTargetException;
|
jaroslav@123
|
46 |
import java.lang.reflect.Method;
|
jaroslav@153
|
47 |
import java.lang.reflect.Modifier;
|
jaroslav@123
|
48 |
import static org.testng.Assert.*;
|
jaroslav@123
|
49 |
import org.testng.annotations.BeforeMethod;
|
jaroslav@123
|
50 |
import org.testng.annotations.Test;
|
jaroslav@123
|
51 |
|
jaroslav@123
|
52 |
/**
|
jaroslav@123
|
53 |
*
|
jaroslav@123
|
54 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@123
|
55 |
*/
|
jaroslav@123
|
56 |
public class JsClassLoaderBase {
|
jaroslav@123
|
57 |
protected static Class<?> methodClass;
|
jaroslav@123
|
58 |
|
jaroslav@123
|
59 |
public JsClassLoaderBase() {
|
jaroslav@123
|
60 |
}
|
jaroslav@123
|
61 |
|
jaroslav@123
|
62 |
@BeforeMethod
|
jaroslav@123
|
63 |
public void assertClassDefined() {
|
jaroslav@123
|
64 |
assertNotNull(methodClass, "BeforeClass set up code should provide methodClass");
|
jaroslav@123
|
65 |
}
|
jaroslav@123
|
66 |
|
jaroslav@123
|
67 |
@Test public void noParamMethod() throws Throwable {
|
jaroslav@123
|
68 |
Method plus = methodClass.getMethod("fortyTwo");
|
jaroslav@123
|
69 |
try {
|
jaroslav@123
|
70 |
final Object val = plus.invoke(null);
|
jaroslav@123
|
71 |
assertTrue(val instanceof Number, "A number returned " + val);
|
jaroslav@123
|
72 |
assertEquals(((Number)val).intValue(), 42);
|
jaroslav@123
|
73 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
74 |
throw ex.getTargetException();
|
jaroslav@123
|
75 |
}
|
jaroslav@123
|
76 |
}
|
jaroslav@123
|
77 |
|
jaroslav@123
|
78 |
@Test public void testExecuteScript() throws Throwable {
|
jaroslav@123
|
79 |
Method plus = methodClass.getMethod("plus", int.class, int.class);
|
jaroslav@123
|
80 |
try {
|
jaroslav@123
|
81 |
assertEquals(plus.invoke(null, 10, 20), 30);
|
jaroslav@123
|
82 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
83 |
throw ex.getTargetException();
|
jaroslav@123
|
84 |
}
|
jaroslav@123
|
85 |
}
|
jaroslav@123
|
86 |
|
jaroslav@123
|
87 |
@Test public void overloadedMethod() throws Throwable {
|
jaroslav@123
|
88 |
Method plus = methodClass.getMethod("plus", int.class);
|
jaroslav@123
|
89 |
try {
|
jaroslav@123
|
90 |
assertEquals(plus.invoke(null, 10), 10);
|
jaroslav@123
|
91 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
92 |
throw ex.getTargetException();
|
jaroslav@123
|
93 |
}
|
jaroslav@123
|
94 |
}
|
jaroslav@123
|
95 |
|
jaroslav@123
|
96 |
@Test public void instanceMethod() throws Throwable {
|
jaroslav@123
|
97 |
Method plus = methodClass.getMethod("plusInst", int.class);
|
jaroslav@123
|
98 |
Object inst = methodClass.newInstance();
|
jaroslav@123
|
99 |
try {
|
jaroslav@123
|
100 |
assertEquals(plus.invoke(inst, 10), 10);
|
jaroslav@123
|
101 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
102 |
throw ex.getTargetException();
|
jaroslav@123
|
103 |
}
|
jaroslav@123
|
104 |
}
|
jaroslav@123
|
105 |
|
jaroslav@123
|
106 |
@Test public void staticThis() throws Throwable {
|
jaroslav@123
|
107 |
Method st = methodClass.getMethod("staticThis");
|
jaroslav@123
|
108 |
try {
|
jaroslav@123
|
109 |
assertNull(st.invoke(null));
|
jaroslav@123
|
110 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
111 |
throw ex.getTargetException();
|
jaroslav@123
|
112 |
}
|
jaroslav@123
|
113 |
}
|
jaroslav@123
|
114 |
|
jaroslav@123
|
115 |
@Test public void getThis() throws Throwable {
|
jaroslav@123
|
116 |
Object th = methodClass.newInstance();
|
jaroslav@123
|
117 |
Method st = methodClass.getMethod("getThis");
|
jaroslav@123
|
118 |
try {
|
jaroslav@123
|
119 |
assertEquals(st.invoke(th), th);
|
jaroslav@123
|
120 |
} catch (InvocationTargetException ex) {
|
jaroslav@123
|
121 |
throw ex.getTargetException();
|
jaroslav@123
|
122 |
}
|
jaroslav@123
|
123 |
}
|
jaroslav@123
|
124 |
|
jaroslav@153
|
125 |
@Test public void truth() throws Throwable {
|
jaroslav@153
|
126 |
Method st = methodClass.getMethod("truth");
|
jaroslav@153
|
127 |
assertTrue((st.getModifiers() & Modifier.STATIC) != 0, "Is static");
|
jaroslav@153
|
128 |
assertEquals(st.invoke(null), Boolean.TRUE, "Can return boolean");
|
jaroslav@153
|
129 |
}
|
jaroslav@153
|
130 |
|
jaroslav@160
|
131 |
@Test public void callback() throws Throwable {
|
jaroslav@160
|
132 |
class R implements Runnable {
|
jaroslav@160
|
133 |
int cnt;
|
jaroslav@160
|
134 |
|
jaroslav@160
|
135 |
@Override
|
jaroslav@160
|
136 |
public void run() {
|
jaroslav@160
|
137 |
cnt++;
|
jaroslav@160
|
138 |
}
|
jaroslav@160
|
139 |
}
|
jaroslav@160
|
140 |
R r = new R();
|
jaroslav@160
|
141 |
|
jaroslav@160
|
142 |
Method inc = methodClass.getMethod("callback", Runnable.class);
|
jaroslav@160
|
143 |
inc.invoke(null, r);
|
jaroslav@160
|
144 |
|
jaroslav@160
|
145 |
assertEquals(r.cnt, 1, "Callback happened");
|
jaroslav@160
|
146 |
}
|
jaroslav@160
|
147 |
|
jaroslav@161
|
148 |
@Test public void sumArray() throws Throwable {
|
jaroslav@161
|
149 |
Method st = methodClass.getMethod("sumArr", int[].class);
|
jaroslav@161
|
150 |
assertEquals(st.invoke(null, new int[] { 1, 2, 3 }), 6, "1+2+3 is six");
|
jaroslav@161
|
151 |
}
|
jaroslav@163
|
152 |
|
jaroslav@163
|
153 |
@Test public void javaScriptResource() throws Throwable {
|
jaroslav@163
|
154 |
try {
|
jaroslav@163
|
155 |
Method st = methodClass.getMethod("useExternalMul", int.class, int.class);
|
jaroslav@163
|
156 |
assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
|
jaroslav@163
|
157 |
} catch (InvocationTargetException ex) {
|
jaroslav@163
|
158 |
throw ex.getTargetException();
|
jaroslav@163
|
159 |
}
|
jaroslav@163
|
160 |
}
|
jaroslav@171
|
161 |
|
jaroslav@171
|
162 |
@Test public void callJavaScriptMethodOnOwnClass() throws Throwable {
|
jaroslav@171
|
163 |
try {
|
jaroslav@171
|
164 |
Object thiz = methodClass.newInstance();
|
jaroslav@171
|
165 |
Method st = methodClass.getMethod("returnYourSelf", methodClass);
|
jaroslav@171
|
166 |
assertEquals(st.invoke(null, thiz), thiz, "Returns this");
|
jaroslav@171
|
167 |
} catch (InvocationTargetException ex) {
|
jaroslav@171
|
168 |
throw ex.getTargetException();
|
jaroslav@171
|
169 |
}
|
jaroslav@191
|
170 |
}
|
jaroslav@191
|
171 |
|
jaroslav@191
|
172 |
@Test public void callStaticJavaMethod() throws Throwable {
|
jaroslav@191
|
173 |
Method st = methodClass.getMethod("staticCallback", int.class, int.class);
|
jaroslav@191
|
174 |
assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
|
jaroslav@191
|
175 |
}
|
jaroslav@191
|
176 |
|
jaroslav@191
|
177 |
@Test public void callStaticStringParamMethod() throws Throwable {
|
jaroslav@191
|
178 |
Method st = methodClass.getMethod("parseInt", String.class);
|
jaroslav@191
|
179 |
assertEquals(st.invoke(null, "42"), 42, "Meaning of JavaScript?");
|
jaroslav@171
|
180 |
}
|
jaroslav@192
|
181 |
|
jaroslav@192
|
182 |
@Test public void firstLong() throws Throwable {
|
jaroslav@192
|
183 |
Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
|
jaroslav@192
|
184 |
assertEquals(st.invoke(null, true, false, 10, 20), 10L, "Take first value");
|
jaroslav@192
|
185 |
}
|
jaroslav@192
|
186 |
|
jaroslav@192
|
187 |
@Test public void secondLong() throws Throwable {
|
jaroslav@192
|
188 |
Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
|
jaroslav@192
|
189 |
assertEquals(st.invoke(null, false, true, 10, 20), 20L, "Take 2nd value");
|
jaroslav@192
|
190 |
}
|
jaroslav@192
|
191 |
|
jaroslav@192
|
192 |
@Test public void bothLong() throws Throwable {
|
jaroslav@192
|
193 |
Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
|
jaroslav@192
|
194 |
assertEquals(st.invoke(null, true, true, 10, 20), 30L, "Take both values");
|
jaroslav@192
|
195 |
}
|
jaroslav@196
|
196 |
|
jaroslav@196
|
197 |
@Test public void recordError() throws Throwable {
|
jaroslav@196
|
198 |
Method st = methodClass.getMethod("recordError", Object.class);
|
jaroslav@196
|
199 |
assertEquals(st.invoke(methodClass.newInstance(), "Hello"), "Hello", "The same parameter returned");
|
jaroslav@196
|
200 |
}
|
jaroslav@123
|
201 |
} |