jaroslav@1170
|
1 |
/**
|
jaroslav@1170
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1170
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1170
|
4 |
*
|
jaroslav@1170
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1170
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1170
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1170
|
8 |
*
|
jaroslav@1170
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1170
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1170
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1170
|
12 |
* GNU General Public License for more details.
|
jaroslav@1170
|
13 |
*
|
jaroslav@1170
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1170
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1170
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1170
|
17 |
*/
|
jaroslav@1170
|
18 |
package org.apidesign.bck2brwsr.launcher.fximpl;
|
jaroslav@1170
|
19 |
|
jaroslav@1227
|
20 |
import java.io.Reader;
|
jaroslav@1170
|
21 |
import java.lang.reflect.InvocationTargetException;
|
jaroslav@1170
|
22 |
import java.lang.reflect.Method;
|
jaroslav@1170
|
23 |
import java.net.URL;
|
jaroslav@1174
|
24 |
import java.net.URLClassLoader;
|
jaroslav@1182
|
25 |
import java.util.ArrayList;
|
jaroslav@1182
|
26 |
import java.util.Arrays;
|
jaroslav@1184
|
27 |
import java.util.Collection;
|
jaroslav@1182
|
28 |
import java.util.List;
|
jaroslav@1170
|
29 |
import javax.script.Invocable;
|
jaroslav@1170
|
30 |
import javax.script.ScriptEngine;
|
jaroslav@1170
|
31 |
import javax.script.ScriptEngineManager;
|
jaroslav@1170
|
32 |
import javax.script.ScriptException;
|
jaroslav@1184
|
33 |
import org.apidesign.html.boot.spi.Fn;
|
jaroslav@1418
|
34 |
import org.netbeans.html.boot.impl.FindResources;
|
jaroslav@1418
|
35 |
import org.netbeans.html.boot.impl.FnUtils;
|
jaroslav@1170
|
36 |
import static org.testng.Assert.*;
|
jaroslav@1170
|
37 |
import org.testng.annotations.BeforeClass;
|
jaroslav@1282
|
38 |
import org.testng.annotations.BeforeMethod;
|
jaroslav@1170
|
39 |
import org.testng.annotations.Test;
|
jaroslav@1170
|
40 |
|
jaroslav@1170
|
41 |
/**
|
jaroslav@1170
|
42 |
*
|
jaroslav@1170
|
43 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1170
|
44 |
*/
|
jaroslav@1170
|
45 |
public class JsClassLoaderTest {
|
jaroslav@1170
|
46 |
private static ClassLoader loader;
|
jaroslav@1170
|
47 |
private static Class<?> methodClass;
|
jaroslav@1282
|
48 |
private static Fn.Presenter presenter;
|
jaroslav@1170
|
49 |
|
jaroslav@1170
|
50 |
public JsClassLoaderTest() {
|
jaroslav@1170
|
51 |
}
|
jaroslav@1170
|
52 |
|
jaroslav@1170
|
53 |
@BeforeClass
|
jaroslav@1170
|
54 |
public static void setUpClass() throws Exception {
|
jaroslav@1170
|
55 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@1170
|
56 |
final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
|
jaroslav@1170
|
57 |
|
jaroslav@1174
|
58 |
final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
|
jaroslav@1170
|
59 |
ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
|
jaroslav@1174
|
60 |
final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
|
jaroslav@1184
|
61 |
class Fr implements FindResources, Fn.Presenter {
|
jaroslav@1174
|
62 |
@Override
|
jaroslav@1184
|
63 |
public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
|
jaroslav@1184
|
64 |
URL u = ul.getResource(path);
|
jaroslav@1184
|
65 |
if (u != null) {
|
jaroslav@1184
|
66 |
results.add(u);
|
jaroslav@1184
|
67 |
}
|
jaroslav@1174
|
68 |
}
|
jaroslav@1184
|
69 |
|
jaroslav@1170
|
70 |
@Override
|
jaroslav@1184
|
71 |
public Fn defineFn(String code, String... names) {
|
jaroslav@1170
|
72 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1170
|
73 |
sb.append("(function() {");
|
jaroslav@1182
|
74 |
sb.append("return function(");
|
jaroslav@1170
|
75 |
String sep = "";
|
jaroslav@1170
|
76 |
for (String n : names) {
|
jaroslav@1170
|
77 |
sb.append(sep);
|
jaroslav@1170
|
78 |
sb.append(n);
|
jaroslav@1170
|
79 |
sep = ", ";
|
jaroslav@1170
|
80 |
}
|
jaroslav@1170
|
81 |
sb.append(") {");
|
jaroslav@1170
|
82 |
sb.append(code);
|
jaroslav@1170
|
83 |
sb.append("};");
|
jaroslav@1170
|
84 |
sb.append("})()");
|
jaroslav@1170
|
85 |
try {
|
jaroslav@1170
|
86 |
final Object val = eng.eval(sb.toString());
|
jaroslav@1282
|
87 |
return new Fn(this) {
|
jaroslav@1170
|
88 |
@Override
|
jaroslav@1181
|
89 |
public Object invoke(Object thiz, Object... args) throws Exception {
|
jaroslav@1182
|
90 |
List<Object> all = new ArrayList<Object>(args.length + 1);
|
jaroslav@1182
|
91 |
all.add(thiz == null ? val : thiz);
|
jaroslav@1182
|
92 |
all.addAll(Arrays.asList(args));
|
jaroslav@1170
|
93 |
Invocable inv = (Invocable)eng;
|
jaroslav@1182
|
94 |
Object ret = inv.invokeMethod(val, "call", all.toArray());
|
jaroslav@1393
|
95 |
return val.equals(ret) ? null : ret;
|
jaroslav@1170
|
96 |
}
|
jaroslav@1170
|
97 |
};
|
jaroslav@1170
|
98 |
} catch (ScriptException ex) {
|
jaroslav@1170
|
99 |
throw new LinkageError("Can't parse: " + sb, ex);
|
jaroslav@1170
|
100 |
}
|
jaroslav@1170
|
101 |
}
|
jaroslav@1175
|
102 |
|
jaroslav@1175
|
103 |
@Override
|
jaroslav@1184
|
104 |
public void displayPage(URL page, Runnable onPageLoad) {
|
jaroslav@1175
|
105 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
jaroslav@1175
|
106 |
}
|
jaroslav@1227
|
107 |
|
jaroslav@1227
|
108 |
@Override
|
jaroslav@1227
|
109 |
public void loadScript(Reader code) throws Exception {
|
jaroslav@1227
|
110 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
jaroslav@1227
|
111 |
}
|
jaroslav@1184
|
112 |
}
|
jaroslav@1170
|
113 |
|
jaroslav@1282
|
114 |
Fr fr = new Fr();
|
jaroslav@1282
|
115 |
presenter = fr;
|
jaroslav@1282
|
116 |
loader = FnUtils.newLoader(fr, fr, parent);
|
jaroslav@1170
|
117 |
methodClass = loader.loadClass(JsMethods.class.getName());
|
jaroslav@1170
|
118 |
}
|
jaroslav@1170
|
119 |
|
jaroslav@1282
|
120 |
@BeforeMethod public void registerPresenter() {
|
jaroslav@1418
|
121 |
Fn.activate(presenter);
|
jaroslav@1282
|
122 |
}
|
jaroslav@1282
|
123 |
|
jaroslav@1170
|
124 |
@Test public void noParamMethod() throws Throwable {
|
jaroslav@1170
|
125 |
Method plus = methodClass.getMethod("fortyTwo");
|
jaroslav@1170
|
126 |
try {
|
jaroslav@1170
|
127 |
final Object val = plus.invoke(null);
|
jaroslav@1170
|
128 |
assertTrue(val instanceof Number, "A number returned " + val);
|
jaroslav@1170
|
129 |
assertEquals(((Number)val).intValue(), 42);
|
jaroslav@1170
|
130 |
} catch (InvocationTargetException ex) {
|
jaroslav@1170
|
131 |
throw ex.getTargetException();
|
jaroslav@1170
|
132 |
}
|
jaroslav@1170
|
133 |
}
|
jaroslav@1172
|
134 |
|
jaroslav@1170
|
135 |
@Test public void testExecuteScript() throws Throwable {
|
jaroslav@1170
|
136 |
Method plus = methodClass.getMethod("plus", int.class, int.class);
|
jaroslav@1170
|
137 |
try {
|
jaroslav@1170
|
138 |
assertEquals(plus.invoke(null, 10, 20), 30);
|
jaroslav@1170
|
139 |
} catch (InvocationTargetException ex) {
|
jaroslav@1170
|
140 |
throw ex.getTargetException();
|
jaroslav@1170
|
141 |
}
|
jaroslav@1170
|
142 |
}
|
jaroslav@1178
|
143 |
|
jaroslav@1178
|
144 |
@Test public void overloadedMethod() throws Throwable {
|
jaroslav@1178
|
145 |
Method plus = methodClass.getMethod("plus", int.class);
|
jaroslav@1178
|
146 |
try {
|
jaroslav@1178
|
147 |
assertEquals(plus.invoke(null, 10), 10);
|
jaroslav@1178
|
148 |
} catch (InvocationTargetException ex) {
|
jaroslav@1178
|
149 |
throw ex.getTargetException();
|
jaroslav@1178
|
150 |
}
|
jaroslav@1178
|
151 |
}
|
jaroslav@1181
|
152 |
|
jaroslav@1181
|
153 |
@Test public void instanceMethod() throws Throwable {
|
jaroslav@1181
|
154 |
Method plus = methodClass.getMethod("plusInst", int.class);
|
jaroslav@1181
|
155 |
Object inst = methodClass.newInstance();
|
jaroslav@1181
|
156 |
try {
|
jaroslav@1181
|
157 |
assertEquals(plus.invoke(inst, 10), 10);
|
jaroslav@1181
|
158 |
} catch (InvocationTargetException ex) {
|
jaroslav@1181
|
159 |
throw ex.getTargetException();
|
jaroslav@1181
|
160 |
}
|
jaroslav@1181
|
161 |
}
|
jaroslav@1181
|
162 |
|
jaroslav@1181
|
163 |
@Test public void staticThis() throws Throwable {
|
jaroslav@1181
|
164 |
Method st = methodClass.getMethod("staticThis");
|
jaroslav@1181
|
165 |
try {
|
jaroslav@1181
|
166 |
assertNull(st.invoke(null));
|
jaroslav@1181
|
167 |
} catch (InvocationTargetException ex) {
|
jaroslav@1181
|
168 |
throw ex.getTargetException();
|
jaroslav@1181
|
169 |
}
|
jaroslav@1181
|
170 |
}
|
jaroslav@1181
|
171 |
|
jaroslav@1181
|
172 |
@Test public void getThis() throws Throwable {
|
jaroslav@1181
|
173 |
Object th = methodClass.newInstance();
|
jaroslav@1181
|
174 |
Method st = methodClass.getMethod("getThis");
|
jaroslav@1181
|
175 |
try {
|
jaroslav@1181
|
176 |
assertEquals(st.invoke(th), th);
|
jaroslav@1181
|
177 |
} catch (InvocationTargetException ex) {
|
jaroslav@1181
|
178 |
throw ex.getTargetException();
|
jaroslav@1181
|
179 |
}
|
jaroslav@1181
|
180 |
}
|
jaroslav@1181
|
181 |
|
jaroslav@1170
|
182 |
} |