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