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