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