Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /** |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 8002157 |
aoqi@0 | 27 | * @author sogoel |
aoqi@0 | 28 | * @summary Combo test for all possible combinations for Retention Values |
aoqi@0 | 29 | * @build Helper |
aoqi@0 | 30 | * @compile RetentionAnnoCombo.java |
aoqi@0 | 31 | * @run main RetentionAnnoCombo |
aoqi@0 | 32 | */ |
aoqi@0 | 33 | |
aoqi@0 | 34 | import java.util.HashMap; |
aoqi@0 | 35 | import java.util.Map; |
aoqi@0 | 36 | |
aoqi@0 | 37 | import javax.tools.DiagnosticCollector; |
aoqi@0 | 38 | import javax.tools.JavaFileObject; |
aoqi@0 | 39 | import javax.tools.Diagnostic; |
aoqi@0 | 40 | |
aoqi@0 | 41 | /* |
aoqi@0 | 42 | * Generate all combinations for the use of @Retention on base anno or container |
aoqi@0 | 43 | * anno or both. The test passes if valid test src compile as expected and |
aoqi@0 | 44 | * and invalid test src fail as expected. |
aoqi@0 | 45 | * Repeating annotations used only on class for these generated test src. |
aoqi@0 | 46 | */ |
aoqi@0 | 47 | |
aoqi@0 | 48 | public class RetentionAnnoCombo extends Helper { |
aoqi@0 | 49 | static int errors = 0; |
aoqi@0 | 50 | static boolean exitMode = false; |
aoqi@0 | 51 | static { |
aoqi@0 | 52 | String exitOnFail = System.getenv("EXIT_ON_FAIL"); |
aoqi@0 | 53 | if (exitOnFail == null || exitOnFail == "" ) { |
aoqi@0 | 54 | exitMode = false; |
aoqi@0 | 55 | } |
aoqi@0 | 56 | else { |
aoqi@0 | 57 | if (exitOnFail.equalsIgnoreCase("YES") || |
aoqi@0 | 58 | exitOnFail.equalsIgnoreCase("Y") || |
aoqi@0 | 59 | exitOnFail.equalsIgnoreCase("TRUE") || |
aoqi@0 | 60 | exitOnFail.equalsIgnoreCase("T")) { |
aoqi@0 | 61 | exitMode = true; |
aoqi@0 | 62 | } |
aoqi@0 | 63 | } |
aoqi@0 | 64 | } |
aoqi@0 | 65 | |
aoqi@0 | 66 | public static void main(String args[]) throws Exception { |
aoqi@0 | 67 | new RetentionAnnoCombo().runTest(); |
aoqi@0 | 68 | } |
aoqi@0 | 69 | |
aoqi@0 | 70 | public void runTest() throws Exception { |
aoqi@0 | 71 | |
aoqi@0 | 72 | /* 4x4 matrix for Retention values SOURCE, DEFAULT, CLASS, RUNTIME |
aoqi@0 | 73 | * i -> Retention value on ContainerAnno |
aoqi@0 | 74 | * j -> Retention value on BaseAnno |
aoqi@0 | 75 | * 1 -> retention value combo should compile |
aoqi@0 | 76 | */ |
aoqi@0 | 77 | int[][] retention = { {1, 0, 0, 0}, |
aoqi@0 | 78 | {1, 1, 1, 0}, |
aoqi@0 | 79 | {1, 1, 1, 0}, |
aoqi@0 | 80 | {1, 1, 1, 1} }; |
aoqi@0 | 81 | |
aoqi@0 | 82 | Map<Integer, String> retMap = setRetentionValMatrix(); |
aoqi@0 | 83 | String contents = ""; |
aoqi@0 | 84 | boolean result = false; |
aoqi@0 | 85 | int testCtr = 0; |
aoqi@0 | 86 | for (int i = 0; i < 4 ; i ++) { |
aoqi@0 | 87 | for (int j = 0; j < 4; j++ ) { |
aoqi@0 | 88 | testCtr++; |
aoqi@0 | 89 | String className = "RetentionTest_"+i+"_"+j; |
aoqi@0 | 90 | contents = getContent(className, retMap, i, j); |
aoqi@0 | 91 | if (retention[i][j] == 1) { |
aoqi@0 | 92 | // Code generated should compile |
aoqi@0 | 93 | result = getCompileResult(contents,className, true); |
aoqi@0 | 94 | if (!result) { |
aoqi@0 | 95 | error("FAIL: " + className + " did not compile as expected!", contents); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | } else { |
aoqi@0 | 98 | result = getCompileResult(contents,className, false); |
aoqi@0 | 99 | if (!result) { |
aoqi@0 | 100 | error("FAIL: " + className + " compiled unexpectedly!", contents); |
aoqi@0 | 101 | } |
aoqi@0 | 102 | } |
aoqi@0 | 103 | if (result) { |
aoqi@0 | 104 | System.out.println("Test passed for className = " + className); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | } |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | System.out.println("Total number of tests run: " + testCtr); |
aoqi@0 | 110 | System.out.println("Total number of errors: " + errors); |
aoqi@0 | 111 | |
aoqi@0 | 112 | if (errors > 0) |
aoqi@0 | 113 | throw new Exception(errors + " errors found"); |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | private boolean getCompileResult(String contents, String className, |
aoqi@0 | 117 | boolean shouldCompile) throws Exception{ |
aoqi@0 | 118 | |
aoqi@0 | 119 | DiagnosticCollector<JavaFileObject> diagnostics = |
aoqi@0 | 120 | new DiagnosticCollector<JavaFileObject>(); |
aoqi@0 | 121 | boolean ok = compileCode(className, contents, diagnostics); |
aoqi@0 | 122 | |
aoqi@0 | 123 | String expectedErrKey = "compiler.err.invalid.repeatable" + |
aoqi@0 | 124 | ".annotation.retention"; |
aoqi@0 | 125 | if (!shouldCompile && !ok) { |
aoqi@0 | 126 | for (Diagnostic<?> d : diagnostics.getDiagnostics()) { |
aoqi@0 | 127 | if (!((d.getKind() == Diagnostic.Kind.ERROR) && |
aoqi@0 | 128 | d.getCode().contains(expectedErrKey))) { |
aoqi@0 | 129 | error("FAIL: Incorrect error given, expected = " |
aoqi@0 | 130 | + expectedErrKey + ", Actual = " + d.getCode() |
aoqi@0 | 131 | + " for className = " + className, contents); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | } |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | return (shouldCompile == ok); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | private Map<Integer,String> setRetentionValMatrix() { |
aoqi@0 | 140 | HashMap<Integer,String> hm = new HashMap<>(); |
aoqi@0 | 141 | hm.put(0,"SOURCE"); |
aoqi@0 | 142 | hm.put(1,"DEFAULT"); |
aoqi@0 | 143 | hm.put(2,"CLASS"); |
aoqi@0 | 144 | hm.put(3,"RUNTIME"); |
aoqi@0 | 145 | return hm; |
aoqi@0 | 146 | } |
aoqi@0 | 147 | |
aoqi@0 | 148 | private String getContent(String className, Map<Integer, String> retMap, |
aoqi@0 | 149 | int i, int j) { |
aoqi@0 | 150 | |
aoqi@0 | 151 | String retContainerVal = retMap.get(i).toString(); |
aoqi@0 | 152 | String retBaseVal = retMap.get(j).toString(); |
aoqi@0 | 153 | String replacedRetBaseVal = "", replacedRetCAVal = ""; |
aoqi@0 | 154 | String retention = Helper.ContentVars.RETENTION.getVal(); |
aoqi@0 | 155 | |
aoqi@0 | 156 | // @Retention is available as default for both base and container anno |
aoqi@0 | 157 | if (retContainerVal.equalsIgnoreCase("DEFAULT") |
aoqi@0 | 158 | && retBaseVal.equalsIgnoreCase("DEFAULT")) { |
aoqi@0 | 159 | replacedRetBaseVal = ""; |
aoqi@0 | 160 | replacedRetCAVal = ""; |
aoqi@0 | 161 | // @Retention is available as default for container anno |
aoqi@0 | 162 | } else if (retContainerVal.equalsIgnoreCase("DEFAULT")) { |
aoqi@0 | 163 | replacedRetBaseVal = retention.replace("#VAL", retBaseVal); |
aoqi@0 | 164 | replacedRetCAVal = ""; |
aoqi@0 | 165 | // @Retention is available as default for base anno |
aoqi@0 | 166 | } else if (retBaseVal.equalsIgnoreCase("DEFAULT")) { |
aoqi@0 | 167 | replacedRetBaseVal = ""; |
aoqi@0 | 168 | replacedRetCAVal = retention.replace("#VAL", retContainerVal); |
aoqi@0 | 169 | // @Retention is not available as default for both base and container anno |
aoqi@0 | 170 | } else { |
aoqi@0 | 171 | replacedRetBaseVal = retention.replace("#VAL", retBaseVal); |
aoqi@0 | 172 | replacedRetCAVal = retention.replace("#VAL", retContainerVal); |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | StringBuilder annoData = new StringBuilder(); |
aoqi@0 | 176 | annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal()) |
aoqi@0 | 177 | .append(Helper.ContentVars.IMPORTRETENTION.getVal()) |
aoqi@0 | 178 | .append(replacedRetCAVal) |
aoqi@0 | 179 | .append(Helper.ContentVars.CONTAINER.getVal()) |
aoqi@0 | 180 | .append(Helper.ContentVars.REPEATABLE.getVal()) |
aoqi@0 | 181 | .append(replacedRetBaseVal) |
aoqi@0 | 182 | .append(Helper.ContentVars.BASE.getVal()); |
aoqi@0 | 183 | |
aoqi@0 | 184 | String contents = annoData |
aoqi@0 | 185 | + Helper.ContentVars.REPEATABLEANNO.getVal() |
aoqi@0 | 186 | + "\nclass "+ className + "{}"; |
aoqi@0 | 187 | return contents; |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | private void error(String msg,String... contents) throws Exception { |
aoqi@0 | 191 | System.out.println("error: " + msg); |
aoqi@0 | 192 | errors++; |
aoqi@0 | 193 | if (contents.length == 1) { |
aoqi@0 | 194 | System.out.println("Contents = " + contents[0]); |
aoqi@0 | 195 | } |
aoqi@0 | 196 | // Test exits as soon as it gets a failure |
aoqi@0 | 197 | if (exitMode) throw new Exception(); |
aoqi@0 | 198 | } |
aoqi@0 | 199 | } |
aoqi@0 | 200 |