Fri, 02 Nov 2012 14:35:57 -0700
7169362: JDK8: Write compiler tests for repeating annotations for JDK8
Reviewed-by: darcy, jjg
Contributed-by: sonali.goel@oracle.com
1 /*
2 * Copyright (c) 2008, 2010, 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 */
24 /*
25 * @test
26 * @bug 6731573
27 * @summary diagnostic output should optionally include source line
28 * @author Maurizio Cimadamore
29 * @library ../lib
30 */
32 import java.io.*;
33 import java.util.*;
34 import javax.tools.*;
36 public class T6731573 extends ToolTester {
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 };
50 String optValue;
52 DiagnosticType(String optValue) {
53 this.optValue = optValue;
54 }
56 abstract boolean shouldDisplaySource(SourceLine sourceLine);
57 }
59 enum SourceLine {
60 STANDARD(null),
61 ENABLED("-XDshowSource=true"),
62 DISABLED("-XDshowSource=false");
64 String optValue;
66 SourceLine(String optValue) {
67 this.optValue = optValue;
68 }
69 }
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 }
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 }
98 void test() {
99 for (DiagnosticType dt : DiagnosticType.values()) {
100 for (SourceLine sl : SourceLine.values()) {
101 exec(dt, sl);
102 }
103 }
104 }
106 public static void main(String... args) throws Exception {
107 new T6731573().test();
108 }
109 }