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 ScriptObject oldGlobal = Context.getGlobal();
  68         Context.setGlobal(cx.createGlobal());
  69 
  70         try {
  71             final String code = "var obj = { x: 344, y: 42 }";
  72             eval(cx, "<reflectionTest>", code);
  73 
  74             final Object obj = cx.getGlobal().get("obj");
  75 
  76             assertTrue(obj instanceof ScriptObject);
  77 
  78             final ScriptObject sobj = (ScriptObject)obj;
  79             int count = 0;
  80             for (final Map.Entry<?, ?> ex : sobj.entrySet()) {
  81                 final Object key = ex.getKey();
  82                 if (key.equals("x")) {
  83                     assertTrue(ex.getValue() instanceof Number);
  84                     assertTrue(344.0 == ((Number)ex.getValue()).doubleValue());
  85 
  86                     count++;
  87                 } else if (key.equals("y")) {
  88                     assertTrue(ex.getValue() instanceof Number);
  89                     assertTrue(42.0 == ((Number)ex.getValue()).doubleValue());
  90 
  91                     count++;
  92                 }
  93             }
  94             assertEquals(count, 2);
  95             assertEquals(sobj.size(), 2);
  96 
  97             // add property
  98             sobj.put("zee", "hello");
  99             assertEquals(sobj.get("zee"), "hello");
 100             assertEquals(sobj.size(), 3);
 101 
 102         } finally {
 103             Context.setGlobal(oldGlobal);
 104         }
 105     }
 106 
 107     private Object eval(final Context cx, final String name, final String code) {
 108         final Source source = new Source(name, code);
 109         final ScriptObject global = Context.getGlobal();
 110         final ScriptFunction func = cx.compileScript(source, global);
 111         return func != null ? ScriptRuntime.apply(func, global) : null;
 112     }
 113 }
--- EOF ---