author | stefank |
Thu, 07 Jun 2018 10:11:36 +0200 | |
changeset 50621 | 4216de02077e |
parent 47318 | 423f5e46016e |
permissions | -rw-r--r-- |
33362 | 1 |
/* |
36526 | 2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33362 | 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 |
|
47318
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
26 |
* @bug 8188225 |
33362 | 27 |
* @summary Tests for shell error translation |
35359 | 28 |
* @modules jdk.compiler/com.sun.tools.javac.api |
29 |
* jdk.compiler/com.sun.tools.javac.main |
|
36526 | 30 |
* jdk.jdeps/com.sun.tools.javap |
35359 | 31 |
* jdk.jshell/jdk.internal.jshell.tool |
33362 | 32 |
* @library /tools/lib |
36778
e04318f39f92
8152897: refactor ToolBox to allow reduced documented dependencies
jjg
parents:
36526
diff
changeset
|
33 |
* @build KullaTesting TestingInputStream ExpectedDiagnostic toolbox.ToolBox Compiler |
33362 | 34 |
* @run testng ErrorTranslationTest |
35 |
*/ |
|
36 |
||
37 |
import java.nio.file.Path; |
|
38 |
import java.util.ArrayList; |
|
39 |
import java.util.List; |
|
40 |
import java.util.function.Consumer; |
|
41 |
||
42 |
import javax.tools.Diagnostic; |
|
43 |
||
44 |
import org.testng.annotations.Test; |
|
45 |
||
46 |
import static org.testng.Assert.assertEquals; |
|
47 |
import static org.testng.Assert.assertTrue; |
|
48 |
||
49 |
@Test |
|
50 |
public class ErrorTranslationTest extends ReplToolTesting { |
|
51 |
||
52 |
@Test(enabled = false) // TODO 8080353 |
|
53 |
public void testErrors() { |
|
54 |
test( |
|
55 |
a -> assertDiagnostic(a, "abstract void f();", newExpectedDiagnostic(0, 8, 0, -1, -1, Diagnostic.Kind.ERROR)), |
|
56 |
a -> assertDiagnostic(a, "native void f();", newExpectedDiagnostic(0, 6, 0, -1, -1, Diagnostic.Kind.ERROR)), |
|
57 |
a -> assertDiagnostic(a, "static void f();", newExpectedDiagnostic(0, 16, 0, -1, -1, Diagnostic.Kind.ERROR)), |
|
58 |
a -> assertDiagnostic(a, "synchronized void f();", newExpectedDiagnostic(0, 12, 0, -1, -1, Diagnostic.Kind.ERROR)), |
|
59 |
a -> assertDiagnostic(a, "default void f();", newExpectedDiagnostic(0, 7, 0, -1, -1, Diagnostic.Kind.ERROR)) |
|
60 |
); |
|
61 |
} |
|
62 |
||
47318
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
63 |
public void testlvtiErrors() { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
64 |
test( |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
65 |
a -> assertDiagnostic(a, "var broken = () -> {};", newExpectedDiagnostic(0, 22, 0, -1, -1, Diagnostic.Kind.ERROR)), |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
66 |
a -> assertDiagnostic(a, "void t () { var broken = () -> {}; }", newExpectedDiagnostic(12, 34, 0, -1, -1, Diagnostic.Kind.ERROR)) |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
67 |
); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
68 |
} |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
69 |
|
33362 | 70 |
public void testWarnings() { |
71 |
List<ReplTest> list = new ArrayList<>(); |
|
72 |
ExpectedDiagnostic[] diagnostics = new ExpectedDiagnostic[]{ |
|
73 |
newExpectedDiagnostic(0, 6, 0, -1, -1, Diagnostic.Kind.WARNING), |
|
74 |
newExpectedDiagnostic(0, 5, 0, -1, -1, Diagnostic.Kind.WARNING)}; |
|
41858
5843b57ce3a6
8167643: JShell: silently ignore access modifiers (as semantically irrelevant)
rfield
parents:
36778
diff
changeset
|
75 |
String[] mods = {"static", "final"}; |
33362 | 76 |
for (int i = 0; i < mods.length; ++i) { |
77 |
for (String code : new String[] {"class A {}", "void f() {}", "int a;"}) { |
|
78 |
final int finalI = i; |
|
79 |
list.add(a -> assertDiagnostic(a, mods[finalI] + " " + code, diagnostics[finalI])); |
|
80 |
} |
|
81 |
} |
|
82 |
test(list.toArray(new ReplTest[list.size()])); |
|
83 |
} |
|
84 |
||
85 |
@Test(enabled = false) // TODO 8132147 |
|
86 |
public void stressTest() { |
|
87 |
Compiler compiler = new Compiler(); |
|
88 |
Path oome = compiler.getPath("OOME.repl"); |
|
89 |
Path soe = compiler.getPath("SOE.repl"); |
|
90 |
compiler.writeToFile(oome, |
|
91 |
"List<byte[]> list = new ArrayList<>();\n", |
|
92 |
"while (true) {\n" + |
|
93 |
" list.add(new byte[1000000]);\n" + |
|
94 |
"}"); |
|
95 |
compiler.writeToFile(soe, |
|
96 |
"void f() { f(); }", |
|
97 |
"f();"); |
|
98 |
List<ReplTest> tests = new ArrayList<>(); |
|
99 |
for (int i = 0; i < 25; ++i) { |
|
100 |
tests.add(a -> assertCommandCheckOutput(a, "/o " + soe.toString(), |
|
101 |
assertStartsWith("| java.lang.StackOverflowError thrown"))); |
|
102 |
tests.add(a -> assertCommandCheckOutput(a, "/o " + oome.toString(), |
|
103 |
assertStartsWith("| java.lang.OutOfMemoryError thrown: Java heap space"))); |
|
104 |
} |
|
105 |
test(tests.toArray(new ReplTest[tests.size()])); |
|
106 |
} |
|
107 |
||
108 |
private ExpectedDiagnostic newExpectedDiagnostic(long startPosition, long endPosition, long position, |
|
109 |
long lineNumber, long columnNumber, Diagnostic.Kind kind) { |
|
110 |
return new ExpectedDiagnostic("", startPosition, endPosition, position, lineNumber, columnNumber, kind); |
|
111 |
} |
|
112 |
||
113 |
private void assertDiagnostic(boolean after, String cmd, ExpectedDiagnostic expectedDiagnostic) { |
|
114 |
assertCommandCheckOutput(after, cmd, assertDiagnostic(cmd, expectedDiagnostic)); |
|
115 |
} |
|
116 |
||
117 |
private Consumer<String> assertDiagnostic(String expectedSource, ExpectedDiagnostic expectedDiagnostic) { |
|
118 |
int start = (int) expectedDiagnostic.getStartPosition(); |
|
119 |
int end = (int) expectedDiagnostic.getEndPosition(); |
|
120 |
String expectedMarkingLine = createMarkingLine(start, end); |
|
121 |
return s -> { |
|
122 |
String[] lines = s.split("\n"); |
|
123 |
if (lines.length <= 3) { |
|
124 |
throw new AssertionError("Not enough lines: " + s); |
|
125 |
} |
|
126 |
String kind = getKind(expectedDiagnostic.getKind()); |
|
127 |
assertEquals(lines[0], kind); |
|
47318
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
128 |
boolean found = false; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
129 |
for (int i = 0; i < lines.length; i++) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
130 |
if (lines[i].endsWith(expectedSource)) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
131 |
assertEquals(lines[i + 1], expectedMarkingLine, "Input: " + expectedSource + ", marking line: "); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
132 |
found = true; |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
133 |
} |
33362 | 134 |
} |
47318
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
135 |
if (!found) { |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
136 |
throw new AssertionError("Did not find: " + expectedSource + " in: " + s); |
423f5e46016e
8188225: AST could be improved in presence of var types.
jlahoda
parents:
47216
diff
changeset
|
137 |
} |
33362 | 138 |
}; |
139 |
} |
|
140 |
||
141 |
private String createMarkingLine(int start, int end) { |
|
142 |
assertTrue(end >= start, String.format("End position %d is less than start position %d", end, start)); |
|
143 |
StringBuilder sb = new StringBuilder(); |
|
144 |
sb.append("| "); |
|
145 |
for (int i = 0; i < start; ++i) { |
|
146 |
sb.append(' '); |
|
147 |
} |
|
148 |
sb.append('^'); |
|
149 |
for (int i = 1; i < end - start - 1; ++i) { |
|
150 |
sb.append('-'); |
|
151 |
} |
|
152 |
if (start < end) { |
|
153 |
sb.append('^'); |
|
154 |
} |
|
155 |
return sb.toString(); |
|
156 |
} |
|
157 |
||
158 |
public String getKind(Diagnostic.Kind kind) { |
|
159 |
switch (kind) { |
|
160 |
case WARNING: |
|
161 |
return "| Warning:"; |
|
162 |
case ERROR: |
|
163 |
return "| Error:"; |
|
164 |
default: |
|
165 |
throw new AssertionError("Unsupported kind: " + kind); |
|
166 |
} |
|
167 |
} |
|
168 |
} |