T6731573.java revision 2689:f839b50088bc
11592Srgrimes/* 21592Srgrimes * Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved. 31592Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 41592Srgrimes * 51592Srgrimes * This code is free software; you can redistribute it and/or modify it 61592Srgrimes * under the terms of the GNU General Public License version 2 only, as 71592Srgrimes * published by the Free Software Foundation. 81592Srgrimes * 91592Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT 101592Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 111592Srgrimes * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 121592Srgrimes * version 2 for more details (a copy is included in the LICENSE file that 131592Srgrimes * accompanied this code). 141592Srgrimes * 151592Srgrimes * You should have received a copy of the GNU General Public License version 161592Srgrimes * 2 along with this work; if not, write to the Free Software Foundation, 171592Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 181592Srgrimes * 191592Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 201592Srgrimes * or visit www.oracle.com if you need additional information or have any 211592Srgrimes * questions. 221592Srgrimes */ 231592Srgrimes 241592Srgrimes/* 251592Srgrimes * @test 261592Srgrimes * @bug 6731573 271592Srgrimes * @summary diagnostic output should optionally include source line 281592Srgrimes * @author Maurizio Cimadamore 291592Srgrimes * @library ../lib 301592Srgrimes * @build ToolTester 311592Srgrimes * @run main T6731573 321592Srgrimes */ 331592Srgrimes 3417478Smarkmimport java.io.*; 351592Srgrimesimport java.util.*; 361592Srgrimesimport javax.tools.*; 371592Srgrimes 381592Srgrimespublic class T6731573 extends ToolTester { 391592Srgrimes 4017478Smarkm enum DiagnosticType { 411592Srgrimes BASIC(null) { 4231329Scharnier boolean shouldDisplaySource(SourceLine sourceLine) { 4317478Smarkm return sourceLine != SourceLine.DISABLED; 441592Srgrimes } 4531329Scharnier }, 4631329Scharnier RAW("-XDrawDiagnostics") { 4736612Sjb boolean shouldDisplaySource(SourceLine sourceLine) { 481592Srgrimes return sourceLine == SourceLine.ENABLED; 491592Srgrimes } 501592Srgrimes }; 511592Srgrimes 521592Srgrimes String optValue; 531592Srgrimes 541592Srgrimes DiagnosticType(String optValue) { 551592Srgrimes this.optValue = optValue; 561592Srgrimes } 571592Srgrimes 588240Swollman abstract boolean shouldDisplaySource(SourceLine sourceLine); 591592Srgrimes } 601592Srgrimes 611592Srgrimes enum SourceLine { 621592Srgrimes STANDARD(null), 638240Swollman ENABLED("-XDshowSource=true"), 641592Srgrimes DISABLED("-XDshowSource=false"); 651592Srgrimes 661592Srgrimes String optValue; 671592Srgrimes 681592Srgrimes SourceLine(String optValue) { 691592Srgrimes this.optValue = optValue; 701592Srgrimes } 711592Srgrimes } 721592Srgrimes 731592Srgrimes void checkErrorLine(String output, boolean expected, List<String> options) { 741592Srgrimes System.err.println("\noptions = "+options); 751592Srgrimes System.err.println(output); 761592Srgrimes boolean errLinePresent = output.contains("^"); 771592Srgrimes if (errLinePresent != expected) { 781592Srgrimes throw new AssertionError("Error in diagnostic: error line" + 7925187Sdavidn (expected ? "" : " not") + " expected but" + 801592Srgrimes (errLinePresent ? "" : " not") + " found"); 811592Srgrimes } 821592Srgrimes } 831592Srgrimes 841592Srgrimes void exec(DiagnosticType diagType, SourceLine sourceLine) { 851592Srgrimes final Iterable<? extends JavaFileObject> compilationUnits = 861592Srgrimes fm.getJavaFileObjects(new File(test_src, "Erroneous.java")); 871592Srgrimes StringWriter pw = new StringWriter(); 8813139Speter ArrayList<String> options = new ArrayList<String>(); 8925101Sdavidn if (diagType.optValue != null) 9025101Sdavidn options.add(diagType.optValue); 9125101Sdavidn if (sourceLine.optValue != null) 921592Srgrimes options.add(sourceLine.optValue); 933938Spst task = tool.getTask(pw, fm, null, options, null, compilationUnits); 943938Spst task.call(); 953938Spst checkErrorLine(pw.toString(), 963938Spst diagType.shouldDisplaySource(sourceLine), 971592Srgrimes options); 981592Srgrimes } 991592Srgrimes 1001592Srgrimes void test() { 1011592Srgrimes for (DiagnosticType dt : DiagnosticType.values()) { 1021592Srgrimes for (SourceLine sl : SourceLine.values()) { 1031592Srgrimes exec(dt, sl); 1041592Srgrimes } 1051592Srgrimes } 10625165Sdavidn } 10725165Sdavidn 10825165Sdavidn public static void main(String... args) throws Exception { 10925165Sdavidn try (T6731573 t = new T6731573()) { 1101592Srgrimes t.test(); 11125165Sdavidn } 1121592Srgrimes } 1131592Srgrimes} 1141592Srgrimes