1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/RetentionAnnoCombo.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,200 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, 2013, 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 Combo test for all possible combinations for Retention Values 1.32 + * @build Helper 1.33 + * @compile RetentionAnnoCombo.java 1.34 + * @run main RetentionAnnoCombo 1.35 + */ 1.36 + 1.37 +import java.util.HashMap; 1.38 +import java.util.Map; 1.39 + 1.40 +import javax.tools.DiagnosticCollector; 1.41 +import javax.tools.JavaFileObject; 1.42 +import javax.tools.Diagnostic; 1.43 + 1.44 +/* 1.45 + * Generate all combinations for the use of @Retention on base anno or container 1.46 + * anno or both. The test passes if valid test src compile as expected and 1.47 + * and invalid test src fail as expected. 1.48 + * Repeating annotations used only on class for these generated test src. 1.49 + */ 1.50 + 1.51 +public class RetentionAnnoCombo extends Helper { 1.52 + static int errors = 0; 1.53 + static boolean exitMode = false; 1.54 + static { 1.55 + String exitOnFail = System.getenv("EXIT_ON_FAIL"); 1.56 + if (exitOnFail == null || exitOnFail == "" ) { 1.57 + exitMode = false; 1.58 + } 1.59 + else { 1.60 + if (exitOnFail.equalsIgnoreCase("YES") || 1.61 + exitOnFail.equalsIgnoreCase("Y") || 1.62 + exitOnFail.equalsIgnoreCase("TRUE") || 1.63 + exitOnFail.equalsIgnoreCase("T")) { 1.64 + exitMode = true; 1.65 + } 1.66 + } 1.67 + } 1.68 + 1.69 + public static void main(String args[]) throws Exception { 1.70 + new RetentionAnnoCombo().runTest(); 1.71 + } 1.72 + 1.73 + public void runTest() throws Exception { 1.74 + 1.75 + /* 4x4 matrix for Retention values SOURCE, DEFAULT, CLASS, RUNTIME 1.76 + * i -> Retention value on ContainerAnno 1.77 + * j -> Retention value on BaseAnno 1.78 + * 1 -> retention value combo should compile 1.79 + */ 1.80 + int[][] retention = { {1, 0, 0, 0}, 1.81 + {1, 1, 1, 0}, 1.82 + {1, 1, 1, 0}, 1.83 + {1, 1, 1, 1} }; 1.84 + 1.85 + Map<Integer, String> retMap = setRetentionValMatrix(); 1.86 + String contents = ""; 1.87 + boolean result = false; 1.88 + int testCtr = 0; 1.89 + for (int i = 0; i < 4 ; i ++) { 1.90 + for (int j = 0; j < 4; j++ ) { 1.91 + testCtr++; 1.92 + String className = "RetentionTest_"+i+"_"+j; 1.93 + contents = getContent(className, retMap, i, j); 1.94 + if (retention[i][j] == 1) { 1.95 + // Code generated should compile 1.96 + result = getCompileResult(contents,className, true); 1.97 + if (!result) { 1.98 + error("FAIL: " + className + " did not compile as expected!", contents); 1.99 + } 1.100 + } else { 1.101 + result = getCompileResult(contents,className, false); 1.102 + if (!result) { 1.103 + error("FAIL: " + className + " compiled unexpectedly!", contents); 1.104 + } 1.105 + } 1.106 + if (result) { 1.107 + System.out.println("Test passed for className = " + className); 1.108 + } 1.109 + } 1.110 + } 1.111 + 1.112 + System.out.println("Total number of tests run: " + testCtr); 1.113 + System.out.println("Total number of errors: " + errors); 1.114 + 1.115 + if (errors > 0) 1.116 + throw new Exception(errors + " errors found"); 1.117 + } 1.118 + 1.119 + private boolean getCompileResult(String contents, String className, 1.120 + boolean shouldCompile) throws Exception{ 1.121 + 1.122 + DiagnosticCollector<JavaFileObject> diagnostics = 1.123 + new DiagnosticCollector<JavaFileObject>(); 1.124 + boolean ok = compileCode(className, contents, diagnostics); 1.125 + 1.126 + String expectedErrKey = "compiler.err.invalid.repeatable" + 1.127 + ".annotation.retention"; 1.128 + if (!shouldCompile && !ok) { 1.129 + for (Diagnostic<?> d : diagnostics.getDiagnostics()) { 1.130 + if (!((d.getKind() == Diagnostic.Kind.ERROR) && 1.131 + d.getCode().contains(expectedErrKey))) { 1.132 + error("FAIL: Incorrect error given, expected = " 1.133 + + expectedErrKey + ", Actual = " + d.getCode() 1.134 + + " for className = " + className, contents); 1.135 + } 1.136 + } 1.137 + } 1.138 + 1.139 + return (shouldCompile == ok); 1.140 + } 1.141 + 1.142 + private Map<Integer,String> setRetentionValMatrix() { 1.143 + HashMap<Integer,String> hm = new HashMap<>(); 1.144 + hm.put(0,"SOURCE"); 1.145 + hm.put(1,"DEFAULT"); 1.146 + hm.put(2,"CLASS"); 1.147 + hm.put(3,"RUNTIME"); 1.148 + return hm; 1.149 + } 1.150 + 1.151 + private String getContent(String className, Map<Integer, String> retMap, 1.152 + int i, int j) { 1.153 + 1.154 + String retContainerVal = retMap.get(i).toString(); 1.155 + String retBaseVal = retMap.get(j).toString(); 1.156 + String replacedRetBaseVal = "", replacedRetCAVal = ""; 1.157 + String retention = Helper.ContentVars.RETENTION.getVal(); 1.158 + 1.159 + // @Retention is available as default for both base and container anno 1.160 + if (retContainerVal.equalsIgnoreCase("DEFAULT") 1.161 + && retBaseVal.equalsIgnoreCase("DEFAULT")) { 1.162 + replacedRetBaseVal = ""; 1.163 + replacedRetCAVal = ""; 1.164 + // @Retention is available as default for container anno 1.165 + } else if (retContainerVal.equalsIgnoreCase("DEFAULT")) { 1.166 + replacedRetBaseVal = retention.replace("#VAL", retBaseVal); 1.167 + replacedRetCAVal = ""; 1.168 + // @Retention is available as default for base anno 1.169 + } else if (retBaseVal.equalsIgnoreCase("DEFAULT")) { 1.170 + replacedRetBaseVal = ""; 1.171 + replacedRetCAVal = retention.replace("#VAL", retContainerVal); 1.172 + // @Retention is not available as default for both base and container anno 1.173 + } else { 1.174 + replacedRetBaseVal = retention.replace("#VAL", retBaseVal); 1.175 + replacedRetCAVal = retention.replace("#VAL", retContainerVal); 1.176 + } 1.177 + 1.178 + StringBuilder annoData = new StringBuilder(); 1.179 + annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()) 1.180 + .append(Helper.ContentVars.IMPORTRETENTION.getVal()) 1.181 + .append(replacedRetCAVal) 1.182 + .append(Helper.ContentVars.CONTAINER.getVal()) 1.183 + .append(Helper.ContentVars.REPEATABLE.getVal()) 1.184 + .append(replacedRetBaseVal) 1.185 + .append(Helper.ContentVars.BASE.getVal()); 1.186 + 1.187 + String contents = annoData 1.188 + + Helper.ContentVars.REPEATABLEANNO.getVal() 1.189 + + "\nclass "+ className + "{}"; 1.190 + return contents; 1.191 + } 1.192 + 1.193 + private void error(String msg,String... contents) throws Exception { 1.194 + System.out.println("error: " + msg); 1.195 + errors++; 1.196 + if (contents.length == 1) { 1.197 + System.out.println("Contents = " + contents[0]); 1.198 + } 1.199 + // Test exits as soon as it gets a failure 1.200 + if (exitMode) throw new Exception(); 1.201 + } 1.202 +} 1.203 +