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@1170
|
23 |
import javax.script.Invocable;
|
jaroslav@1170
|
24 |
import javax.script.ScriptEngine;
|
jaroslav@1170
|
25 |
import javax.script.ScriptEngineManager;
|
jaroslav@1170
|
26 |
import javax.script.ScriptException;
|
jaroslav@1170
|
27 |
import static org.testng.Assert.*;
|
jaroslav@1170
|
28 |
import org.testng.annotations.BeforeClass;
|
jaroslav@1170
|
29 |
import org.testng.annotations.Test;
|
jaroslav@1170
|
30 |
|
jaroslav@1170
|
31 |
/**
|
jaroslav@1170
|
32 |
*
|
jaroslav@1170
|
33 |
* @author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1170
|
34 |
*/
|
jaroslav@1170
|
35 |
public class JsClassLoaderTest {
|
jaroslav@1170
|
36 |
private static ClassLoader loader;
|
jaroslav@1170
|
37 |
private static Class<?> methodClass;
|
jaroslav@1170
|
38 |
|
jaroslav@1170
|
39 |
public JsClassLoaderTest() {
|
jaroslav@1170
|
40 |
}
|
jaroslav@1170
|
41 |
|
jaroslav@1170
|
42 |
@BeforeClass
|
jaroslav@1170
|
43 |
public static void setUpClass() throws Exception {
|
jaroslav@1170
|
44 |
ScriptEngineManager sem = new ScriptEngineManager();
|
jaroslav@1170
|
45 |
final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
|
jaroslav@1170
|
46 |
|
jaroslav@1170
|
47 |
URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
|
jaroslav@1170
|
48 |
ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
|
jaroslav@1170
|
49 |
loader = new JsClassLoader(new URL[] { my }, parent) {
|
jaroslav@1170
|
50 |
@Override
|
jaroslav@1170
|
51 |
protected JsClassLoader.Fn defineFn(String code, String... names) {
|
jaroslav@1170
|
52 |
StringBuilder sb = new StringBuilder();
|
jaroslav@1170
|
53 |
sb.append("(function() {");
|
jaroslav@1170
|
54 |
sb.append("var r = {};");
|
jaroslav@1170
|
55 |
sb.append("r.fn = function(");
|
jaroslav@1170
|
56 |
String sep = "";
|
jaroslav@1170
|
57 |
for (String n : names) {
|
jaroslav@1170
|
58 |
sb.append(sep);
|
jaroslav@1170
|
59 |
sb.append(n);
|
jaroslav@1170
|
60 |
sep = ", ";
|
jaroslav@1170
|
61 |
}
|
jaroslav@1170
|
62 |
sb.append(") {");
|
jaroslav@1170
|
63 |
sb.append(code);
|
jaroslav@1170
|
64 |
sb.append("};");
|
jaroslav@1170
|
65 |
sb.append("return r;");
|
jaroslav@1170
|
66 |
sb.append("})()");
|
jaroslav@1170
|
67 |
try {
|
jaroslav@1170
|
68 |
final Object val = eng.eval(sb.toString());
|
jaroslav@1170
|
69 |
return new JsClassLoader.Fn() {
|
jaroslav@1170
|
70 |
@Override
|
jaroslav@1170
|
71 |
public Object invoke(Object... args) throws Exception {
|
jaroslav@1170
|
72 |
Invocable inv = (Invocable)eng;
|
jaroslav@1170
|
73 |
return inv.invokeMethod(val, "fn", args);
|
jaroslav@1170
|
74 |
}
|
jaroslav@1170
|
75 |
};
|
jaroslav@1170
|
76 |
} catch (ScriptException ex) {
|
jaroslav@1170
|
77 |
throw new LinkageError("Can't parse: " + sb, ex);
|
jaroslav@1170
|
78 |
}
|
jaroslav@1170
|
79 |
}
|
jaroslav@1170
|
80 |
};
|
jaroslav@1170
|
81 |
|
jaroslav@1170
|
82 |
methodClass = loader.loadClass(JsMethods.class.getName());
|
jaroslav@1170
|
83 |
}
|
jaroslav@1170
|
84 |
|
jaroslav@1170
|
85 |
@Test public void noParamMethod() throws Throwable {
|
jaroslav@1170
|
86 |
Method plus = methodClass.getMethod("fortyTwo");
|
jaroslav@1170
|
87 |
try {
|
jaroslav@1170
|
88 |
final Object val = plus.invoke(null);
|
jaroslav@1170
|
89 |
assertTrue(val instanceof Number, "A number returned " + val);
|
jaroslav@1170
|
90 |
assertEquals(((Number)val).intValue(), 42);
|
jaroslav@1170
|
91 |
} catch (InvocationTargetException ex) {
|
jaroslav@1170
|
92 |
throw ex.getTargetException();
|
jaroslav@1170
|
93 |
}
|
jaroslav@1170
|
94 |
}
|
jaroslav@1170
|
95 |
/*
|
jaroslav@1170
|
96 |
@Test public void testExecuteScript() throws Throwable {
|
jaroslav@1170
|
97 |
Method plus = methodClass.getMethod("plus", int.class, int.class);
|
jaroslav@1170
|
98 |
try {
|
jaroslav@1170
|
99 |
assertEquals(plus.invoke(null, 10, 20), 30);
|
jaroslav@1170
|
100 |
} catch (InvocationTargetException ex) {
|
jaroslav@1170
|
101 |
throw ex.getTargetException();
|
jaroslav@1170
|
102 |
}
|
jaroslav@1170
|
103 |
}
|
jaroslav@1170
|
104 |
*/
|
jaroslav@1170
|
105 |
|
jaroslav@1170
|
106 |
} |