langtools/test/tools/javac/api/6731573/T6731573.java
changeset 1472 1e09e143438c
child 5213 6efe8a6d26eb
equal deleted inserted replaced
1471:57506cdfb7b4 1472:1e09e143438c
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any 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  */
       
    31 
       
    32 import java.io.*;
       
    33 import java.util.*;
       
    34 import javax.tools.*;
       
    35 
       
    36 public class T6731573 extends ToolTester {
       
    37 
       
    38     enum DiagnosticType {
       
    39         BASIC(null) {
       
    40             boolean shouldDisplaySource(SourceLine sourceLine) {
       
    41                 return sourceLine != SourceLine.DISABLED;
       
    42             }
       
    43         },
       
    44         RAW("-XDrawDiagnostics") {
       
    45             boolean shouldDisplaySource(SourceLine sourceLine) {
       
    46                 return sourceLine == SourceLine.ENABLED;
       
    47             }
       
    48         };
       
    49 
       
    50         String optValue;
       
    51 
       
    52         DiagnosticType(String optValue) {
       
    53             this.optValue = optValue;
       
    54         }
       
    55 
       
    56         abstract boolean shouldDisplaySource(SourceLine sourceLine);
       
    57     }
       
    58 
       
    59     enum SourceLine {
       
    60         STANDARD(null),
       
    61         ENABLED("-XDshowSource=true"),
       
    62         DISABLED("-XDshowSource=false");
       
    63 
       
    64         String optValue;
       
    65 
       
    66         SourceLine(String optValue) {
       
    67             this.optValue = optValue;
       
    68         }
       
    69     }
       
    70 
       
    71     void checkErrorLine(String output, boolean expected, List<String> options) {
       
    72         System.err.println("\noptions = "+options);
       
    73         System.err.println(output);
       
    74         boolean errLinePresent = output.contains("^");
       
    75         if (errLinePresent != expected) {
       
    76             throw new AssertionError("Error in diagnostic: error line" +
       
    77                     (expected ? "" : " not") + " expected but" +
       
    78                     (errLinePresent ? "" : " not") + " found");
       
    79         }
       
    80     }
       
    81 
       
    82     void exec(DiagnosticType diagType, SourceLine sourceLine) {
       
    83         final Iterable<? extends JavaFileObject> compilationUnits =
       
    84             fm.getJavaFileObjects(new File(test_src, "Erroneous.java"));
       
    85         StringWriter pw = new StringWriter();
       
    86         ArrayList<String> options = new ArrayList<String>();
       
    87         if (diagType.optValue != null)
       
    88             options.add(diagType.optValue);
       
    89         if (sourceLine.optValue != null)
       
    90             options.add(sourceLine.optValue);
       
    91         task = tool.getTask(pw, fm, null, options, null, compilationUnits);
       
    92         task.call();
       
    93         checkErrorLine(pw.toString(),
       
    94                 diagType.shouldDisplaySource(sourceLine),
       
    95                 options);
       
    96     }
       
    97 
       
    98     void test() {
       
    99         for (DiagnosticType dt : DiagnosticType.values()) {
       
   100             for (SourceLine sl : SourceLine.values()) {
       
   101                 exec(dt, sl);
       
   102             }
       
   103         }
       
   104     }
       
   105 
       
   106     public static void main(String... args) throws Exception {
       
   107         new T6731573().test();
       
   108     }
       
   109 }