Wed, 07 Nov 2012 17:01:19 -0800
8002157: Write combo compiler tests for repeating annotations for JDK8
Reviewed-by: darcy, jjg
Contributed-by: sonali.goel@oracle.com
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/BasicSyntaxCombo.java Wed Nov 07 17:01:19 2012 -0800 1.3 @@ -0,0 +1,209 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/** 1.28 + * @test 1.29 + * @bug 8002157 1.30 + * @author sogoel 1.31 + * @summary Basic Syntax test for repeating annotations on all elements 1.32 + * @build Helper 1.33 + * @compile BasicSyntaxCombo.java 1.34 + * @run main BasicSyntaxCombo 1.35 + */ 1.36 + 1.37 + 1.38 +import java.util.Arrays; 1.39 +import javax.tools.DiagnosticCollector; 1.40 +import javax.tools.JavaFileObject; 1.41 +import javax.tools.Diagnostic; 1.42 + 1.43 +/* 1.44 + * Generate test src for element kinds with repeating annotations. 1.45 + * The test uses Helper.java to get the template to create test src and 1.46 + * compile the test src. 1.47 + * The test passes if valid test src compile as expected and 1.48 + * and invalid test src fail as expected. 1.49 + */ 1.50 + 1.51 +public class BasicSyntaxCombo extends Helper{ 1.52 + static int errors = 0; 1.53 + static boolean exitMode = false; 1.54 + static String TESTPKG = "testpkg"; 1.55 + static String srcContent = ""; 1.56 + static String pkgInfoContent = ""; 1.57 + 1.58 + static { 1.59 + // If EXIT_ON_FAIL is set, the combo test will exit at the first error 1.60 + String exitOnFail = System.getenv("EXIT_ON_FAIL"); 1.61 + if (exitOnFail == null || exitOnFail == "" ) { 1.62 + exitMode = false; 1.63 + } 1.64 + else { 1.65 + if (exitOnFail.equalsIgnoreCase("YES") || 1.66 + exitOnFail.equalsIgnoreCase("Y") || 1.67 + exitOnFail.equalsIgnoreCase("TRUE") || 1.68 + exitOnFail.equalsIgnoreCase("T")) { 1.69 + exitMode = true; 1.70 + } 1.71 + } 1.72 + } 1.73 + 1.74 + enum TestElem { 1.75 + ANNOTATION_TYPE(true), 1.76 + PACKAGE(true), 1.77 + CONSTRUCTOR(true), 1.78 + FIELD(true), 1.79 + LOCAL_VARIABLE(true), 1.80 + METHOD(true), 1.81 + TYPE(true), 1.82 + PARAMETER(true), 1.83 + INNER_CLASS(true), 1.84 + STATIC_INI(false), 1.85 + INSTANCE_INI(false); 1.86 + 1.87 + TestElem(boolean compile) { 1.88 + this.compile = compile; 1.89 + } 1.90 + 1.91 + boolean compile; 1.92 + boolean shouldCompile() { 1.93 + return compile; 1.94 + } 1.95 + } 1.96 + 1.97 + public static void main(String[] args) throws Exception { 1.98 + new BasicSyntaxCombo().runTest(); 1.99 + } 1.100 + 1.101 + public void runTest() throws Exception { 1.102 + boolean result = false; 1.103 + Iterable<? extends JavaFileObject> files = null; 1.104 + int testCtr = 0; 1.105 + for (TestElem type : TestElem.values()) { 1.106 + testCtr++; 1.107 + String className = "BasicCombo_"+type; 1.108 + files = getFileList(className, type); 1.109 + 1.110 + boolean shouldCompile = type.shouldCompile(); 1.111 + result = getCompileResult(className, shouldCompile,files); 1.112 + 1.113 + if (shouldCompile && !result) { 1.114 + error(className + " did not compile as expected", srcContent); 1.115 + if(!pkgInfoContent.isEmpty()) { 1.116 + System.out.println("package-info.java contents: " + pkgInfoContent); 1.117 + } 1.118 + } 1.119 + 1.120 + if (!shouldCompile && !result) { 1.121 + error(className + " compiled unexpectedly", srcContent); 1.122 + if(!pkgInfoContent.isEmpty()) { 1.123 + System.out.println("package-info.java contents: " + pkgInfoContent); 1.124 + } 1.125 + } 1.126 + } 1.127 + 1.128 + System.out.println("Total number of tests run: " + testCtr); 1.129 + System.out.println("Total number of errors: " + errors); 1.130 + 1.131 + if (errors > 0) 1.132 + throw new Exception(errors + " errors found"); 1.133 + } 1.134 + 1.135 + private boolean getCompileResult(String className, boolean shouldCompile, 1.136 + Iterable<? extends JavaFileObject> files) throws Exception { 1.137 + 1.138 + DiagnosticCollector<JavaFileObject> diagnostics = 1.139 + new DiagnosticCollector<JavaFileObject>(); 1.140 + boolean ok = Helper.compileCode(diagnostics,files); 1.141 + if (!shouldCompile && !ok) { 1.142 + checkErrorKeys(className, diagnostics); 1.143 + } 1.144 + return (shouldCompile == ok); 1.145 + } 1.146 + 1.147 + private void checkErrorKeys ( 1.148 + String className, DiagnosticCollector<JavaFileObject> diagnostics) throws Exception { 1.149 + String expectedErrKey = "compiler.err.illegal.start.of.type"; 1.150 + for (Diagnostic<?> d : diagnostics.getDiagnostics()) { 1.151 + if ((d.getKind() == Diagnostic.Kind.ERROR) && 1.152 + d.getCode().contains(expectedErrKey)) { 1.153 + break; // Found the expected error 1.154 + } else { 1.155 + error("Incorrect error key, expected = " 1.156 + + expectedErrKey + ", Actual = " + d.getCode() 1.157 + + " for className = " + className, srcContent); 1.158 + } 1.159 + } 1.160 + } 1.161 + 1.162 + private Iterable<? extends JavaFileObject> getFileList(String className, 1.163 + TestElem type ) { 1.164 + 1.165 + String template = Helper.template; 1.166 + String replaceStr = "/*"+type+"*/"; 1.167 + StringBuilder annoData = new StringBuilder(); 1.168 + annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()) 1.169 + .append(Helper.ContentVars.CONTAINERFOR.getVal()) 1.170 + .append(Helper.ContentVars.CONTAINER.getVal()) 1.171 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 1.172 + .append(Helper.ContentVars.BASE.getVal()); 1.173 + 1.174 + JavaFileObject pkgInfoFile = null; 1.175 + 1.176 + if (type.equals("PACKAGE")) { 1.177 + srcContent = template.replace(replaceStr, "package testpkg;") 1.178 + .replace("#ClassName", className); 1.179 + 1.180 + String pkgInfoName = TESTPKG+"."+"package-info"; 1.181 + pkgInfoContent = Helper.ContentVars.REPEATABLEANNO.getVal() 1.182 + + "package " + TESTPKG + ";" 1.183 + + annoData; 1.184 + pkgInfoFile = getFile(pkgInfoName, pkgInfoContent); 1.185 + } else { 1.186 + template = template.replace(replaceStr, Helper.ContentVars.REPEATABLEANNO.getVal()) 1.187 + .replace("#ClassName", className); 1.188 + srcContent = annoData + template; 1.189 + } 1.190 + 1.191 + JavaFileObject srcFile = getFile(className, srcContent); 1.192 + 1.193 + Iterable<? extends JavaFileObject> files = null; 1.194 + if (pkgInfoFile != null) { 1.195 + files = Arrays.asList(pkgInfoFile,srcFile); 1.196 + } 1.197 + else { 1.198 + files = Arrays.asList(srcFile); 1.199 + } 1.200 + return files; 1.201 + } 1.202 + 1.203 + private void error(String msg, String... contents) throws Exception { 1.204 + System.out.println("error: " + msg); 1.205 + errors++; 1.206 + if (contents.length == 1) { 1.207 + System.out.println("Contents = " + contents[0]); 1.208 + } 1.209 + // Test exits as soon as it gets a failure 1.210 + if (exitMode) throw new Exception(); 1.211 + } 1.212 +}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/DeprecatedAnnoCombo.java Wed Nov 07 17:01:19 2012 -0800 2.3 @@ -0,0 +1,155 @@ 2.4 +/* 2.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/** 2.28 + * @test 2.29 + * @bug 8002157 2.30 + * @author sogoel 2.31 + * @summary Combo test to check for usage of Deprecated 2.32 + * @build Helper 2.33 + * @compile DeprecatedAnnoCombo.java 2.34 + * @run main DeprecatedAnnoCombo 2.35 + */ 2.36 + 2.37 +import java.util.List; 2.38 +import javax.tools.Diagnostic; 2.39 +import javax.tools.DiagnosticCollector; 2.40 +import javax.tools.JavaFileObject; 2.41 + 2.42 +/* 2.43 + * Generate test src for use of @Deprecated on base anno 2.44 + * or container anno or on both. In all cases, test src should compile and a 2.45 + * warning should be generated. Repeating annotations used only on class for 2.46 + * these generated test src. 2.47 + */ 2.48 + 2.49 +public class DeprecatedAnnoCombo extends Helper { 2.50 + static int errors = 0; 2.51 + 2.52 + enum TestCases { 2.53 + DeprecatedonBoth, 2.54 + DeprecatedonContainer, 2.55 + DeprecatedonBase; 2.56 + } 2.57 + 2.58 + public static void main(String[] args) throws Exception { 2.59 + new DeprecatedAnnoCombo().runTest(); 2.60 + } 2.61 + 2.62 + public void runTest() throws Exception { 2.63 + boolean ok = false; 2.64 + int testCtr = 0; 2.65 + 2.66 + for (TestCases clName : TestCases.values()) { 2.67 + testCtr++; 2.68 + 2.69 + // Create test source content 2.70 + String contents = getContent(clName.toString()); 2.71 + 2.72 + // Compile the generated source file 2.73 + DiagnosticCollector<JavaFileObject> diagnostics = 2.74 + new DiagnosticCollector<JavaFileObject>(); 2.75 + ok = compileCode(clName.toString(), contents, diagnostics); 2.76 + 2.77 + String errorKey1 = "compiler.note.deprecated.filename"; 2.78 + String errorKey2 = "compiler.note.deprecated.recompile"; 2.79 + List<Diagnostic<? extends JavaFileObject>> diags = diagnostics.getDiagnostics(); 2.80 + 2.81 + //Check for deprecated warnings 2.82 + if (ok) { 2.83 + if (diags.size() == 0) { 2.84 + error("Did not get any warnings for @Deprecated usage"); 2.85 + } else { 2.86 + for (Diagnostic<?> d : diags) { 2.87 + if (d.getKind() == Diagnostic.Kind.NOTE) { 2.88 + if (d.getCode().contains(errorKey1) 2.89 + || d.getCode().contains(errorKey2)) { 2.90 + System.out.println("TestCase =" + clName + " passed as expected"); 2.91 + } else { 2.92 + error("TestCase =" + clName + " did not give correct warnings" + 2.93 + "Expected warning keys: " + 2.94 + "errorKey1 = " + errorKey1 + 2.95 + "errorKey2 = " + errorKey2 + 2.96 + "actualErrorKey = " + d.getCode(), contents); 2.97 + } 2.98 + } else { 2.99 + error("Diagnostic Kind is incorrect, expected = " + 2.100 + Diagnostic.Kind.NOTE + "actual = " + d.getKind(), contents); 2.101 + } 2.102 + } 2.103 + } 2.104 + } else { 2.105 + error("TestCase =" + clName + " did not compile as expected", contents); 2.106 + } 2.107 + } 2.108 + 2.109 + System.out.println("Total number of tests run: " + testCtr); 2.110 + System.out.println("Total number of errors: " + errors); 2.111 + if (errors > 0) 2.112 + throw new Exception(errors + " errors found"); 2.113 + } 2.114 + 2.115 + private String getContent(String className) { 2.116 + StringBuilder annoData = new StringBuilder(); 2.117 + 2.118 + switch(className) { 2.119 + case "DeprecatedonBoth": 2.120 + annoData.append(Helper.ContentVars.DEPRECATED.getVal()) 2.121 + .append(Helper.ContentVars.CONTAINERFOR.getVal()) 2.122 + .append(Helper.ContentVars.CONTAINER.getVal()) 2.123 + .append(Helper.ContentVars.DEPRECATED.getVal()) 2.124 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 2.125 + .append(Helper.ContentVars.BASE.getVal()); 2.126 + break; 2.127 + case "DeprecatedonBase": 2.128 + annoData.append(Helper.ContentVars.CONTAINERFOR.getVal()) 2.129 + .append(Helper.ContentVars.CONTAINER.getVal()) 2.130 + .append(Helper.ContentVars.DEPRECATED.getVal()) 2.131 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 2.132 + .append(Helper.ContentVars.BASE.getVal()); 2.133 + break; 2.134 + case "DeprecatedonContainer": 2.135 + annoData.append(Helper.ContentVars.DEPRECATED.getVal()) 2.136 + .append(Helper.ContentVars.CONTAINERFOR.getVal()) 2.137 + .append(Helper.ContentVars.CONTAINER.getVal()) 2.138 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 2.139 + .append(Helper.ContentVars.BASE.getVal()); 2.140 + break; 2.141 + } 2.142 + 2.143 + String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal() 2.144 + + Helper.ContentVars.IMPORTDEPRECATED.getVal() 2.145 + + annoData 2.146 + + Helper.ContentVars.REPEATABLEANNO.getVal() 2.147 + + "\nclass "+ className + "{}"; 2.148 + return contents; 2.149 + } 2.150 + 2.151 + private void error(String msg, String... contents) { 2.152 + System.out.println("error: " + msg); 2.153 + errors++; 2.154 + if (contents.length == 1) { 2.155 + System.out.println("Contents = " + contents[0]); 2.156 + } 2.157 + } 2.158 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/DocumentedAnnoCombo.java Wed Nov 07 17:01:19 2012 -0800 3.3 @@ -0,0 +1,127 @@ 3.4 +/* 3.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 + * 3.8 + * This code is free software; you can redistribute it and/or modify it 3.9 + * under the terms of the GNU General Public License version 2 only, as 3.10 + * published by the Free Software Foundation. 3.11 + * 3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.15 + * version 2 for more details (a copy is included in the LICENSE file that 3.16 + * accompanied this code). 3.17 + * 3.18 + * You should have received a copy of the GNU General Public License version 3.19 + * 2 along with this work; if not, write to the Free Software Foundation, 3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.21 + * 3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.23 + * or visit www.oracle.com if you need additional information or have any 3.24 + * questions. 3.25 + */ 3.26 + 3.27 +/** 3.28 + * @test 3.29 + * @bug 8002157 3.30 + * @author sogoel 3.31 + * @summary Positive combo test for use of Documented on baseAnno/containerAnno 3.32 + * @build Helper 3.33 + * @compile DocumentedAnnoCombo.java 3.34 + * @run main DocumentedAnnoCombo 3.35 + */ 3.36 + 3.37 +import javax.tools.DiagnosticCollector; 3.38 +import javax.tools.JavaFileObject; 3.39 + 3.40 +/* 3.41 + * Generate valid test src for the use of @Documented on container anno 3.42 + * or on both base anno and container anno. Both test src should compile. 3.43 + * Repeating annotations used only on class for these generated test src. 3.44 + */ 3.45 +public class DocumentedAnnoCombo extends Helper { 3.46 + static int errors = 0; 3.47 + 3.48 + enum TestCases { 3.49 + DocumentedonBothAnno(true), 3.50 + DocumentedonContainer(true); 3.51 + 3.52 + TestCases(boolean compile) { 3.53 + this.compile = compile; 3.54 + } 3.55 + 3.56 + boolean compile; 3.57 + boolean shouldCompile() { 3.58 + return compile; 3.59 + } 3.60 + } 3.61 + 3.62 + public static void main(String[] args) throws Exception { 3.63 + new DocumentedAnnoCombo().runTest(); 3.64 + } 3.65 + 3.66 + public void runTest() throws Exception { 3.67 + boolean ok = false; 3.68 + int testCtr = 0; 3.69 + 3.70 + // Create test source content 3.71 + for (TestCases className : TestCases.values()) { 3.72 + testCtr++; 3.73 + String contents = getContent(className.toString()); 3.74 + 3.75 + // Compile the generated source file 3.76 + DiagnosticCollector<JavaFileObject> diagnostics = 3.77 + new DiagnosticCollector<JavaFileObject>(); 3.78 + ok = compileCode(className.toString(), contents, diagnostics); 3.79 + if (!ok) { 3.80 + error("Class="+ className +" did not compile as expected", contents); 3.81 + } else { 3.82 + System.out.println("Test passed for className: " + className); 3.83 + } 3.84 + } 3.85 + 3.86 + System.out.println("Total number of tests run: " + testCtr); 3.87 + System.out.println("Total number of errors: " + errors); 3.88 + 3.89 + if (errors > 0) 3.90 + throw new Exception(errors + " errors found"); 3.91 + } 3.92 + 3.93 + private String getContent(String className) { 3.94 + 3.95 + StringBuilder annoData = new StringBuilder(); 3.96 + switch(className) { 3.97 + case "DocumentedonBothAnno": 3.98 + annoData.append(Helper.ContentVars.DOCUMENTED.getVal()) 3.99 + .append(Helper.ContentVars.CONTAINERFOR.getVal()) 3.100 + .append(Helper.ContentVars.CONTAINER.getVal()) 3.101 + .append(Helper.ContentVars.DOCUMENTED.getVal()) 3.102 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 3.103 + .append(Helper.ContentVars.BASE.getVal()); 3.104 + break; 3.105 + case "DocumentedonContainer": 3.106 + annoData.append(Helper.ContentVars.DOCUMENTED.getVal()) 3.107 + .append(Helper.ContentVars.CONTAINERFOR.getVal()) 3.108 + .append(Helper.ContentVars.CONTAINER.getVal()) 3.109 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 3.110 + .append(Helper.ContentVars.BASE.getVal()); 3.111 + break; 3.112 + } 3.113 + 3.114 + String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal() 3.115 + + Helper.ContentVars.IMPORTDOCUMENTED.getVal() 3.116 + + annoData 3.117 + + Helper.ContentVars.REPEATABLEANNO.getVal() 3.118 + + "\nclass "+ className + "{}"; 3.119 + return contents; 3.120 + } 3.121 + 3.122 + private void error(String msg, String... contents) { 3.123 + System.out.println("error: " + msg); 3.124 + errors++; 3.125 + if (contents.length == 1) { 3.126 + System.out.println("Contents = " + contents[0]); 3.127 + } 3.128 + } 3.129 +} 3.130 +
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/Helper.java Wed Nov 07 17:01:19 2012 -0800 4.3 @@ -0,0 +1,154 @@ 4.4 +/* 4.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 4.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4.7 + * 4.8 + * This code is free software; you can redistribute it and/or modify it 4.9 + * under the terms of the GNU General Public License version 2 only, as 4.10 + * published by the Free Software Foundation. 4.11 + * 4.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 4.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 4.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 4.15 + * version 2 for more details (a copy is included in the LICENSE file that 4.16 + * accompanied this code). 4.17 + * 4.18 + * You should have received a copy of the GNU General Public License version 4.19 + * 2 along with this work; if not, write to the Free Software Foundation, 4.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 4.21 + * 4.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 4.23 + * or visit www.oracle.com if you need additional information or have any 4.24 + * questions. 4.25 + */ 4.26 + 4.27 +import java.net.URI; 4.28 +import java.net.URISyntaxException; 4.29 +import java.util.Arrays; 4.30 +import javax.tools.DiagnosticCollector; 4.31 +import javax.tools.JavaCompiler; 4.32 +import javax.tools.JavaFileObject; 4.33 +import javax.tools.SimpleJavaFileObject; 4.34 +import javax.tools.ToolProvider; 4.35 +import javax.tools.JavaCompiler.CompilationTask; 4.36 + 4.37 +public class Helper { 4.38 + 4.39 + enum ContentVars { 4.40 + IMPORTCONTAINERSTMTS("\nimport java.lang.annotation.ContainedBy;\n" + 4.41 + "\nimport java.lang.annotation.ContainerFor;\n"), 4.42 + IMPORTDEPRECATED("import java.lang.Deprecated;\n"), 4.43 + IMPORTDOCUMENTED("import java.lang.annotation.Documented;\n"), 4.44 + IMPORTINHERITED("import java.lang.annotation.Inherited;\n"), 4.45 + IMPORTRETENTION("import java.lang.annotation.Retention;\n" + 4.46 + "\nimport java.lang.annotation.RetentionPolicy;\n"), 4.47 + CONTAINEDBY("\n@ContainedBy(FooContainer.class)\n"), 4.48 + CONTAINERFOR("@ContainerFor(Foo.class)\n"), 4.49 + CONTAINER("@interface FooContainer {\n" +" Foo[] value();\n}\n"), 4.50 + BASE("@interface Foo {}\n"), 4.51 + REPEATABLEANNO("\n@Foo() @Foo()"), 4.52 + DEPRECATED("\n@Deprecated"), 4.53 + DOCUMENTED("\n@Documented"), 4.54 + INHERITED("\n@Inherited"), 4.55 + RETENTION("@Retention(RetentionPolicy.#VAL)\n"); 4.56 + 4.57 + private String val; 4.58 + 4.59 + 4.60 + private ContentVars(String val) { 4.61 + this.val = val; 4.62 + } 4.63 + 4.64 + public String getVal() { 4.65 + return val; 4.66 + } 4.67 + } 4.68 + 4.69 + /* String template where /*<TYPE>*/ /*gets replaced by repeating anno 4.70 + * Used to generate test src for combo tests 4.71 + * - BasicSyntaxCombo.java 4.72 + * - TargetAnnoCombo.java 4.73 + */ 4.74 + public static final String template = 4.75 + "/*PACKAGE*/\n" + 4.76 + "//pkg test;\n\n" + 4.77 + "/*TYPE*/ //class\n" + 4.78 + "class #ClassName {\n" + 4.79 + " /*FIELD*/ //instance var\n" + 4.80 + " public int x = 0;\n\n" + 4.81 + " /*FIELD*/ //Enum constants\n" + 4.82 + " TestEnum testEnum;\n\n" + 4.83 + " /*FIELD*/ // Static field\n" + 4.84 + " public static int num;\n\n" + 4.85 + " /*STATIC_INI*/\n" + 4.86 + " static { \n" + "num = 10; \n }\n\n" + 4.87 + " /*CONSTRUCTOR*/\n" + 4.88 + " #ClassName() {}\n\n" + 4.89 + " /*INSTANCE_INI*/\n" + 4.90 + " { \n x = 10; \n }" + 4.91 + " /*INNER_CLASS*/\n" + 4.92 + " class innerClass {}\n" + 4.93 + " /*METHOD*/\n" + 4.94 + " void bar(/*PARAMETER*/ int baz) {\n" + 4.95 + " /*LOCAL_VARIABLE*/\n" + 4.96 + " int y = 0;\n" + 4.97 + " }\n" + 4.98 + "}\n\n" + 4.99 + "/*TYPE*/ //Enum\n" + 4.100 + "enum TestEnum {}\n\n" + 4.101 + "/*TYPE*/ //Interface\n" + 4.102 + "interface TestInterface {}\n\n" + 4.103 + "/*TYPE*/\n" + 4.104 + "/*ANNOTATION_TYPE*/\n" + 4.105 + "@interface TestAnnotationType{}\n"; 4.106 + 4.107 + // Create and compile FileObject using values for className and contents 4.108 + public static boolean compileCode(String className, String contents, 4.109 + DiagnosticCollector<JavaFileObject> diagnostics) { 4.110 + boolean ok = false; 4.111 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 4.112 + if (compiler == null) { 4.113 + throw new RuntimeException("can't get javax.tools.JavaCompiler!"); 4.114 + } 4.115 + 4.116 + JavaFileObject file = getFile(className, contents); 4.117 + Iterable<? extends JavaFileObject> compilationUnit = Arrays.asList(file); 4.118 + 4.119 + CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, compilationUnit); 4.120 + ok = task.call(); 4.121 + return ok; 4.122 + 4.123 + } 4.124 + 4.125 + // Compile a list of FileObjects 4.126 + public static boolean compileCode(DiagnosticCollector<JavaFileObject> diagnostics, Iterable<? extends JavaFileObject> files) { 4.127 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 4.128 + if (compiler == null) { 4.129 + throw new RuntimeException("can't get javax.tools.JavaCompiler!"); 4.130 + } 4.131 + 4.132 + CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, files); 4.133 + boolean ok = task.call(); 4.134 + return ok; 4.135 + } 4.136 + 4.137 + static JavaFileObject getFile(String name, String code) { 4.138 + JavaFileObject o = null; 4.139 + try { 4.140 + o = new JavaStringFileObject(name, code); 4.141 + } catch (URISyntaxException e) { 4.142 + throw new RuntimeException(e); 4.143 + } 4.144 + return o; 4.145 + } 4.146 + static class JavaStringFileObject extends SimpleJavaFileObject { 4.147 + final String theCode; 4.148 + public JavaStringFileObject(String fileName, String theCode) throws URISyntaxException { 4.149 + super(new URI("string:///" + fileName.replace('.','/') + ".java"), Kind.SOURCE); 4.150 + this.theCode = theCode; 4.151 + } 4.152 + @Override 4.153 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 4.154 + return theCode; 4.155 + } 4.156 + } 4.157 +}
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/InheritedAnnoCombo.java Wed Nov 07 17:01:19 2012 -0800 5.3 @@ -0,0 +1,128 @@ 5.4 +/* 5.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +/** 5.28 + * @test 5.29 + * @bug 8002157 5.30 + * @author sogoel 5.31 + * @summary Positive combo test for use of Inherited on baseAnno/containerAnno 5.32 + * @build Helper 5.33 + * @compile InheritedAnnoCombo.java 5.34 + * @run main InheritedAnnoCombo 5.35 + */ 5.36 + 5.37 +import javax.tools.DiagnosticCollector; 5.38 +import javax.tools.JavaFileObject; 5.39 + 5.40 +/* 5.41 + * Generate valid test src for the use of @Inherited on container anno 5.42 + * or on both base anno and container anno. Both test src should compile. 5.43 + * Repeating annotations used only on class for these generated test src. 5.44 + */ 5.45 + 5.46 +public class InheritedAnnoCombo extends Helper { 5.47 + static int errors = 0; 5.48 + enum TestCases { 5.49 + InheritedonBothAnno(true), 5.50 + InheritedonBase(true); 5.51 + 5.52 + TestCases(boolean compile) { 5.53 + this.compile = compile; 5.54 + } 5.55 + 5.56 + boolean compile; 5.57 + boolean shouldCompile() { 5.58 + return compile; 5.59 + } 5.60 + } 5.61 + 5.62 + public static void main(String[] args) throws Exception { 5.63 + new InheritedAnnoCombo().runTest(); 5.64 + } 5.65 + 5.66 + public void runTest() throws Exception { 5.67 + int testCtr = 0; 5.68 + boolean ok = false; 5.69 + 5.70 + // Create test source content 5.71 + for (TestCases className : TestCases.values()) { 5.72 + testCtr++; 5.73 + String contents = getContent(className.toString()); 5.74 + 5.75 + // Compile the generated code 5.76 + DiagnosticCollector<JavaFileObject> diagnostics = 5.77 + new DiagnosticCollector<JavaFileObject>(); 5.78 + ok = compileCode(className.toString(), contents, diagnostics); 5.79 + 5.80 + if (!ok) { 5.81 + error("Class="+ className +" did not compile as expected", contents); 5.82 + } else { 5.83 + System.out.println("Test passed for className: " + className); 5.84 + } 5.85 + } 5.86 + 5.87 + System.out.println("Total number of tests run: " + testCtr); 5.88 + System.out.println("Total number of errors: " + errors); 5.89 + 5.90 + if (errors > 0) 5.91 + throw new Exception(errors + " errors found"); 5.92 + } 5.93 + 5.94 + private String getContent(String className) { 5.95 + 5.96 + StringBuilder annoData = new StringBuilder(); 5.97 + switch(className) { 5.98 + case "InheritedonBothAnno": 5.99 + annoData.append(Helper.ContentVars.INHERITED.getVal()) 5.100 + .append(Helper.ContentVars.CONTAINERFOR.getVal()) 5.101 + .append(Helper.ContentVars.CONTAINER.getVal()) 5.102 + .append(Helper.ContentVars.INHERITED.getVal()) 5.103 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 5.104 + .append(Helper.ContentVars.BASE.getVal()); 5.105 + break; 5.106 + case "InheritedonBase": 5.107 + annoData.append(Helper.ContentVars.INHERITED.getVal()) 5.108 + .append(Helper.ContentVars.CONTAINERFOR.getVal()) 5.109 + .append(Helper.ContentVars.CONTAINER.getVal()) 5.110 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 5.111 + .append(Helper.ContentVars.BASE.getVal()); 5.112 + break; 5.113 + } 5.114 + 5.115 + String contents = Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal() 5.116 + + Helper.ContentVars.IMPORTINHERITED.getVal() 5.117 + + annoData 5.118 + + Helper.ContentVars.REPEATABLEANNO.getVal() 5.119 + + "\nclass "+ className + "{}"; 5.120 + return contents; 5.121 + } 5.122 + 5.123 + private void error(String msg, String... contents) { 5.124 + System.out.println("error: " + msg); 5.125 + errors++; 5.126 + if (contents.length == 1) { 5.127 + System.out.println("Contents = " + contents[0]); 5.128 + } 5.129 + } 5.130 +} 5.131 +
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/RetentionAnnoCombo.java Wed Nov 07 17:01:19 2012 -0800 6.3 @@ -0,0 +1,201 @@ 6.4 +/* 6.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +/** 6.28 + * @test 6.29 + * @bug 8002157 6.30 + * @author sogoel 6.31 + * @summary Combo test for all possible combinations for Retention Values 6.32 + * @build Helper 6.33 + * @compile RetentionAnnoCombo.java 6.34 + * @run main RetentionAnnoCombo 6.35 + */ 6.36 + 6.37 +import java.util.HashMap; 6.38 +import java.util.Map; 6.39 + 6.40 +import javax.tools.DiagnosticCollector; 6.41 +import javax.tools.JavaFileObject; 6.42 +import javax.tools.Diagnostic; 6.43 + 6.44 +/* 6.45 + * Generate all combinations for the use of @Retention on base anno or container 6.46 + * anno or both. The test passes if valid test src compile as expected and 6.47 + * and invalid test src fail as expected. 6.48 + * Repeating annotations used only on class for these generated test src. 6.49 + */ 6.50 + 6.51 +public class RetentionAnnoCombo extends Helper { 6.52 + static int errors = 0; 6.53 + static boolean exitMode = false; 6.54 + static { 6.55 + String exitOnFail = System.getenv("EXIT_ON_FAIL"); 6.56 + if (exitOnFail == null || exitOnFail == "" ) { 6.57 + exitMode = false; 6.58 + } 6.59 + else { 6.60 + if (exitOnFail.equalsIgnoreCase("YES") || 6.61 + exitOnFail.equalsIgnoreCase("Y") || 6.62 + exitOnFail.equalsIgnoreCase("TRUE") || 6.63 + exitOnFail.equalsIgnoreCase("T")) { 6.64 + exitMode = true; 6.65 + } 6.66 + } 6.67 + } 6.68 + 6.69 + public static void main(String args[]) throws Exception { 6.70 + new RetentionAnnoCombo().runTest(); 6.71 + } 6.72 + 6.73 + public void runTest() throws Exception { 6.74 + 6.75 + /* 4x4 matrix for Retention values SOURCE, DEFAULT, CLASS, RUNTIME 6.76 + * i -> Retention value on ContainerAnno 6.77 + * j -> Retention value on BaseAnno 6.78 + * 1 -> retention value combo should compile 6.79 + */ 6.80 + int[][] retention = { {1, 0, 0, 0}, 6.81 + {1, 1, 1, 0}, 6.82 + {1, 1, 1, 0}, 6.83 + {1, 1, 1, 1} }; 6.84 + 6.85 + Map<Integer, String> retMap = setRetentionValMatrix(); 6.86 + String contents = ""; 6.87 + boolean result = false; 6.88 + int testCtr = 0; 6.89 + for (int i = 0; i < 4 ; i ++) { 6.90 + for (int j = 0; j < 4; j++ ) { 6.91 + testCtr++; 6.92 + String className = "RetentionTest_"+i+"_"+j; 6.93 + contents = getContent(className, retMap, i, j); 6.94 + if (retention[i][j] == 1) { 6.95 + // Code generated should compile 6.96 + result = getCompileResult(contents,className, true); 6.97 + if (!result) { 6.98 + error("FAIL: " + className + " did not compile as expected!", contents); 6.99 + } 6.100 + } else { 6.101 + result = getCompileResult(contents,className, false); 6.102 + if (!result) { 6.103 + error("FAIL: " + className + " compiled unexpectedly!", contents); 6.104 + } 6.105 + } 6.106 + if (result) { 6.107 + System.out.println("Test passed for className = " + className); 6.108 + } 6.109 + } 6.110 + } 6.111 + 6.112 + System.out.println("Total number of tests run: " + testCtr); 6.113 + System.out.println("Total number of errors: " + errors); 6.114 + 6.115 + if (errors > 0) 6.116 + throw new Exception(errors + " errors found"); 6.117 + } 6.118 + 6.119 + private boolean getCompileResult(String contents, String className, 6.120 + boolean shouldCompile) throws Exception{ 6.121 + 6.122 + DiagnosticCollector<JavaFileObject> diagnostics = 6.123 + new DiagnosticCollector<JavaFileObject>(); 6.124 + boolean ok = compileCode(className, contents, diagnostics); 6.125 + 6.126 + String expectedErrKey = "compiler.err.invalid.containedby" + 6.127 + ".annotation.retention"; 6.128 + if (!shouldCompile && !ok) { 6.129 + for (Diagnostic<?> d : diagnostics.getDiagnostics()) { 6.130 + if (!((d.getKind() == Diagnostic.Kind.ERROR) && 6.131 + d.getCode().contains(expectedErrKey))) { 6.132 + error("FAIL: Incorrect error given, expected = " 6.133 + + expectedErrKey + ", Actual = " + d.getCode() 6.134 + + " for className = " + className, contents); 6.135 + } 6.136 + } 6.137 + } 6.138 + 6.139 + return (shouldCompile == ok); 6.140 + } 6.141 + 6.142 + private Map<Integer,String> setRetentionValMatrix() { 6.143 + HashMap<Integer,String> hm = new HashMap<>(); 6.144 + hm.put(0,"SOURCE"); 6.145 + hm.put(1,"DEFAULT"); 6.146 + hm.put(2,"CLASS"); 6.147 + hm.put(3,"RUNTIME"); 6.148 + return hm; 6.149 + } 6.150 + 6.151 + private String getContent(String className, Map<Integer, String> retMap, 6.152 + int i, int j) { 6.153 + 6.154 + String retContainerVal = retMap.get(i).toString(); 6.155 + String retBaseVal = retMap.get(j).toString(); 6.156 + String replacedRetBaseVal = "", replacedRetCAVal = ""; 6.157 + String retention = Helper.ContentVars.RETENTION.getVal(); 6.158 + 6.159 + // @Retention is available as default for both base and container anno 6.160 + if (retContainerVal.equalsIgnoreCase("DEFAULT") 6.161 + && retBaseVal.equalsIgnoreCase("DEFAULT")) { 6.162 + replacedRetBaseVal = ""; 6.163 + replacedRetCAVal = ""; 6.164 + // @Retention is available as default for container anno 6.165 + } else if (retContainerVal.equalsIgnoreCase("DEFAULT")) { 6.166 + replacedRetBaseVal = retention.replace("#VAL", retBaseVal); 6.167 + replacedRetCAVal = ""; 6.168 + // @Retention is available as default for base anno 6.169 + } else if (retBaseVal.equalsIgnoreCase("DEFAULT")) { 6.170 + replacedRetBaseVal = ""; 6.171 + replacedRetCAVal = retention.replace("#VAL", retContainerVal); 6.172 + // @Retention is not available as default for both base and container anno 6.173 + } else { 6.174 + replacedRetBaseVal = retention.replace("#VAL", retBaseVal); 6.175 + replacedRetCAVal = retention.replace("#VAL", retContainerVal); 6.176 + } 6.177 + 6.178 + StringBuilder annoData = new StringBuilder(); 6.179 + annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()) 6.180 + .append(Helper.ContentVars.IMPORTRETENTION.getVal()) 6.181 + .append(Helper.ContentVars.CONTAINERFOR.getVal()) 6.182 + .append(replacedRetCAVal) 6.183 + .append(Helper.ContentVars.CONTAINER.getVal()) 6.184 + .append(Helper.ContentVars.CONTAINEDBY.getVal()) 6.185 + .append(replacedRetBaseVal) 6.186 + .append(Helper.ContentVars.BASE.getVal()); 6.187 + 6.188 + String contents = annoData 6.189 + + Helper.ContentVars.REPEATABLEANNO.getVal() 6.190 + + "\nclass "+ className + "{}"; 6.191 + return contents; 6.192 + } 6.193 + 6.194 + private void error(String msg,String... contents) throws Exception { 6.195 + System.out.println("error: " + msg); 6.196 + errors++; 6.197 + if (contents.length == 1) { 6.198 + System.out.println("Contents = " + contents[0]); 6.199 + } 6.200 + // Test exits as soon as it gets a failure 6.201 + if (exitMode) throw new Exception(); 6.202 + } 6.203 +} 6.204 +