1 /* 2 * Copyright (c) 2015, 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.scripting.test; 27 28 import static org.testng.Assert.assertEquals; 29 import static org.testng.Assert.assertTrue; 30 31 import java.util.Arrays; 32 import java.util.List; 33 import java.util.Map; 34 import javax.script.ScriptEngine; 35 import javax.script.ScriptException; 36 import jdk.nashorn.api.scripting.JSObject; 37 import jdk.nashorn.api.scripting.NashornScriptEngine; 38 import jdk.nashorn.api.scripting.NashornScriptEngineFactory; 39 import org.testng.Assert; 40 import org.testng.annotations.Test; 41 42 /** 43 * @test 44 * @run testng jdk.nashorn.api.scripting.test.JSONCompatibleTest 45 */ 46 public class JSONCompatibleTest { 47 48 /** 49 * Wrap a top-level array as a list. 50 */ 51 @Test 52 public void testWrapArray() throws ScriptException { 53 final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(); 54 final Object val = engine.eval("Java.asJSONCompatible([1, 2, 3])"); 55 assertEquals(asList(val), Arrays.asList(1, 2, 3)); 56 } 57 58 /** 59 * Wrap an embedded array as a list. 60 */ 61 @Test 62 public void testWrapObjectWithArray() throws ScriptException { 63 final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(); 64 final Object val = engine.eval("Java.asJSONCompatible({x: [1, 2, 3]})"); 65 assertEquals(asList(asMap(val).get("x")), Arrays.asList(1, 2, 3)); 66 } 67 68 /** 69 * Check it all works transitively several more levels down. 70 */ 71 @Test 72 public void testDeepWrapping() throws ScriptException { 73 final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(); 74 final Object val = engine.eval("Java.asJSONCompatible({x: [1, {y: [2, {z: [3]}]}, [4, 5]]})"); 75 final Map<String, Object> root = asMap(val); 76 final List<Object> x = asList(root.get("x")); 77 assertEquals(x.get(0), 1); 78 final Map<String, Object> x1 = asMap(x.get(1)); 79 final List<Object> y = asList(x1.get("y")); 80 assertEquals(y.get(0), 2); 81 final Map<String, Object> y1 = asMap(y.get(1)); 82 assertEquals(asList(y1.get("z")), Arrays.asList(3)); 83 assertEquals(asList(x.get(2)), Arrays.asList(4, 5)); 84 } 85 86 /** 87 * Ensure that the old behaviour (every object is a Map) is unchanged. 88 */ 89 @Test 90 public void testNonWrapping() throws ScriptException { 91 final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(); 92 final Object val = engine.eval("({x: [1, {y: [2, {z: [3]}]}, [4, 5]]})"); 93 final Map<String, Object> root = asMap(val); 94 final Map<String, Object> x = asMap(root.get("x")); 95 assertEquals(x.get("0"), 1); 96 final Map<String, Object> x1 = asMap(x.get("1")); 97 final Map<String, Object> y = asMap(x1.get("y")); 98 assertEquals(y.get("0"), 2); 99 final Map<String, Object> y1 = asMap(y.get("1")); 100 final Map<String, Object> z = asMap(y1.get("z")); 101 assertEquals(z.get("0"), 3); 102 final Map<String, Object> x2 = asMap(x.get("2")); 103 assertEquals(x2.get("0"), 4); 104 assertEquals(x2.get("1"), 5); 105 } 106 107 @SuppressWarnings("unchecked") 108 private static List<Object> asList(final Object obj) { 109 assertJSObject(obj); 110 Assert.assertTrue(obj instanceof List); 111 return (List)obj; 112 } 113 114 @SuppressWarnings("unchecked") 115 private static Map<String, Object> asMap(final Object obj) { 116 assertJSObject(obj); 117 Assert.assertTrue(obj instanceof Map); 118 return (Map)obj; 119 } 120 121 private static void assertJSObject(final Object obj) { 122 assertTrue(obj instanceof JSObject); 123 } 124 }