1 /* |
|
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.nashorn.internal.runtime; |
|
27 |
|
28 import static jdk.nashorn.internal.runtime.Source.sourceFor; |
|
29 import static org.testng.Assert.assertEquals; |
|
30 import static org.testng.Assert.assertTrue; |
|
31 import static org.testng.Assert.fail; |
|
32 import java.util.Map; |
|
33 import jdk.nashorn.internal.objects.Global; |
|
34 import jdk.nashorn.internal.runtime.options.Options; |
|
35 import org.testng.annotations.Test; |
|
36 |
|
37 /** |
|
38 * Basic Context API tests. |
|
39 * |
|
40 * @test |
|
41 * @run testng jdk.nashorn.internal.runtime.ContextTest |
|
42 */ |
|
43 @SuppressWarnings("javadoc") |
|
44 public class ContextTest { |
|
45 // basic context eval test |
|
46 @Test |
|
47 public void evalTest() { |
|
48 final Options options = new Options(""); |
|
49 final ErrorManager errors = new ErrorManager(); |
|
50 final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); |
|
51 final Global oldGlobal = Context.getGlobal(); |
|
52 Context.setGlobal(cx.createGlobal()); |
|
53 try { |
|
54 String code = "22 + 10"; |
|
55 assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue()); |
|
56 |
|
57 code = "obj = { js: 'nashorn' }; obj.js"; |
|
58 assertEquals(eval(cx, "<evalTest2>", code), "nashorn"); |
|
59 } finally { |
|
60 Context.setGlobal(oldGlobal); |
|
61 } |
|
62 } |
|
63 |
|
64 // Make sure trying to compile an invalid script returns null - see JDK-8046215. |
|
65 @Test |
|
66 public void compileErrorTest() { |
|
67 final Options options = new Options(""); |
|
68 final ErrorManager errors = new ErrorManager(); |
|
69 final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); |
|
70 final Global oldGlobal = Context.getGlobal(); |
|
71 Context.setGlobal(cx.createGlobal()); |
|
72 try { |
|
73 final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal()); |
|
74 if (script != null) { |
|
75 fail("Invalid script compiled without errors"); |
|
76 } |
|
77 if (errors.getNumberOfErrors() != 1) { |
|
78 fail("Wrong number of errors: " + errors.getNumberOfErrors()); |
|
79 } |
|
80 } finally { |
|
81 Context.setGlobal(oldGlobal); |
|
82 } |
|
83 } |
|
84 |
|
85 // basic check for JS reflection access - java.util.Map-like access on ScriptObject |
|
86 @Test |
|
87 public void reflectionTest() { |
|
88 final Options options = new Options(""); |
|
89 final ErrorManager errors = new ErrorManager(); |
|
90 final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); |
|
91 final boolean strict = cx.getEnv()._strict; |
|
92 final Global oldGlobal = Context.getGlobal(); |
|
93 Context.setGlobal(cx.createGlobal()); |
|
94 |
|
95 try { |
|
96 final String code = "var obj = { x: 344, y: 42 }"; |
|
97 eval(cx, "<reflectionTest>", code); |
|
98 |
|
99 final Object obj = Context.getGlobal().get("obj"); |
|
100 |
|
101 assertTrue(obj instanceof ScriptObject); |
|
102 |
|
103 final ScriptObject sobj = (ScriptObject)obj; |
|
104 int count = 0; |
|
105 for (final Map.Entry<?, ?> ex : sobj.entrySet()) { |
|
106 final Object key = ex.getKey(); |
|
107 if (key.equals("x")) { |
|
108 assertTrue(ex.getValue() instanceof Number); |
|
109 assertTrue(344.0 == ((Number)ex.getValue()).doubleValue()); |
|
110 |
|
111 count++; |
|
112 } else if (key.equals("y")) { |
|
113 assertTrue(ex.getValue() instanceof Number); |
|
114 assertTrue(42.0 == ((Number)ex.getValue()).doubleValue()); |
|
115 |
|
116 count++; |
|
117 } |
|
118 } |
|
119 assertEquals(count, 2); |
|
120 assertEquals(sobj.size(), 2); |
|
121 |
|
122 // add property |
|
123 sobj.put("zee", "hello", strict); |
|
124 assertEquals(sobj.get("zee"), "hello"); |
|
125 assertEquals(sobj.size(), 3); |
|
126 |
|
127 } finally { |
|
128 Context.setGlobal(oldGlobal); |
|
129 } |
|
130 } |
|
131 |
|
132 private static Object eval(final Context cx, final String name, final String code) { |
|
133 final Source source = sourceFor(name, code); |
|
134 final ScriptObject global = Context.getGlobal(); |
|
135 final ScriptFunction func = cx.compileScript(source, global); |
|
136 return func != null ? ScriptRuntime.apply(func, global) : null; |
|
137 } |
|
138 } |
|