Path: blob/master/test/langtools/jdk/jshell/ErrorTranslationTest.java
40931 views
/*1* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8188225 824355726* @summary Tests for shell error translation27* @modules jdk.compiler/com.sun.tools.javac.api28* jdk.compiler/com.sun.tools.javac.main29* jdk.jdeps/com.sun.tools.javap30* jdk.jshell/jdk.internal.jshell.tool31* @library /tools/lib32* @build KullaTesting TestingInputStream ExpectedDiagnostic toolbox.ToolBox Compiler33* @run testng ErrorTranslationTest34*/3536import java.nio.file.Path;37import java.util.ArrayList;38import java.util.List;39import java.util.function.Consumer;4041import javax.tools.Diagnostic;4243import org.testng.annotations.Test;4445import static org.testng.Assert.assertEquals;46import static org.testng.Assert.assertTrue;4748@Test49public class ErrorTranslationTest extends ReplToolTesting {5051@Test(enabled = false) // TODO 808035352public void testErrors() {53test(54a -> assertDiagnostic(a, "abstract void f();", newExpectedDiagnostic(0, 8, 0, -1, -1, Diagnostic.Kind.ERROR)),55a -> assertDiagnostic(a, "native void f();", newExpectedDiagnostic(0, 6, 0, -1, -1, Diagnostic.Kind.ERROR)),56a -> assertDiagnostic(a, "static void f();", newExpectedDiagnostic(0, 16, 0, -1, -1, Diagnostic.Kind.ERROR)),57a -> assertDiagnostic(a, "synchronized void f();", newExpectedDiagnostic(0, 12, 0, -1, -1, Diagnostic.Kind.ERROR)),58a -> assertDiagnostic(a, "default void f();", newExpectedDiagnostic(0, 7, 0, -1, -1, Diagnostic.Kind.ERROR))59);60}6162public void testlvtiErrors() {63test(64a -> assertDiagnostic(a, "var broken = () -> {};", newExpectedDiagnostic(0, 22, 0, -1, -1, Diagnostic.Kind.ERROR)),65a -> assertDiagnostic(a, "void t () { var broken = () -> {}; }", newExpectedDiagnostic(12, 34, 0, -1, -1, Diagnostic.Kind.ERROR))66);67}6869public void testExceptionErrors() {70test(71a -> assertDiagnostic(a, "try { } catch (IllegalStateException | java.io.IOException ex) { }", newExpectedDiagnostic(39, 58, -1, -1, -1, Diagnostic.Kind.ERROR))72);73}7475@Test(enabled = false) // TODO 813214776public void stressTest() {77Compiler compiler = new Compiler();78Path oome = compiler.getPath("OOME.repl");79Path soe = compiler.getPath("SOE.repl");80compiler.writeToFile(oome,81"List<byte[]> list = new ArrayList<>();\n",82"while (true) {\n" +83" list.add(new byte[1000000]);\n" +84"}");85compiler.writeToFile(soe,86"void f() { f(); }",87"f();");88List<ReplTest> tests = new ArrayList<>();89for (int i = 0; i < 25; ++i) {90tests.add(a -> assertCommandCheckOutput(a, "/o " + soe.toString(),91assertStartsWith("| java.lang.StackOverflowError thrown")));92tests.add(a -> assertCommandCheckOutput(a, "/o " + oome.toString(),93assertStartsWith("| java.lang.OutOfMemoryError thrown: Java heap space")));94}95test(tests.toArray(new ReplTest[tests.size()]));96}9798private ExpectedDiagnostic newExpectedDiagnostic(long startPosition, long endPosition, long position,99long lineNumber, long columnNumber, Diagnostic.Kind kind) {100return new ExpectedDiagnostic("", startPosition, endPosition, position, lineNumber, columnNumber, kind);101}102103private void assertDiagnostic(boolean after, String cmd, ExpectedDiagnostic expectedDiagnostic) {104assertCommandCheckOutput(after, cmd, assertDiagnostic(cmd, expectedDiagnostic));105}106107private Consumer<String> assertDiagnostic(String expectedSource, ExpectedDiagnostic expectedDiagnostic) {108int start = (int) expectedDiagnostic.getStartPosition();109int end = (int) expectedDiagnostic.getEndPosition();110String expectedMarkingLine = createMarkingLine(start, end);111return s -> {112String[] lines = s.split("\n");113if (lines.length <= 3) {114throw new AssertionError("Not enough lines: " + s);115}116String kind = getKind(expectedDiagnostic.getKind());117assertEquals(lines[0], kind);118boolean found = false;119for (int i = 0; i < lines.length; i++) {120if (lines[i].endsWith(expectedSource)) {121assertEquals(lines[i + 1], expectedMarkingLine, "Input: " + expectedSource + ", marking line: ");122found = true;123}124}125if (!found) {126throw new AssertionError("Did not find: " + expectedSource + " in: " + s);127}128};129}130131private String createMarkingLine(int start, int end) {132assertTrue(end >= start, String.format("End position %d is less than start position %d", end, start));133StringBuilder sb = new StringBuilder();134sb.append("| ");135for (int i = 0; i < start; ++i) {136sb.append(' ');137}138sb.append('^');139for (int i = 1; i < end - start - 1; ++i) {140sb.append('-');141}142if (start < end) {143sb.append('^');144}145return sb.toString();146}147148public String getKind(Diagnostic.Kind kind) {149switch (kind) {150case WARNING:151return "| Warning:";152case ERROR:153return "| Error:";154default:155throw new AssertionError("Unsupported kind: " + kind);156}157}158}159160161