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