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