test/tools/javac/annotations/repeatingAnnotations/combo/RetentionAnnoCombo.java

Wed, 07 Nov 2012 17:01:19 -0800

author
jjg
date
Wed, 07 Nov 2012 17:01:19 -0800
changeset 1401
2986e7052952
child 1492
df694c775e8a
permissions
-rw-r--r--

8002157: Write combo compiler tests for repeating annotations for JDK8
Reviewed-by: darcy, jjg
Contributed-by: sonali.goel@oracle.com

jjg@1401 1 /*
jjg@1401 2 * Copyright (c) 2012, 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 Combo test for all possible combinations for Retention Values
jjg@1401 29 * @build Helper
jjg@1401 30 * @compile RetentionAnnoCombo.java
jjg@1401 31 * @run main RetentionAnnoCombo
jjg@1401 32 */
jjg@1401 33
jjg@1401 34 import java.util.HashMap;
jjg@1401 35 import java.util.Map;
jjg@1401 36
jjg@1401 37 import javax.tools.DiagnosticCollector;
jjg@1401 38 import javax.tools.JavaFileObject;
jjg@1401 39 import javax.tools.Diagnostic;
jjg@1401 40
jjg@1401 41 /*
jjg@1401 42 * Generate all combinations for the use of @Retention on base anno or container
jjg@1401 43 * anno or both. The test passes if valid test src compile as expected and
jjg@1401 44 * and invalid test src fail as expected.
jjg@1401 45 * Repeating annotations used only on class for these generated test src.
jjg@1401 46 */
jjg@1401 47
jjg@1401 48 public class RetentionAnnoCombo extends Helper {
jjg@1401 49 static int errors = 0;
jjg@1401 50 static boolean exitMode = false;
jjg@1401 51 static {
jjg@1401 52 String exitOnFail = System.getenv("EXIT_ON_FAIL");
jjg@1401 53 if (exitOnFail == null || exitOnFail == "" ) {
jjg@1401 54 exitMode = false;
jjg@1401 55 }
jjg@1401 56 else {
jjg@1401 57 if (exitOnFail.equalsIgnoreCase("YES") ||
jjg@1401 58 exitOnFail.equalsIgnoreCase("Y") ||
jjg@1401 59 exitOnFail.equalsIgnoreCase("TRUE") ||
jjg@1401 60 exitOnFail.equalsIgnoreCase("T")) {
jjg@1401 61 exitMode = true;
jjg@1401 62 }
jjg@1401 63 }
jjg@1401 64 }
jjg@1401 65
jjg@1401 66 public static void main(String args[]) throws Exception {
jjg@1401 67 new RetentionAnnoCombo().runTest();
jjg@1401 68 }
jjg@1401 69
jjg@1401 70 public void runTest() throws Exception {
jjg@1401 71
jjg@1401 72 /* 4x4 matrix for Retention values SOURCE, DEFAULT, CLASS, RUNTIME
jjg@1401 73 * i -> Retention value on ContainerAnno
jjg@1401 74 * j -> Retention value on BaseAnno
jjg@1401 75 * 1 -> retention value combo should compile
jjg@1401 76 */
jjg@1401 77 int[][] retention = { {1, 0, 0, 0},
jjg@1401 78 {1, 1, 1, 0},
jjg@1401 79 {1, 1, 1, 0},
jjg@1401 80 {1, 1, 1, 1} };
jjg@1401 81
jjg@1401 82 Map<Integer, String> retMap = setRetentionValMatrix();
jjg@1401 83 String contents = "";
jjg@1401 84 boolean result = false;
jjg@1401 85 int testCtr = 0;
jjg@1401 86 for (int i = 0; i < 4 ; i ++) {
jjg@1401 87 for (int j = 0; j < 4; j++ ) {
jjg@1401 88 testCtr++;
jjg@1401 89 String className = "RetentionTest_"+i+"_"+j;
jjg@1401 90 contents = getContent(className, retMap, i, j);
jjg@1401 91 if (retention[i][j] == 1) {
jjg@1401 92 // Code generated should compile
jjg@1401 93 result = getCompileResult(contents,className, true);
jjg@1401 94 if (!result) {
jjg@1401 95 error("FAIL: " + className + " did not compile as expected!", contents);
jjg@1401 96 }
jjg@1401 97 } else {
jjg@1401 98 result = getCompileResult(contents,className, false);
jjg@1401 99 if (!result) {
jjg@1401 100 error("FAIL: " + className + " compiled unexpectedly!", contents);
jjg@1401 101 }
jjg@1401 102 }
jjg@1401 103 if (result) {
jjg@1401 104 System.out.println("Test passed for className = " + className);
jjg@1401 105 }
jjg@1401 106 }
jjg@1401 107 }
jjg@1401 108
jjg@1401 109 System.out.println("Total number of tests run: " + testCtr);
jjg@1401 110 System.out.println("Total number of errors: " + errors);
jjg@1401 111
jjg@1401 112 if (errors > 0)
jjg@1401 113 throw new Exception(errors + " errors found");
jjg@1401 114 }
jjg@1401 115
jjg@1401 116 private boolean getCompileResult(String contents, String className,
jjg@1401 117 boolean shouldCompile) throws Exception{
jjg@1401 118
jjg@1401 119 DiagnosticCollector<JavaFileObject> diagnostics =
jjg@1401 120 new DiagnosticCollector<JavaFileObject>();
jjg@1401 121 boolean ok = compileCode(className, contents, diagnostics);
jjg@1401 122
jjg@1401 123 String expectedErrKey = "compiler.err.invalid.containedby" +
jjg@1401 124 ".annotation.retention";
jjg@1401 125 if (!shouldCompile && !ok) {
jjg@1401 126 for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
jjg@1401 127 if (!((d.getKind() == Diagnostic.Kind.ERROR) &&
jjg@1401 128 d.getCode().contains(expectedErrKey))) {
jjg@1401 129 error("FAIL: Incorrect error given, expected = "
jjg@1401 130 + expectedErrKey + ", Actual = " + d.getCode()
jjg@1401 131 + " for className = " + className, contents);
jjg@1401 132 }
jjg@1401 133 }
jjg@1401 134 }
jjg@1401 135
jjg@1401 136 return (shouldCompile == ok);
jjg@1401 137 }
jjg@1401 138
jjg@1401 139 private Map<Integer,String> setRetentionValMatrix() {
jjg@1401 140 HashMap<Integer,String> hm = new HashMap<>();
jjg@1401 141 hm.put(0,"SOURCE");
jjg@1401 142 hm.put(1,"DEFAULT");
jjg@1401 143 hm.put(2,"CLASS");
jjg@1401 144 hm.put(3,"RUNTIME");
jjg@1401 145 return hm;
jjg@1401 146 }
jjg@1401 147
jjg@1401 148 private String getContent(String className, Map<Integer, String> retMap,
jjg@1401 149 int i, int j) {
jjg@1401 150
jjg@1401 151 String retContainerVal = retMap.get(i).toString();
jjg@1401 152 String retBaseVal = retMap.get(j).toString();
jjg@1401 153 String replacedRetBaseVal = "", replacedRetCAVal = "";
jjg@1401 154 String retention = Helper.ContentVars.RETENTION.getVal();
jjg@1401 155
jjg@1401 156 // @Retention is available as default for both base and container anno
jjg@1401 157 if (retContainerVal.equalsIgnoreCase("DEFAULT")
jjg@1401 158 && retBaseVal.equalsIgnoreCase("DEFAULT")) {
jjg@1401 159 replacedRetBaseVal = "";
jjg@1401 160 replacedRetCAVal = "";
jjg@1401 161 // @Retention is available as default for container anno
jjg@1401 162 } else if (retContainerVal.equalsIgnoreCase("DEFAULT")) {
jjg@1401 163 replacedRetBaseVal = retention.replace("#VAL", retBaseVal);
jjg@1401 164 replacedRetCAVal = "";
jjg@1401 165 // @Retention is available as default for base anno
jjg@1401 166 } else if (retBaseVal.equalsIgnoreCase("DEFAULT")) {
jjg@1401 167 replacedRetBaseVal = "";
jjg@1401 168 replacedRetCAVal = retention.replace("#VAL", retContainerVal);
jjg@1401 169 // @Retention is not available as default for both base and container anno
jjg@1401 170 } else {
jjg@1401 171 replacedRetBaseVal = retention.replace("#VAL", retBaseVal);
jjg@1401 172 replacedRetCAVal = retention.replace("#VAL", retContainerVal);
jjg@1401 173 }
jjg@1401 174
jjg@1401 175 StringBuilder annoData = new StringBuilder();
jjg@1401 176 annoData.append(Helper.ContentVars.IMPORTCONTAINERSTMTS.getVal())
jjg@1401 177 .append(Helper.ContentVars.IMPORTRETENTION.getVal())
jjg@1401 178 .append(Helper.ContentVars.CONTAINERFOR.getVal())
jjg@1401 179 .append(replacedRetCAVal)
jjg@1401 180 .append(Helper.ContentVars.CONTAINER.getVal())
jjg@1401 181 .append(Helper.ContentVars.CONTAINEDBY.getVal())
jjg@1401 182 .append(replacedRetBaseVal)
jjg@1401 183 .append(Helper.ContentVars.BASE.getVal());
jjg@1401 184
jjg@1401 185 String contents = annoData
jjg@1401 186 + Helper.ContentVars.REPEATABLEANNO.getVal()
jjg@1401 187 + "\nclass "+ className + "{}";
jjg@1401 188 return contents;
jjg@1401 189 }
jjg@1401 190
jjg@1401 191 private void error(String msg,String... contents) throws Exception {
jjg@1401 192 System.out.println("error: " + msg);
jjg@1401 193 errors++;
jjg@1401 194 if (contents.length == 1) {
jjg@1401 195 System.out.println("Contents = " + contents[0]);
jjg@1401 196 }
jjg@1401 197 // Test exits as soon as it gets a failure
jjg@1401 198 if (exitMode) throw new Exception();
jjg@1401 199 }
jjg@1401 200 }
jjg@1401 201

mercurial