langtools/test/tools/javac/api/6731573/T6731573.java
author vromero
Tue, 06 Sep 2016 17:04:43 -0700
changeset 40835 6ab9ed1abc46
parent 39812 6272642715a1
permissions -rw-r--r--
8162546: change hidden options -Xdebug to --debug, -XshouldStop to --should-stop, and -diags to --diags Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
     1
/*
39812
6272642715a1 8161019: javac, fold formatter options
vromero
parents: 30730
diff changeset
     2
 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
     4
 *
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
     8
 *
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    14
 *
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    18
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5213
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5213
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5213
diff changeset
    21
 * questions.
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    22
 */
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    23
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    24
/*
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    25
 * @test
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    26
 * @bug     6731573
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    27
 * @summary diagnostic output should optionally include source line
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    28
 * @author  Maurizio Cimadamore
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    29
 * @library ../lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27321
diff changeset
    30
 * @modules java.compiler
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27321
diff changeset
    31
 *          jdk.compiler
17556
eced45696767 8014363: javac test class ToolTester handles classpath incorrectly
jjg
parents: 7681
diff changeset
    32
 * @build ToolTester
eced45696767 8014363: javac test class ToolTester handles classpath incorrectly
jjg
parents: 7681
diff changeset
    33
 * @run main T6731573
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    34
 */
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    35
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    36
import java.io.*;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    37
import java.util.*;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    38
import javax.tools.*;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    39
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    40
public class T6731573 extends ToolTester {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    41
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    42
    enum DiagnosticType {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    43
        BASIC(null) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    44
            boolean shouldDisplaySource(SourceLine sourceLine) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    45
                return sourceLine != SourceLine.DISABLED;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    46
            }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    47
        },
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    48
        RAW("-XDrawDiagnostics") {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    49
            boolean shouldDisplaySource(SourceLine sourceLine) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    50
                return sourceLine == SourceLine.ENABLED;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    51
            }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    52
        };
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    53
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    54
        String optValue;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    55
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    56
        DiagnosticType(String optValue) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    57
            this.optValue = optValue;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    58
        }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    59
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    60
        abstract boolean shouldDisplaySource(SourceLine sourceLine);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    61
    }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    62
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    63
    enum SourceLine {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    64
        STANDARD(null),
40835
6ab9ed1abc46 8162546: change hidden options -Xdebug to --debug, -XshouldStop to --should-stop, and -diags to --diags
vromero
parents: 39812
diff changeset
    65
        ENABLED("--diags:showSource=true"),
6ab9ed1abc46 8162546: change hidden options -Xdebug to --debug, -XshouldStop to --should-stop, and -diags to --diags
vromero
parents: 39812
diff changeset
    66
        DISABLED("--diags:showSource=false");
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    67
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    68
        String optValue;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    69
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    70
        SourceLine(String optValue) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    71
            this.optValue = optValue;
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    72
        }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    73
    }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    74
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    75
    void checkErrorLine(String output, boolean expected, List<String> options) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    76
        System.err.println("\noptions = "+options);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    77
        System.err.println(output);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    78
        boolean errLinePresent = output.contains("^");
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    79
        if (errLinePresent != expected) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    80
            throw new AssertionError("Error in diagnostic: error line" +
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    81
                    (expected ? "" : " not") + " expected but" +
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    82
                    (errLinePresent ? "" : " not") + " found");
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    83
        }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    84
    }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    85
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    86
    void exec(DiagnosticType diagType, SourceLine sourceLine) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    87
        final Iterable<? extends JavaFileObject> compilationUnits =
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    88
            fm.getJavaFileObjects(new File(test_src, "Erroneous.java"));
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    89
        StringWriter pw = new StringWriter();
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    90
        ArrayList<String> options = new ArrayList<String>();
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    91
        if (diagType.optValue != null)
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    92
            options.add(diagType.optValue);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    93
        if (sourceLine.optValue != null)
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    94
            options.add(sourceLine.optValue);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    95
        task = tool.getTask(pw, fm, null, options, null, compilationUnits);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    96
        task.call();
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    97
        checkErrorLine(pw.toString(),
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    98
                diagType.shouldDisplaySource(sourceLine),
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
    99
                options);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   100
    }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   101
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   102
    void test() {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   103
        for (DiagnosticType dt : DiagnosticType.values()) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   104
            for (SourceLine sl : SourceLine.values()) {
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   105
                exec(dt, sl);
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   106
            }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   107
        }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   108
    }
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   109
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   110
    public static void main(String... args) throws Exception {
27321
c8eceba990bf 8062514: Update ToolTester tests to close file manager
jjg
parents: 17556
diff changeset
   111
        try (T6731573 t = new T6731573()) {
c8eceba990bf 8062514: Update ToolTester tests to close file manager
jjg
parents: 17556
diff changeset
   112
            t.test();
c8eceba990bf 8062514: Update ToolTester tests to close file manager
jjg
parents: 17556
diff changeset
   113
        }
1472
1e09e143438c 6731573: diagnostic output should optionally include source line
mcimadamore
parents:
diff changeset
   114
    }
5213
6efe8a6d26eb 6933914: fix missing newlines
jjg
parents: 1472
diff changeset
   115
}