ContextTest.java revision 1239:77609e069f9f
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
26package jdk.nashorn.internal.runtime.test;
27
28import static jdk.nashorn.internal.runtime.Source.sourceFor;
29import static org.testng.Assert.assertEquals;
30import static org.testng.Assert.assertTrue;
31import static org.testng.Assert.fail;
32import java.util.Map;
33import jdk.nashorn.internal.objects.Global;
34import jdk.nashorn.internal.runtime.Context;
35import jdk.nashorn.internal.runtime.ErrorManager;
36import jdk.nashorn.internal.runtime.ScriptFunction;
37import jdk.nashorn.internal.runtime.ScriptObject;
38import jdk.nashorn.internal.runtime.ScriptRuntime;
39import jdk.nashorn.internal.runtime.Source;
40import jdk.nashorn.internal.runtime.options.Options;
41import org.testng.annotations.Test;
42
43/**
44 * Basic Context API tests.
45 *
46 * @test
47 * @run testng jdk.nashorn.internal.runtime.ContextTest
48 */
49@SuppressWarnings("javadoc")
50public class ContextTest {
51    // basic context eval test
52    @Test
53    public void evalTest() {
54        final Options options = new Options("");
55        final ErrorManager errors = new ErrorManager();
56        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
57        final Global oldGlobal = Context.getGlobal();
58        Context.setGlobal(cx.createGlobal());
59        try {
60            String code = "22 + 10";
61            assertTrue(32.0 == ((Number)(eval(cx, "<evalTest>", code))).doubleValue());
62
63            code = "obj = { js: 'nashorn' }; obj.js";
64            assertEquals(eval(cx, "<evalTest2>", code), "nashorn");
65        } finally {
66            Context.setGlobal(oldGlobal);
67        }
68    }
69
70    // Make sure trying to compile an invalid script returns null - see JDK-8046215.
71    @Test
72    public void compileErrorTest() {
73        final Options options = new Options("");
74        final ErrorManager errors = new ErrorManager();
75        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
76        final Global oldGlobal = Context.getGlobal();
77        Context.setGlobal(cx.createGlobal());
78        try {
79            final ScriptFunction script = cx.compileScript(sourceFor("<evalCompileErrorTest>", "*/"), Context.getGlobal());
80            if (script != null) {
81                fail("Invalid script compiled without errors");
82            }
83            if (errors.getNumberOfErrors() != 1) {
84                fail("Wrong number of errors: " + errors.getNumberOfErrors());
85            }
86        } finally {
87            Context.setGlobal(oldGlobal);
88        }
89    }
90
91    // basic check for JS reflection access - java.util.Map-like access on ScriptObject
92    @Test
93    public void reflectionTest() {
94        final Options options = new Options("");
95        final ErrorManager errors = new ErrorManager();
96        final Context cx = new Context(options, errors, Thread.currentThread().getContextClassLoader());
97        final boolean strict = cx.getEnv()._strict;
98        final Global oldGlobal = Context.getGlobal();
99        Context.setGlobal(cx.createGlobal());
100
101        try {
102            final String code = "var obj = { x: 344, y: 42 }";
103            eval(cx, "<reflectionTest>", code);
104
105            final Object obj = Context.getGlobal().get("obj");
106
107            assertTrue(obj instanceof ScriptObject);
108
109            final ScriptObject sobj = (ScriptObject)obj;
110            int count = 0;
111            for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
112                final Object key = ex.getKey();
113                if (key.equals("x")) {
114                    assertTrue(ex.getValue() instanceof Number);
115                    assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());
116
117                    count++;
118                } else if (key.equals("y")) {
119                    assertTrue(ex.getValue() instanceof Number);
120                    assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());
121
122                    count++;
123                }
124            }
125            assertEquals(count, 2);
126            assertEquals(sobj.size(), 2);
127
128            // add property
129            sobj.put("zee", "hello", strict);
130            assertEquals(sobj.get("zee"), "hello");
131            assertEquals(sobj.size(), 3);
132
133        } finally {
134            Context.setGlobal(oldGlobal);
135        }
136    }
137
138    private static Object eval(final Context cx, final String name, final String code) {
139        final Source source = sourceFor(name, code);
140        final ScriptObject global = Context.getGlobal();
141        final ScriptFunction func = cx.compileScript(source, global);
142        return func != null ? ScriptRuntime.apply(func, global) : null;
143    }
144}
145