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.internal.runtime.test;
27 
28 import static org.testng.Assert.assertEquals;
29 
30 import java.util.Arrays;
31 import java.util.Deque;
32 import java.util.List;
33 import javax.script.ScriptEngine;
34 import javax.script.ScriptException;
35 import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
36 import org.testng.annotations.Test;
37 
38 /**
39  * @bug 8081204
40  * @summary adding and removing elements to a ListAdapter outside of JS context should work.
41  */
42 @SuppressWarnings("javadoc")
43 public class AddAndRemoveOnListAdapterOutsideOfJavaScriptContextTest {
44 
45     @SuppressWarnings("unchecked")
getListAdapter()46     private static <T> T getListAdapter() throws ScriptException {
47         final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine();
48         return (T)engine.eval("Java.to([1, 2, 3, 4], 'java.util.List')");
49     }
50 
51     @Test
testInvokePush()52     public void testInvokePush() throws ScriptException {
53         final Deque<Object> l = getListAdapter();
54         l.addLast(5);
55         assertEquals(l.size(), 5);
56         assertEquals(l.getLast(), 5);
57         assertEquals(l.getFirst(), 1);
58     }
59 
60     @Test
testPop()61     public void testPop() throws ScriptException {
62         final Deque<Object> l = getListAdapter();
63         assertEquals(l.removeLast(), 4);
64         assertEquals(l.size(), 3);
65         assertEquals(l.getLast(), 3);
66     }
67 
68     @Test
testUnshift()69     public void testUnshift() throws ScriptException {
70         final Deque<Object> l = getListAdapter();
71         l.addFirst(0);
72         assertEquals(l.getFirst(), 0);
73         assertEquals(l.getLast(), 4);
74         assertEquals(l.size(), 5);
75     }
76 
77     @Test
testShift()78     public void testShift() throws ScriptException {
79         final Deque<Object> l = getListAdapter();
80         l.removeFirst();
81         assertEquals(l.getFirst(), 2);
82         assertEquals(l.getLast(), 4);
83         assertEquals(l.size(), 3);
84     }
85 
86     @Test
testSpliceAdd()87     public void testSpliceAdd() throws ScriptException {
88         final List<Object> l = getListAdapter();
89         assertEquals(l, Arrays.asList(1, 2, 3, 4));
90         l.add(2, "foo");
91         assertEquals(l, Arrays.asList(1, 2, "foo", 3, 4));
92     }
93 
94 
95     @Test
testSpliceRemove()96     public void testSpliceRemove() throws ScriptException {
97         final List<Object> l = getListAdapter();
98         assertEquals(l, Arrays.asList(1, 2, 3, 4));
99         l.remove(2);
100         assertEquals(l, Arrays.asList(1, 2, 4));
101     }
102 }
103