1/* 2 * Copyright (c) 2008, 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 6731573 27 * @summary diagnostic output should optionally include source line 28 * @author Maurizio Cimadamore 29 * @library ../lib 30 * @modules java.compiler 31 * jdk.compiler 32 * @build ToolTester 33 * @run main T6731573 34 */ 35 36import java.io.*; 37import java.util.*; 38import javax.tools.*; 39 40public class T6731573 extends ToolTester { 41 42 enum DiagnosticType { 43 BASIC(null) { 44 boolean shouldDisplaySource(SourceLine sourceLine) { 45 return sourceLine != SourceLine.DISABLED; 46 } 47 }, 48 RAW("-XDrawDiagnostics") { 49 boolean shouldDisplaySource(SourceLine sourceLine) { 50 return sourceLine == SourceLine.ENABLED; 51 } 52 }; 53 54 String optValue; 55 56 DiagnosticType(String optValue) { 57 this.optValue = optValue; 58 } 59 60 abstract boolean shouldDisplaySource(SourceLine sourceLine); 61 } 62 63 enum SourceLine { 64 STANDARD(null), 65 ENABLED("--diags:showSource=true"), 66 DISABLED("--diags:showSource=false"); 67 68 String optValue; 69 70 SourceLine(String optValue) { 71 this.optValue = optValue; 72 } 73 } 74 75 void checkErrorLine(String output, boolean expected, List<String> options) { 76 System.err.println("\noptions = "+options); 77 System.err.println(output); 78 boolean errLinePresent = output.contains("^"); 79 if (errLinePresent != expected) { 80 throw new AssertionError("Error in diagnostic: error line" + 81 (expected ? "" : " not") + " expected but" + 82 (errLinePresent ? "" : " not") + " found"); 83 } 84 } 85 86 void exec(DiagnosticType diagType, SourceLine sourceLine) { 87 final Iterable<? extends JavaFileObject> compilationUnits = 88 fm.getJavaFileObjects(new File(test_src, "Erroneous.java")); 89 StringWriter pw = new StringWriter(); 90 ArrayList<String> options = new ArrayList<String>(); 91 if (diagType.optValue != null) 92 options.add(diagType.optValue); 93 if (sourceLine.optValue != null) 94 options.add(sourceLine.optValue); 95 task = tool.getTask(pw, fm, null, options, null, compilationUnits); 96 task.call(); 97 checkErrorLine(pw.toString(), 98 diagType.shouldDisplaySource(sourceLine), 99 options); 100 } 101 102 void test() { 103 for (DiagnosticType dt : DiagnosticType.values()) { 104 for (SourceLine sl : SourceLine.values()) { 105 exec(dt, sl); 106 } 107 } 108 } 109 110 public static void main(String... args) throws Exception { 111 try (T6731573 t = new T6731573()) { 112 t.test(); 113 } 114 } 115} 116