test/src/jdk/nashorn/api/scripting/PluggableJSObjectTest.java

changeset 1326
6787fa783196
parent 1274
7aaa64363e1a
child 1327
fb53538ea56b
     1.1 --- a/test/src/jdk/nashorn/api/scripting/PluggableJSObjectTest.java	Wed Apr 01 13:22:52 2015 -0700
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,288 +0,0 @@
     1.4 -/*
     1.5 - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 - *
     1.8 - * This code is free software; you can redistribute it and/or modify it
     1.9 - * under the terms of the GNU General Public License version 2 only, as
    1.10 - * published by the Free Software Foundation.  Oracle designates this
    1.11 - * particular file as subject to the "Classpath" exception as provided
    1.12 - * by Oracle in the LICENSE file that accompanied this code.
    1.13 - *
    1.14 - * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 - * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 - * version 2 for more details (a copy is included in the LICENSE file that
    1.18 - * accompanied this code).
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License version
    1.21 - * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 - *
    1.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 - * or visit www.oracle.com if you need additional information or have any
    1.26 - * questions.
    1.27 - */
    1.28 -
    1.29 -package jdk.nashorn.api.scripting;
    1.30 -
    1.31 -import static org.testng.Assert.assertEquals;
    1.32 -import static org.testng.Assert.assertFalse;
    1.33 -import static org.testng.Assert.fail;
    1.34 -
    1.35 -import java.nio.IntBuffer;
    1.36 -import java.util.Collection;
    1.37 -import java.util.HashMap;
    1.38 -import java.util.LinkedHashMap;
    1.39 -import java.util.Set;
    1.40 -import javax.script.ScriptEngine;
    1.41 -import javax.script.ScriptEngineManager;
    1.42 -import org.testng.annotations.Test;
    1.43 -
    1.44 -/**
    1.45 - * Tests for pluggable external impls. of jdk.nashorn.api.scripting.JSObject.
    1.46 - *
    1.47 - * JDK-8024615: Refactor ScriptObjectMirror and JSObject to support external
    1.48 - * JSObject implementations.
    1.49 - */
    1.50 -@SuppressWarnings("javadoc")
    1.51 -public class PluggableJSObjectTest {
    1.52 -    public static class MapWrapperObject extends AbstractJSObject {
    1.53 -        private final HashMap<String, Object> map = new LinkedHashMap<>();
    1.54 -
    1.55 -        public HashMap<String, Object> getMap() {
    1.56 -            return map;
    1.57 -        }
    1.58 -
    1.59 -        @Override
    1.60 -        public Object getMember(final String name) {
    1.61 -            return map.get(name);
    1.62 -        }
    1.63 -
    1.64 -        @Override
    1.65 -        public void setMember(final String name, final Object value) {
    1.66 -            map.put(name, value);
    1.67 -        }
    1.68 -
    1.69 -        @Override
    1.70 -        public boolean hasMember(final String name) {
    1.71 -            return map.containsKey(name);
    1.72 -        }
    1.73 -
    1.74 -        @Override
    1.75 -        public void removeMember(final String name) {
    1.76 -            map.remove(name);
    1.77 -        }
    1.78 -
    1.79 -        @Override
    1.80 -        public Set<String> keySet() {
    1.81 -            return map.keySet();
    1.82 -        }
    1.83 -
    1.84 -        @Override
    1.85 -        public Collection<Object> values() {
    1.86 -            return map.values();
    1.87 -        }
    1.88 -    }
    1.89 -
    1.90 -    @Test
    1.91 -    // Named property access on a JSObject
    1.92 -    public void namedAccessTest() {
    1.93 -        final ScriptEngineManager m = new ScriptEngineManager();
    1.94 -        final ScriptEngine e = m.getEngineByName("nashorn");
    1.95 -        try {
    1.96 -            final MapWrapperObject obj = new MapWrapperObject();
    1.97 -            e.put("obj", obj);
    1.98 -            obj.getMap().put("foo", "bar");
    1.99 -
   1.100 -            // property-like access on MapWrapperObject objects
   1.101 -            assertEquals(e.eval("obj.foo"), "bar");
   1.102 -            e.eval("obj.foo = 'hello'");
   1.103 -            assertEquals(e.eval("'foo' in obj"), Boolean.TRUE);
   1.104 -            assertEquals(e.eval("obj.foo"), "hello");
   1.105 -            assertEquals(obj.getMap().get("foo"), "hello");
   1.106 -            e.eval("delete obj.foo");
   1.107 -            assertFalse(obj.getMap().containsKey("foo"));
   1.108 -            assertEquals(e.eval("'foo' in obj"), Boolean.FALSE);
   1.109 -        } catch (final Exception exp) {
   1.110 -            exp.printStackTrace();
   1.111 -            fail(exp.getMessage());
   1.112 -        }
   1.113 -    }
   1.114 -
   1.115 -    // @bug 8062030: Nashorn bug retrieving array property after key string concatenation
   1.116 -    @Test
   1.117 -    // ConsString attribute access on a JSObject
   1.118 -    public void consStringTest() {
   1.119 -        final ScriptEngineManager m = new ScriptEngineManager();
   1.120 -        final ScriptEngine e = m.getEngineByName("nashorn");
   1.121 -        try {
   1.122 -            final MapWrapperObject obj = new MapWrapperObject();
   1.123 -            e.put("obj", obj);
   1.124 -            e.put("f", "f");
   1.125 -            e.eval("obj[f + 'oo'] = 'bar';");
   1.126 -
   1.127 -            assertEquals(obj.getMap().get("foo"), "bar");
   1.128 -            assertEquals(e.eval("obj[f + 'oo']"), "bar");
   1.129 -            assertEquals(e.eval("obj['foo']"), "bar");
   1.130 -            assertEquals(e.eval("f + 'oo' in obj"), Boolean.TRUE);
   1.131 -            assertEquals(e.eval("'foo' in obj"), Boolean.TRUE);
   1.132 -            e.eval("delete obj[f + 'oo']");
   1.133 -            assertFalse(obj.getMap().containsKey("foo"));
   1.134 -            assertEquals(e.eval("obj[f + 'oo']"), null);
   1.135 -            assertEquals(e.eval("obj['foo']"), null);
   1.136 -            assertEquals(e.eval("f + 'oo' in obj"), Boolean.FALSE);
   1.137 -            assertEquals(e.eval("'foo' in obj"), Boolean.FALSE);
   1.138 -        } catch (final Exception exp) {
   1.139 -            exp.printStackTrace();
   1.140 -            fail(exp.getMessage());
   1.141 -        }
   1.142 -    }
   1.143 -
   1.144 -    public static class BufferObject extends AbstractJSObject {
   1.145 -        private final IntBuffer buf;
   1.146 -
   1.147 -        public BufferObject(final int size) {
   1.148 -            buf = IntBuffer.allocate(size);
   1.149 -        }
   1.150 -
   1.151 -        public IntBuffer getBuffer() {
   1.152 -            return buf;
   1.153 -        }
   1.154 -
   1.155 -        @Override
   1.156 -        public Object getMember(final String name) {
   1.157 -            return name.equals("length")? buf.capacity() : null;
   1.158 -        }
   1.159 -
   1.160 -        @Override
   1.161 -        public boolean hasSlot(final int i) {
   1.162 -            return i > -1 && i < buf.capacity();
   1.163 -        }
   1.164 -
   1.165 -        @Override
   1.166 -        public Object getSlot(final int i) {
   1.167 -            return buf.get(i);
   1.168 -        }
   1.169 -
   1.170 -        @Override
   1.171 -        public void setSlot(final int i, final Object value) {
   1.172 -            buf.put(i, ((Number)value).intValue());
   1.173 -        }
   1.174 -
   1.175 -        @Override
   1.176 -        public boolean isArray() {
   1.177 -            return true;
   1.178 -        }
   1.179 -    }
   1.180 -
   1.181 -    @Test
   1.182 -    // array-like indexed access for a JSObject
   1.183 -    public void indexedAccessTest() {
   1.184 -        final ScriptEngineManager m = new ScriptEngineManager();
   1.185 -        final ScriptEngine e = m.getEngineByName("nashorn");
   1.186 -        try {
   1.187 -            final BufferObject buf = new BufferObject(2);
   1.188 -            e.put("buf", buf);
   1.189 -
   1.190 -            // array-like access on BufferObject objects
   1.191 -            assertEquals(e.eval("buf.length"), buf.getBuffer().capacity());
   1.192 -            e.eval("buf[0] = 23");
   1.193 -            assertEquals(buf.getBuffer().get(0), 23);
   1.194 -            assertEquals(e.eval("buf[0]"), 23);
   1.195 -            assertEquals(e.eval("buf[1]"), 0);
   1.196 -            buf.getBuffer().put(1, 42);
   1.197 -            assertEquals(e.eval("buf[1]"), 42);
   1.198 -            assertEquals(e.eval("Array.isArray(buf)"), Boolean.TRUE);
   1.199 -        } catch (final Exception exp) {
   1.200 -            exp.printStackTrace();
   1.201 -            fail(exp.getMessage());
   1.202 -        }
   1.203 -    }
   1.204 -
   1.205 -    public static class Adder extends AbstractJSObject {
   1.206 -        @Override
   1.207 -        public Object call(final Object thiz, final Object... args) {
   1.208 -            double res = 0.0;
   1.209 -            for (final Object arg : args) {
   1.210 -                res += ((Number)arg).doubleValue();
   1.211 -            }
   1.212 -            return res;
   1.213 -        }
   1.214 -
   1.215 -        @Override
   1.216 -        public boolean isFunction() {
   1.217 -            return true;
   1.218 -        }
   1.219 -    }
   1.220 -
   1.221 -    @Test
   1.222 -    // a callable JSObject
   1.223 -    public void callableJSObjectTest() {
   1.224 -        final ScriptEngineManager m = new ScriptEngineManager();
   1.225 -        final ScriptEngine e = m.getEngineByName("nashorn");
   1.226 -        try {
   1.227 -            e.put("sum", new Adder());
   1.228 -            // check callability of Adder objects
   1.229 -            assertEquals(e.eval("typeof sum"), "function");
   1.230 -            assertEquals(((Number)e.eval("sum(1, 2, 3, 4, 5)")).intValue(), 15);
   1.231 -        } catch (final Exception exp) {
   1.232 -            exp.printStackTrace();
   1.233 -            fail(exp.getMessage());
   1.234 -        }
   1.235 -    }
   1.236 -
   1.237 -    public static class Factory extends AbstractJSObject {
   1.238 -        @SuppressWarnings("unused")
   1.239 -        @Override
   1.240 -        public Object newObject(final Object... args) {
   1.241 -            return new HashMap<Object, Object>();
   1.242 -        }
   1.243 -
   1.244 -        @Override
   1.245 -        public boolean isFunction() {
   1.246 -            return true;
   1.247 -        }
   1.248 -    }
   1.249 -
   1.250 -    @Test
   1.251 -    // a factory JSObject
   1.252 -    public void factoryJSObjectTest() {
   1.253 -        final ScriptEngineManager m = new ScriptEngineManager();
   1.254 -        final ScriptEngine e = m.getEngineByName("nashorn");
   1.255 -        try {
   1.256 -            e.put("Factory", new Factory());
   1.257 -
   1.258 -            // check new on Factory
   1.259 -            assertEquals(e.eval("typeof Factory"), "function");
   1.260 -            assertEquals(e.eval("typeof new Factory()"), "object");
   1.261 -            assertEquals(e.eval("(new Factory()) instanceof java.util.Map"), Boolean.TRUE);
   1.262 -        } catch (final Exception exp) {
   1.263 -            exp.printStackTrace();
   1.264 -            fail(exp.getMessage());
   1.265 -        }
   1.266 -    }
   1.267 -
   1.268 -    @Test
   1.269 -    // iteration tests
   1.270 -    public void iteratingJSObjectTest() {
   1.271 -        final ScriptEngineManager m = new ScriptEngineManager();
   1.272 -        final ScriptEngine e = m.getEngineByName("nashorn");
   1.273 -        try {
   1.274 -            final MapWrapperObject obj = new MapWrapperObject();
   1.275 -            obj.setMember("foo", "hello");
   1.276 -            obj.setMember("bar", "world");
   1.277 -            e.put("obj", obj);
   1.278 -
   1.279 -            // check for..in
   1.280 -            Object val = e.eval("var str = ''; for (i in obj) str += i; str");
   1.281 -            assertEquals(val.toString(), "foobar");
   1.282 -
   1.283 -            // check for..each..in
   1.284 -            val = e.eval("var str = ''; for each (i in obj) str += i; str");
   1.285 -            assertEquals(val.toString(), "helloworld");
   1.286 -        } catch (final Exception exp) {
   1.287 -            exp.printStackTrace();
   1.288 -            fail(exp.getMessage());
   1.289 -        }
   1.290 -    }
   1.291 -}

mercurial