ErrorTranslationTest.java revision 3294:9adfb22ff08f
157109Speter/* 222514Sdarrenr * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 322514Sdarrenr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4255332Scy * 522514Sdarrenr * This code is free software; you can redistribute it and/or modify it 622514Sdarrenr * under the terms of the GNU General Public License version 2 only, as 7255332Scy * published by the Free Software Foundation. 8255332Scy * 9255332Scy * This code is distributed in the hope that it will be useful, but WITHOUT 10255332Scy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11255332Scy * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12255332Scy * version 2 for more details (a copy is included in the LICENSE file that 13255332Scy * accompanied this code). 14255332Scy * 15255332Scy * You should have received a copy of the GNU General Public License version 16255332Scy * 2 along with this work; if not, write to the Free Software Foundation, 17255332Scy * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1822514Sdarrenr * 19255332Scy * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20255332Scy * or visit www.oracle.com if you need additional information or have any 2122514Sdarrenr * questions. 2222514Sdarrenr */ 23255332Scy 24255332Scy/* 25255332Scy * @test 26255332Scy * @summary Tests for shell error translation 27255332Scy * @modules jdk.compiler/com.sun.tools.javac.api 28255332Scy * jdk.compiler/com.sun.tools.javac.main 29255332Scy * jdk.jdeps/com.sun.tools.javap 30255332Scy * jdk.jshell/jdk.internal.jshell.tool 31255332Scy * @library /tools/lib 32255332Scy * @build KullaTesting TestingInputStream ExpectedDiagnostic ToolBox Compiler 33255332Scy * @run testng ErrorTranslationTest 34255332Scy */ 35255332Scy 36255332Scyimport java.nio.file.Path; 3722514Sdarrenrimport java.util.ArrayList; 38255332Scyimport java.util.List; 39255332Scyimport java.util.function.Consumer; 4022514Sdarrenr 4122514Sdarrenrimport javax.tools.Diagnostic; 42255332Scy 4322514Sdarrenrimport org.testng.annotations.Test; 4422514Sdarrenr 45255332Scyimport static org.testng.Assert.assertEquals; 46255332Scyimport static org.testng.Assert.assertTrue; 4722514Sdarrenr 48255332Scy@Test 4922514Sdarrenrpublic class ErrorTranslationTest extends ReplToolTesting { 50255332Scy 51255332Scy @Test(enabled = false) // TODO 8080353 5222514Sdarrenr public void testErrors() { 53255332Scy test( 54255332Scy a -> assertDiagnostic(a, "abstract void f();", newExpectedDiagnostic(0, 8, 0, -1, -1, Diagnostic.Kind.ERROR)), 55255332Scy a -> assertDiagnostic(a, "native void f();", newExpectedDiagnostic(0, 6, 0, -1, -1, Diagnostic.Kind.ERROR)), 56255332Scy a -> assertDiagnostic(a, "static void f();", newExpectedDiagnostic(0, 16, 0, -1, -1, Diagnostic.Kind.ERROR)), 5722514Sdarrenr a -> assertDiagnostic(a, "synchronized void f();", newExpectedDiagnostic(0, 12, 0, -1, -1, Diagnostic.Kind.ERROR)), 58255332Scy a -> assertDiagnostic(a, "default void f();", newExpectedDiagnostic(0, 7, 0, -1, -1, Diagnostic.Kind.ERROR)) 59255332Scy ); 6057109Speter } 61255332Scy 6257109Speter public void testWarnings() { 63255332Scy List<ReplTest> list = new ArrayList<>(); 6457109Speter ExpectedDiagnostic[] diagnostics = new ExpectedDiagnostic[]{ 65255332Scy newExpectedDiagnostic(0, 6, 0, -1, -1, Diagnostic.Kind.WARNING), 66255332Scy newExpectedDiagnostic(0, 9, 0, -1, -1, Diagnostic.Kind.WARNING), 6722514Sdarrenr newExpectedDiagnostic(0, 7, 0, -1, -1, Diagnostic.Kind.WARNING), 68255332Scy newExpectedDiagnostic(0, 6, 0, -1, -1, Diagnostic.Kind.WARNING), 69255332Scy newExpectedDiagnostic(0, 5, 0, -1, -1, Diagnostic.Kind.WARNING)}; 70255332Scy String[] mods = {"public", "protected", "private", "static", "final"}; 71255332Scy for (int i = 0; i < mods.length; ++i) { 72255332Scy for (String code : new String[] {"class A {}", "void f() {}", "int a;"}) { 7322514Sdarrenr final int finalI = i; 74255332Scy list.add(a -> assertDiagnostic(a, mods[finalI] + " " + code, diagnostics[finalI])); 75255332Scy } 76255332Scy } 77255332Scy test(list.toArray(new ReplTest[list.size()])); 78255332Scy } 79255332Scy 80255332Scy @Test(enabled = false) // TODO 8132147 81255332Scy public void stressTest() { 82255332Scy Compiler compiler = new Compiler(); 83255332Scy Path oome = compiler.getPath("OOME.repl"); 84255332Scy Path soe = compiler.getPath("SOE.repl"); 85255332Scy compiler.writeToFile(oome, 86255332Scy "List<byte[]> list = new ArrayList<>();\n", 87255332Scy "while (true) {\n" + 88255332Scy " list.add(new byte[1000000]);\n" + 89255332Scy "}"); 90255332Scy compiler.writeToFile(soe, 91255332Scy "void f() { f(); }", 92255332Scy "f();"); 93255332Scy List<ReplTest> tests = new ArrayList<>(); 94255332Scy for (int i = 0; i < 25; ++i) { 95255332Scy tests.add(a -> assertCommandCheckOutput(a, "/o " + soe.toString(), 96255332Scy assertStartsWith("| java.lang.StackOverflowError thrown"))); 97255332Scy tests.add(a -> assertCommandCheckOutput(a, "/o " + oome.toString(), 98255332Scy assertStartsWith("| java.lang.OutOfMemoryError thrown: Java heap space"))); 99255332Scy } 100255332Scy test(tests.toArray(new ReplTest[tests.size()])); 101255332Scy } 102255332Scy 103255332Scy private ExpectedDiagnostic newExpectedDiagnostic(long startPosition, long endPosition, long position, 104255332Scy long lineNumber, long columnNumber, Diagnostic.Kind kind) { 105255332Scy return new ExpectedDiagnostic("", startPosition, endPosition, position, lineNumber, columnNumber, kind); 106255332Scy } 107255332Scy 108255332Scy private void assertDiagnostic(boolean after, String cmd, ExpectedDiagnostic expectedDiagnostic) { 109255332Scy assertCommandCheckOutput(after, cmd, assertDiagnostic(cmd, expectedDiagnostic)); 110255332Scy } 111255332Scy 112255332Scy private Consumer<String> assertDiagnostic(String expectedSource, ExpectedDiagnostic expectedDiagnostic) { 113255332Scy int start = (int) expectedDiagnostic.getStartPosition(); 114255332Scy int end = (int) expectedDiagnostic.getEndPosition(); 115255332Scy String expectedMarkingLine = createMarkingLine(start, end); 116255332Scy return s -> { 117255332Scy String[] lines = s.split("\n"); 118255332Scy if (lines.length <= 3) { 119255332Scy throw new AssertionError("Not enough lines: " + s); 120255332Scy } 121255332Scy String kind = getKind(expectedDiagnostic.getKind()); 122255332Scy assertEquals(lines[0], kind); 123255332Scy String source; 124255332Scy String markingLine; 125255332Scy switch (expectedDiagnostic.getKind()) { 126255332Scy case ERROR: 127255332Scy case WARNING: 128255332Scy source = lines[2]; 129255332Scy markingLine = lines[3]; 130255332Scy break; 131255332Scy default: 132255332Scy throw new AssertionError("Unsupported diagnostic kind: " + expectedDiagnostic.getKind()); 133255332Scy } 134255332Scy assertTrue(source.endsWith(expectedSource), "Expected: " + expectedSource + ", found: " + source); 135255332Scy assertEquals(markingLine, expectedMarkingLine, "Input: " + expectedSource + ", marking line: "); 136255332Scy }; 137255332Scy } 138255332Scy 139255332Scy private String createMarkingLine(int start, int end) { 140255332Scy assertTrue(end >= start, String.format("End position %d is less than start position %d", end, start)); 141255332Scy StringBuilder sb = new StringBuilder(); 142255332Scy sb.append("| "); 143255332Scy for (int i = 0; i < start; ++i) { 144255332Scy sb.append(' '); 145255332Scy } 146255332Scy sb.append('^'); 147255332Scy for (int i = 1; i < end - start - 1; ++i) { 148255332Scy sb.append('-'); 149255332Scy } 150255332Scy if (start < end) { 151255332Scy sb.append('^'); 152255332Scy } 153255332Scy return sb.toString(); 154255332Scy } 155255332Scy 156255332Scy public String getKind(Diagnostic.Kind kind) { 157255332Scy switch (kind) { 158255332Scy case WARNING: 159255332Scy return "| Warning:"; 160255332Scy case ERROR: 161255332Scy return "| Error:"; 162255332Scy default: 163255332Scy throw new AssertionError("Unsupported kind: " + kind); 164255332Scy } 165255332Scy } 166255332Scy} 167255332Scy