test/src/jdk/nashorn/api/scripting/ScriptObjectMirrorTest.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.api.scripting;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.util.HashMap;
  30 import java.util.List;
  31 import java.util.Map;


  32 import javax.script.ScriptEngine;
  33 import javax.script.ScriptEngineManager;
  34 import javax.script.ScriptException;
  35 import static org.testng.Assert.assertEquals;
  36 import static org.testng.Assert.assertFalse;
  37 import static org.testng.Assert.assertTrue;
  38 import static org.testng.Assert.fail;
  39 import org.testng.annotations.Test;
  40 
  41 /**
  42  * Tests to check jdk.nashorn.api.scripting.ScriptObjectMirror API.
  43  */
  44 public class ScriptObjectMirrorTest {
  45 
  46     @SuppressWarnings("unchecked")
  47     @Test
  48     public void reflectionTest() throws ScriptException {
  49         final ScriptEngineManager m = new ScriptEngineManager();
  50         final ScriptEngine e = m.getEngineByName("nashorn");
  51 


 258         final ScriptEngineManager m = new ScriptEngineManager();
 259         final ScriptEngine e = m.getEngineByName("nashorn");
 260         final ScriptObjectMirror arr = (ScriptObjectMirror)e.eval("[33, 45, 23]");
 261         final int[] intArr = arr.to(int[].class);
 262         assertEquals(intArr[0], 33);
 263         assertEquals(intArr[1], 45);
 264         assertEquals(intArr[2], 23);
 265 
 266         final List<?> list = arr.to(List.class);
 267         assertEquals(list.get(0), 33);
 268         assertEquals(list.get(1), 45);
 269         assertEquals(list.get(2), 23);
 270 
 271         ScriptObjectMirror obj = (ScriptObjectMirror)e.eval(
 272             "({ valueOf: function() { return 42 } })");
 273         assertEquals(Double.valueOf(42.0), obj.to(Double.class));
 274 
 275         obj = (ScriptObjectMirror)e.eval(
 276             "({ toString: function() { return 'foo' } })");
 277         assertEquals("foo", obj.to(String.class));



























 278     }
 279 }


  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.api.scripting;
  27 
  28 import java.nio.ByteBuffer;
  29 import java.util.HashMap;
  30 import java.util.List;
  31 import java.util.Map;
  32 import javax.script.Bindings;
  33 import javax.script.ScriptContext;
  34 import javax.script.ScriptEngine;
  35 import javax.script.ScriptEngineManager;
  36 import javax.script.ScriptException;
  37 import static org.testng.Assert.assertEquals;
  38 import static org.testng.Assert.assertFalse;
  39 import static org.testng.Assert.assertTrue;
  40 import static org.testng.Assert.fail;
  41 import org.testng.annotations.Test;
  42 
  43 /**
  44  * Tests to check jdk.nashorn.api.scripting.ScriptObjectMirror API.
  45  */
  46 public class ScriptObjectMirrorTest {
  47 
  48     @SuppressWarnings("unchecked")
  49     @Test
  50     public void reflectionTest() throws ScriptException {
  51         final ScriptEngineManager m = new ScriptEngineManager();
  52         final ScriptEngine e = m.getEngineByName("nashorn");
  53 


 260         final ScriptEngineManager m = new ScriptEngineManager();
 261         final ScriptEngine e = m.getEngineByName("nashorn");
 262         final ScriptObjectMirror arr = (ScriptObjectMirror)e.eval("[33, 45, 23]");
 263         final int[] intArr = arr.to(int[].class);
 264         assertEquals(intArr[0], 33);
 265         assertEquals(intArr[1], 45);
 266         assertEquals(intArr[2], 23);
 267 
 268         final List<?> list = arr.to(List.class);
 269         assertEquals(list.get(0), 33);
 270         assertEquals(list.get(1), 45);
 271         assertEquals(list.get(2), 23);
 272 
 273         ScriptObjectMirror obj = (ScriptObjectMirror)e.eval(
 274             "({ valueOf: function() { return 42 } })");
 275         assertEquals(Double.valueOf(42.0), obj.to(Double.class));
 276 
 277         obj = (ScriptObjectMirror)e.eval(
 278             "({ toString: function() { return 'foo' } })");
 279         assertEquals("foo", obj.to(String.class));
 280     }
 281 
 282     // @bug 8044000: Access to undefined property yields "null" instead of "undefined"
 283     @Test
 284     public void mapScriptObjectMirrorCallsiteTest() throws ScriptException {
 285         final ScriptEngineManager m = new ScriptEngineManager();
 286         final ScriptEngine engine = m.getEngineByName("nashorn");
 287         final String TEST_SCRIPT = "typeof obj.foo";
 288 
 289         final Bindings global = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
 290         engine.eval("var obj = java.util.Collections.emptyMap()");
 291         // this will drive callsite "obj.foo" of TEST_SCRIPT
 292         // to use "obj instanceof Map" as it's guard
 293         engine.eval(TEST_SCRIPT, global);
 294         // redefine 'obj' to be a script object
 295         engine.eval("obj = {}");
 296 
 297         final Bindings newGlobal = engine.createBindings();
 298         // transfer 'obj' from default global to new global
 299         // new global will get a ScriptObjectMirror wrapping 'obj'
 300         newGlobal.put("obj", global.get("obj"));
 301 
 302         // Every ScriptObjectMirror is a Map! If callsite "obj.foo"
 303         // does not see the new 'obj' is a ScriptObjectMirror, it'll
 304         // continue to use Map's get("obj.foo") instead of ScriptObjectMirror's
 305         // getMember("obj.foo") - thereby getting null instead of undefined
 306         assertEquals("undefined", engine.eval(TEST_SCRIPT, newGlobal));
 307     }
 308 }