Mon, 18 Feb 2013 14:29:40 -0800
8008339: Test TargetAnnoCombo.java is broken
Reviewed-by: jjh
1 /*
2 * Copyright (c) 2012, 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 */
24 /**
25 * @test
26 * @bug 8002157
27 * @author sogoel
28 * @summary Basic Syntax test for repeating annotations on all elements
29 * @build Helper
30 * @compile BasicSyntaxCombo.java
31 * @run main BasicSyntaxCombo
32 */
35 import java.util.Arrays;
36 import javax.tools.DiagnosticCollector;
37 import javax.tools.JavaFileObject;
38 import javax.tools.Diagnostic;
40 /*
41 * Generate test src for element kinds with repeating annotations.
42 * The test uses Helper.java to get the template to create test src and
43 * compile the test src.
44 * The test passes if valid test src compile as expected and
45 * and invalid test src fail as expected.
46 */
48 public class BasicSyntaxCombo extends Helper{
49 static int errors = 0;
50 static boolean exitMode = false;
51 static String TESTPKG = "testpkg";
52 static String srcContent = "";
53 static String pkgInfoContent = "";
55 static {
56 // If EXIT_ON_FAIL is set, the combo test will exit at the first error
57 String exitOnFail = System.getenv("EXIT_ON_FAIL");
58 if (exitOnFail == null || exitOnFail == "" ) {
59 exitMode = false;
60 }
61 else {
62 if (exitOnFail.equalsIgnoreCase("YES") ||
63 exitOnFail.equalsIgnoreCase("Y") ||
64 exitOnFail.equalsIgnoreCase("TRUE") ||
65 exitOnFail.equalsIgnoreCase("T")) {
66 exitMode = true;
67 }
68 }
69 }
71 enum TestElem {
72 ANNOTATION_TYPE(true),
73 PACKAGE(true),
74 CONSTRUCTOR(true),
75 FIELD(true),
76 LOCAL_VARIABLE(true),
77 METHOD(true),
78 TYPE(true),
79 PARAMETER(true),
80 INNER_CLASS(true),
81 STATIC_INI(false),
82 INSTANCE_INI(false);
84 TestElem(boolean compile) {
85 this.compile = compile;
86 }
88 boolean compile;
89 boolean shouldCompile() {
90 return compile;
91 }
92 }
94 public static void main(String[] args) throws Exception {
95 new BasicSyntaxCombo().runTest();
96 }
98 public void runTest() throws Exception {
99 boolean result = false;
100 Iterable<? extends JavaFileObject> files = null;
101 int testCtr = 0;
102 for (TestElem type : TestElem.values()) {
103 testCtr++;
104 String className = "BasicCombo_"+type;
105 files = getFileList(className, type);
107 boolean shouldCompile = type.shouldCompile();
108 result = getCompileResult(className, shouldCompile,files);
110 if (shouldCompile && !result) {
111 error(className + " did not compile as expected", srcContent);
112 if(!pkgInfoContent.isEmpty()) {
113 System.out.println("package-info.java contents: " + pkgInfoContent);
114 }
115 }
117 if (!shouldCompile && !result) {
118 error(className + " compiled unexpectedly", srcContent);
119 if(!pkgInfoContent.isEmpty()) {
120 System.out.println("package-info.java contents: " + pkgInfoContent);
121 }
122 }
123 }
125 System.out.println("Total number of tests run: " + testCtr);
126 System.out.println("Total number of errors: " + errors);
128 if (errors > 0)
129 throw new Exception(errors + " errors found");
130 }
132 private boolean getCompileResult(String className, boolean shouldCompile,
133 Iterable<? extends JavaFileObject> files) throws Exception {
135 DiagnosticCollector<JavaFileObject> diagnostics =
136 new DiagnosticCollector<JavaFileObject>();
137 boolean ok = Helper.compileCode(diagnostics,files);
138 if (!shouldCompile && !ok) {
139 checkErrorKeys(className, diagnostics);
140 }
141 return (shouldCompile == ok);
142 }
144 private void checkErrorKeys (
145 String className, DiagnosticCollector<JavaFileObject> diagnostics) throws Exception {
146 String expectedErrKey = "compiler.err.illegal.start.of.type";
147 for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
148 if ((d.getKind() == Diagnostic.Kind.ERROR) &&
149 d.getCode().contains(expectedErrKey)) {
150 break; // Found the expected error
151 } else {
152 error("Incorrect error key, expected = "
153 + expectedErrKey + ", Actual = " + d.getCode()
154 + " for className = " + className, srcContent);
155 }
156 }
157 }
159 private Iterable<? extends JavaFileObject> getFileList(String className,
160 TestElem type ) {
162 String template = Helper.template;
163 String replaceStr = "/*"+type+"*/";
164 StringBuilder annoData = new StringBuilder();
165 annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal())
166 .append(Helper.ContentVars.CONTAINER.getVal())
167 .append(Helper.ContentVars.REPEATABLE.getVal())
168 .append(Helper.ContentVars.BASE.getVal());
170 JavaFileObject pkgInfoFile = null;
172 if (type.equals("PACKAGE")) {
173 srcContent = template.replace(replaceStr, "package testpkg;")
174 .replace("#ClassName", className);
176 String pkgInfoName = TESTPKG+"."+"package-info";
177 pkgInfoContent = Helper.ContentVars.REPEATABLEANNO.getVal()
178 + "package " + TESTPKG + ";"
179 + annoData;
180 pkgInfoFile = getFile(pkgInfoName, pkgInfoContent);
181 } else {
182 template = template.replace(replaceStr, Helper.ContentVars.REPEATABLEANNO.getVal())
183 .replace("#ClassName", className);
184 srcContent = annoData + template;
185 }
187 JavaFileObject srcFile = getFile(className, srcContent);
189 Iterable<? extends JavaFileObject> files = null;
190 if (pkgInfoFile != null) {
191 files = Arrays.asList(pkgInfoFile,srcFile);
192 }
193 else {
194 files = Arrays.asList(srcFile);
195 }
196 return files;
197 }
199 private void error(String msg, String... contents) throws Exception {
200 System.out.println("error: " + msg);
201 errors++;
202 if (contents.length == 1) {
203 System.out.println("Contents = " + contents[0]);
204 }
205 // Test exits as soon as it gets a failure
206 if (exitMode) throw new Exception();
207 }
208 }