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