test/src/jdk/nashorn/internal/runtime/ContextTest.java

Print this page




  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")) {




  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")) {