Fri, 15 Mar 2013 13:39:04 +0100
8007767: TargetAnnoCombo.java need to be updated to add a new test mode
Reviewed-by: jjg, strarup
darcy@1554 | 1 | /* |
darcy@1554 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
darcy@1554 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
darcy@1554 | 4 | * |
darcy@1554 | 5 | * This code is free software; you can redistribute it and/or modify it |
darcy@1554 | 6 | * under the terms of the GNU General Public License version 2 only, as |
darcy@1554 | 7 | * published by the Free Software Foundation. |
darcy@1554 | 8 | * |
darcy@1554 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
darcy@1554 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
darcy@1554 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
darcy@1554 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
darcy@1554 | 13 | * accompanied this code). |
darcy@1554 | 14 | * |
darcy@1554 | 15 | * You should have received a copy of the GNU General Public License version |
darcy@1554 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
darcy@1554 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
darcy@1554 | 18 | * |
darcy@1554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
darcy@1554 | 20 | * or visit www.oracle.com if you need additional information or have any |
darcy@1554 | 21 | * questions. |
darcy@1554 | 22 | */ |
darcy@1554 | 23 | |
mnunez@1641 | 24 | /* |
darcy@1554 | 25 | * @test |
mnunez@1641 | 26 | * @bug 7151010 8006547 8007766 |
mnunez@1641 | 27 | * @summary Default test cases for running combinations for Target values |
darcy@1554 | 28 | * @build Helper |
darcy@1554 | 29 | * @run main TargetAnnoCombo |
darcy@1554 | 30 | */ |
darcy@1554 | 31 | |
mnunez@1641 | 32 | import java.util.Set; |
mnunez@1641 | 33 | import java.util.List; |
darcy@1554 | 34 | import java.io.IOException; |
mnunez@1641 | 35 | import java.lang.annotation.ElementType; |
darcy@1554 | 36 | import java.util.ArrayList; |
darcy@1554 | 37 | import java.util.Arrays; |
mnunez@1641 | 38 | import java.util.EnumSet; |
darcy@1554 | 39 | import javax.tools.Diagnostic; |
darcy@1554 | 40 | import javax.tools.DiagnosticCollector; |
darcy@1554 | 41 | import javax.tools.JavaFileObject; |
darcy@1554 | 42 | |
mnunez@1641 | 43 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; |
mnunez@1641 | 44 | import static java.lang.annotation.ElementType.CONSTRUCTOR; |
mnunez@1641 | 45 | import static java.lang.annotation.ElementType.FIELD; |
mnunez@1641 | 46 | import static java.lang.annotation.ElementType.METHOD; |
mnunez@1641 | 47 | import static java.lang.annotation.ElementType.PARAMETER; |
mnunez@1641 | 48 | import static java.lang.annotation.ElementType.TYPE; |
mnunez@1641 | 49 | import static java.lang.annotation.ElementType.PACKAGE; |
mnunez@1641 | 50 | import static java.lang.annotation.ElementType.LOCAL_VARIABLE; |
mnunez@1641 | 51 | import static java.lang.annotation.ElementType.TYPE_USE; |
mnunez@1641 | 52 | import static java.lang.annotation.ElementType.TYPE_PARAMETER; |
darcy@1554 | 53 | |
darcy@1554 | 54 | public class TargetAnnoCombo { |
mnunez@1641 | 55 | |
darcy@1554 | 56 | static final String TESTPKG = "testpkg"; |
mnunez@1641 | 57 | |
mnunez@1641 | 58 | // Set it to true to get more debug information including base and container |
mnunez@1641 | 59 | // target sets for a given test case. |
darcy@1554 | 60 | static final boolean DEBUG = false; |
darcy@1554 | 61 | |
mnunez@1641 | 62 | // Define constant target sets to be used for the combination of the target values. |
mnunez@1641 | 63 | final static Set<ElementType> noSet = null; |
mnunez@1641 | 64 | final static Set<ElementType> empty = EnumSet.noneOf(ElementType.class); |
darcy@1554 | 65 | |
mnunez@1641 | 66 | // [TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, |
mnunez@1641 | 67 | // PACKAGE, TYPE_PARAMETER, TYPE_USE] |
mnunez@1641 | 68 | final static Set<ElementType> allTargets = EnumSet.allOf(ElementType.class); |
mnunez@1641 | 69 | |
mnunez@1641 | 70 | // [TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, |
mnunez@1641 | 71 | // PACKAGE] |
mnunez@1641 | 72 | final static Set<ElementType> jdk7 = EnumSet.range(TYPE, PACKAGE); |
mnunez@1641 | 73 | |
mnunez@1641 | 74 | // [TYPE_USE, TYPE_PARAMETER] |
mnunez@1641 | 75 | final static Set<ElementType> jdk8 = EnumSet.range(TYPE_PARAMETER, TYPE_USE); |
mnunez@1641 | 76 | |
mnunez@1641 | 77 | // List of test cases to run. This list is created in generate(). |
mnunez@1641 | 78 | // To run a specific test cases add case number in @run main line. |
mnunez@1641 | 79 | List<TestCase> testCases = new ArrayList<TestCase>(); |
mnunez@1641 | 80 | |
mnunez@1641 | 81 | int errors = 0; |
mnunez@1641 | 82 | |
mnunez@1641 | 83 | // Identify test cases that fail. |
mnunez@1641 | 84 | enum IgnoreKind { |
mnunez@1641 | 85 | RUN, |
mnunez@1641 | 86 | IGNORE |
mnunez@1641 | 87 | }; |
mnunez@1641 | 88 | |
mnunez@1641 | 89 | private class TestCase { |
mnunez@1641 | 90 | |
mnunez@1641 | 91 | private Set<ElementType> baseAnnotations; |
mnunez@1641 | 92 | private Set<ElementType> containerAnnotations; |
mnunez@1641 | 93 | private IgnoreKind ignore; |
mnunez@1641 | 94 | |
mnunez@1641 | 95 | public TestCase(Set<ElementType> baseAnnotations, Set<ElementType> containerAnnotations) { |
mnunez@1641 | 96 | this(baseAnnotations, containerAnnotations, IgnoreKind.RUN); |
mnunez@1641 | 97 | } |
mnunez@1641 | 98 | |
mnunez@1641 | 99 | public TestCase(Set<ElementType> baseAnnotations, Set<ElementType> containerAnnotations, |
mnunez@1641 | 100 | IgnoreKind ignoreKind) { |
mnunez@1641 | 101 | this.baseAnnotations = baseAnnotations; |
mnunez@1641 | 102 | this.containerAnnotations = containerAnnotations; |
mnunez@1641 | 103 | this.ignore = ignoreKind; |
mnunez@1641 | 104 | } |
mnunez@1641 | 105 | |
mnunez@1641 | 106 | public Set getBaseAnnotations() { |
mnunez@1641 | 107 | return baseAnnotations; |
mnunez@1641 | 108 | } |
mnunez@1641 | 109 | |
mnunez@1641 | 110 | public Set getContainerAnnotations() { |
mnunez@1641 | 111 | return containerAnnotations; |
mnunez@1641 | 112 | } |
mnunez@1641 | 113 | |
mnunez@1641 | 114 | public boolean isIgnored() { |
mnunez@1641 | 115 | return ignore == IgnoreKind.IGNORE; |
mnunez@1641 | 116 | } |
mnunez@1641 | 117 | |
mnunez@1641 | 118 | // Determine if a testCase should compile or not. |
mnunez@1641 | 119 | private boolean isValidSubSet() { |
mnunez@1641 | 120 | /* |
mnunez@1641 | 121 | * RULE 1: conAnnoTarget should be a subset of baseAnnoTarget |
mnunez@1641 | 122 | * RULE 2: For empty @Target ({}) - annotation cannot be applied anywhere |
mnunez@1641 | 123 | * - Empty sets for both is valid |
mnunez@1641 | 124 | * - Empty baseTarget set is invalid with non-empty conTarget set |
mnunez@1641 | 125 | * - Non-empty baseTarget set is valid with empty conTarget set |
mnunez@1641 | 126 | * RULE 3: For no @Target specified - annotation can be applied to any JDK 7 targets |
mnunez@1641 | 127 | * - No @Target for both is valid |
mnunez@1641 | 128 | * - No @Target for baseTarget set with @Target conTarget set is valid |
mnunez@1641 | 129 | * - @Target for baseTarget set with no @Target for conTarget is invalid |
mnunez@1641 | 130 | */ |
mnunez@1641 | 131 | |
mnunez@1641 | 132 | |
mnunez@1641 | 133 | /* If baseAnno has no @Target, Foo can be either applied to @Target specified |
mnunez@1641 | 134 | * for container annotation else will be applicable for all default targets |
mnunez@1641 | 135 | * if no @Target is present for container annotation. |
mnunez@1641 | 136 | * In both cases, the set will be a valid set with no @Target for base annotation |
mnunez@1641 | 137 | */ |
mnunez@1641 | 138 | if (baseAnnotations == null) { |
mnunez@1641 | 139 | if (containerAnnotations == null) { |
mnunez@1641 | 140 | return true; |
mnunez@1641 | 141 | } |
mnunez@1641 | 142 | return !(containerAnnotations.contains(TYPE_USE) || |
mnunez@1641 | 143 | containerAnnotations.contains(TYPE_PARAMETER)); |
mnunez@1641 | 144 | } |
mnunez@1641 | 145 | |
mnunez@1641 | 146 | Set<ElementType> tempBaseSet = EnumSet.noneOf(ElementType.class); |
mnunez@1641 | 147 | tempBaseSet.addAll(baseAnnotations); |
mnunez@1641 | 148 | // If BaseAnno has TYPE, then ANNOTATION_TYPE is allowed by default. |
mnunez@1641 | 149 | if (baseAnnotations.contains(TYPE)) { |
mnunez@1641 | 150 | tempBaseSet.add(ANNOTATION_TYPE); |
mnunez@1641 | 151 | } |
mnunez@1641 | 152 | |
mnunez@1641 | 153 | // If containerAnno has no @Target, only valid case if baseAnnoTarget has |
mnunez@1641 | 154 | // all targets defined else invalid set. |
mnunez@1641 | 155 | if (containerAnnotations == null) { |
mnunez@1641 | 156 | return tempBaseSet.containsAll(jdk7); |
mnunez@1641 | 157 | } |
mnunez@1641 | 158 | |
mnunez@1641 | 159 | // At this point, neither conAnnoTarget or baseAnnoTarget are null. |
mnunez@1641 | 160 | if (containerAnnotations.isEmpty()) { |
mnunez@1641 | 161 | return true; |
mnunez@1641 | 162 | } |
mnunez@1641 | 163 | |
mnunez@1641 | 164 | // At this point, conAnnoTarget is non-empty. |
mnunez@1641 | 165 | if (baseAnnotations.isEmpty()) { |
mnunez@1641 | 166 | return false; |
mnunez@1641 | 167 | } |
mnunez@1641 | 168 | |
mnunez@1641 | 169 | // At this point, neither conAnnoTarget or baseAnnoTarget are empty. |
mnunez@1641 | 170 | return tempBaseSet.containsAll(containerAnnotations); |
mnunez@1641 | 171 | } |
mnunez@1641 | 172 | } |
darcy@1554 | 173 | |
darcy@1554 | 174 | public static void main(String args[]) throws Exception { |
darcy@1554 | 175 | TargetAnnoCombo tac = new TargetAnnoCombo(); |
mnunez@1641 | 176 | // Generates all test cases to be run. |
mnunez@1641 | 177 | tac.generate(); |
mnunez@1641 | 178 | List<Integer> cases = new ArrayList<Integer>(); |
mnunez@1641 | 179 | for (int i = 0; i < args.length; i++) { |
mnunez@1641 | 180 | cases.add(Integer.parseInt(args[i])); |
darcy@1554 | 181 | } |
mnunez@1641 | 182 | if (cases.isEmpty()) { |
mnunez@1641 | 183 | tac.run(); |
mnunez@1641 | 184 | } else { |
mnunez@1641 | 185 | for (int index : cases) { |
mnunez@1641 | 186 | tac.executeTestCase(tac.testCases.get(index), index); |
mnunez@1641 | 187 | } |
mnunez@1641 | 188 | } |
darcy@1554 | 189 | } |
darcy@1554 | 190 | |
mnunez@1641 | 191 | private void generate() { |
mnunez@1641 | 192 | // Adding test cases to run. |
mnunez@1641 | 193 | testCases.addAll(Arrays.asList( |
mnunez@1641 | 194 | // No base target against no container target. |
mnunez@1641 | 195 | new TestCase(noSet, noSet), |
mnunez@1641 | 196 | // No base target against empty container target. |
mnunez@1641 | 197 | new TestCase(noSet, empty), |
mnunez@1641 | 198 | // No base target against TYPE_USE only container target. |
mnunez@1641 | 199 | new TestCase(noSet, less(jdk8, TYPE_PARAMETER)), |
mnunez@1641 | 200 | // No base target against TYPE_PARAMETER only container target. |
mnunez@1641 | 201 | new TestCase(noSet, less(jdk8, TYPE_USE)), |
mnunez@1641 | 202 | // No base target against TYPE_USE + TYPE_PARAMETER only container target. |
mnunez@1641 | 203 | new TestCase(noSet, jdk8), |
mnunez@1641 | 204 | // No base target against TYPE_USE + some selection of jdk7 targets. |
mnunez@1641 | 205 | new TestCase(noSet, |
mnunez@1641 | 206 | plus(EnumSet.range(TYPE, LOCAL_VARIABLE), TYPE_USE)), |
mnunez@1641 | 207 | // No base target against TYPE_PARAMETER + some selection of jdk7 targets. |
mnunez@1641 | 208 | new TestCase(noSet, |
mnunez@1641 | 209 | plus(EnumSet.range(TYPE, LOCAL_VARIABLE), TYPE_PARAMETER)), |
mnunez@1641 | 210 | // No base target against each jdk7 target alone as container target. |
mnunez@1641 | 211 | new TestCase(noSet, plus(empty, TYPE)), |
mnunez@1641 | 212 | new TestCase(noSet, plus(empty, PARAMETER)), |
mnunez@1641 | 213 | new TestCase(noSet, plus(empty, PACKAGE)), |
mnunez@1641 | 214 | new TestCase(noSet, plus(empty, METHOD)), |
mnunez@1641 | 215 | new TestCase(noSet, plus(empty, LOCAL_VARIABLE)), |
mnunez@1641 | 216 | new TestCase(noSet, plus(empty, FIELD)), |
mnunez@1641 | 217 | new TestCase(noSet, plus(empty, CONSTRUCTOR)), |
mnunez@1641 | 218 | new TestCase(noSet, plus(empty, ANNOTATION_TYPE)), |
mnunez@1641 | 219 | // Empty base target against no container target. |
mnunez@1641 | 220 | new TestCase(empty, noSet), |
mnunez@1641 | 221 | // Empty base target against empty container target. |
mnunez@1641 | 222 | new TestCase(empty, empty), |
mnunez@1641 | 223 | // Empty base target against any lone container target. |
mnunez@1641 | 224 | new TestCase(empty, plus(empty, TYPE)), |
mnunez@1641 | 225 | new TestCase(empty, plus(empty, PARAMETER)), |
mnunez@1641 | 226 | new TestCase(empty, plus(empty, PACKAGE)), |
mnunez@1641 | 227 | new TestCase(empty, plus(empty, METHOD)), |
mnunez@1641 | 228 | new TestCase(empty, plus(empty, LOCAL_VARIABLE)), |
mnunez@1641 | 229 | new TestCase(empty, plus(empty, FIELD)), |
mnunez@1641 | 230 | new TestCase(empty, plus(empty, CONSTRUCTOR)), |
mnunez@1641 | 231 | new TestCase(empty, plus(empty, ANNOTATION_TYPE)), |
mnunez@1641 | 232 | new TestCase(empty, less(jdk8, TYPE_USE)), |
mnunez@1641 | 233 | new TestCase(empty, less(jdk8, TYPE_PARAMETER)), |
mnunez@1641 | 234 | // No container target against all all-but one jdk7 targets. |
mnunez@1641 | 235 | new TestCase(less(jdk7, TYPE), noSet), |
mnunez@1641 | 236 | new TestCase(less(jdk7, PARAMETER), noSet), |
mnunez@1641 | 237 | new TestCase(less(jdk7, PACKAGE), noSet), |
mnunez@1641 | 238 | new TestCase(less(jdk7, METHOD), noSet), |
mnunez@1641 | 239 | new TestCase(less(jdk7, LOCAL_VARIABLE), noSet), |
mnunez@1641 | 240 | new TestCase(less(jdk7, FIELD), noSet), |
mnunez@1641 | 241 | new TestCase(less(jdk7, CONSTRUCTOR), noSet), |
mnunez@1641 | 242 | new TestCase(less(jdk7, ANNOTATION_TYPE), noSet), |
mnunez@1641 | 243 | // No container against all but TYPE and ANNOTATION_TYPE |
mnunez@1641 | 244 | new TestCase(less(jdk7, TYPE, ANNOTATION_TYPE), noSet), |
mnunez@1641 | 245 | // No container against jdk7 targets. |
mnunez@1641 | 246 | new TestCase(jdk7, noSet), |
mnunez@1641 | 247 | // No container against jdk7 targets plus one or both of TYPE_USE, TYPE_PARAMETER |
mnunez@1641 | 248 | new TestCase(plus(jdk7, TYPE_USE), noSet), |
mnunez@1641 | 249 | new TestCase(plus(jdk7, TYPE_PARAMETER), noSet), |
mnunez@1641 | 250 | new TestCase(allTargets, noSet), |
mnunez@1641 | 251 | // Empty container target against any lone target. |
mnunez@1641 | 252 | new TestCase(plus(empty, TYPE), empty), |
mnunez@1641 | 253 | new TestCase(plus(empty, PARAMETER), empty), |
mnunez@1641 | 254 | new TestCase(plus(empty, PACKAGE), empty), |
mnunez@1641 | 255 | new TestCase(plus(empty, METHOD), empty), |
mnunez@1641 | 256 | new TestCase(plus(empty, LOCAL_VARIABLE), empty), |
mnunez@1641 | 257 | new TestCase(plus(empty, FIELD), empty), |
mnunez@1641 | 258 | new TestCase(plus(empty, CONSTRUCTOR), empty), |
mnunez@1641 | 259 | new TestCase(plus(empty, ANNOTATION_TYPE), empty), |
mnunez@1641 | 260 | new TestCase(plus(empty, TYPE_USE), empty), |
mnunez@1641 | 261 | new TestCase(plus(empty, TYPE_PARAMETER), empty), |
mnunez@1641 | 262 | // All base targets against all container targets. |
mnunez@1641 | 263 | new TestCase(allTargets, allTargets), |
mnunez@1641 | 264 | // All base targets against all but one container targets. |
mnunez@1641 | 265 | new TestCase(allTargets, less(allTargets, TYPE)), |
mnunez@1641 | 266 | new TestCase(allTargets, less(allTargets, PARAMETER)), |
mnunez@1641 | 267 | new TestCase(allTargets, less(allTargets, PACKAGE)), |
mnunez@1641 | 268 | new TestCase(allTargets, less(allTargets, METHOD)), |
mnunez@1641 | 269 | new TestCase(allTargets, less(allTargets, LOCAL_VARIABLE)), |
mnunez@1641 | 270 | new TestCase(allTargets, less(allTargets, FIELD)), |
mnunez@1641 | 271 | new TestCase(allTargets, less(allTargets, CONSTRUCTOR)), |
mnunez@1641 | 272 | new TestCase(allTargets, less(allTargets, ANNOTATION_TYPE)), |
mnunez@1641 | 273 | new TestCase(allTargets, less(allTargets, TYPE_USE)), |
mnunez@1641 | 274 | new TestCase(allTargets, less(allTargets, TYPE_PARAMETER)), |
mnunez@1641 | 275 | // All container targets against all but one base targets. |
mnunez@1641 | 276 | new TestCase(less(allTargets, TYPE), allTargets), |
mnunez@1641 | 277 | new TestCase(less(allTargets, PARAMETER), allTargets), |
mnunez@1641 | 278 | new TestCase(less(allTargets, PACKAGE), allTargets), |
mnunez@1641 | 279 | new TestCase(less(allTargets, METHOD), allTargets), |
mnunez@1641 | 280 | new TestCase(less(allTargets, LOCAL_VARIABLE), allTargets), |
mnunez@1641 | 281 | new TestCase(less(allTargets, FIELD), allTargets), |
mnunez@1641 | 282 | new TestCase(less(allTargets, CONSTRUCTOR), allTargets), |
mnunez@1641 | 283 | new TestCase(less(allTargets, ANNOTATION_TYPE), allTargets), |
mnunez@1641 | 284 | new TestCase(less(allTargets, TYPE_USE), allTargets), |
mnunez@1641 | 285 | new TestCase(less(allTargets, TYPE_PARAMETER), allTargets))); |
mnunez@1641 | 286 | // Generates 100 test cases for any lone base target contained in Set |
mnunez@1641 | 287 | // allTargets against any lone container target. |
mnunez@1641 | 288 | for (ElementType b : allTargets) { |
mnunez@1641 | 289 | for (ElementType c : allTargets) { |
mnunez@1641 | 290 | testCases.add(new TestCase(plus(empty, b), plus(empty, c))); |
mnunez@1641 | 291 | } |
mnunez@1641 | 292 | } |
mnunez@1641 | 293 | } |
darcy@1554 | 294 | |
mnunez@1641 | 295 | void run() throws Exception { |
mnunez@1641 | 296 | int testCtr = 0; |
mnunez@1641 | 297 | for (TestCase tc : testCases) { |
mnunez@1641 | 298 | if (!tc.isIgnored()) { |
mnunez@1641 | 299 | executeTestCase(tc, testCases.indexOf(tc)); |
mnunez@1641 | 300 | testCtr++; |
mnunez@1641 | 301 | } |
mnunez@1641 | 302 | } |
mnunez@1641 | 303 | System.out.println("Total tests run: " + testCtr); |
mnunez@1641 | 304 | if (errors > 0) { |
mnunez@1641 | 305 | throw new Exception(errors + " errors found"); |
mnunez@1641 | 306 | } |
mnunez@1641 | 307 | } |
darcy@1554 | 308 | |
mnunez@1641 | 309 | private void executeTestCase(TestCase testCase, int index) { |
mnunez@1641 | 310 | debugPrint("Test case number = " + index); |
mnunez@1641 | 311 | debugPrint(" => baseAnnoTarget = " + testCase.getBaseAnnotations()); |
mnunez@1641 | 312 | debugPrint(" => containerAnnoTarget = " + testCase.getContainerAnnotations()); |
darcy@1554 | 313 | |
mnunez@1641 | 314 | String className = "TC" + index; |
mnunez@1641 | 315 | boolean shouldCompile = testCase.isValidSubSet(); |
mnunez@1641 | 316 | Iterable<? extends JavaFileObject> files = getFileList(className, testCase, shouldCompile); |
mnunez@1641 | 317 | // Get result of compiling test src file(s). |
darcy@1554 | 318 | boolean result = getCompileResult(className, shouldCompile, files); |
mnunez@1641 | 319 | // List test src code if test fails. |
mnunez@1641 | 320 | if (!result) { |
mnunez@1641 | 321 | System.out.println("FAIL: Test " + index); |
darcy@1554 | 322 | try { |
mnunez@1641 | 323 | for (JavaFileObject f : files) { |
darcy@1554 | 324 | System.out.println("File: " + f.getName() + "\n" + f.getCharContent(true)); |
darcy@1554 | 325 | } |
darcy@1554 | 326 | } catch (IOException ioe) { |
darcy@1554 | 327 | System.out.println("Exception: " + ioe); |
darcy@1554 | 328 | } |
darcy@1554 | 329 | } else { |
mnunez@1641 | 330 | debugPrint("PASS: Test " + index); |
darcy@1554 | 331 | } |
mnunez@1641 | 332 | |
darcy@1554 | 333 | } |
darcy@1554 | 334 | |
mnunez@1641 | 335 | // Create src code and corresponding JavaFileObjects. |
mnunez@1641 | 336 | private Iterable<? extends JavaFileObject> getFileList(String className, |
mnunez@1641 | 337 | TestCase testCase, boolean shouldCompile) { |
mnunez@1641 | 338 | Set<ElementType> baseAnnoTarget = testCase.getBaseAnnotations(); |
mnunez@1641 | 339 | Set<ElementType> conAnnoTarget = testCase.getContainerAnnotations(); |
mnunez@1641 | 340 | String srcContent = ""; |
mnunez@1641 | 341 | String pkgInfoContent = ""; |
mnunez@1641 | 342 | String template = Helper.template; |
mnunez@1641 | 343 | String baseTarget = "", conTarget = ""; |
mnunez@1641 | 344 | |
mnunez@1641 | 345 | String target = Helper.ContentVars.TARGET.getVal(); |
mnunez@1641 | 346 | if (baseAnnoTarget != null) { |
mnunez@1641 | 347 | String tmp = target.replace("#VAL", convertToString(baseAnnoTarget).toString()); |
mnunez@1641 | 348 | baseTarget = tmp.replace("[", "{").replace("]", "}"); |
mnunez@1641 | 349 | } |
mnunez@1641 | 350 | if (conAnnoTarget != null) { |
mnunez@1641 | 351 | String tmp = target.replace("#VAL", convertToString(conAnnoTarget).toString()); |
mnunez@1641 | 352 | conTarget = tmp.replace("[", "{").replace("]", "}"); |
mnunez@1641 | 353 | } |
mnunez@1641 | 354 | |
mnunez@1641 | 355 | String annoData = Helper.ContentVars.IMPORTSTMTS.getVal() |
mnunez@1641 | 356 | + conTarget |
mnunez@1641 | 357 | + Helper.ContentVars.CONTAINER.getVal() |
mnunez@1641 | 358 | + baseTarget |
mnunez@1641 | 359 | + Helper.ContentVars.REPEATABLE.getVal() |
mnunez@1641 | 360 | + Helper.ContentVars.BASE.getVal(); |
mnunez@1641 | 361 | |
mnunez@1641 | 362 | JavaFileObject pkgInfoFile = null; |
mnunez@1641 | 363 | |
mnunez@1641 | 364 | // If shouldCompile = true and no @Target is specified for container annotation, |
mnunez@1641 | 365 | // then all 8 ElementType enum constants are applicable as targets for |
mnunez@1641 | 366 | // container annotation. |
mnunez@1641 | 367 | if (shouldCompile && conAnnoTarget == null) { |
mnunez@1641 | 368 | Set<ElementType> copySet = EnumSet.noneOf(ElementType.class); |
mnunez@1641 | 369 | copySet.addAll(jdk7); |
mnunez@1641 | 370 | conAnnoTarget = copySet; |
mnunez@1641 | 371 | } |
mnunez@1641 | 372 | |
mnunez@1641 | 373 | if (shouldCompile) { |
mnunez@1641 | 374 | boolean isPkgCasePresent = conAnnoTarget.contains(PACKAGE); |
mnunez@1641 | 375 | String repeatableAnno = Helper.ContentVars.BASEANNO.getVal() |
mnunez@1641 | 376 | + " " + Helper.ContentVars.BASEANNO.getVal(); |
mnunez@1641 | 377 | for (ElementType s : conAnnoTarget) { |
mnunez@1641 | 378 | String replaceStr = "/*" + s.name() + "*/"; |
mnunez@1641 | 379 | if (s.name().equalsIgnoreCase("PACKAGE")) { |
mnunez@1641 | 380 | //Create packageInfo file. |
mnunez@1641 | 381 | String pkgInfoName = TESTPKG + "." + "package-info"; |
mnunez@1641 | 382 | pkgInfoContent = repeatableAnno + "\npackage " + TESTPKG + ";" + annoData; |
mnunez@1641 | 383 | pkgInfoFile = Helper.getFile(pkgInfoName, pkgInfoContent); |
mnunez@1641 | 384 | } else { |
mnunez@1641 | 385 | template = template.replace(replaceStr, repeatableAnno); |
mnunez@1641 | 386 | if (!isPkgCasePresent) { |
mnunez@1641 | 387 | srcContent = template.replace( |
mnunez@1641 | 388 | "/*ANNODATA*/", annoData).replace("#ClassName", className); |
mnunez@1641 | 389 | } else { |
mnunez@1641 | 390 | replaceStr = "/*PACKAGE*/"; |
mnunez@1641 | 391 | String tmp = template.replace(replaceStr, "package " + TESTPKG + ";"); |
mnunez@1641 | 392 | srcContent = tmp.replace("#ClassName", className); |
mnunez@1641 | 393 | } |
mnunez@1641 | 394 | } |
darcy@1554 | 395 | } |
mnunez@1641 | 396 | } else { |
mnunez@1641 | 397 | // For invalid cases, compilation should fail at declaration site. |
mnunez@1641 | 398 | template = "class #ClassName {}"; |
mnunez@1641 | 399 | srcContent = annoData + template.replace("#ClassName", className); |
darcy@1554 | 400 | } |
mnunez@1641 | 401 | JavaFileObject srcFile = Helper.getFile(className, srcContent); |
mnunez@1641 | 402 | Iterable<? extends JavaFileObject> files = null; |
mnunez@1641 | 403 | if (pkgInfoFile != null) { |
mnunez@1641 | 404 | files = Arrays.asList(pkgInfoFile, srcFile); |
mnunez@1641 | 405 | } else { |
mnunez@1641 | 406 | files = Arrays.asList(srcFile); |
mnunez@1641 | 407 | } |
mnunez@1641 | 408 | return files; |
darcy@1554 | 409 | } |
darcy@1554 | 410 | |
mnunez@1641 | 411 | // Compile the test source file(s) and return test result. |
darcy@1554 | 412 | private boolean getCompileResult(String className, boolean shouldCompile, |
darcy@1554 | 413 | Iterable<? extends JavaFileObject> files) { |
darcy@1554 | 414 | |
darcy@1554 | 415 | DiagnosticCollector<JavaFileObject> diagnostics = |
darcy@1554 | 416 | new DiagnosticCollector<JavaFileObject>(); |
darcy@1554 | 417 | Helper.compileCode(diagnostics, files); |
mnunez@1641 | 418 | // Test case pass or fail. |
darcy@1554 | 419 | boolean ok = false; |
darcy@1554 | 420 | String errMesg = ""; |
darcy@1554 | 421 | int numDiags = diagnostics.getDiagnostics().size(); |
darcy@1554 | 422 | if (numDiags == 0) { |
darcy@1554 | 423 | if (shouldCompile) { |
darcy@1554 | 424 | debugPrint("Test passed, compiled as expected."); |
darcy@1554 | 425 | ok = true; |
darcy@1554 | 426 | } else { |
darcy@1554 | 427 | errMesg = "Test failed, compiled unexpectedly."; |
darcy@1554 | 428 | ok = false; |
darcy@1554 | 429 | } |
darcy@1554 | 430 | } else { |
darcy@1554 | 431 | if (shouldCompile) { |
mnunez@1641 | 432 | // did not compile. |
darcy@1554 | 433 | errMesg = "Test failed, did not compile."; |
darcy@1554 | 434 | ok = false; |
darcy@1554 | 435 | } else { |
mnunez@1641 | 436 | // Error in compilation as expected. |
mnunez@1641 | 437 | String expectedErrKey = "compiler.err.invalid.repeatable." |
mnunez@1641 | 438 | + "annotation.incompatible.target"; |
darcy@1554 | 439 | for (Diagnostic<?> d : diagnostics.getDiagnostics()) { |
mnunez@1641 | 440 | if ((d.getKind() == Diagnostic.Kind.ERROR) |
mnunez@1641 | 441 | && d.getCode().contains(expectedErrKey)) { |
mnunez@1641 | 442 | // Error message as expected. |
darcy@1554 | 443 | debugPrint("Error message as expected."); |
darcy@1554 | 444 | ok = true; |
darcy@1554 | 445 | break; |
darcy@1554 | 446 | } else { |
mnunez@1641 | 447 | // error message is incorrect. |
darcy@1554 | 448 | ok = false; |
darcy@1554 | 449 | } |
darcy@1554 | 450 | } |
darcy@1554 | 451 | if (!ok) { |
mnunez@1641 | 452 | errMesg = "Incorrect error received when compiling " |
mnunez@1641 | 453 | + className + ", expected: " + expectedErrKey; |
darcy@1554 | 454 | } |
darcy@1554 | 455 | } |
darcy@1554 | 456 | } |
darcy@1554 | 457 | |
mnunez@1641 | 458 | if (!ok) { |
darcy@1554 | 459 | error(errMesg); |
mnunez@1641 | 460 | for (Diagnostic<?> d : diagnostics.getDiagnostics()) { |
darcy@1554 | 461 | System.out.println(" Diags: " + d); |
mnunez@1641 | 462 | } |
darcy@1554 | 463 | } |
darcy@1554 | 464 | return ok; |
darcy@1554 | 465 | } |
darcy@1554 | 466 | |
mnunez@1641 | 467 | private Set<ElementType> less(Set<ElementType> base, ElementType... sub) { |
mnunez@1641 | 468 | Set<ElementType> res = EnumSet.noneOf(ElementType.class); |
mnunez@1641 | 469 | res.addAll(base); |
mnunez@1641 | 470 | for (ElementType t : sub) { |
mnunez@1641 | 471 | res.remove(t); |
mnunez@1641 | 472 | } |
mnunez@1641 | 473 | return res; |
darcy@1554 | 474 | } |
darcy@1554 | 475 | |
mnunez@1641 | 476 | private Set<ElementType> plus(Set<ElementType> base, ElementType... add) { |
mnunez@1641 | 477 | Set<ElementType> res = EnumSet.noneOf(ElementType.class); |
mnunez@1641 | 478 | res.addAll(base); |
mnunez@1641 | 479 | for (ElementType t : add) { |
mnunez@1641 | 480 | res.add(t); |
darcy@1554 | 481 | } |
mnunez@1641 | 482 | return res; |
darcy@1554 | 483 | } |
darcy@1554 | 484 | |
mnunez@1641 | 485 | // Iterate target set and add "ElementType." in front of every target type. |
mnunez@1641 | 486 | private List<String> convertToString(Set<ElementType> annoTarget) { |
mnunez@1641 | 487 | if (annoTarget == null) { |
mnunez@1641 | 488 | return null; |
darcy@1554 | 489 | } |
mnunez@1641 | 490 | List<String> annoTargets = new ArrayList<String>(); |
mnunez@1641 | 491 | for (ElementType e : annoTarget) { |
mnunez@1641 | 492 | annoTargets.add("ElementType." + e.name()); |
mnunez@1641 | 493 | } |
mnunez@1641 | 494 | return annoTargets; |
darcy@1554 | 495 | } |
darcy@1554 | 496 | |
mnunez@1641 | 497 | private void debugPrint(String string) { |
mnunez@1641 | 498 | if (DEBUG) { |
mnunez@1641 | 499 | System.out.println(string); |
darcy@1554 | 500 | } |
darcy@1554 | 501 | } |
darcy@1554 | 502 | |
mnunez@1641 | 503 | private void error(String msg) { |
darcy@1554 | 504 | System.out.println("ERROR: " + msg); |
darcy@1554 | 505 | errors++; |
darcy@1554 | 506 | } |
mnunez@1641 | 507 | } |
darcy@1554 | 508 |