Mon, 18 Feb 2013 14:29:40 -0800
8008339: Test TargetAnnoCombo.java is broken
Reviewed-by: jjh
jjg@1401 | 1 | /* |
jjg@1492 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
jjg@1401 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1401 | 4 | * |
jjg@1401 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1401 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1401 | 7 | * published by the Free Software Foundation. |
jjg@1401 | 8 | * |
jjg@1401 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1401 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1401 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1401 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1401 | 13 | * accompanied this code). |
jjg@1401 | 14 | * |
jjg@1401 | 15 | * You should have received a copy of the GNU General Public License version |
jjg@1401 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1401 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1401 | 18 | * |
jjg@1401 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1401 | 20 | * or visit www.oracle.com if you need additional information or have any |
jjg@1401 | 21 | * questions. |
jjg@1401 | 22 | */ |
jjg@1401 | 23 | |
jjg@1401 | 24 | /** |
jjg@1401 | 25 | * @test |
jjg@1401 | 26 | * @bug 8002157 |
jjg@1401 | 27 | * @author sogoel |
jjg@1401 | 28 | * @summary Basic Syntax test for repeating annotations on all elements |
jjg@1401 | 29 | * @build Helper |
jjg@1401 | 30 | * @compile BasicSyntaxCombo.java |
jjg@1401 | 31 | * @run main BasicSyntaxCombo |
jjg@1401 | 32 | */ |
jjg@1401 | 33 | |
jjg@1401 | 34 | |
jjg@1401 | 35 | import java.util.Arrays; |
jjg@1401 | 36 | import javax.tools.DiagnosticCollector; |
jjg@1401 | 37 | import javax.tools.JavaFileObject; |
jjg@1401 | 38 | import javax.tools.Diagnostic; |
jjg@1401 | 39 | |
jjg@1401 | 40 | /* |
jjg@1401 | 41 | * Generate test src for element kinds with repeating annotations. |
jjg@1401 | 42 | * The test uses Helper.java to get the template to create test src and |
jjg@1401 | 43 | * compile the test src. |
jjg@1401 | 44 | * The test passes if valid test src compile as expected and |
jjg@1401 | 45 | * and invalid test src fail as expected. |
jjg@1401 | 46 | */ |
jjg@1401 | 47 | |
jjg@1401 | 48 | public class BasicSyntaxCombo extends Helper{ |
jjg@1401 | 49 | static int errors = 0; |
jjg@1401 | 50 | static boolean exitMode = false; |
jjg@1401 | 51 | static String TESTPKG = "testpkg"; |
jjg@1401 | 52 | static String srcContent = ""; |
jjg@1401 | 53 | static String pkgInfoContent = ""; |
jjg@1401 | 54 | |
jjg@1401 | 55 | static { |
jjg@1401 | 56 | // If EXIT_ON_FAIL is set, the combo test will exit at the first error |
jjg@1401 | 57 | String exitOnFail = System.getenv("EXIT_ON_FAIL"); |
jjg@1401 | 58 | if (exitOnFail == null || exitOnFail == "" ) { |
jjg@1401 | 59 | exitMode = false; |
jjg@1401 | 60 | } |
jjg@1401 | 61 | else { |
jjg@1401 | 62 | if (exitOnFail.equalsIgnoreCase("YES") || |
jjg@1401 | 63 | exitOnFail.equalsIgnoreCase("Y") || |
jjg@1401 | 64 | exitOnFail.equalsIgnoreCase("TRUE") || |
jjg@1401 | 65 | exitOnFail.equalsIgnoreCase("T")) { |
jjg@1401 | 66 | exitMode = true; |
jjg@1401 | 67 | } |
jjg@1401 | 68 | } |
jjg@1401 | 69 | } |
jjg@1401 | 70 | |
jjg@1401 | 71 | enum TestElem { |
jjg@1401 | 72 | ANNOTATION_TYPE(true), |
jjg@1401 | 73 | PACKAGE(true), |
jjg@1401 | 74 | CONSTRUCTOR(true), |
jjg@1401 | 75 | FIELD(true), |
jjg@1401 | 76 | LOCAL_VARIABLE(true), |
jjg@1401 | 77 | METHOD(true), |
jjg@1401 | 78 | TYPE(true), |
jjg@1401 | 79 | PARAMETER(true), |
jjg@1401 | 80 | INNER_CLASS(true), |
jjg@1401 | 81 | STATIC_INI(false), |
jjg@1401 | 82 | INSTANCE_INI(false); |
jjg@1401 | 83 | |
jjg@1401 | 84 | TestElem(boolean compile) { |
jjg@1401 | 85 | this.compile = compile; |
jjg@1401 | 86 | } |
jjg@1401 | 87 | |
jjg@1401 | 88 | boolean compile; |
jjg@1401 | 89 | boolean shouldCompile() { |
jjg@1401 | 90 | return compile; |
jjg@1401 | 91 | } |
jjg@1401 | 92 | } |
jjg@1401 | 93 | |
jjg@1401 | 94 | public static void main(String[] args) throws Exception { |
jjg@1401 | 95 | new BasicSyntaxCombo().runTest(); |
jjg@1401 | 96 | } |
jjg@1401 | 97 | |
jjg@1401 | 98 | public void runTest() throws Exception { |
jjg@1401 | 99 | boolean result = false; |
jjg@1401 | 100 | Iterable<? extends JavaFileObject> files = null; |
jjg@1401 | 101 | int testCtr = 0; |
jjg@1401 | 102 | for (TestElem type : TestElem.values()) { |
jjg@1401 | 103 | testCtr++; |
jjg@1401 | 104 | String className = "BasicCombo_"+type; |
jjg@1401 | 105 | files = getFileList(className, type); |
jjg@1401 | 106 | |
jjg@1401 | 107 | boolean shouldCompile = type.shouldCompile(); |
jjg@1401 | 108 | result = getCompileResult(className, shouldCompile,files); |
jjg@1401 | 109 | |
jjg@1401 | 110 | if (shouldCompile && !result) { |
jjg@1401 | 111 | error(className + " did not compile as expected", srcContent); |
jjg@1401 | 112 | if(!pkgInfoContent.isEmpty()) { |
jjg@1401 | 113 | System.out.println("package-info.java contents: " + pkgInfoContent); |
jjg@1401 | 114 | } |
jjg@1401 | 115 | } |
jjg@1401 | 116 | |
jjg@1401 | 117 | if (!shouldCompile && !result) { |
jjg@1401 | 118 | error(className + " compiled unexpectedly", srcContent); |
jjg@1401 | 119 | if(!pkgInfoContent.isEmpty()) { |
jjg@1401 | 120 | System.out.println("package-info.java contents: " + pkgInfoContent); |
jjg@1401 | 121 | } |
jjg@1401 | 122 | } |
jjg@1401 | 123 | } |
jjg@1401 | 124 | |
jjg@1401 | 125 | System.out.println("Total number of tests run: " + testCtr); |
jjg@1401 | 126 | System.out.println("Total number of errors: " + errors); |
jjg@1401 | 127 | |
jjg@1401 | 128 | if (errors > 0) |
jjg@1401 | 129 | throw new Exception(errors + " errors found"); |
jjg@1401 | 130 | } |
jjg@1401 | 131 | |
jjg@1401 | 132 | private boolean getCompileResult(String className, boolean shouldCompile, |
jjg@1401 | 133 | Iterable<? extends JavaFileObject> files) throws Exception { |
jjg@1401 | 134 | |
jjg@1401 | 135 | DiagnosticCollector<JavaFileObject> diagnostics = |
jjg@1401 | 136 | new DiagnosticCollector<JavaFileObject>(); |
jjg@1401 | 137 | boolean ok = Helper.compileCode(diagnostics,files); |
jjg@1401 | 138 | if (!shouldCompile && !ok) { |
jjg@1401 | 139 | checkErrorKeys(className, diagnostics); |
jjg@1401 | 140 | } |
jjg@1401 | 141 | return (shouldCompile == ok); |
jjg@1401 | 142 | } |
jjg@1401 | 143 | |
jjg@1401 | 144 | private void checkErrorKeys ( |
jjg@1401 | 145 | String className, DiagnosticCollector<JavaFileObject> diagnostics) throws Exception { |
jjg@1401 | 146 | String expectedErrKey = "compiler.err.illegal.start.of.type"; |
jjg@1401 | 147 | for (Diagnostic<?> d : diagnostics.getDiagnostics()) { |
jjg@1401 | 148 | if ((d.getKind() == Diagnostic.Kind.ERROR) && |
jjg@1401 | 149 | d.getCode().contains(expectedErrKey)) { |
jjg@1401 | 150 | break; // Found the expected error |
jjg@1401 | 151 | } else { |
jjg@1401 | 152 | error("Incorrect error key, expected = " |
jjg@1401 | 153 | + expectedErrKey + ", Actual = " + d.getCode() |
jjg@1401 | 154 | + " for className = " + className, srcContent); |
jjg@1401 | 155 | } |
jjg@1401 | 156 | } |
jjg@1401 | 157 | } |
jjg@1401 | 158 | |
jjg@1401 | 159 | private Iterable<? extends JavaFileObject> getFileList(String className, |
jjg@1401 | 160 | TestElem type ) { |
jjg@1401 | 161 | |
jjg@1401 | 162 | String template = Helper.template; |
jjg@1401 | 163 | String replaceStr = "/*"+type+"*/"; |
jjg@1401 | 164 | StringBuilder annoData = new StringBuilder(); |
jjg@1401 | 165 | annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()) |
jjg@1401 | 166 | .append(Helper.ContentVars.CONTAINER.getVal()) |
jjg@1492 | 167 | .append(Helper.ContentVars.REPEATABLE.getVal()) |
jjg@1401 | 168 | .append(Helper.ContentVars.BASE.getVal()); |
jjg@1401 | 169 | |
jjg@1401 | 170 | JavaFileObject pkgInfoFile = null; |
jjg@1401 | 171 | |
jjg@1401 | 172 | if (type.equals("PACKAGE")) { |
jjg@1401 | 173 | srcContent = template.replace(replaceStr, "package testpkg;") |
jjg@1401 | 174 | .replace("#ClassName", className); |
jjg@1401 | 175 | |
jjg@1401 | 176 | String pkgInfoName = TESTPKG+"."+"package-info"; |
jjg@1401 | 177 | pkgInfoContent = Helper.ContentVars.REPEATABLEANNO.getVal() |
jjg@1401 | 178 | + "package " + TESTPKG + ";" |
jjg@1401 | 179 | + annoData; |
jjg@1401 | 180 | pkgInfoFile = getFile(pkgInfoName, pkgInfoContent); |
jjg@1401 | 181 | } else { |
jjg@1401 | 182 | template = template.replace(replaceStr, Helper.ContentVars.REPEATABLEANNO.getVal()) |
jjg@1401 | 183 | .replace("#ClassName", className); |
jjg@1401 | 184 | srcContent = annoData + template; |
jjg@1401 | 185 | } |
jjg@1401 | 186 | |
jjg@1401 | 187 | JavaFileObject srcFile = getFile(className, srcContent); |
jjg@1401 | 188 | |
jjg@1401 | 189 | Iterable<? extends JavaFileObject> files = null; |
jjg@1401 | 190 | if (pkgInfoFile != null) { |
jjg@1401 | 191 | files = Arrays.asList(pkgInfoFile,srcFile); |
jjg@1401 | 192 | } |
jjg@1401 | 193 | else { |
jjg@1401 | 194 | files = Arrays.asList(srcFile); |
jjg@1401 | 195 | } |
jjg@1401 | 196 | return files; |
jjg@1401 | 197 | } |
jjg@1401 | 198 | |
jjg@1401 | 199 | private void error(String msg, String... contents) throws Exception { |
jjg@1401 | 200 | System.out.println("error: " + msg); |
jjg@1401 | 201 | errors++; |
jjg@1401 | 202 | if (contents.length == 1) { |
jjg@1401 | 203 | System.out.println("Contents = " + contents[0]); |
jjg@1401 | 204 | } |
jjg@1401 | 205 | // Test exits as soon as it gets a failure |
jjg@1401 | 206 | if (exitMode) throw new Exception(); |
jjg@1401 | 207 | } |
jjg@1401 | 208 | } |