7195131: Update 2 compiler combo tests for repeating annotations to include package and default use cases

Thu, 07 Feb 2013 20:47:06 -0800

author
darcy
date
Thu, 07 Feb 2013 20:47:06 -0800
changeset 1554
5125b9854d07
parent 1553
b386b8c45387
child 1555
762d0af062f5

7195131: Update 2 compiler combo tests for repeating annotations to include package and default use cases
Reviewed-by: darcy
Contributed-by: sonali.goel@oracle.com

test/tools/javac/annotations/repeatingAnnotations/combo/Helper.java file | annotate | diff | comparison | revisions
test/tools/javac/annotations/repeatingAnnotations/combo/TargetAnnoCombo.java file | annotate | diff | comparison | revisions
test/tools/javac/annotations/repeatingAnnotations/combo/TestCaseGenerator.java file | annotate | diff | comparison | revisions
     1.1 --- a/test/tools/javac/annotations/repeatingAnnotations/combo/Helper.java	Wed Feb 06 23:10:35 2013 +0000
     1.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/Helper.java	Thu Feb 07 20:47:06 2013 -0800
     1.3 @@ -40,14 +40,17 @@
     1.4          IMPORTINHERITED("import java.lang.annotation.Inherited;\n"),
     1.5          IMPORTRETENTION("import java.lang.annotation.Retention;\n" +
     1.6                          "\nimport java.lang.annotation.RetentionPolicy;\n"),
     1.7 +        IMPORTSTMTS("import java.lang.annotation.*;\n"),
     1.8          REPEATABLE("\n@Repeatable(FooContainer.class)\n"),
     1.9          CONTAINER("@interface FooContainer {\n" +"  Foo[] value();\n}\n"),
    1.10          BASE("@interface Foo {}\n"),
    1.11 +        BASEANNO("@Foo"),
    1.12          REPEATABLEANNO("\n@Foo() @Foo()"),
    1.13          DEPRECATED("\n@Deprecated"),
    1.14          DOCUMENTED("\n@Documented"),
    1.15          INHERITED("\n@Inherited"),
    1.16 -        RETENTION("@Retention(RetentionPolicy.#VAL)\n");
    1.17 +        RETENTION("@Retention(RetentionPolicy.#VAL)\n"),
    1.18 +        TARGET("\n@Target(#VAL)\n");
    1.19  
    1.20          private String val;
    1.21  
    1.22 @@ -69,6 +72,7 @@
    1.23      public static final String template =
    1.24              "/*PACKAGE*/\n" +
    1.25              "//pkg test;\n\n" +
    1.26 +            "/*ANNODATA*/\n" + // import statements, declaration of Foo/FooContainer
    1.27              "/*TYPE*/ //class\n" +
    1.28              "class #ClassName {\n" +
    1.29              "  /*FIELD*/ //instance var\n" +
    1.30 @@ -97,7 +101,11 @@
    1.31              "interface TestInterface {}\n\n" +
    1.32              "/*TYPE*/\n" +
    1.33              "/*ANNOTATION_TYPE*/\n" +
    1.34 -            "@interface TestAnnotationType{}\n";
    1.35 +            "@interface TestAnnotationType{}\n" +
    1.36 +            "class TestPkg {}\n" +
    1.37 +            "class TestTypeAnno </*TYPE_PARAMETER*/ T extends Object> {\n" +
    1.38 +            "  String /*TYPE_USE*/[] arr;\n" +
    1.39 +            "}";
    1.40  
    1.41      // Create and compile FileObject using values for className and contents
    1.42      public static boolean compileCode(String className, String contents,
    1.43 @@ -150,3 +158,4 @@
    1.44          }
    1.45      }
    1.46  }
    1.47 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/TargetAnnoCombo.java	Thu Feb 07 20:47:06 2013 -0800
     2.3 @@ -0,0 +1,469 @@
     2.4 +/*
     2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/**
    2.28 + * @test
    2.29 + * @bug      7195131
    2.30 + * @author   sogoel
    2.31 + * @summary  Combo test for all possible combinations for Target values
    2.32 + * @build    Helper
    2.33 + * @compile  TargetAnnoCombo.java TestCaseGenerator.java
    2.34 + * @run main TargetAnnoCombo
    2.35 + */
    2.36 +
    2.37 +import java.io.IOException;
    2.38 +import java.util.ArrayList;
    2.39 +import java.util.Arrays;
    2.40 +import java.util.HashSet;
    2.41 +import java.util.Set;
    2.42 +import javax.tools.Diagnostic;
    2.43 +import javax.tools.DiagnosticCollector;
    2.44 +import javax.tools.JavaFileObject;
    2.45 +
    2.46 +/*
    2.47 + * TargetAnnoCombo gets a list of test case numbers using TestCaseGenerator.
    2.48 + * For each of the test case number, @Target sets for base and container annotations
    2.49 + * are determined, source files are generated, compiled, and the result is verified
    2.50 + * based on if the @Target set for base and container is a positive or negative combination.
    2.51 + *
    2.52 + * @Target sets for base and container annotations are determined using a bit mapping of
    2.53 + * 10 ElementType enum constants defined in JDK8.
    2.54 + *
    2.55 + * Bit      Target value
    2.56 + *  0  "ElementType.ANNOTATION_TYPE"
    2.57 + *  1  "ElementType.CONSTRUCTOR"
    2.58 + *  2  "ElementType.FIELD"
    2.59 + *  3  "ElementType.LOCAL_VARIABLE"
    2.60 + *  4  "ElementType.METHOD"
    2.61 + *  5  "ElementType.TYPE"
    2.62 + *  6  "ElementType.PARAMETER"
    2.63 + *  7  "ElementType.PACKAGE"
    2.64 + *  8  "ElementType.TYPE_USE"
    2.65 + *  9  "ElementType.TYPE_PARAMETER"
    2.66 + *
    2.67 + * Group 1:
    2.68 + * 20 bits mapping, representing a test case number, is used for all target set
    2.69 + * combinations ( 0 to 1048575 ) including empty @Target sets => @Target({}).
    2.70 + * From this 20 bits, 10 bits are for base followed by 10 bits for container
    2.71 + * where each bit maps to an ElementType enum constant defined in JDK8.
    2.72 + *
    2.73 + * Examples:
    2.74 + * Test case number: 4, binary: 100 => container=100, base=[], container=["ElementType.FIELD"]
    2.75 + * Test case number: 1003575, binary: 11110101000000110111 => base=1111010100, container=0000110111;
    2.76 + *                   base=["ElementType.PARAMETER", "ElementType.TYPE_USE", "ElementType.METHOD", "ElementType.FIELD", "ElementType.PACKAGE", "ElementType.TYPE_PARAMETER"],
    2.77 + *                   container=["ElementType.TYPE", "ElementType.METHOD", "ElementType.ANNOTATION_TYPE", "ElementType.CONSTRUCTOR", "ElementType.FIELD"]
    2.78 + *
    2.79 + * In the following groups, no @Target set is represented by null.
    2.80 + * Group 2:
    2.81 + * @Target is not defined on base.
    2.82 + * Target sets for container are determined using the 10-bit binary number
    2.83 + * resulting in 1024 test cases, mapping them to test case numbers from
    2.84 + * 1048576 to (1048576 + 1023) => 1048576 to 1049599.
    2.85 + *
    2.86 + * Example:
    2.87 + * Test case number: 1048587 => 1048587 - 1048576 = test case 11 in Group 2, binary: 1011 =>
    2.88 + *                   base = null,
    2.89 + *                   container = ["ElementType.ANNOTATION_TYPE","ElementType.CONSTRUCTOR","ElementType.LOCAL_VARIABLE"]
    2.90 + *
    2.91 + * Group 3:
    2.92 + * @Target is not defined on container
    2.93 + * Target sets for base are determined using the 10-bit binary number
    2.94 + * resulting in 1024 test cases, mapping them to test case numbers from
    2.95 + * 1049600 to (1049600 + 1023) => 1049600 to 1050623.
    2.96 + *
    2.97 + * Example:
    2.98 + * Test case number: 1049708 => 1049708 - 1049600 = test case 108 in Group 3, binary: 1101100 =>
    2.99 + *                   base = ["ElementType.FIELD", "ElementType.LOCAL_VARIABLE", "ElementType.TYPE", "ElementType.PARAMETER"],
   2.100 + *                   container = null
   2.101 + *
   2.102 + * For the above group, test case number: 1049855 gives compiler error, JDK-8006547 filed
   2.103 + *
   2.104 + * Group 4:
   2.105 + * @Target not defined for both base and container annotations.
   2.106 + *
   2.107 + * This is the last test and corresponds to test case number 1050624. base=null, container=null
   2.108 + *
   2.109 + * Examples to run this test:
   2.110 + * 1. Run a specific test case number:
   2.111 + *    ${JTREG} -DTestCaseNum=10782 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
   2.112 + * 2. Run specific number of tests:
   2.113 + *    ${JTREG} -DNumberOfTests=4 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
   2.114 + * 3. Run specific number of tests with a seed:
   2.115 + *    ${JTREG} -DNumberOfTests=4 -DTestSeed=-972894659 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
   2.116 + * 4. Run tests in default mode (number of tests = 1000):
   2.117 + *    ${JTREG} -DTestMode=DEFAULT -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
   2.118 + * 5. Run all tests (FULL mode):
   2.119 + *    ${JTREG} -DTestMode=FULL -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
   2.120 + *
   2.121 + */
   2.122 +
   2.123 +public class TargetAnnoCombo {
   2.124 +    int errors = 0;
   2.125 +    static final String TESTPKG = "testpkg";
   2.126 +    /*
   2.127 +     *  Set it to true to get more debug information including base and
   2.128 +     *  container target sets for a given test case number
   2.129 +     */
   2.130 +    static final boolean DEBUG = false;
   2.131 +
   2.132 +    // JDK 5/6/7/8 Targets
   2.133 +    static final String[] targetVals = {"ElementType.ANNOTATION_TYPE",
   2.134 +      "ElementType.CONSTRUCTOR", "ElementType.FIELD",
   2.135 +      "ElementType.LOCAL_VARIABLE", "ElementType.METHOD",
   2.136 +      "ElementType.TYPE", "ElementType.PARAMETER",
   2.137 +      "ElementType.PACKAGE", "ElementType.TYPE_USE",
   2.138 +      "ElementType.TYPE_PARAMETER"};
   2.139 +
   2.140 +    // TYPE_USE and TYPE_PARAMETER (added in JDK8) are not part of default Target set
   2.141 +    static final int DEFAULT_TARGET_CNT = 8;
   2.142 +
   2.143 +    public static void main(String args[]) throws Exception {
   2.144 +
   2.145 +        /* maxTestNum = (base and container combinations of targetVals elems [0 - 1048575 combos])
   2.146 +         *              + (combinations where base or container has no Target [1024 combos])
   2.147 +         *              + (no -1 even though 1st test is number 0 as last test is where both
   2.148 +         *                 base and container have no target)
   2.149 +         */
   2.150 +
   2.151 +        int maxTestNum = (int)Math.pow(2, 2*targetVals.length) + 2*(int)Math.pow(2, targetVals.length);
   2.152 +        TestCaseGenerator tcg = new TestCaseGenerator(maxTestNum);
   2.153 +        TargetAnnoCombo tac = new TargetAnnoCombo();
   2.154 +
   2.155 +        int testCtr = 0;
   2.156 +        int testCase = -1;
   2.157 +        while ( (testCase=tcg.getNextTestCase()) != -1 ) {
   2.158 +            tac.executeTestCase(testCase, maxTestNum);
   2.159 +            testCtr++;
   2.160 +        }
   2.161 +
   2.162 +        System.out.println("Total tests run: " + testCtr);
   2.163 +        if (tac.errors > 0)
   2.164 +            throw new Exception(tac.errors + " errors found");
   2.165 +    }
   2.166 +
   2.167 +    /*
   2.168 +     * For given testCase, determine the base and container annotation Target sets,
   2.169 +     * get if testCase should compile, get test source file(s), get compilation result and verify.
   2.170 +     *
   2.171 +     */
   2.172 +    private void executeTestCase(int testCase, int maxTestNum) {
   2.173 +
   2.174 +        // Determine base and container annotation Target sets for the testCase
   2.175 +        Set<String> baseAnnoTarget = null;
   2.176 +        Set<String> conAnnoTarget = null;
   2.177 +
   2.178 +        //Number of base and container combinations [0 - 1048575 combos]
   2.179 +        int baseContCombos = (int)Math.pow(2, 2*targetVals.length);
   2.180 +        //Number of either base or container combinations when one of them has no @Target [1024 combos]
   2.181 +        int targetValsCombos = (int)Math.pow(2, targetVals.length);
   2.182 +
   2.183 +        if (testCase >= baseContCombos) {
   2.184 +            //Base annotation do not have @Target
   2.185 +            if (testCase < baseContCombos + targetValsCombos) {
   2.186 +                baseAnnoTarget = null;
   2.187 +                conAnnoTarget = getSetFromBitVec(Integer.toBinaryString(testCase - baseContCombos));
   2.188 +            } else if (testCase < baseContCombos + 2*targetValsCombos) {
   2.189 +                //Container annotation do not have @Target
   2.190 +                baseAnnoTarget = getSetFromBitVec(Integer.toBinaryString(testCase - baseContCombos - targetValsCombos));
   2.191 +                conAnnoTarget = null;
   2.192 +            } else {
   2.193 +                //Both Base and Container annotation do not have @Target
   2.194 +                baseAnnoTarget = null;
   2.195 +                conAnnoTarget = null;
   2.196 +            }
   2.197 +        } else {
   2.198 +            //TestCase number is represented as 10-bits for base followed by container bits
   2.199 +            String bin = Integer.toBinaryString(testCase);
   2.200 +            String base="", cont=bin;
   2.201 +            if (bin.length() > targetVals.length){
   2.202 +                base = bin.substring(0, bin.length() - targetVals.length);
   2.203 +                cont = bin.substring(bin.length() - targetVals.length,bin.length());
   2.204 +            }
   2.205 +            baseAnnoTarget = getSetFromBitVec(base);
   2.206 +            conAnnoTarget = getSetFromBitVec(cont);
   2.207 +        }
   2.208 +
   2.209 +        debugPrint("Test case number = " + testCase + " => binary = " + Integer.toBinaryString(testCase));
   2.210 +        debugPrint(" => baseAnnoTarget = " + baseAnnoTarget);
   2.211 +        debugPrint(" => containerAnnoTarget = " + conAnnoTarget);
   2.212 +
   2.213 +        // Determine if a testCase should compile or not
   2.214 +        String className = "TC" + testCase;
   2.215 +        boolean shouldCompile = isValidSubSet(baseAnnoTarget, conAnnoTarget);
   2.216 +
   2.217 +        // Get test source file(s)
   2.218 +        Iterable<? extends JavaFileObject> files = getFileList(className, baseAnnoTarget,
   2.219 +                conAnnoTarget, shouldCompile);
   2.220 +
   2.221 +        // Get result of compiling test src file(s)
   2.222 +        boolean result = getCompileResult(className, shouldCompile, files);
   2.223 +
   2.224 +        // List test src code if test fails
   2.225 +        if(!result) {
   2.226 +            System.out.println("FAIL: Test " + testCase);
   2.227 +            try {
   2.228 +                for (JavaFileObject f: files) {
   2.229 +                    System.out.println("File: " + f.getName() + "\n" + f.getCharContent(true));
   2.230 +                }
   2.231 +            } catch (IOException ioe) {
   2.232 +                System.out.println("Exception: " + ioe);
   2.233 +            }
   2.234 +        } else {
   2.235 +            debugPrint("PASS: Test " + testCase);
   2.236 +        }
   2.237 +    }
   2.238 +
   2.239 +    // Get a Set<String> based on bits that are set to 1
   2.240 +    public Set<String> getSetFromBitVec(String bitVec) {
   2.241 +        Set<String> ret = new HashSet<>();
   2.242 +        char[] bit = bitVec.toCharArray();
   2.243 +        for (int i=bit.length-1, j=0; i>=0; i--, j++){
   2.244 +            if (bit[i] == '1') {
   2.245 +                ret.add(targetVals[j]);
   2.246 +            }
   2.247 +        }
   2.248 +        return ret;
   2.249 +    }
   2.250 +
   2.251 +    // Compile the test source file(s) and return test result
   2.252 +    private boolean getCompileResult(String className, boolean shouldCompile,
   2.253 +            Iterable<? extends JavaFileObject> files) {
   2.254 +
   2.255 +        DiagnosticCollector<JavaFileObject> diagnostics =
   2.256 +                new DiagnosticCollector<JavaFileObject>();
   2.257 +        Helper.compileCode(diagnostics, files);
   2.258 +
   2.259 +        // Test case pass or fail
   2.260 +        boolean ok = false;
   2.261 +
   2.262 +        String errMesg = "";
   2.263 +        int numDiags = diagnostics.getDiagnostics().size();
   2.264 +
   2.265 +        if (numDiags == 0) {
   2.266 +            if (shouldCompile) {
   2.267 +                debugPrint("Test passed, compiled as expected.");
   2.268 +                ok = true;
   2.269 +            } else {
   2.270 +                errMesg = "Test failed, compiled unexpectedly.";
   2.271 +                ok = false;
   2.272 +            }
   2.273 +        } else {
   2.274 +            if (shouldCompile) {
   2.275 +                // did not compile
   2.276 +                errMesg = "Test failed, did not compile.";
   2.277 +                ok = false;
   2.278 +            } else {
   2.279 +                // Error in compilation as expected
   2.280 +                String expectedErrKey = "compiler.err.invalid.repeatable." +
   2.281 +                        "annotation.incompatible.target";
   2.282 +                for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
   2.283 +                    if((d.getKind() == Diagnostic.Kind.ERROR) &&
   2.284 +                        d.getCode().contains(expectedErrKey)) {
   2.285 +                        // Error message as expected
   2.286 +                        debugPrint("Error message as expected.");
   2.287 +                        ok = true;
   2.288 +                        break;
   2.289 +                    } else {
   2.290 +                        // error message is incorrect
   2.291 +                        ok = false;
   2.292 +                    }
   2.293 +                }
   2.294 +                if (!ok) {
   2.295 +                    errMesg = "Incorrect error received when compiling " +
   2.296 +                        className + ", expected: " + expectedErrKey;
   2.297 +                }
   2.298 +            }
   2.299 +        }
   2.300 +
   2.301 +        if(!ok) {
   2.302 +            error(errMesg);
   2.303 +            for (Diagnostic<?> d : diagnostics.getDiagnostics())
   2.304 +                System.out.println(" Diags: " + d);
   2.305 +        }
   2.306 +        return ok;
   2.307 +    }
   2.308 +
   2.309 +    private void debugPrint(String string) {
   2.310 +        if(DEBUG)
   2.311 +            System.out.println(string);
   2.312 +    }
   2.313 +
   2.314 +    // Create src code and corresponding JavaFileObjects
   2.315 +    private Iterable<? extends JavaFileObject> getFileList(String className,
   2.316 +            Set<String> baseAnnoTarget, Set<String> conAnnoTarget,
   2.317 +            boolean shouldCompile) {
   2.318 +
   2.319 +        String srcContent = "";
   2.320 +        String pkgInfoContent = "";
   2.321 +        String template = Helper.template;
   2.322 +        String baseTarget = "", conTarget = "";
   2.323 +
   2.324 +        String target = Helper.ContentVars.TARGET.getVal();
   2.325 +        if(baseAnnoTarget != null) {
   2.326 +            baseTarget = target.replace("#VAL", baseAnnoTarget.toString())
   2.327 +                                  .replace("[", "{").replace("]", "}");
   2.328 +        }
   2.329 +        if(conAnnoTarget != null) {
   2.330 +            conTarget = target.replace("#VAL", conAnnoTarget.toString())
   2.331 +                                 .replace("[", "{").replace("]", "}");
   2.332 +        }
   2.333 +
   2.334 +        String annoData = Helper.ContentVars.IMPORTSTMTS.getVal() +
   2.335 +                          conTarget +
   2.336 +                          Helper.ContentVars.CONTAINER.getVal() +
   2.337 +                          baseTarget +
   2.338 +                          Helper.ContentVars.REPEATABLE.getVal() +
   2.339 +                          Helper.ContentVars.BASE.getVal();
   2.340 +
   2.341 +        JavaFileObject pkgInfoFile = null;
   2.342 +
   2.343 +        /*
   2.344 +         *  If shouldCompile = true and no @Target is specified for container annotation,
   2.345 +         *  then all 8 ElementType enum constants are applicable as targets for
   2.346 +         *  container annotation.
   2.347 +         */
   2.348 +        if(shouldCompile && conAnnoTarget == null) {
   2.349 +            //conAnnoTarget = new HashSet<String>(Arrays.asList(targetVals));
   2.350 +            conAnnoTarget = getDefaultTargetSet();
   2.351 +        }
   2.352 +
   2.353 +        if(shouldCompile) {
   2.354 +            boolean isPkgCasePresent = new ArrayList<String>(conAnnoTarget).contains("ElementType.PACKAGE");
   2.355 +            String repeatableAnno = Helper.ContentVars.BASEANNO.getVal() + " " + Helper.ContentVars.BASEANNO.getVal();
   2.356 +            for(String s: conAnnoTarget) {
   2.357 +                s = s.replace("ElementType.","");
   2.358 +                String replaceStr = "/*"+s+"*/";
   2.359 +                if(s.equalsIgnoreCase("PACKAGE")) {
   2.360 +                    //Create packageInfo file
   2.361 +                    String pkgInfoName = TESTPKG + "." + "package-info";
   2.362 +                    pkgInfoContent = repeatableAnno + "\npackage " + TESTPKG + ";" + annoData;
   2.363 +                    pkgInfoFile = Helper.getFile(pkgInfoName, pkgInfoContent);
   2.364 +                } else {
   2.365 +                    template = template.replace(replaceStr, repeatableAnno);
   2.366 +                    //srcContent = template.replace("#ClassName",className);
   2.367 +                    if(!isPkgCasePresent) {
   2.368 +                        srcContent = template.replace("/*ANNODATA*/", annoData).replace("#ClassName",className);
   2.369 +                    } else {
   2.370 +                        replaceStr = "/*PACKAGE*/";
   2.371 +                        srcContent = template.replace(replaceStr, "package " + TESTPKG + ";")
   2.372 +                                     .replace("#ClassName", className);
   2.373 +                    }
   2.374 +                }
   2.375 +            }
   2.376 +        } else {
   2.377 +            // For invalid cases, compilation should fail at declaration site
   2.378 +            template = "class #ClassName {}";
   2.379 +            srcContent = annoData + template.replace("#ClassName",className);
   2.380 +        }
   2.381 +        JavaFileObject srcFile = Helper.getFile(className, srcContent);
   2.382 +        Iterable<? extends JavaFileObject> files = null;
   2.383 +        if(pkgInfoFile != null)
   2.384 +            files = Arrays.asList(pkgInfoFile,srcFile);
   2.385 +        else
   2.386 +            files = Arrays.asList(srcFile);
   2.387 +        return files;
   2.388 +    }
   2.389 +
   2.390 +    private Set<String> getDefaultTargetSet() {
   2.391 +        Set<String> defaultSet = new HashSet<>();
   2.392 +        int ctr = 0;
   2.393 +        for(String s : targetVals) {
   2.394 +            if(ctr++ < DEFAULT_TARGET_CNT) {
   2.395 +                defaultSet.add(s);
   2.396 +            }
   2.397 +        }
   2.398 +        return defaultSet;
   2.399 +    }
   2.400 +
   2.401 +    private boolean isValidSubSet(Set<String> baseAnnoTarget, Set<String> conAnnoTarget) {
   2.402 +        /*
   2.403 +         *  RULE 1: conAnnoTarget should be a subset of baseAnnoTarget
   2.404 +         *  RULE 2: For empty @Target ({}) - annotation cannot be applied anywhere
   2.405 +         *         - Empty sets for both is valid
   2.406 +         *         - Empty baseTarget set is invalid with non-empty conTarget set
   2.407 +         *         - Non-empty baseTarget set is valid with empty conTarget set
   2.408 +         *  RULE 3: For no @Target specified - annotation can be applied to any JDK 7 targets
   2.409 +         *         - No @Target for both is valid
   2.410 +         *         - No @Target for baseTarget set with @Target conTarget set is valid
   2.411 +         *         - @Target for baseTarget set with no @Target for conTarget is invalid
   2.412 +         */
   2.413 +
   2.414 +
   2.415 +        /* If baseAnno has no @Target, Foo can be either applied to @Target specified for container annotation
   2.416 +         * else will be applicable for all default targets if no @Target is present for container annotation.
   2.417 +         * In both cases, the set will be a valid set with no @Target for base annotation
   2.418 +         */
   2.419 +        if(baseAnnoTarget == null) {
   2.420 +            if(conAnnoTarget == null) return true;
   2.421 +            return !(conAnnoTarget.contains("ElementType.TYPE_USE") || conAnnoTarget.contains("ElementType.TYPE_PARAMETER"));
   2.422 +        }
   2.423 +
   2.424 +        Set<String> tempBaseSet = new HashSet<>(baseAnnoTarget);
   2.425 +        // If BaseAnno has TYPE, then ANNOTATION_TYPE is allowed by default
   2.426 +        if(baseAnnoTarget.contains("ElementType.TYPE")) {
   2.427 +            tempBaseSet.add("ElementType.ANNOTATION_TYPE");
   2.428 +        }
   2.429 +
   2.430 +        /*
   2.431 +         * If containerAnno has no @Target, only valid case if baseAnnoTarget has all targets defined
   2.432 +         * else invalid set
   2.433 +         */
   2.434 +        if(conAnnoTarget == null) {
   2.435 +            return (tempBaseSet.containsAll(getDefaultTargetSet()));
   2.436 +        }
   2.437 +
   2.438 +        // At this point, neither conAnnoTarget or baseAnnoTarget are null
   2.439 +        if(conAnnoTarget.size() == 0) return true;
   2.440 +
   2.441 +        // At this point, conAnnoTarget is non-empty
   2.442 +        if (baseAnnoTarget.size() == 0) return false;
   2.443 +
   2.444 +        // At this point, neither conAnnoTarget or baseAnnoTarget are empty
   2.445 +        return tempBaseSet.containsAll(conAnnoTarget);
   2.446 +    }
   2.447 +
   2.448 +    void error(String msg) {
   2.449 +        System.out.println("ERROR: " + msg);
   2.450 +        errors++;
   2.451 +    }
   2.452 +
   2.453 +    // Lists the start and end range for the given set of target vals
   2.454 +    void showGroups() {
   2.455 +        //Group 1: All target set combinations ( 0 to 1048575 ) including empty @Target sets => @Target({})
   2.456 +        int grpEnd1 = (int)Math.pow(2, 2*targetVals.length) - 1;
   2.457 +        System.out.println("[Group 1]: 0 - " + grpEnd1);
   2.458 +
   2.459 +        //Group 2: @Target not defined for base annotation ( 1048576 - 1049599 ).
   2.460 +        System.out.print("[Group 2]: " + (grpEnd1 + 1) + " - ");
   2.461 +        int grpEnd2 = grpEnd1 + 1 + (int)Math.pow(2, targetVals.length) - 1;
   2.462 +        System.out.println(grpEnd2);
   2.463 +
   2.464 +        //Group 3: @Target not defined for container annotation ( 1049600 - 1050623 ).
   2.465 +        System.out.print("[Group 3]: " + (grpEnd2 + 1) + " - ");
   2.466 +        int grpEnd3 = grpEnd2 + 1 + (int)Math.pow(2, targetVals.length) - 1;
   2.467 +        System.out.println(grpEnd3);
   2.468 +
   2.469 +        //Group 4: @Target not defined for both base and container annotations ( 1050624 ).
   2.470 +        System.out.println("[Group 4]: " + (grpEnd3 + 1));
   2.471 +    }
   2.472 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/annotations/repeatingAnnotations/combo/TestCaseGenerator.java	Thu Feb 07 20:47:06 2013 -0800
     3.3 @@ -0,0 +1,191 @@
     3.4 +/*
     3.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +import java.util.ArrayList;
    3.28 +import java.util.HashSet;
    3.29 +import java.util.List;
    3.30 +import java.util.Random;
    3.31 +
    3.32 +/* System properties:
    3.33 + * NumberOfTests, TestMode, and TestCaseNum are mutually exclusive
    3.34 + * TestSeed will be used only with NumberOfTests or TestMode, otherwise it will be ignored
    3.35 + * -DNumberOfTests=[0 to 2^20+2^11+1]
    3.36 + * -DTestMode=[FULL|DEFAULT]
    3.37 + * -DTestSeed=[seedNumber]
    3.38 + * -DTestCaseNum=[0 to 2^20+2^11+1]
    3.39 + */
    3.40 +public class TestCaseGenerator {
    3.41 +    // Total number of tests to be run
    3.42 +    int numberOfTests = -1;
    3.43 +    //Single test case
    3.44 +    int testCaseNum = -1;
    3.45 +    //Seed used to generate test cases
    3.46 +    int testSeed;
    3.47 +
    3.48 +    int maxTestNum;
    3.49 +    Random randNum;
    3.50 +
    3.51 +    // used in getNextTestCase
    3.52 +    int curTestNum;
    3.53 +    int testCompletedCount;
    3.54 +    HashSet<Integer> uniqueTestSet;
    3.55 +
    3.56 +    static final int DEFAULT_TEST_COUNT = 250;
    3.57 +
    3.58 +    /*
    3.59 +     *  Get parameter values from command line to set numberOfTests, testCaseNum,
    3.60 +     *  and testSeed
    3.61 +     */
    3.62 +    public TestCaseGenerator(int maxTestNum) {
    3.63 +        this.maxTestNum = maxTestNum;
    3.64 +
    3.65 +        // Set values for variables based on input from command line
    3.66 +
    3.67 +        // TestMode system property
    3.68 +        String testModeVal = System.getProperty("TestMode");
    3.69 +        if(testModeVal != null && !testModeVal.isEmpty()) {
    3.70 +            switch (testModeVal.toUpperCase()) {
    3.71 +            case "FULL":
    3.72 +                numberOfTests = maxTestNum;
    3.73 +                break;
    3.74 +            case "DEFAULT":
    3.75 +                numberOfTests = DEFAULT_TEST_COUNT;
    3.76 +                break;
    3.77 +            default:
    3.78 +                System.out.println("Invalid property value " + testModeVal +
    3.79 +                        " for numberOfTests. Possible range: 0 to " +
    3.80 +                        maxTestNum + ". Ignoring property");
    3.81 +                numberOfTests = -1;
    3.82 +            }
    3.83 +        }
    3.84 +
    3.85 +        // NumberOfTests system property
    3.86 +        String numTestsStr = System.getProperty("NumberOfTests");
    3.87 +        if(numTestsStr != null && !numTestsStr.isEmpty()) {
    3.88 +            int numTests = -1;
    3.89 +            try {
    3.90 +                numTests = Integer.parseInt(numTestsStr);
    3.91 +                if (numTests < 0 || numTests > maxTestNum) {
    3.92 +                    throw new NumberFormatException();
    3.93 +                }
    3.94 +            } catch(NumberFormatException nfe) {
    3.95 +                System.out.println("Invalid NumberOfTests property value " +
    3.96 +                        numTestsStr + ". Possible range: 0 to " + maxTestNum +
    3.97 +                        "Reset to default: " + DEFAULT_TEST_COUNT);
    3.98 +                numTests = DEFAULT_TEST_COUNT;
    3.99 +            }
   3.100 +
   3.101 +            if (numberOfTests != -1 && numTests != -1) {
   3.102 +                System.out.println("TestMode and NumberOfTests cannot be set together. Ignoring TestMode.");
   3.103 +            }
   3.104 +            numberOfTests = numTests;
   3.105 +        }
   3.106 +
   3.107 +        // TestSeed system property
   3.108 +        String seedVal = System.getProperty("TestSeed");
   3.109 +        if(seedVal != null && !seedVal.isEmpty()) {
   3.110 +            try {
   3.111 +                testSeed = Integer.parseInt(seedVal);
   3.112 +            } catch(NumberFormatException nfe) {
   3.113 +                Random srand = new Random();
   3.114 +                testSeed = srand.nextInt();
   3.115 +            }
   3.116 +        } else {
   3.117 +            Random srand = new Random();
   3.118 +            testSeed = srand.nextInt();
   3.119 +        }
   3.120 +
   3.121 +        // TestCaseNum system property
   3.122 +        String testNumStr = System.getProperty("TestCaseNum");
   3.123 +        if(testNumStr != null && !testNumStr.isEmpty()) {
   3.124 +            try {
   3.125 +                testCaseNum = Integer.parseInt(testNumStr);
   3.126 +                if (testCaseNum < 0 || testCaseNum > maxTestNum) {
   3.127 +                    throw new NumberFormatException();
   3.128 +                }
   3.129 +            } catch(NumberFormatException nfe) {
   3.130 +                System.out.println("Invalid TestCaseNumber property value " +
   3.131 +                        testNumStr + ". Possible value in range: 0 to " +
   3.132 +                        maxTestNum + ". Defaulting to last test case.");
   3.133 +                testCaseNum = maxTestNum;
   3.134 +            }
   3.135 +
   3.136 +            if ( numberOfTests != -1) {
   3.137 +                System.out.println("TestMode or NumberOfTests cannot be set along with TestCaseNum. Ignoring TestCaseNumber.");
   3.138 +                testCaseNum = -1;
   3.139 +            }
   3.140 +        }
   3.141 +
   3.142 +        if (numberOfTests == -1 && testCaseNum == -1) {
   3.143 +            numberOfTests = DEFAULT_TEST_COUNT;
   3.144 +            System.out.println("Setting TestMode to default, will run " + numberOfTests + "tests.");
   3.145 +        }
   3.146 +
   3.147 +        /*
   3.148 +         *  By this point in code, we will have:
   3.149 +         *  - testSeed: as per TestSeed or a Random one
   3.150 +         *  - numberOfTests to run or -1 to denote not set
   3.151 +         *  - testCaseNum to run or -1 to denote not set
   3.152 +         */
   3.153 +
   3.154 +        /*
   3.155 +         * If numberOfTests = maxTestNum, all tests are to be run,
   3.156 +         * so no randNum will be required
   3.157 +         */
   3.158 +        if (numberOfTests != -1 && numberOfTests < maxTestNum) {
   3.159 +            System.out.println("Seed = " + testSeed);
   3.160 +            randNum = new Random(testSeed);
   3.161 +            uniqueTestSet = new HashSet<>();
   3.162 +        }
   3.163 +
   3.164 +        testCompletedCount = 0;
   3.165 +        // to be used to keep sequential count when running all tests
   3.166 +        curTestNum = 0;
   3.167 +    }
   3.168 +
   3.169 +    /*
   3.170 +     * returns next test case number to run
   3.171 +     * returns -1 when there are no more tests to run
   3.172 +     */
   3.173 +    public int getNextTestCase() {
   3.174 +        if (testCaseNum != -1) {
   3.175 +            int nextTC = testCaseNum;
   3.176 +            testCaseNum = -1;
   3.177 +            return nextTC;
   3.178 +        }
   3.179 +        if (++testCompletedCount <= numberOfTests) {
   3.180 +            if (numberOfTests == maxTestNum) {
   3.181 +                //all the tests need to be run, so just return
   3.182 +                //next test case sequentially
   3.183 +                return curTestNum++;
   3.184 +            } else {
   3.185 +                int nextTC = -1;
   3.186 +                // Ensuring unique test are run
   3.187 +                while(!uniqueTestSet.add(nextTC = randNum.nextInt(maxTestNum))) {
   3.188 +                }
   3.189 +                return nextTC;
   3.190 +            }
   3.191 +        }
   3.192 +        return -1;
   3.193 +    }
   3.194 +}

mercurial