Mon, 11 Feb 2013 21:26:06 +0530
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
Reviewed-by: lagergren, jlaskey, attila
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 */
26 package jdk.nashorn.api.javaaccess;
28 import static org.testng.AssertJUnit.assertEquals;
29 import static org.testng.internal.junit.ArrayAsserts.assertArrayEquals;
31 import javax.script.ScriptEngine;
32 import javax.script.ScriptEngineManager;
33 import javax.script.ScriptException;
34 import org.testng.TestNG;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
38 public class ObjectAccessTest {
40 private static ScriptEngine e = null;
41 private static SharedObject o = new SharedObject();
43 public static void main(final String[] args) {
44 TestNG.main(args);
45 }
47 @BeforeClass
48 public static void setUpClass() throws ScriptException {
49 final ScriptEngineManager m = new ScriptEngineManager();
50 e = m.getEngineByName("nashorn");
51 e.put("o", o);
52 e.eval("var SharedObject = Packages.jdk.nashorn.internal.access.SharedObject;");
53 e.eval("var Person = Packages.jdk.nashorn.internal.access.Person;");
54 }
56 @Test
57 public void accessFieldObject() throws ScriptException {
58 e.eval("var p_object = o.publicObject;");
59 assertEquals(o.publicObject, e.get("p_object"));
60 assertEquals("object", e.eval("typeof p_object;"));
61 e.eval("o.publicObject = new Person(14);");
62 assertEquals(new Person(14), o.publicObject);
63 }
65 @Test
66 public void accessFieldObjectArray() throws ScriptException {
67 e.eval("var p_object_array = o.publicObjectArray;");
68 assertEquals(o.publicObjectArray[0], e.eval("o.publicObjectArray[0]"));
69 assertArrayEquals(o.publicObjectArray, (Object[])e.get("p_object_array"));
70 e.eval("var t_object_arr = java.lang.reflect.Array.newInstance(Person.class, 3);" +
71 "t_object_arr[0] = new Person(100);" +
72 "t_object_arr[1] = new Person(120);" +
73 "t_object_arr[2] = new Person(140);" +
74 "o.publicObjectArray = t_object_arr;");
75 assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, o.publicObjectArray);
76 e.eval("o.publicObjectArray[0] = new Person(10);");
77 assertEquals(new Person(10), o.publicObjectArray[0]);
78 }
80 @Test
81 public void accessStaticFieldObject() throws ScriptException {
82 e.eval("var ps_object = SharedObject.publicStaticObject;");
83 assertEquals(SharedObject.publicStaticObject, e.get("ps_object"));
84 assertEquals("object", e.eval("typeof ps_object;"));
85 e.eval("SharedObject.publicStaticObject = new Person(16);");
86 assertEquals(new Person(16), SharedObject.publicStaticObject);
87 }
89 @Test
90 public void accessStaticFieldObjectArray() throws ScriptException {
91 e.eval("var ps_object_array = SharedObject.publicStaticObjectArray;");
92 assertEquals(SharedObject.publicStaticObjectArray[0], e.eval("SharedObject.publicStaticObjectArray[0]"));
93 assertArrayEquals(SharedObject.publicStaticObjectArray, (Object[])e.get("ps_object_array"));
94 e.eval("var ts_object_arr = java.lang.reflect.Array.newInstance(Person.class, 3);" +
95 "ts_object_arr[0] = new Person(100);" +
96 "ts_object_arr[1] = new Person(120);" +
97 "ts_object_arr[2] = new Person(140);" +
98 "SharedObject.publicStaticObjectArray = ts_object_arr;");
99 assertArrayEquals(new Person[] { new Person(100), new Person(120), new Person(140) }, SharedObject.publicStaticObjectArray);
100 e.eval("SharedObject.publicStaticObjectArray[0] = new Person(10);");
101 assertEquals(new Person(10), SharedObject.publicStaticObjectArray[0]);
102 }
104 @Test
105 public void accessFinalFieldObject() throws ScriptException {
106 e.eval("var pf_object = o.publicFinalObject;");
107 assertEquals(o.publicFinalObject, e.get("pf_object"));
108 assertEquals("object", e.eval("typeof pf_object;"));
109 e.eval("o.publicFinalObject = new Person(-999);");
110 assertEquals(new Person(1024), o.publicFinalObject);
111 }
113 @Test
114 public void accessFinalFieldObjectArray() throws ScriptException {
115 e.eval("var pf_object_array = o.publicFinalObjectArray;");
116 assertEquals(o.publicFinalObjectArray[0], e.eval("o.publicFinalObjectArray[0]"));
117 assertArrayEquals(o.publicFinalObjectArray, (Object[])e.get("pf_object_array"));
118 e.eval("var tf_object_arr = java.lang.reflect.Array.newInstance(Person.class, 3);" +
119 "tf_object_arr[0] = new Person(100);" +
120 "tf_object_arr[1] = new Person(120);" +
121 "tf_object_arr[2] = new Person(140);" +
122 "o.publicOFinalbjectArray = tf_object_arr;");
123 assertArrayEquals(new Person[] { new Person(-900), new Person(1000), new Person(180) }, o.publicFinalObjectArray);
124 e.eval("o.publicFinalObjectArray[0] = new Person(10);");
125 assertEquals(new Person(10), o.publicFinalObjectArray[0]);
126 }
128 @Test
129 public void accessStaticFinalFieldObject() throws ScriptException {
130 e.eval("var psf_object = SharedObject.publicStaticFinalObject;");
131 assertEquals(SharedObject.publicStaticFinalObject, e.get("psf_object"));
132 assertEquals("object", e.eval("typeof psf_object;"));
133 e.eval("SharedObject.publicStaticFinalObject = new Person(6);");
134 assertEquals(new Person(2048), SharedObject.publicStaticFinalObject);
135 }
137 @Test
138 public void accessStaticFinalFieldObjectArray() throws ScriptException {
139 e.eval("var psf_object_array = SharedObject.publicStaticFinalObjectArray;");
140 assertEquals(SharedObject.publicStaticFinalObjectArray[0], e.eval("SharedObject.publicStaticFinalObjectArray[0]"));
141 assertArrayEquals(SharedObject.publicStaticFinalObjectArray, (Object[])e.get("psf_object_array"));
142 e.eval("var tsf_object_arr = java.lang.reflect.Array.newInstance(Person.class, 3);" +
143 "tsf_object_arr[0] = new Person(100);" +
144 "tsf_object_arr[1] = new Person(120);" +
145 "tsf_object_arr[2] = new Person(140);" +
146 "SharedObject.publicStaticFinalObjectArray = tsf_object_arr;");
147 assertArrayEquals(new Person[] { new Person(-9), new Person(110), new Person(Integer.MAX_VALUE) }, SharedObject.publicStaticFinalObjectArray);
148 e.eval("SharedObject.publicStaticFinalObjectArray[0] = new Person(90);");
149 assertEquals(new Person(90), SharedObject.publicStaticFinalObjectArray[0]);
150 }
152 }