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.api.javaaccess.test; 27 28 import static org.testng.AssertJUnit.assertEquals; 29 import java.util.HashMap; 30 import java.util.Map; 31 import javax.script.Bindings; 32 import javax.script.ScriptContext; 33 import javax.script.ScriptEngine; 34 import javax.script.ScriptEngineManager; 35 import javax.script.ScriptException; 36 import jdk.nashorn.api.scripting.JSObject; 37 import org.testng.TestNG; 38 import org.testng.annotations.AfterClass; 39 import org.testng.annotations.BeforeClass; 40 import org.testng.annotations.Test; 41 42 @SuppressWarnings("javadoc") 43 public class ConsStringTest { 44 private static ScriptEngine e = null; 45 46 public static void main(final String[] args) { 47 TestNG.main(args); 48 } 49 50 @BeforeClass 51 public static void setUpClass() { 52 e = new ScriptEngineManager().getEngineByName("nashorn"); 53 } 54 55 @AfterClass 56 public static void tearDownClass() { 57 e = null; 58 } 59 60 @Test 61 public void testConsStringFlattening() throws ScriptException { 62 final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 63 final Map<Object, Object> m = new HashMap<>(); 64 b.put("m", m); 65 e.eval("var x = 'f'; x += 'oo'; var y = 'b'; y += 'ar'; m.put(x, y)"); 66 assertEquals("bar", m.get("foo")); 67 } 68 69 @Test 70 public void testConsStringFromMirror() throws ScriptException { 71 final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 72 //final Map<Object, Object> m = new HashMap<>(); 73 e.eval("var x = 'f'; x += 'oo'; var obj = {x: x};"); 74 assertEquals("foo", ((JSObject)b.get("obj")).getMember("x")); 75 } 76 77 @Test 78 public void testArrayConsString() throws ScriptException { 79 final Bindings b = e.getBindings(ScriptContext.ENGINE_SCOPE); 80 final ArrayHolder h = new ArrayHolder(); 81 b.put("h", h); 82 e.eval("var x = 'f'; x += 'oo'; h.array = [x];"); 83 assertEquals(1, h.array.length); 84 assertEquals("foo", h.array[0]); 85 } 86 87 88 public static class ArrayHolder { 89 private Object[] array; 90 91 public void setArray(final Object[] array) { 92 this.array = array; 93 } 94 95 public Object[] getArray() { 96 return array; 97 } 98 } 99 }