During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package org.netbeans.html.boot.impl;
45 import java.io.Closeable;
46 import java.lang.reflect.InvocationTargetException;
47 import java.lang.reflect.Method;
48 import java.lang.reflect.Modifier;
49 import org.netbeans.html.boot.spi.Fn;
50 import static org.testng.Assert.*;
51 import org.testng.annotations.BeforeMethod;
52 import org.testng.annotations.Test;
56 * @author Jaroslav Tulach
58 public class JsClassLoaderBase {
59 protected static Class<?> methodClass;
61 public JsClassLoaderBase() {
65 public void assertClassDefined() {
66 assertNotNull(methodClass, "BeforeClass set up code should provide methodClass");
69 @Test public void noParamMethod() throws Throwable {
70 Method plus = methodClass.getMethod("fortyTwo");
72 final Object val = plus.invoke(null);
73 assertTrue(val instanceof Number, "A number returned " + val);
74 assertEquals(((Number)val).intValue(), 42);
75 } catch (InvocationTargetException ex) {
76 throw ex.getTargetException();
80 @Test public void testExecuteScript() throws Throwable {
81 Method plus = methodClass.getMethod("plus", int.class, int.class);
83 assertEquals(plus.invoke(null, 10, 20), 30);
84 } catch (InvocationTargetException ex) {
85 throw ex.getTargetException();
89 @Test public void overloadedMethod() throws Throwable {
90 Method plus = methodClass.getMethod("plus", int.class);
92 assertEquals(plus.invoke(null, 10), 10);
93 } catch (InvocationTargetException ex) {
94 throw ex.getTargetException();
98 @Test public void instanceMethod() throws Throwable {
99 Method plus = methodClass.getMethod("plusInst", int.class);
100 Object inst = methodClass.newInstance();
102 assertEquals(plus.invoke(inst, 10), 10);
103 } catch (InvocationTargetException ex) {
104 throw ex.getTargetException();
108 @Test public void staticThis() throws Throwable {
109 Method st = methodClass.getMethod("staticThis");
111 assertNull(st.invoke(null));
112 } catch (InvocationTargetException ex) {
113 throw ex.getTargetException();
117 @Test public void getThis() throws Throwable {
118 Object th = methodClass.newInstance();
119 Method st = methodClass.getMethod("getThis");
121 assertEquals(st.invoke(th), th);
122 } catch (InvocationTargetException ex) {
123 throw ex.getTargetException();
127 @Test public void truth() throws Throwable {
128 Method st = methodClass.getMethod("truth");
129 assertTrue((st.getModifiers() & Modifier.STATIC) != 0, "Is static");
130 assertEquals(st.invoke(null), Boolean.TRUE, "Can return boolean");
133 @Test public void callback() throws Throwable {
134 class R implements Runnable {
144 Method inc = methodClass.getMethod("callback", Runnable.class);
147 assertEquals(r.cnt, 1, "Callback happened");
150 @Test public void sumArray() throws Throwable {
151 Method st = methodClass.getMethod("sumArr", int[].class);
152 assertEquals(st.invoke(null, new int[] { 1, 2, 3 }), 6, "1+2+3 is six");
155 @Test public void javaScriptResource() throws Throwable {
157 Method st = methodClass.getMethod("useExternalMul", int.class, int.class);
158 assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
159 } catch (InvocationTargetException ex) {
160 throw ex.getTargetException();
164 @Test public void callJavaScriptMethodOnOwnClass() throws Throwable {
166 Object thiz = methodClass.newInstance();
167 Method st = methodClass.getMethod("returnYourSelf", methodClass);
168 assertEquals(st.invoke(null, thiz), thiz, "Returns this");
169 } catch (InvocationTargetException ex) {
170 throw ex.getTargetException();
174 @Test public void callStaticJavaMethod() throws Throwable {
175 Method st = methodClass.getMethod("staticCallback", int.class, int.class);
176 assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
179 @Test public void callStaticStringParamMethod() throws Throwable {
180 Method st = methodClass.getMethod("parseInt", String.class);
181 assertEquals(st.invoke(null, "42"), 42, "Meaning of JavaScript?");
184 @Test public void firstLong() throws Throwable {
185 Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
186 assertEquals(st.invoke(null, true, false, 10, 20), 10L, "Take first value");
189 @Test public void secondLong() throws Throwable {
190 Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
191 assertEquals(st.invoke(null, false, true, 10, 20), 20L, "Take 2nd value");
194 @Test public void bothLong() throws Throwable {
195 Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
196 assertEquals(st.invoke(null, true, true, 10, 20), 30L, "Take both values");
199 @Test public void recordError() throws Throwable {
200 Method st = methodClass.getMethod("recordError", Object.class);
201 assertEquals(st.invoke(methodClass.newInstance(), "Hello"), "Hello", "The same parameter returned");
204 @Test public void plusOrMul() throws Throwable {
205 Method st = methodClass.getMethod("plusOrMul", int.class, int.class);
206 assertNotNull(Fn.activePresenter(), "Is there a presenter?");
207 Closeable c = Fn.activate(null);
209 assertNull(Fn.activePresenter(), "No presenter now");
210 assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
214 assertNotNull(Fn.activePresenter(), "Is there a presenter again");
215 assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript");
216 c = Fn.activate(null);
218 assertNull(Fn.activePresenter(), "No presenter again");
219 assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
223 assertNotNull(Fn.activePresenter(), "Is there a presenter again");
224 assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript again");
227 @Test public void arrayInOut() throws Throwable {
228 String[] arr = { "Ahoj" };
229 Method st = methodClass.getMethod("arr", Object[].class);
230 Object ret = st.invoke(null, (Object) arr);
231 assertTrue(ret instanceof Object[], "Expecting array: " + ret);
232 Object[] res = (Object[]) ret;
233 assertEquals(res.length, 1, "One element");
234 assertEquals(res[0], "Ahoj", "The right string");