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