test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java

Print this page




 954             Assert.assertNotEquals(x1, x2);
 955             Assert.assertEquals(x1, "hello");
 956             Assert.assertEquals(x2, "world");
 957 
 958             final ScriptContext origCtxt = e.getContext();
 959             e.setContext(newCtxt);
 960             e.eval("y = new Object()");
 961             e.eval("y = new Object()", origCtxt);
 962 
 963             Object y1 = origCtxt.getAttribute("y");
 964             Object y2 = newCtxt.getAttribute("y");
 965             Assert.assertNotEquals(y1, y2);
 966             Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt));
 967             Assert.assertEquals("[object Object]", y1.toString());
 968             Assert.assertEquals("[object Object]", y2.toString());
 969         } catch (final ScriptException se) {
 970             se.printStackTrace();
 971             fail(se.getMessage());
 972         }
 973     }
































































































 974 }


 954             Assert.assertNotEquals(x1, x2);
 955             Assert.assertEquals(x1, "hello");
 956             Assert.assertEquals(x2, "world");
 957 
 958             final ScriptContext origCtxt = e.getContext();
 959             e.setContext(newCtxt);
 960             e.eval("y = new Object()");
 961             e.eval("y = new Object()", origCtxt);
 962 
 963             Object y1 = origCtxt.getAttribute("y");
 964             Object y2 = newCtxt.getAttribute("y");
 965             Assert.assertNotEquals(y1, y2);
 966             Assert.assertNotEquals(e.eval("y"), e.eval("y", origCtxt));
 967             Assert.assertEquals("[object Object]", y1.toString());
 968             Assert.assertEquals("[object Object]", y2.toString());
 969         } catch (final ScriptException se) {
 970             se.printStackTrace();
 971             fail(se.getMessage());
 972         }
 973     }
 974 
 975     private static class MyClassLoader extends ClassLoader {
 976         // to check if script engine uses the specified class loader
 977         private final boolean[] reached = new boolean[1];
 978 
 979         @Override
 980         protected Class findClass(final String name) throws ClassNotFoundException {
 981             // flag that it reached here
 982             reached[0] = true;
 983             return super.findClass(name);
 984         }
 985 
 986         public boolean reached() {
 987             return reached[0];
 988         }
 989     };
 990 
 991     @Test
 992     public void factoryClassLoaderTest() {
 993         final ScriptEngineManager sm = new ScriptEngineManager();
 994         for (ScriptEngineFactory fac : sm.getEngineFactories()) {
 995             if (fac instanceof NashornScriptEngineFactory) {
 996                 final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
 997                 final MyClassLoader loader = new MyClassLoader();
 998                 // set the classloader as app class loader
 999                 final ScriptEngine e = nfac.getScriptEngine(loader);
1000                 try {
1001                     e.eval("Packages.foo");
1002                     // check that the class loader was attempted
1003                     assertTrue(loader.reached(), "did not reach class loader!");
1004                 } catch (final ScriptException se) {
1005                     se.printStackTrace();
1006                     fail(se.getMessage());
1007                 }
1008                 return;
1009             }
1010         }
1011 
1012         fail("Cannot find nashorn factory!");
1013     }
1014 
1015     @Test
1016     public void factoryOptionsTest() {
1017         final ScriptEngineManager sm = new ScriptEngineManager();
1018         for (ScriptEngineFactory fac : sm.getEngineFactories()) {
1019             if (fac instanceof NashornScriptEngineFactory) {
1020                 final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
1021                 // specify --no-syntax-extensions flag
1022                 final String[] options = new String[] { "--no-syntax-extensions" };
1023                 final ScriptEngine e = nfac.getScriptEngine(options);
1024                 try {
1025                     // try nashorn specific extension
1026                     e.eval("var f = funtion(x) 2*x;");
1027                     fail("should have thrown exception!");
1028                 } catch (final ScriptException se) {
1029                 }
1030                 return;
1031             }
1032         }
1033 
1034         fail("Cannot find nashorn factory!");
1035     }
1036 
1037     @Test
1038     public void factoryClassLoaderAndOptionsTest() {
1039         final ScriptEngineManager sm = new ScriptEngineManager();
1040         for (ScriptEngineFactory fac : sm.getEngineFactories()) {
1041             if (fac instanceof NashornScriptEngineFactory) {
1042                 final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
1043                 final String[] options = new String[] { "-strict" };
1044                 final MyClassLoader loader = new MyClassLoader();
1045                 // set the classloader as app class loader
1046                 final ScriptEngine e = nfac.getScriptEngine(options, loader);
1047                 try {
1048                     e.eval("Packages.foo");
1049                     // check that the class loader was attempted
1050                     assertTrue(loader.reached(), "did not reach class loader!");
1051                 } catch (final ScriptException se) {
1052                     se.printStackTrace();
1053                     fail(se.getMessage());
1054                 }
1055 
1056                 try {
1057                     // strict mode - delete of a var should throw SyntaxError
1058                     e.eval("var d = 2; delete d;");
1059                 } catch (final ScriptException se) {
1060                     // check that the error message contains "SyntaxError"
1061                     assertTrue(se.getMessage().contains("SyntaxError"));
1062                 }
1063 
1064                 return;
1065             }
1066         }
1067 
1068         fail("Cannot find nashorn factory!");
1069     }
1070 }