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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/TargetAnnoCombo.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,516 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, 2014, 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      7151010 8006547 8007766 8029017
    1.30 + * @summary  Default test cases for running combinations for Target values
    1.31 + * @build    Helper
    1.32 + * @run main TargetAnnoCombo
    1.33 + */
    1.34 +
    1.35 +import java.util.Set;
    1.36 +import java.util.List;
    1.37 +import java.io.IOException;
    1.38 +import java.lang.annotation.ElementType;
    1.39 +import java.util.ArrayList;
    1.40 +import java.util.Arrays;
    1.41 +import java.util.EnumSet;
    1.42 +import javax.tools.Diagnostic;
    1.43 +import javax.tools.DiagnosticCollector;
    1.44 +import javax.tools.JavaFileObject;
    1.45 +
    1.46 +import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
    1.47 +import static java.lang.annotation.ElementType.CONSTRUCTOR;
    1.48 +import static java.lang.annotation.ElementType.FIELD;
    1.49 +import static java.lang.annotation.ElementType.METHOD;
    1.50 +import static java.lang.annotation.ElementType.PARAMETER;
    1.51 +import static java.lang.annotation.ElementType.TYPE;
    1.52 +import static java.lang.annotation.ElementType.PACKAGE;
    1.53 +import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
    1.54 +import static java.lang.annotation.ElementType.TYPE_USE;
    1.55 +import static java.lang.annotation.ElementType.TYPE_PARAMETER;
    1.56 +
    1.57 +public class TargetAnnoCombo {
    1.58 +
    1.59 +    static final String TESTPKG = "testpkg";
    1.60 +
    1.61 +    // Set it to true to get more debug information including base and container
    1.62 +    // target sets for a given test case.
    1.63 +    static final boolean DEBUG = false;
    1.64 +
    1.65 +    // Define constant target sets to be used for the combination of the target values.
    1.66 +    final static Set<ElementType> noSet = null;
    1.67 +    final static Set<ElementType> empty = EnumSet.noneOf(ElementType.class);
    1.68 +
    1.69 +    // [TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,
    1.70 +    // PACKAGE, TYPE_PARAMETER, TYPE_USE]
    1.71 +    final static Set<ElementType> allTargets = EnumSet.allOf(ElementType.class);
    1.72 +
    1.73 +    // [TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,
    1.74 +    // PACKAGE]
    1.75 +    final static Set<ElementType> jdk7 = EnumSet.range(TYPE, PACKAGE);
    1.76 +
    1.77 +    // [TYPE_USE, TYPE_PARAMETER]
    1.78 +    final static Set<ElementType> jdk8 = EnumSet.range(TYPE_PARAMETER, TYPE_USE);
    1.79 +
    1.80 +    // List of test cases to run. This list is created in generate().
    1.81 +    // To run a specific test cases add case number in @run main line.
    1.82 +    List<TestCase> testCases = new ArrayList<TestCase>();
    1.83 +
    1.84 +    int errors = 0;
    1.85 +
    1.86 +    // Identify test cases that fail.
    1.87 +    enum IgnoreKind {
    1.88 +        RUN,
    1.89 +        IGNORE
    1.90 +    };
    1.91 +
    1.92 +    private class TestCase {
    1.93 +
    1.94 +        private Set<ElementType> baseAnnotations;
    1.95 +        private Set<ElementType> containerAnnotations;
    1.96 +        private IgnoreKind ignore;
    1.97 +
    1.98 +        public TestCase(Set<ElementType> baseAnnotations, Set<ElementType> containerAnnotations) {
    1.99 +            this(baseAnnotations, containerAnnotations, IgnoreKind.RUN);
   1.100 +        }
   1.101 +
   1.102 +        public TestCase(Set<ElementType> baseAnnotations, Set<ElementType> containerAnnotations,
   1.103 +                        IgnoreKind ignoreKind) {
   1.104 +            this.baseAnnotations = baseAnnotations;
   1.105 +            this.containerAnnotations = containerAnnotations;
   1.106 +            this.ignore = ignoreKind;
   1.107 +        }
   1.108 +
   1.109 +        public Set getBaseAnnotations() {
   1.110 +            return baseAnnotations;
   1.111 +        }
   1.112 +
   1.113 +        public Set getContainerAnnotations() {
   1.114 +            return containerAnnotations;
   1.115 +        }
   1.116 +
   1.117 +        public boolean isIgnored() {
   1.118 +            return ignore == IgnoreKind.IGNORE;
   1.119 +        }
   1.120 +
   1.121 +        // Determine if a testCase should compile or not.
   1.122 +        private boolean isValidSubSet() {
   1.123 +            /*
   1.124 +             *  RULE 1: conAnnoTarget should be a subset of baseAnnoTarget
   1.125 +             *  RULE 2: For empty @Target ({}) - annotation cannot be applied anywhere
   1.126 +             *         - Empty sets for both is valid
   1.127 +             *         - Empty baseTarget set is invalid with non-empty conTarget set
   1.128 +             *         - Non-empty baseTarget set is valid with empty conTarget set
   1.129 +             *  RULE 3: For no @Target specified - annotation can be applied to any JDK 7 targets
   1.130 +             *         - No @Target for both is valid
   1.131 +             *         - No @Target for baseTarget set with @Target conTarget set is valid
   1.132 +             *         - @Target for baseTarget set with no @Target for conTarget is invalid
   1.133 +             */
   1.134 +
   1.135 +
   1.136 +            /* If baseAnno has no @Target, Foo can be either applied to @Target specified
   1.137 +             * for container annotation else will be applicable for all default targets
   1.138 +             * if no @Target is present for container annotation.
   1.139 +             * In both cases, the set will be a valid set with no @Target for base annotation
   1.140 +             */
   1.141 +            if (baseAnnotations == null) {
   1.142 +                if (containerAnnotations == null) {
   1.143 +                    return true;
   1.144 +                }
   1.145 +                return !(containerAnnotations.contains(TYPE_USE) ||
   1.146 +                         containerAnnotations.contains(TYPE_PARAMETER));
   1.147 +            }
   1.148 +
   1.149 +            Set<ElementType> tempBaseSet = EnumSet.noneOf(ElementType.class);
   1.150 +            tempBaseSet.addAll(baseAnnotations);
   1.151 +
   1.152 +            // If BaseAnno has TYPE, then ANNOTATION_TYPE is allowed by default.
   1.153 +            if (baseAnnotations.contains(TYPE)) {
   1.154 +                tempBaseSet.add(ANNOTATION_TYPE);
   1.155 +            }
   1.156 +
   1.157 +            // If BaseAnno has TYPE_USE, then add the extra allowed types
   1.158 +            if (baseAnnotations.contains(TYPE_USE)) {
   1.159 +                tempBaseSet.add(ANNOTATION_TYPE);
   1.160 +                tempBaseSet.add(TYPE);
   1.161 +                tempBaseSet.add(TYPE_PARAMETER);
   1.162 +            }
   1.163 +
   1.164 +            // If containerAnno has no @Target, only valid case if baseAnnoTarget has
   1.165 +            // all targets defined else invalid set.
   1.166 +            if (containerAnnotations == null) {
   1.167 +                return tempBaseSet.containsAll(jdk7);
   1.168 +            }
   1.169 +
   1.170 +            // At this point, neither conAnnoTarget or baseAnnoTarget are null.
   1.171 +            if (containerAnnotations.isEmpty()) {
   1.172 +                return true;
   1.173 +            }
   1.174 +
   1.175 +            // At this point, conAnnoTarget is non-empty.
   1.176 +            if (baseAnnotations.isEmpty()) {
   1.177 +                return false;
   1.178 +            }
   1.179 +
   1.180 +            // At this point, neither conAnnoTarget or baseAnnoTarget are empty.
   1.181 +            return tempBaseSet.containsAll(containerAnnotations);
   1.182 +        }
   1.183 +    }
   1.184 +
   1.185 +    public static void main(String args[]) throws Exception {
   1.186 +        TargetAnnoCombo tac = new TargetAnnoCombo();
   1.187 +        // Generates all test cases to be run.
   1.188 +        tac.generate();
   1.189 +        List<Integer> cases = new ArrayList<Integer>();
   1.190 +        for (int i = 0; i < args.length; i++) {
   1.191 +            cases.add(Integer.parseInt(args[i]));
   1.192 +        }
   1.193 +        if (cases.isEmpty()) {
   1.194 +            tac.run();
   1.195 +        } else {
   1.196 +            for (int index : cases) {
   1.197 +                tac.executeTestCase(tac.testCases.get(index), index);
   1.198 +            }
   1.199 +        }
   1.200 +    }
   1.201 +
   1.202 +    private void generate() {
   1.203 +        // Adding test cases to run.
   1.204 +        testCases.addAll(Arrays.asList(
   1.205 +                // No base target against no container target.
   1.206 +                new TestCase(noSet, noSet),
   1.207 +                // No base target against empty container target.
   1.208 +                new TestCase(noSet, empty),
   1.209 +                // No base target against TYPE_USE only container target.
   1.210 +                new TestCase(noSet, less(jdk8, TYPE_PARAMETER)),
   1.211 +                // No base target against TYPE_PARAMETER only container target.
   1.212 +                new TestCase(noSet, less(jdk8, TYPE_USE)),
   1.213 +                // No base target against TYPE_USE + TYPE_PARAMETER only container target.
   1.214 +                new TestCase(noSet, jdk8),
   1.215 +                // No base target against TYPE_USE + some selection of jdk7 targets.
   1.216 +                new TestCase(noSet,
   1.217 +                plus(EnumSet.range(TYPE, LOCAL_VARIABLE), TYPE_USE)),
   1.218 +                // No base target against TYPE_PARAMETER + some selection of jdk7 targets.
   1.219 +                new TestCase(noSet,
   1.220 +                plus(EnumSet.range(TYPE, LOCAL_VARIABLE), TYPE_PARAMETER)),
   1.221 +                // No base target against each jdk7 target alone as container target.
   1.222 +                new TestCase(noSet, plus(empty, TYPE)),
   1.223 +                new TestCase(noSet, plus(empty, PARAMETER)),
   1.224 +                new TestCase(noSet, plus(empty, PACKAGE)),
   1.225 +                new TestCase(noSet, plus(empty, METHOD)),
   1.226 +                new TestCase(noSet, plus(empty, LOCAL_VARIABLE)),
   1.227 +                new TestCase(noSet, plus(empty, FIELD)),
   1.228 +                new TestCase(noSet, plus(empty, CONSTRUCTOR)),
   1.229 +                new TestCase(noSet, plus(empty, ANNOTATION_TYPE)),
   1.230 +                // Empty base target against no container target.
   1.231 +                new TestCase(empty, noSet),
   1.232 +                // Empty base target against empty container target.
   1.233 +                new TestCase(empty, empty),
   1.234 +                // Empty base target against any lone container target.
   1.235 +                new TestCase(empty, plus(empty, TYPE)),
   1.236 +                new TestCase(empty, plus(empty, PARAMETER)),
   1.237 +                new TestCase(empty, plus(empty, PACKAGE)),
   1.238 +                new TestCase(empty, plus(empty, METHOD)),
   1.239 +                new TestCase(empty, plus(empty, LOCAL_VARIABLE)),
   1.240 +                new TestCase(empty, plus(empty, FIELD)),
   1.241 +                new TestCase(empty, plus(empty, CONSTRUCTOR)),
   1.242 +                new TestCase(empty, plus(empty, ANNOTATION_TYPE)),
   1.243 +                new TestCase(empty, less(jdk8, TYPE_USE)),
   1.244 +                new TestCase(empty, less(jdk8, TYPE_PARAMETER)),
   1.245 +                // No container target against all all-but one jdk7 targets.
   1.246 +                new TestCase(less(jdk7, TYPE), noSet),
   1.247 +                new TestCase(less(jdk7, PARAMETER), noSet),
   1.248 +                new TestCase(less(jdk7, PACKAGE), noSet),
   1.249 +                new TestCase(less(jdk7, METHOD), noSet),
   1.250 +                new TestCase(less(jdk7, LOCAL_VARIABLE), noSet),
   1.251 +                new TestCase(less(jdk7, FIELD), noSet),
   1.252 +                new TestCase(less(jdk7, CONSTRUCTOR), noSet),
   1.253 +                new TestCase(less(jdk7, ANNOTATION_TYPE), noSet),
   1.254 +                // No container against all but TYPE and ANNOTATION_TYPE
   1.255 +                new TestCase(less(jdk7, TYPE, ANNOTATION_TYPE), noSet),
   1.256 +                // No container against jdk7 targets.
   1.257 +                new TestCase(jdk7, noSet),
   1.258 +                // No container against jdk7 targets plus one or both of TYPE_USE, TYPE_PARAMETER
   1.259 +                new TestCase(plus(jdk7, TYPE_USE), noSet),
   1.260 +                new TestCase(plus(jdk7, TYPE_PARAMETER), noSet),
   1.261 +                new TestCase(allTargets, noSet),
   1.262 +                // Empty container target against any lone target.
   1.263 +                new TestCase(plus(empty, TYPE), empty),
   1.264 +                new TestCase(plus(empty, PARAMETER), empty),
   1.265 +                new TestCase(plus(empty, PACKAGE), empty),
   1.266 +                new TestCase(plus(empty, METHOD), empty),
   1.267 +                new TestCase(plus(empty, LOCAL_VARIABLE), empty),
   1.268 +                new TestCase(plus(empty, FIELD), empty),
   1.269 +                new TestCase(plus(empty, CONSTRUCTOR), empty),
   1.270 +                new TestCase(plus(empty, ANNOTATION_TYPE), empty),
   1.271 +                new TestCase(plus(empty, TYPE_USE), empty),
   1.272 +                new TestCase(plus(empty, TYPE_PARAMETER), empty),
   1.273 +                // All base targets against all container targets.
   1.274 +                new TestCase(allTargets, allTargets),
   1.275 +                // All base targets against all but one container targets.
   1.276 +                new TestCase(allTargets, less(allTargets, TYPE)),
   1.277 +                new TestCase(allTargets, less(allTargets, PARAMETER)),
   1.278 +                new TestCase(allTargets, less(allTargets, PACKAGE)),
   1.279 +                new TestCase(allTargets, less(allTargets, METHOD)),
   1.280 +                new TestCase(allTargets, less(allTargets, LOCAL_VARIABLE)),
   1.281 +                new TestCase(allTargets, less(allTargets, FIELD)),
   1.282 +                new TestCase(allTargets, less(allTargets, CONSTRUCTOR)),
   1.283 +                new TestCase(allTargets, less(allTargets, ANNOTATION_TYPE)),
   1.284 +                new TestCase(allTargets, less(allTargets, TYPE_USE)),
   1.285 +                new TestCase(allTargets, less(allTargets, TYPE_PARAMETER)),
   1.286 +                // All container targets against all but one base targets.
   1.287 +                new TestCase(less(allTargets, TYPE), allTargets),
   1.288 +                new TestCase(less(allTargets, PARAMETER), allTargets),
   1.289 +                new TestCase(less(allTargets, PACKAGE), allTargets),
   1.290 +                new TestCase(less(allTargets, METHOD), allTargets),
   1.291 +                new TestCase(less(allTargets, LOCAL_VARIABLE), allTargets),
   1.292 +                new TestCase(less(allTargets, FIELD), allTargets),
   1.293 +                new TestCase(less(allTargets, CONSTRUCTOR), allTargets),
   1.294 +                new TestCase(less(allTargets, ANNOTATION_TYPE), allTargets),
   1.295 +                new TestCase(less(allTargets, TYPE_USE), allTargets),
   1.296 +                new TestCase(less(allTargets, TYPE_PARAMETER), allTargets)));
   1.297 +        // Generates 100 test cases for any lone base target contained in Set
   1.298 +        // allTargets against any lone container target.
   1.299 +        for (ElementType b : allTargets) {
   1.300 +            for (ElementType c : allTargets) {
   1.301 +                testCases.add(new TestCase(plus(empty, b), plus(empty, c)));
   1.302 +            }
   1.303 +        }
   1.304 +    }
   1.305 +
   1.306 +    void run() throws Exception {
   1.307 +        int testCtr = 0;
   1.308 +        for (TestCase tc : testCases) {
   1.309 +            if (!tc.isIgnored()) {
   1.310 +                executeTestCase(tc, testCases.indexOf(tc));
   1.311 +                testCtr++;
   1.312 +            }
   1.313 +        }
   1.314 +        System.out.println("Total tests run: " + testCtr);
   1.315 +        if (errors > 0) {
   1.316 +            throw new Exception(errors + " errors found");
   1.317 +        }
   1.318 +    }
   1.319 +
   1.320 +    private void executeTestCase(TestCase testCase, int index) {
   1.321 +        debugPrint("Test case number = " + index);
   1.322 +        debugPrint(" => baseAnnoTarget = " + testCase.getBaseAnnotations());
   1.323 +        debugPrint(" => containerAnnoTarget = " + testCase.getContainerAnnotations());
   1.324 +
   1.325 +        String className = "TC" + index;
   1.326 +        boolean shouldCompile = testCase.isValidSubSet();
   1.327 +        Iterable<? extends JavaFileObject> files = getFileList(className, testCase, shouldCompile);
   1.328 +        // Get result of compiling test src file(s).
   1.329 +        boolean result = getCompileResult(className, shouldCompile, files);
   1.330 +        // List test src code if test fails.
   1.331 +        if (!result) {
   1.332 +            System.out.println("FAIL: Test " + index);
   1.333 +            try {
   1.334 +                for (JavaFileObject f : files) {
   1.335 +                    System.out.println("File: " + f.getName() + "\n" + f.getCharContent(true));
   1.336 +                }
   1.337 +            } catch (IOException ioe) {
   1.338 +                System.out.println("Exception: " + ioe);
   1.339 +            }
   1.340 +        } else {
   1.341 +            debugPrint("PASS: Test " + index);
   1.342 +        }
   1.343 +
   1.344 +    }
   1.345 +
   1.346 +    // Create src code and corresponding JavaFileObjects.
   1.347 +    private Iterable<? extends JavaFileObject> getFileList(String className,
   1.348 +            TestCase testCase, boolean shouldCompile) {
   1.349 +        Set<ElementType> baseAnnoTarget = testCase.getBaseAnnotations();
   1.350 +        Set<ElementType> conAnnoTarget = testCase.getContainerAnnotations();
   1.351 +        String srcContent = "";
   1.352 +        String pkgInfoContent = "";
   1.353 +        String template = Helper.template;
   1.354 +        String baseTarget = "", conTarget = "";
   1.355 +
   1.356 +        String target = Helper.ContentVars.TARGET.getVal();
   1.357 +        if (baseAnnoTarget != null) {
   1.358 +            String tmp = target.replace("#VAL", convertToString(baseAnnoTarget).toString());
   1.359 +            baseTarget = tmp.replace("[", "{").replace("]", "}");
   1.360 +        }
   1.361 +        if (conAnnoTarget != null) {
   1.362 +            String tmp = target.replace("#VAL", convertToString(conAnnoTarget).toString());
   1.363 +            conTarget = tmp.replace("[", "{").replace("]", "}");
   1.364 +        }
   1.365 +
   1.366 +        String annoData = Helper.ContentVars.IMPORTSTMTS.getVal()
   1.367 +                + conTarget
   1.368 +                + Helper.ContentVars.CONTAINER.getVal()
   1.369 +                + baseTarget
   1.370 +                + Helper.ContentVars.REPEATABLE.getVal()
   1.371 +                + Helper.ContentVars.BASE.getVal();
   1.372 +
   1.373 +        JavaFileObject pkgInfoFile = null;
   1.374 +
   1.375 +        // If shouldCompile = true and no @Target is specified for container annotation,
   1.376 +        // then all 8 ElementType enum constants are applicable as targets for
   1.377 +        // container annotation.
   1.378 +        if (shouldCompile && conAnnoTarget == null) {
   1.379 +            Set<ElementType> copySet = EnumSet.noneOf(ElementType.class);
   1.380 +            copySet.addAll(jdk7);
   1.381 +            conAnnoTarget = copySet;
   1.382 +        }
   1.383 +
   1.384 +        if (shouldCompile) {
   1.385 +            boolean isPkgCasePresent = conAnnoTarget.contains(PACKAGE);
   1.386 +            String repeatableAnno = Helper.ContentVars.BASEANNO.getVal()
   1.387 +                    + " " + Helper.ContentVars.BASEANNO.getVal();
   1.388 +            for (ElementType s : conAnnoTarget) {
   1.389 +                String replaceStr = "/*" + s.name() + "*/";
   1.390 +                if (s.name().equalsIgnoreCase("PACKAGE")) {
   1.391 +                    //Create packageInfo file.
   1.392 +                    String pkgInfoName = TESTPKG + "." + "package-info";
   1.393 +                    pkgInfoContent = repeatableAnno + "\npackage " + TESTPKG + ";" + annoData;
   1.394 +                    pkgInfoFile = Helper.getFile(pkgInfoName, pkgInfoContent);
   1.395 +                } else {
   1.396 +                    template = template.replace(replaceStr, repeatableAnno);
   1.397 +                    if (!isPkgCasePresent) {
   1.398 +                        srcContent = template.replace(
   1.399 +                                "/*ANNODATA*/", annoData).replace("#ClassName", className);
   1.400 +                    } else {
   1.401 +                        replaceStr = "/*PACKAGE*/";
   1.402 +                        String tmp = template.replace(replaceStr, "package " + TESTPKG + ";");
   1.403 +                        srcContent = tmp.replace("#ClassName", className);
   1.404 +                    }
   1.405 +                }
   1.406 +            }
   1.407 +        } else {
   1.408 +            // For invalid cases, compilation should fail at declaration site.
   1.409 +            template = "class #ClassName {}";
   1.410 +            srcContent = annoData + template.replace("#ClassName", className);
   1.411 +        }
   1.412 +        JavaFileObject srcFile = Helper.getFile(className, srcContent);
   1.413 +        Iterable<? extends JavaFileObject> files = null;
   1.414 +        if (pkgInfoFile != null) {
   1.415 +            files = Arrays.asList(pkgInfoFile, srcFile);
   1.416 +        } else {
   1.417 +            files = Arrays.asList(srcFile);
   1.418 +        }
   1.419 +        return files;
   1.420 +    }
   1.421 +
   1.422 +    // Compile the test source file(s) and return test result.
   1.423 +    private boolean getCompileResult(String className, boolean shouldCompile,
   1.424 +            Iterable<? extends JavaFileObject> files) {
   1.425 +
   1.426 +        DiagnosticCollector<JavaFileObject> diagnostics =
   1.427 +                new DiagnosticCollector<JavaFileObject>();
   1.428 +        Helper.compileCode(diagnostics, files);
   1.429 +        // Test case pass or fail.
   1.430 +        boolean ok = false;
   1.431 +        String errMesg = "";
   1.432 +        int numDiags = diagnostics.getDiagnostics().size();
   1.433 +        if (numDiags == 0) {
   1.434 +            if (shouldCompile) {
   1.435 +                debugPrint("Test passed, compiled as expected.");
   1.436 +                ok = true;
   1.437 +            } else {
   1.438 +                errMesg = "Test failed, compiled unexpectedly.";
   1.439 +                ok = false;
   1.440 +            }
   1.441 +        } else {
   1.442 +            if (shouldCompile) {
   1.443 +                // did not compile.
   1.444 +                errMesg = "Test failed, did not compile.";
   1.445 +                ok = false;
   1.446 +            } else {
   1.447 +                // Error in compilation as expected.
   1.448 +                String expectedErrKey = "compiler.err.invalid.repeatable."
   1.449 +                        + "annotation.incompatible.target";
   1.450 +                for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
   1.451 +                    if ((d.getKind() == Diagnostic.Kind.ERROR)
   1.452 +                            && d.getCode().contains(expectedErrKey)) {
   1.453 +                        // Error message as expected.
   1.454 +                        debugPrint("Error message as expected.");
   1.455 +                        ok = true;
   1.456 +                        break;
   1.457 +                    } else {
   1.458 +                        // error message is incorrect.
   1.459 +                        ok = false;
   1.460 +                    }
   1.461 +                }
   1.462 +                if (!ok) {
   1.463 +                    errMesg = "Incorrect error received when compiling "
   1.464 +                            + className + ", expected: " + expectedErrKey;
   1.465 +                }
   1.466 +            }
   1.467 +        }
   1.468 +
   1.469 +        if (!ok) {
   1.470 +            error(errMesg);
   1.471 +            for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
   1.472 +                System.out.println(" Diags: " + d);
   1.473 +            }
   1.474 +        }
   1.475 +        return ok;
   1.476 +    }
   1.477 +
   1.478 +    private Set<ElementType> less(Set<ElementType> base, ElementType... sub) {
   1.479 +        Set<ElementType> res = EnumSet.noneOf(ElementType.class);
   1.480 +        res.addAll(base);
   1.481 +        for (ElementType t : sub) {
   1.482 +            res.remove(t);
   1.483 +        }
   1.484 +        return res;
   1.485 +    }
   1.486 +
   1.487 +    private Set<ElementType> plus(Set<ElementType> base, ElementType... add) {
   1.488 +        Set<ElementType> res = EnumSet.noneOf(ElementType.class);
   1.489 +        res.addAll(base);
   1.490 +        for (ElementType t : add) {
   1.491 +            res.add(t);
   1.492 +        }
   1.493 +        return res;
   1.494 +    }
   1.495 +
   1.496 +    // Iterate target set and add "ElementType." in front of every target type.
   1.497 +    private List<String> convertToString(Set<ElementType> annoTarget) {
   1.498 +        if (annoTarget == null) {
   1.499 +            return null;
   1.500 +        }
   1.501 +        List<String> annoTargets = new ArrayList<String>();
   1.502 +        for (ElementType e : annoTarget) {
   1.503 +            annoTargets.add("ElementType." + e.name());
   1.504 +        }
   1.505 +        return annoTargets;
   1.506 +    }
   1.507 +
   1.508 +    private void debugPrint(String string) {
   1.509 +        if (DEBUG) {
   1.510 +            System.out.println(string);
   1.511 +        }
   1.512 +    }
   1.513 +
   1.514 +    private void error(String msg) {
   1.515 +        System.out.println("ERROR: " + msg);
   1.516 +        errors++;
   1.517 +    }
   1.518 +}
   1.519 +

mercurial