ContextTest.java revision 1239:77609e069f9f
14Srgrimes/*
24Srgrimes * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
34Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44Srgrimes *
54Srgrimes * This code is free software; you can redistribute it and/or modify it
64Srgrimes * under the terms of the GNU General Public License version 2 only, as
74Srgrimes * published by the Free Software Foundation.  Oracle designates this
84Srgrimes * particular file as subject to the "Classpath" exception as provided
94Srgrimes * by Oracle in the LICENSE file that accompanied this code.
104Srgrimes *
114Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
124Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
134Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
144Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
154Srgrimes * accompanied this code).
164Srgrimes *
174Srgrimes * You should have received a copy of the GNU General Public License version
184Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
194Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
204Srgrimes *
214Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
224Srgrimes * or visit www.oracle.com if you need additional information or have any
234Srgrimes * questions.
244Srgrimes */
254Srgrimes
264Srgrimespackage jdk.nashorn.internal.runtime.test;
274Srgrimes
284Srgrimesimport static jdk.nashorn.internal.runtime.Source.sourceFor;
294Srgrimesimport static org.testng.Assert.assertEquals;
304Srgrimesimport static org.testng.Assert.assertTrue;
314Srgrimesimport static org.testng.Assert.fail;
324Srgrimesimport java.util.Map;
334Srgrimesimport jdk.nashorn.internal.objects.Global;
34593Srgrimesimport jdk.nashorn.internal.runtime.Context;
3550477Speterimport jdk.nashorn.internal.runtime.ErrorManager;
364Srgrimesimport jdk.nashorn.internal.runtime.ScriptFunction;
374Srgrimesimport jdk.nashorn.internal.runtime.ScriptObject;
3832929Seivindimport jdk.nashorn.internal.runtime.ScriptRuntime;
3913290Speterimport jdk.nashorn.internal.runtime.Source;
4013225Swollmanimport jdk.nashorn.internal.runtime.options.Options;
412056Swollmanimport org.testng.annotations.Test;
422056Swollman
4345720Speter/**
4411865Sphk * Basic Context API tests.
4533281Sbde *
4645720Speter * @test
4711865Sphk * @run testng jdk.nashorn.internal.runtime.ContextTest
482056Swollman */
4945720Speter@SuppressWarnings("javadoc")
5045720Speterpublic class ContextTest {
5122093Sbde    // basic context eval test
524478Sbde    @Test
5322093Sbde    public void evalTest() {
544478Sbde        final Options options = new Options("");
553816Swollman        final ErrorManager errors = new ErrorManager();
5631255Sbde        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
5725083Sjdp        final Global oldGlobal = Context.getGlobal();
5831255Sbde        Context.setGlobal(cx.createGlobal());
5930805Sbde        try {
6030805Sbde            String code = "22 + 10";
6126309Speter            assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue());
6226309Speter
632056Swollman            code = "obj = { js: 'nashorn' }; obj.js";
6430805Sbde            assertEquals(eval(cx, "<evalTest2>", code), "nashorn");
6531255Sbde        } finally {
664478Sbde            Context.setGlobal(oldGlobal);
6731255Sbde        }
6845720Speter    }
692056Swollman
7030805Sbde    // Make sure trying to compile an invalid script returns null - see JDK-8046215.
713816Swollman    @Test
7231255Sbde    public void compileErrorTest() {
732056Swollman        final Options options = new Options("");
7426373Sdfr        final ErrorManager errors = new ErrorManager();
752056Swollman        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
7631255Sbde        final Global oldGlobal = Context.getGlobal();
7760008Swollman        Context.setGlobal(cx.createGlobal());
784Srgrimes        try {
794Srgrimes            final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
804Srgrimes            if (script != null) {
814Srgrimes                fail("Invalid script compiled without errors");
824Srgrimes            }
8319653Sbde            if (errors.getNumberOfErrors() != 1) {
8419653Sbde                fail("Wrong number of errors: " + errors.getNumberOfErrors());
8519653Sbde            }
8619653Sbde        } finally {
8749081Scracauer            Context.setGlobal(oldGlobal);
8819653Sbde        }
894Srgrimes    }
904Srgrimes
915351Sbde    // basic check for JS reflection access - java.util.Map-like access on ScriptObject
924Srgrimes    @Test
934Srgrimes    public void reflectionTest() {
945351Sbde        final Options options = new Options("");
9535215Sbde        final ErrorManager errors = new ErrorManager();
9635215Sbde        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
9735215Sbde        final boolean strict = cx.getEnv()._strict;
985351Sbde        final Global oldGlobal = Context.getGlobal();
995351Sbde        Context.setGlobal(cx.createGlobal());
1004Srgrimes
1014Srgrimes        try {
1024Srgrimes            final String code = "var obj = { x: 344, y: 42 }";
1034Srgrimes            eval(cx, "<reflectionTest>", code);
1044Srgrimes
1054Srgrimes            final Object obj = Context.getGlobal().get("obj");
1064Srgrimes
1074Srgrimes            assertTrue(obj instanceof ScriptObject);
1084Srgrimes
1095351Sbde            final ScriptObject sobj = (ScriptObject)obj;
1104Srgrimes            int count = 0;
1114Srgrimes            for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
1124Srgrimes                final Object key = ex.getKey();
1134Srgrimes                if (key.equals("x")) {
1144Srgrimes                    assertTrue(ex.getValue() instanceof Number);
1154Srgrimes                    assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());
1164Srgrimes
1174Srgrimes                    count++;
1184Srgrimes                } else if (key.equals("y")) {
1194Srgrimes                    assertTrue(ex.getValue() instanceof Number);
1204Srgrimes                    assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());
1214Srgrimes
12245720Speter                    count++;
12345720Speter                }
12450181Speter            }
12545720Speter            assertEquals(count, 2);
12645720Speter            assertEquals(sobj.size(), 2);
12745100Sdt
12833281Sbde            // add property
12933281Sbde            sobj.put("zee", "hello", strict);
13045100Sdt            assertEquals(sobj.get("zee"), "hello");
1314Srgrimes            assertEquals(sobj.size(), 3);
1325351Sbde
13311865Sphk        } finally {
13411865Sphk            Context.setGlobal(oldGlobal);
13511865Sphk        }
13611865Sphk    }
13711865Sphk
13841591Sarchie    private static Object eval(final Context cx, final String name, final String code) {
13941591Sarchie        final Source source = sourceFor(name, code);
14041591Sarchie        final ScriptObject global = Context.getGlobal();
14141797Sbde        final ScriptFunction func = cx.compileScript(source, global);
14241591Sarchie        return func != null ? ScriptRuntime.apply(func, global) : null;
14341591Sarchie    }
14441591Sarchie}
14526812Speter