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.test.models;
  27 
  28 import java.util.List;
  29 import java.util.Map;
  30 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  31 import jdk.nashorn.internal.runtime.ScriptObject;
  32 import org.testng.Assert;
  33 
  34 public class Jdk8072596TestSubject {
  35 
  36     public Jdk8072596TestSubject(final Object x) {
  37         Assert.assertTrue(x instanceof ScriptObjectMirror);
  38         Assert.assertEquals(((ScriptObjectMirror)x).get("bar"), 0);
  39     }
  40 
  41     // Test having to wrap some arguments but not others
  42     public void test1(final String x, final Object y, final ScriptObject w) {
  43         Assert.assertEquals(x, "true");
  44 
  45         Assert.assertTrue(y instanceof ScriptObjectMirror);
  46         Assert.assertEquals(((ScriptObjectMirror)y).get("foo"), 1);
  47 
  48         Assert.assertEquals(w.get("bar"), 2);
  49     }
  50 
  51     // Test having to wrap some arguments but not others, and a vararg array
  52     public void test2(final String x, final Object y, final ScriptObject w, final Object... z) {
  53         test1(x, y, w);
  54 
  55         Assert.assertEquals(z.length, 2);
  56 
  57         Assert.assertTrue(z[0] instanceof ScriptObjectMirror);
  58         Assert.assertEquals(((ScriptObjectMirror)z[0]).get("baz"), 3);
  59 
  60         Assert.assertTrue(z[1] instanceof ScriptObjectMirror);
  61         Assert.assertEquals(((ScriptObjectMirror)z[1]).get("bing"), 4);
  62     }
  63 
  64     // Test mixed (wrappable and non-wrappable) elements in a vararg array
  65     public void test3(final Object... z) {
  66         Assert.assertEquals(z.length, 5);
  67 
  68         Assert.assertEquals(z[0], true);
  69 
  70         Assert.assertTrue(z[1] instanceof ScriptObjectMirror);
  71         Assert.assertEquals(((ScriptObjectMirror)z[1]).get("foo"), 5);
  72 
  73         Assert.assertEquals(z[2], "hello");
  74 
  75         Assert.assertTrue(z[3] instanceof ScriptObjectMirror);
  76         Assert.assertEquals(((ScriptObjectMirror)z[3]).getSlot(0), 6);
  77         Assert.assertEquals(((ScriptObjectMirror)z[3]).getSlot(1), 7);
  78 
  79         Assert.assertEquals(z[4], 8);
  80     }
  81 
  82     // test wrapping the first argument of a static method
  83     public static void test4(final Object x) {
  84         Assert.assertTrue(x instanceof ScriptObjectMirror);
  85         Assert.assertEquals(((ScriptObjectMirror)x).get("foo"), 9);
  86     }
  87 
  88     public void testListHasWrappedObject(final List<?> l) {
  89         Assert.assertEquals(l.size(), 1);
  90         Assert.assertTrue(l.get(0) instanceof ScriptObjectMirror);
  91         Assert.assertEquals(((ScriptObjectMirror)l.get(0)).get("foo"), 10);
  92     }
  93 
  94     public void testArrayHasWrappedObject(final Object[] a) {
  95         Assert.assertEquals(a.length, 1);
  96         Assert.assertTrue(a[0] instanceof ScriptObjectMirror);
  97         Assert.assertEquals(((ScriptObjectMirror)a[0]).get("bar"), 11);
  98     }
  99 
 100     public void testMapHasWrappedObject(final Map<?, ?> m, final Object key) {
 101         Assert.assertEquals(m.size(), 1);
 102         Assert.assertTrue(key instanceof ScriptObjectMirror);
 103         Assert.assertTrue(m.get(key) instanceof ScriptObjectMirror);
 104         Assert.assertEquals(((ScriptObjectMirror)m.get(key)).get("bar"), 12);
 105     }
 106 }