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@1170
|
20 |
import java.lang.reflect.InvocationTargetException;
|
jaroslav@1170
|
21 |
import java.lang.reflect.Method;
|
jaroslav@1170
|
22 |
import java.net.URL;
|
jaroslav@1174
|
23 |
import java.net.URLClassLoader;
|
jaroslav@1175
|
24 |
import java.util.Enumeration;
|
jaroslav@1170
|
25 |
import javax.script.Invocable;
|
jaroslav@1170
|
26 |
import javax.script.ScriptEngine;
|
jaroslav@1170
|
27 |
import javax.script.ScriptEngineManager;
|
jaroslav@1170
|
28 |
import javax.script.ScriptException;
|
jaroslav@1172
|
29 |
import org.objectweb.asm.signature.SignatureReader;
|
jaroslav@1170
|
30 |
import static org.testng.Assert.*;
|
jaroslav@1170
|
31 |
import org.testng.annotations.BeforeClass;
|
jaroslav@1170
|
32 |
import org.testng.annotations.Test;
|
jaroslav@1170
|
33 |
|
jaroslav@1170
|
34 |
/**
|
jaroslav@1170
|
35 |
*
|
jaroslav@1170
|
36 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1170
|
37 |
*/
|
jaroslav@1170
|
38 |
public class JsClassLoaderTest {
|
jaroslav@1170
|
39 |
private static ClassLoader loader;
|
jaroslav@1170
|
40 |
private static Class<?> methodClass;
|
jaroslav@1170
|
41 |
|
jaroslav@1170
|
42 |
public JsClassLoaderTest() {
|
jaroslav@1170
|
43 |
}
|
jaroslav@1170
|
44 |
|
jaroslav@1170
|
45 |
@BeforeClass
|
jaroslav@1170
|
46 |
public static void setUpClass() throws Exception {
|
jaroslav@1170
|
47 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@1170
|
48 |
final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
|
jaroslav@1170
|
49 |
|
jaroslav@1174
|
50 |
final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
|
jaroslav@1170
|
51 |
ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
|
jaroslav@1174
|
52 |
final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
|
jaroslav@1174
|
53 |
loader = new JsClassLoader(parent) {
|
jaroslav@1174
|
54 |
@Override
|
jaroslav@1174
|
55 |
protected URL findResource(String name) {
|
jaroslav@1174
|
56 |
return ul.getResource(name);
|
jaroslav@1174
|
57 |
}
|
jaroslav@1170
|
58 |
@Override
|
jaroslav@1171
|
59 |
protected Fn defineFn(String code, String... names) {
|
jaroslav@1170
|
60 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1170
|
61 |
sb.append("(function() {");
|
jaroslav@1170
|
62 |
sb.append("var r = {};");
|
jaroslav@1170
|
63 |
sb.append("r.fn = function(");
|
jaroslav@1170
|
64 |
String sep = "";
|
jaroslav@1170
|
65 |
for (String n : names) {
|
jaroslav@1170
|
66 |
sb.append(sep);
|
jaroslav@1170
|
67 |
sb.append(n);
|
jaroslav@1170
|
68 |
sep = ", ";
|
jaroslav@1170
|
69 |
}
|
jaroslav@1170
|
70 |
sb.append(") {");
|
jaroslav@1170
|
71 |
sb.append(code);
|
jaroslav@1170
|
72 |
sb.append("};");
|
jaroslav@1170
|
73 |
sb.append("return r;");
|
jaroslav@1170
|
74 |
sb.append("})()");
|
jaroslav@1170
|
75 |
try {
|
jaroslav@1170
|
76 |
final Object val = eng.eval(sb.toString());
|
jaroslav@1171
|
77 |
return new Fn() {
|
jaroslav@1170
|
78 |
@Override
|
jaroslav@1181
|
79 |
public Object invoke(Object thiz, Object... args) throws Exception {
|
jaroslav@1170
|
80 |
Invocable inv = (Invocable)eng;
|
jaroslav@1170
|
81 |
return inv.invokeMethod(val, "fn", args);
|
jaroslav@1170
|
82 |
}
|
jaroslav@1170
|
83 |
};
|
jaroslav@1170
|
84 |
} catch (ScriptException ex) {
|
jaroslav@1170
|
85 |
throw new LinkageError("Can't parse: " + sb, ex);
|
jaroslav@1170
|
86 |
}
|
jaroslav@1170
|
87 |
}
|
jaroslav@1175
|
88 |
|
jaroslav@1175
|
89 |
@Override
|
jaroslav@1175
|
90 |
protected Enumeration<URL> findResources(String name) {
|
jaroslav@1175
|
91 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
jaroslav@1175
|
92 |
}
|
jaroslav@1170
|
93 |
};
|
jaroslav@1170
|
94 |
|
jaroslav@1170
|
95 |
methodClass = loader.loadClass(JsMethods.class.getName());
|
jaroslav@1170
|
96 |
}
|
jaroslav@1170
|
97 |
|
jaroslav@1170
|
98 |
@Test public void noParamMethod() throws Throwable {
|
jaroslav@1170
|
99 |
Method plus = methodClass.getMethod("fortyTwo");
|
jaroslav@1170
|
100 |
try {
|
jaroslav@1170
|
101 |
final Object val = plus.invoke(null);
|
jaroslav@1170
|
102 |
assertTrue(val instanceof Number, "A number returned " + val);
|
jaroslav@1170
|
103 |
assertEquals(((Number)val).intValue(), 42);
|
jaroslav@1170
|
104 |
} catch (InvocationTargetException ex) {
|
jaroslav@1170
|
105 |
throw ex.getTargetException();
|
jaroslav@1170
|
106 |
}
|
jaroslav@1170
|
107 |
}
|
jaroslav@1172
|
108 |
|
jaroslav@1170
|
109 |
@Test public void testExecuteScript() throws Throwable {
|
jaroslav@1170
|
110 |
Method plus = methodClass.getMethod("plus", int.class, int.class);
|
jaroslav@1170
|
111 |
try {
|
jaroslav@1170
|
112 |
assertEquals(plus.invoke(null, 10, 20), 30);
|
jaroslav@1170
|
113 |
} catch (InvocationTargetException ex) {
|
jaroslav@1170
|
114 |
throw ex.getTargetException();
|
jaroslav@1170
|
115 |
}
|
jaroslav@1170
|
116 |
}
|
jaroslav@1178
|
117 |
|
jaroslav@1178
|
118 |
@Test public void overloadedMethod() throws Throwable {
|
jaroslav@1178
|
119 |
Method plus = methodClass.getMethod("plus", int.class);
|
jaroslav@1178
|
120 |
try {
|
jaroslav@1178
|
121 |
assertEquals(plus.invoke(null, 10), 10);
|
jaroslav@1178
|
122 |
} catch (InvocationTargetException ex) {
|
jaroslav@1178
|
123 |
throw ex.getTargetException();
|
jaroslav@1178
|
124 |
}
|
jaroslav@1178
|
125 |
}
|
jaroslav@1181
|
126 |
|
jaroslav@1181
|
127 |
@Test public void instanceMethod() throws Throwable {
|
jaroslav@1181
|
128 |
Method plus = methodClass.getMethod("plusInst", int.class);
|
jaroslav@1181
|
129 |
Object inst = methodClass.newInstance();
|
jaroslav@1181
|
130 |
try {
|
jaroslav@1181
|
131 |
assertEquals(plus.invoke(inst, 10), 10);
|
jaroslav@1181
|
132 |
} catch (InvocationTargetException ex) {
|
jaroslav@1181
|
133 |
throw ex.getTargetException();
|
jaroslav@1181
|
134 |
}
|
jaroslav@1181
|
135 |
}
|
jaroslav@1181
|
136 |
|
jaroslav@1181
|
137 |
@Test public void staticThis() throws Throwable {
|
jaroslav@1181
|
138 |
Method st = methodClass.getMethod("staticThis");
|
jaroslav@1181
|
139 |
try {
|
jaroslav@1181
|
140 |
assertNull(st.invoke(null));
|
jaroslav@1181
|
141 |
} catch (InvocationTargetException ex) {
|
jaroslav@1181
|
142 |
throw ex.getTargetException();
|
jaroslav@1181
|
143 |
}
|
jaroslav@1181
|
144 |
}
|
jaroslav@1181
|
145 |
|
jaroslav@1181
|
146 |
@Test public void getThis() throws Throwable {
|
jaroslav@1181
|
147 |
Object th = methodClass.newInstance();
|
jaroslav@1181
|
148 |
Method st = methodClass.getMethod("getThis");
|
jaroslav@1181
|
149 |
try {
|
jaroslav@1181
|
150 |
assertEquals(st.invoke(th), th);
|
jaroslav@1181
|
151 |
} catch (InvocationTargetException ex) {
|
jaroslav@1181
|
152 |
throw ex.getTargetException();
|
jaroslav@1181
|
153 |
}
|
jaroslav@1181
|
154 |
}
|
jaroslav@1181
|
155 |
|
jaroslav@1170
|
156 |
} |