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 org.testng.Assert.assertEquals; 29 import static org.testng.Assert.assertTrue; 30 31 import java.util.Map; 32 import jdk.nashorn.internal.runtime.options.Options; 33 import org.testng.annotations.Test; 34 35 /** 36 * Basic Context API tests. 37 * 38 * @test 39 * @run testng jdk.nashorn.internal.runtime.ContextTest 40 */ 41 public class ContextTest { 42 // basic context eval test 43 @Test 44 public void evalTest() { 45 final Options options = new Options(""); 46 final ErrorManager errors = new ErrorManager(); 47 final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 48 final ScriptObject oldGlobal = Context.getGlobal(); 49 Context.setGlobal(cx.createGlobal()); 50 try { 51 String code = "22 + 10"; 52 assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue()); 53 54 code = "obj = { js: 'nashorn' }; obj.js"; 55 assertEquals(eval(cx, "<evalTest2>", code), "nashorn"); 56 } finally { 57 Context.setGlobal(oldGlobal); 58 } 59 } 60 61 // basic check for JS reflection access - java.util.Map-like access on ScriptObject 62 @Test 63 public void reflectionTest() { 64 final Options options = new Options(""); 65 final ErrorManager errors = new ErrorManager(); 66 final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader()); 67 final boolean strict = cx.getEnv()._strict; 68 final ScriptObject oldGlobal = Context.getGlobal(); 69 Context.setGlobal(cx.createGlobal()); 70 71 try { 72 final String code = "var obj = { x: 344, y: 42 }"; 73 eval(cx, "<reflectionTest>", code); 74 75 final Object obj = cx.getGlobal().get("obj"); 76 77 assertTrue(obj instanceof ScriptObject); 78 79 final ScriptObject sobj = (ScriptObject)obj; 80 int count = 0; 81 for (final Map.Entry<?, ?> ex : sobj.entrySet()) { 82 final Object key = ex.getKey(); 83 if (key.equals("x")) { 84 assertTrue(ex.getValue() instanceof Number); 85 assertTrue(344.0 == ((Number)ex.getValue()).doubleValue()); 86 87 count++; 88 } else if (key.equals("y")) { 89 assertTrue(ex.getValue() instanceof Number); 90 assertTrue(42.0 == ((Number)ex.getValue()).doubleValue()); 91 92 count++; 93 } 94 } 95 assertEquals(count, 2); 96 assertEquals(sobj.size(), 2); 97 98 // add property 99 sobj.put("zee", "hello", strict); 100 assertEquals(sobj.get("zee"), "hello"); 101 assertEquals(sobj.size(), 3); 102 103 } finally { 104 Context.setGlobal(oldGlobal); 105 } 106 } 107 108 private Object eval(final Context cx, final String name, final String code) { 109 final Source source = new Source(name, code); 110 final ScriptObject global = Context.getGlobal(); 111 final ScriptFunction func = cx.compileScript(source, global); 112 return func != null ? ScriptRuntime.apply(func, global) : null; 113 } 114 }