Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/jdk/jshell/ErrorTranslationTest.java
40931 views
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
26
* @bug 8188225 8243557
27
* @summary Tests for shell error translation
28
* @modules jdk.compiler/com.sun.tools.javac.api
29
* jdk.compiler/com.sun.tools.javac.main
30
* jdk.jdeps/com.sun.tools.javap
31
* jdk.jshell/jdk.internal.jshell.tool
32
* @library /tools/lib
33
* @build KullaTesting TestingInputStream ExpectedDiagnostic toolbox.ToolBox Compiler
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
63
public void testlvtiErrors() {
64
test(
65
a -> assertDiagnostic(a, "var broken = () -> {};", newExpectedDiagnostic(0, 22, 0, -1, -1, Diagnostic.Kind.ERROR)),
66
a -> assertDiagnostic(a, "void t () { var broken = () -> {}; }", newExpectedDiagnostic(12, 34, 0, -1, -1, Diagnostic.Kind.ERROR))
67
);
68
}
69
70
public void testExceptionErrors() {
71
test(
72
a -> assertDiagnostic(a, "try { } catch (IllegalStateException | java.io.IOException ex) { }", newExpectedDiagnostic(39, 58, -1, -1, -1, Diagnostic.Kind.ERROR))
73
);
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
boolean found = false;
120
for (int i = 0; i < lines.length; i++) {
121
if (lines[i].endsWith(expectedSource)) {
122
assertEquals(lines[i + 1], expectedMarkingLine, "Input: " + expectedSource + ", marking line: ");
123
found = true;
124
}
125
}
126
if (!found) {
127
throw new AssertionError("Did not find: " + expectedSource + " in: " + s);
128
}
129
};
130
}
131
132
private String createMarkingLine(int start, int end) {
133
assertTrue(end >= start, String.format("End position %d is less than start position %d", end, start));
134
StringBuilder sb = new StringBuilder();
135
sb.append("| ");
136
for (int i = 0; i < start; ++i) {
137
sb.append(' ');
138
}
139
sb.append('^');
140
for (int i = 1; i < end - start - 1; ++i) {
141
sb.append('-');
142
}
143
if (start < end) {
144
sb.append('^');
145
}
146
return sb.toString();
147
}
148
149
public String getKind(Diagnostic.Kind kind) {
150
switch (kind) {
151
case WARNING:
152
return "| Warning:";
153
case ERROR:
154
return "| Error:";
155
default:
156
throw new AssertionError("Unsupported kind: " + kind);
157
}
158
}
159
}
160
161