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