1/*
2 * Copyright (c) 2015, 2016, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test 8130450 8158906 8154374 8166400 8171892 8173807 8173848
26 * @summary simple regression test
27 * @build KullaTesting TestingInputStream
28 * @run testng SimpleRegressionTest
29 */
30
31
32import java.util.List;
33
34import javax.tools.Diagnostic;
35
36import jdk.jshell.Snippet;
37import jdk.jshell.VarSnippet;
38import jdk.jshell.SnippetEvent;
39import org.testng.annotations.Test;
40
41import static org.testng.Assert.assertEquals;
42import static org.testng.Assert.assertFalse;
43import static org.testng.Assert.assertTrue;
44import static jdk.jshell.Snippet.Status.OVERWRITTEN;
45import static jdk.jshell.Snippet.SubKind.TEMP_VAR_EXPRESSION_SUBKIND;
46import static jdk.jshell.Snippet.Status.VALID;
47
48@Test
49public class SimpleRegressionTest extends KullaTesting {
50
51    public void testSnippetMemberAssignment() {
52        assertEval("class C { int y; }");
53        assertEval("C c = new C();");
54        assertVarKeyMatch("c.y = 4;", true, "$1", TEMP_VAR_EXPRESSION_SUBKIND, "int", added(VALID));
55    }
56
57    public void testUserTakesTempVarName() {
58        assertEval("int $2 = 4;");
59        assertEval("String $1;");
60        assertVarKeyMatch("1234;", true, "$3", TEMP_VAR_EXPRESSION_SUBKIND, "int", added(VALID));
61    }
62
63    public void testCompileThrow() {
64        assertEvalException("throw new Exception();");
65    }
66
67    public void testMultiSnippetDependencies() {
68        List<SnippetEvent> events = assertEval("int a = 3, b = a+a, c = b *100;",
69                DiagCheck.DIAG_OK, DiagCheck.DIAG_OK,
70                chain(added(VALID)),
71                chain(added(VALID)),
72                chain(added(VALID)));
73        assertEquals(events.get(0).value(), "3");
74        assertEquals(events.get(1).value(), "6");
75        assertEquals(events.get(2).value(), "600");
76        assertEval("c;", "600");
77    }
78
79    public void testLessThanParsing() {
80        assertEval("int x = 3;", "3");
81        assertEval("int y = 4;", "4");
82        assertEval("int z = 5;", "5");
83        assertEval("x < y", "true");
84        assertEval("x < y;", "true");
85        assertEval("x < y && y < z", "true");
86    }
87
88    public void testNotStmtCannotResolve() {
89        assertDeclareFail("dfasder;", new ExpectedDiagnostic("compiler.err.cant.resolve.location", 0, 7, 0, -1, -1, Diagnostic.Kind.ERROR));
90    }
91
92    public void testNotStmtIncomparable() {
93        assertDeclareFail("true == 5.0;", new ExpectedDiagnostic("compiler.err.incomparable.types", 0, 11, 5, -1, -1, Diagnostic.Kind.ERROR));
94    }
95
96    public void testStringAdd() {
97        assertEval("String s = \"a\" + \"b\";", "\"ab\"");
98    }
99
100    public void testExprSanity() {
101        assertEval("int x = 3;", "3");
102        assertEval("int y = 4;", "4");
103        assertEval("x + y;", "7");
104        assertActiveKeys();
105    }
106
107    public void testGenericMethodCrash() {
108        assertDeclareWarn1("<T> void f(T...a) {}", (ExpectedDiagnostic) null);
109        Snippet sn = methodKey(assertEval("<R> R n(R x) { return x; }", added(VALID)));
110        VarSnippet sne = varKey(assertEval("n(5)", added(VALID)));
111        assertEquals(sne.typeName(), "Integer");
112    }
113
114    public void testLongRemoteStrings() { //8158906
115        assertEval("String m(int x) { byte[] b = new byte[x]; for (int i = 0; i < x; ++i) b[i] = (byte) 'a'; return new String(b); }");
116        boolean[] shut = new boolean[1];
117        getState().onShutdown(j -> {
118            shut[0] = true;
119        });
120        for (String len : new String[]{"12345", "64000", "65535", "65536", "120000"}) {
121            List<SnippetEvent> el = assertEval("m(" + len + ");");
122            assertFalse(shut[0], "JShell died with long string");
123            assertEquals(el.size(), 1, "Excepted one event");
124            assertTrue(el.get(0).value().length() > 10000,
125                    "Expected truncated but long String, got: " + el.get(0).value().length());
126        }
127    }
128
129    public void testLongRemoteJapaneseStrings() { //8158906
130        assertEval("import java.util.stream.*;");
131        assertEval("String m(int x) { return Stream.generate(() -> \"\u3042\").limit(x).collect(Collectors.joining()); }");
132        boolean[] shut = new boolean[1];
133        getState().onShutdown(j -> {
134            shut[0] = true;
135        });
136        for (String len : new String[]{"12345", "21843", "21844", "21845", "21846", "64000", "65535", "65536", "120000"}) {
137            List<SnippetEvent> el = assertEval("m(" + len + ");");
138            assertFalse(shut[0], "JShell died with long string");
139            assertEquals(el.size(), 1, "Excepted one event");
140            assertTrue(el.get(0).value().length() > 10000,
141                    "Expected truncated but long String, got: " + el.get(0).value().length());
142        }
143    }
144
145    // 8130450
146    public void testDuplicate() {
147        Snippet snm = methodKey(assertEval("void mm() {}", added(VALID)));
148        assertEval("void mm() {}",
149                ste(MAIN_SNIPPET, VALID, VALID, false, null),
150                ste(snm, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
151        Snippet snv = varKey(assertEval("boolean b;", added(VALID)));
152        assertEval("boolean b;",
153                ste(MAIN_SNIPPET, VALID, VALID, false, null),
154                ste(snv, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
155    }
156
157    public void testContextClassLoader() {
158        assertEval("class C {}");
159        assertEval("C.class.getClassLoader() == Thread.currentThread().getContextClassLoader()", "true");
160    }
161
162    public void testArrayRepresentation() {
163        assertEval("new int[4]", "int[4] { 0, 0, 0, 0 }");
164        assertEval("new int[0]", "int[0] {  }");
165        assertEval("new byte[2]", "byte[2] { 0, 0 }");
166        assertEval("new short[] { 1234, 4321 }", "short[2] { 1234, 4321 }");
167        assertEval("new long[] { 123456789 }", "long[1] { 123456789 }");
168        assertEval("new float[] { -23.5645f, 1.0101f }", "float[2] { -23.5645, 1.0101 }");
169        assertEval("new double[] { 1/8, Math.PI }", "double[2] { 0.0, 3.141592653589793 }");
170        assertEval("new String[] { \"hi\", \"low\", null }", "String[3] { \"hi\", \"low\", null }");
171        assertEval("new char[] { 'a', 34, 77 }", "char[3] { 'a', '\"', 'M' }");
172        assertEval("new boolean[] { false, true }", "boolean[2] { false, true }");
173        assertEval("new int[][] { new int[] {44, 55}, new int[] {88,99}}",
174                "int[2][] { int[2] { 44, 55 }, int[2] { 88, 99 } }");
175        assertEval("new Object[] { \"howdy\", new int[] { 33, 44, 55 }, new String[] { \"up\", \"down\" }}",
176                "Object[3] { \"howdy\", int[3] { 33, 44, 55 }, String[2] { \"up\", \"down\" } }");
177    }
178
179    public void testMultiDimArrayRepresentation() {
180        assertEval("new int[3][1]",
181                "int[3][] { int[1] { 0 }, int[1] { 0 }, int[1] { 0 } }");
182        assertEval("new int[3][]",
183                "int[3][] { null, null, null }");
184        assertEval("new int[][] { new int[] {44}, new int[] {77, 88,99}}",
185                "int[2][] { int[1] { 44 }, int[3] { 77, 88, 99 } }");
186        assertEval("new String[3][1]",
187                "String[3][] { String[1] { null }, String[1] { null }, String[1] { null } }");
188        assertEval("class C { }");
189        assertEval("new C[3][2]",
190                "C[3][] { C[2] { null, null }, C[2] { null, null }, C[2] { null, null } }");
191        assertEval("new boolean[2][1][3]",
192                "boolean[2][][] { boolean[1][] { boolean[3] { false, false, false } }, boolean[1][] { boolean[3] { false, false, false } } }");
193    }
194
195    public void testStringRepresentation() {
196        assertEval("\"A!\\rB!\"",
197                   "\"A!\\rB!\"");
198        assertEval("\"a\\bB\\tc\\nd\\fe\\rf\\\"g'\\\\h\"",
199                   "\"a\\bB\\tc\\nd\\fe\\rf\\\"g'\\\\h\"");
200        assertEval("\"\\141\\10B\\11c\\nd\\fe\\15f\\42g\\'\\134h\"",
201                   "\"a\\bB\\tc\\nd\\fe\\rf\\\"g'\\\\h\"");
202        assertEval("\"1234567890!@#$%^&*()-_=+qwertQWERT,./<>?;:[]{}\"",
203                   "\"1234567890!@#$%^&*()-_=+qwertQWERT,./<>?;:[]{}\"");
204        assertEval("\"AA\\1\\7\\35\\25\"",
205                   "\"AA\\001\\007\\035\\025\"");
206        assertEval("\"\"",
207                   "\"\"");
208        assertEval("(String)null",
209                   "null");
210    }
211
212    public void testCharRepresentation() {
213        for (String s : new String[]{"'A'", "'Z'", "'0'", "'9'",
214            "'a'", "'z'", "'*'", "'%'",
215            "'\\b'", "'\\t'", "'\\n'", "'\\f'", "'\\r'",
216            "'\"'", "'\\\''", "'\\\\'", "'\\007'", "'\\034'",}) {
217            assertEval(s, s);
218        }
219        assertEval("'\\3'",
220                "'\\003'");
221        assertEval("'\\u001D'",
222                "'\\035'");
223        assertEval("\"a\\bb\\tc\\nd\\fe\\rf\\\"g'\\\\h\".charAt(1)",
224                "'\\b'");
225    }
226}
227