下面列出了javax.script.ScriptException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
/**
* Test that we can use same script name in repeated evals with --loader-per-compile=false
* We used to get "class redefinition error" as name was derived from script name.
*/
public void noLoaderPerCompilerWithSameNameTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
e.put(ScriptEngine.FILENAME, "test.js");
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
public void factoryClassLoaderTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final MyClassLoader loader = new MyClassLoader();
// set the classloader as app class loader
final ScriptEngine e = nfac.getScriptEngine(loader);
try {
e.eval("Packages.foo");
// check that the class loader was attempted
assertTrue(loader.reached(), "did not reach class loader!");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
/**
* Test repeated evals with --loader-per-compile=false
* We used to get "class redefinition error".
*/
public void noLoaderPerCompilerTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
/**
* Test repeated evals with --loader-per-compile=false
* We used to get "class redefinition error".
*/
public void noLoaderPerCompilerTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
public void putGlobalFunctionTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
e.put("callable", new Callable<String>() {
@Override
public String call() throws Exception {
return "callable was called";
}
});
try {
e.eval("print(callable.call())");
} catch (final ScriptException exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
private static void prettyPrintFile(File file) throws IOException {
Path path = Paths.get(file.getPath());
String content = new String(Files.readAllBytes(path));
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine scriptEngine = manager.getEngineByName("JavaScript");
scriptEngine.put("jsonString", content);
try {
scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, \"\t\")");
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String prettyPrintedJson = (String) scriptEngine.get("result");
Files.write(path, prettyPrintedJson.getBytes());
}
@Test
/**
* Test that we can use same script name in repeated evals with --loader-per-compile=false
* We used to get "class redefinition error" as name was derived from script name.
*/
public void noLoaderPerCompilerWithSameNameTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
e.put(ScriptEngine.FILENAME, "test.js");
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
/**
* Test repeated evals with --loader-per-compile=false
* We used to get "class redefinition error".
*/
public void noLoaderPerCompilerTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
/**
* Test repeated evals with --loader-per-compile=false
* We used to get "class redefinition error".
*/
public void noLoaderPerCompilerTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (final ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "--loader-per-compile=false" };
final ScriptEngine e = nfac.getScriptEngine(options);
try {
e.eval("2 + 3");
e.eval("4 + 4");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
public void accessGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.eval("var x = 'hello'");
assertEquals(e.get("x"), "hello");
} catch (final ScriptException exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
@Test
public void accessGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.eval("var x = 'hello'");
assertEquals(e.get("x"), "hello");
} catch (final ScriptException exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
private static int calc(String verifyCode) {
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
return (int) engine.eval(verifyCode);
} catch (ScriptException e) {
e.printStackTrace();
return 0;
}
}
@Test
public void exposeGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.put("y", "foo");
e.eval("print(y)");
} catch (final ScriptException exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
@Test
public void accessGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.eval("var x = 'hello'");
assertEquals(e.get("x"), "hello");
} catch (final ScriptException exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
/**
* 生成随机验证码
*
* @return 验证码字符数组
*/
@Override
protected char[] alphas() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
sb.append(num(10));
if (i < len - 1) {
int type = num(1, 4);
if (type == 1) {
sb.append("+");
} else if (type == 2) {
sb.append("-");
} else if (type == 3) {
sb.append("x");
}
}
}
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
try {
chars = String.valueOf(engine.eval(sb.toString().replaceAll("x", "*")));
} catch (ScriptException e) {
e.printStackTrace();
}
sb.append("=?");
arithmeticString = sb.toString();
return chars.toCharArray();
}
public static ScriptEngine create() {
ScriptEngineManager sem = new ScriptEngineManager(); // creation of an engine manager
ScriptEngine engine = sem.getEngineByName(ENGINE_NAME); // engine creation based on nashorn
try {
InputStream stream = ScriptEngineFactory.class.getResourceAsStream(MOMENT_JS_SRC);
InputStreamReader momentJS = new InputStreamReader(stream);
engine.eval(momentJS); // evaluating moment.js
} catch (ScriptException se) {
se.printStackTrace();
}
return engine;
}
@Test
public void multiGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Bindings b = e.createBindings();
final ScriptContext newCtxt = new SimpleScriptContext();
newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
try {
final Object obj1 = e.eval("Object");
final Object obj2 = e.eval("Object", newCtxt);
Assert.assertNotEquals(obj1, obj2);
Assert.assertNotNull(obj1);
Assert.assertNotNull(obj2);
Assert.assertEquals(obj1.toString(), obj2.toString());
e.eval("x = 'hello'");
e.eval("x = 'world'", newCtxt);
Object x1 = e.getContext().getAttribute("x");
Object x2 = newCtxt.getAttribute("x");
Assert.assertNotEquals(x1, x2);
Assert.assertEquals(x1, "hello");
Assert.assertEquals(x2, "world");
x1 = e.eval("x");
x2 = e.eval("x", newCtxt);
Assert.assertNotEquals(x1, x2);
Assert.assertEquals(x1, "hello");
Assert.assertEquals(x2, "world");
final ScriptContext origCtxt = e.getContext();
e.setContext(newCtxt);
e.eval("y = new Object()");
e.eval("y = new Object()", origCtxt);
final Object y1 = origCtxt.getAttribute("y");
final Object y2 = newCtxt.getAttribute("y");
Assert.assertNotEquals(y1, y2);
final Object yeval1 = e.eval("y");
final Object yeval2 = e.eval("y", origCtxt);
Assert.assertNotEquals(yeval1, yeval2);
Assert.assertEquals("[object Object]", y1.toString());
Assert.assertEquals("[object Object]", y2.toString());
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
}
@Test
public void multiGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Bindings b = e.createBindings();
final ScriptContext newCtxt = new SimpleScriptContext();
newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
try {
Object obj1 = e.eval("Object");
Object obj2 = e.eval("Object", newCtxt);
Assert.assertNotEquals(obj1, obj2);
Assert.assertNotNull(obj1);
Assert.assertNotNull(obj2);
Assert.assertEquals(obj1.toString(), obj2.toString());
e.eval("x = 'hello'");
e.eval("x = 'world'", newCtxt);
Object x1 = e.getContext().getAttribute("x");
Object x2 = newCtxt.getAttribute("x");
Assert.assertNotEquals(x1, x2);
Assert.assertEquals(x1, "hello");
Assert.assertEquals(x2, "world");
x1 = e.eval("x");
x2 = e.eval("x", newCtxt);
Assert.assertNotEquals(x1, x2);
Assert.assertEquals(x1, "hello");
Assert.assertEquals(x2, "world");
final ScriptContext origCtxt = e.getContext();
e.setContext(newCtxt);
e.eval("y = new Object()");
e.eval("y = new Object()", origCtxt);
Object y1 = origCtxt.getAttribute("y");
Object y2 = newCtxt.getAttribute("y");
Assert.assertNotEquals(y1, y2);
Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt));
Assert.assertEquals("[object Object]", y1.toString());
Assert.assertEquals("[object Object]", y2.toString());
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
}
@Test
public void multiGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Bindings b = e.createBindings();
final ScriptContext newCtxt = new SimpleScriptContext();
newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
try {
final Object obj1 = e.eval("Object");
final Object obj2 = e.eval("Object", newCtxt);
Assert.assertNotEquals(obj1, obj2);
Assert.assertNotNull(obj1);
Assert.assertNotNull(obj2);
Assert.assertEquals(obj1.toString(), obj2.toString());
e.eval("x = 'hello'");
e.eval("x = 'world'", newCtxt);
Object x1 = e.getContext().getAttribute("x");
Object x2 = newCtxt.getAttribute("x");
Assert.assertNotEquals(x1, x2);
Assert.assertEquals(x1, "hello");
Assert.assertEquals(x2, "world");
x1 = e.eval("x");
x2 = e.eval("x", newCtxt);
Assert.assertNotEquals(x1, x2);
Assert.assertEquals(x1, "hello");
Assert.assertEquals(x2, "world");
final ScriptContext origCtxt = e.getContext();
e.setContext(newCtxt);
e.eval("y = new Object()");
e.eval("y = new Object()", origCtxt);
final Object y1 = origCtxt.getAttribute("y");
final Object y2 = newCtxt.getAttribute("y");
Assert.assertNotEquals(y1, y2);
final Object yeval1 = e.eval("y");
final Object yeval2 = e.eval("y", origCtxt);
Assert.assertNotEquals(yeval1, yeval2);
Assert.assertEquals("[object Object]", y1.toString());
Assert.assertEquals("[object Object]", y2.toString());
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
}
@Test
public void multiGlobalTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
final Bindings b = e.createBindings();
final ScriptContext newCtxt = new SimpleScriptContext();
newCtxt.setBindings(b, ScriptContext.ENGINE_SCOPE);
try {
Object obj1 = e.eval("Object");
Object obj2 = e.eval("Object", newCtxt);
Assert.assertNotEquals(obj1, obj2);
Assert.assertNotNull(obj1);
Assert.assertNotNull(obj2);
Assert.assertEquals(obj1.toString(), obj2.toString());
e.eval("x = 'hello'");
e.eval("x = 'world'", newCtxt);
Object x1 = e.getContext().getAttribute("x");
Object x2 = newCtxt.getAttribute("x");
Assert.assertNotEquals(x1, x2);
Assert.assertEquals(x1, "hello");
Assert.assertEquals(x2, "world");
x1 = e.eval("x");
x2 = e.eval("x", newCtxt);
Assert.assertNotEquals(x1, x2);
Assert.assertEquals(x1, "hello");
Assert.assertEquals(x2, "world");
final ScriptContext origCtxt = e.getContext();
e.setContext(newCtxt);
e.eval("y = new Object()");
e.eval("y = new Object()", origCtxt);
Object y1 = origCtxt.getAttribute("y");
Object y2 = newCtxt.getAttribute("y");
Assert.assertNotEquals(y1, y2);
Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt));
Assert.assertEquals("[object Object]", y1.toString());
Assert.assertEquals("[object Object]", y2.toString());
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
}