8007767: TargetAnnoCombo.java need to be updated to add a new test mode

Reviewed-by: jjg, strarup
This commit is contained in:
Matherey Nunez 2013-03-15 13:39:04 +01:00
parent a7e53ae56d
commit cd78ad2c8a
3 changed files with 391 additions and 543 deletions

View File

@ -146,6 +146,7 @@ public class Helper {
public static final String template = public static final String template =
"/*PACKAGE*/\n" "/*PACKAGE*/\n"
+ "//pkg test;\n\n" + "//pkg test;\n\n"
+ "/*ANNODATA*/\n" // import statements, declaration of Foo/FooContainer
+ "/*TYPE*/ //class\n" + "/*TYPE*/ //class\n"
+ "class #ClassName {\n" + "class #ClassName {\n"
+ " /*FIELD*/ //instance var\n" + " /*FIELD*/ //instance var\n"

View File

@ -21,382 +21,102 @@
* questions. * questions.
*/ */
/** /*
* @test * @test
* @bug 7195131 * @bug 7151010 8006547 8007766
* @author sogoel * @summary Default test cases for running combinations for Target values
* @summary Combo test for all possible combinations for Target values
* @ignore 8008339 Test TargetAnnoCombo.java is broken
* @build Helper * @build Helper
* @compile TargetAnnoCombo.java TestCaseGenerator.java
* @run main TargetAnnoCombo * @run main TargetAnnoCombo
*/ */
import java.util.Set;
import java.util.List;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.ElementType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.EnumSet;
import java.util.Set;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector; import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject; import javax.tools.JavaFileObject;
/* import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
* TargetAnnoCombo gets a list of test case numbers using TestCaseGenerator. import static java.lang.annotation.ElementType.CONSTRUCTOR;
* For each of the test case number, @Target sets for base and container annotations import static java.lang.annotation.ElementType.FIELD;
* are determined, source files are generated, compiled, and the result is verified import static java.lang.annotation.ElementType.METHOD;
* based on if the @Target set for base and container is a positive or negative combination. import static java.lang.annotation.ElementType.PARAMETER;
* import static java.lang.annotation.ElementType.TYPE;
* @Target sets for base and container annotations are determined using a bit mapping of import static java.lang.annotation.ElementType.PACKAGE;
* 10 ElementType enum constants defined in JDK8. import static java.lang.annotation.ElementType.LOCAL_VARIABLE;
* import static java.lang.annotation.ElementType.TYPE_USE;
* Bit Target value import static java.lang.annotation.ElementType.TYPE_PARAMETER;
* 0 "ElementType.ANNOTATION_TYPE"
* 1 "ElementType.CONSTRUCTOR"
* 2 "ElementType.FIELD"
* 3 "ElementType.LOCAL_VARIABLE"
* 4 "ElementType.METHOD"
* 5 "ElementType.TYPE"
* 6 "ElementType.PARAMETER"
* 7 "ElementType.PACKAGE"
* 8 "ElementType.TYPE_USE"
* 9 "ElementType.TYPE_PARAMETER"
*
* Group 1:
* 20 bits mapping, representing a test case number, is used for all target set
* combinations ( 0 to 1048575 ) including empty @Target sets => @Target({}).
* From this 20 bits, 10 bits are for base followed by 10 bits for container
* where each bit maps to an ElementType enum constant defined in JDK8.
*
* Examples:
* Test case number: 4, binary: 100 => container=100, base=[], container=["ElementType.FIELD"]
* Test case number: 1003575, binary: 11110101000000110111 => base=1111010100, container=0000110111;
* base=["ElementType.PARAMETER", "ElementType.TYPE_USE", "ElementType.METHOD", "ElementType.FIELD", "ElementType.PACKAGE", "ElementType.TYPE_PARAMETER"],
* container=["ElementType.TYPE", "ElementType.METHOD", "ElementType.ANNOTATION_TYPE", "ElementType.CONSTRUCTOR", "ElementType.FIELD"]
*
* In the following groups, no @Target set is represented by null.
* Group 2:
* @Target is not defined on base.
* Target sets for container are determined using the 10-bit binary number
* resulting in 1024 test cases, mapping them to test case numbers from
* 1048576 to (1048576 + 1023) => 1048576 to 1049599.
*
* Example:
* Test case number: 1048587 => 1048587 - 1048576 = test case 11 in Group 2, binary: 1011 =>
* base = null,
* container = ["ElementType.ANNOTATION_TYPE","ElementType.CONSTRUCTOR","ElementType.LOCAL_VARIABLE"]
*
* Group 3:
* @Target is not defined on container
* Target sets for base are determined using the 10-bit binary number
* resulting in 1024 test cases, mapping them to test case numbers from
* 1049600 to (1049600 + 1023) => 1049600 to 1050623.
*
* Example:
* Test case number: 1049708 => 1049708 - 1049600 = test case 108 in Group 3, binary: 1101100 =>
* base = ["ElementType.FIELD", "ElementType.LOCAL_VARIABLE", "ElementType.TYPE", "ElementType.PARAMETER"],
* container = null
*
* For the above group, test case number: 1049855 gives compiler error, JDK-8006547 filed
*
* Group 4:
* @Target not defined for both base and container annotations.
*
* This is the last test and corresponds to test case number 1050624. base=null, container=null
*
* Examples to run this test:
* 1. Run a specific test case number:
* ${JTREG} -DTestCaseNum=10782 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
* 2. Run specific number of tests:
* ${JTREG} -DNumberOfTests=4 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
* 3. Run specific number of tests with a seed:
* ${JTREG} -DNumberOfTests=4 -DTestSeed=-972894659 -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
* 4. Run tests in default mode (number of tests = 1000):
* ${JTREG} -DTestMode=DEFAULT -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
* 5. Run all tests (FULL mode):
* ${JTREG} -DTestMode=FULL -samevm -jdk:${JAVA_TEST} -reportDir ${REPORT} -workDir ${WORK} TargetAnnoCombo.java
*
*/
public class TargetAnnoCombo { public class TargetAnnoCombo {
int errors = 0;
static final String TESTPKG = "testpkg"; static final String TESTPKG = "testpkg";
/*
* Set it to true to get more debug information including base and // Set it to true to get more debug information including base and container
* container target sets for a given test case number // target sets for a given test case.
*/
static final boolean DEBUG = false; static final boolean DEBUG = false;
// JDK 5/6/7/8 Targets // Define constant target sets to be used for the combination of the target values.
static final String[] targetVals = {"ElementType.ANNOTATION_TYPE", final static Set<ElementType> noSet = null;
"ElementType.CONSTRUCTOR", "ElementType.FIELD", final static Set<ElementType> empty = EnumSet.noneOf(ElementType.class);
"ElementType.LOCAL_VARIABLE", "ElementType.METHOD",
"ElementType.TYPE", "ElementType.PARAMETER",
"ElementType.PACKAGE", "ElementType.TYPE_USE",
"ElementType.TYPE_PARAMETER"};
// TYPE_USE and TYPE_PARAMETER (added in JDK8) are not part of default Target set // [TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,
static final int DEFAULT_TARGET_CNT = 8; // PACKAGE, TYPE_PARAMETER, TYPE_USE]
final static Set<ElementType> allTargets = EnumSet.allOf(ElementType.class);
public static void main(String args[]) throws Exception { // [TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE,
// PACKAGE]
final static Set<ElementType> jdk7 = EnumSet.range(TYPE, PACKAGE);
/* maxTestNum = (base and container combinations of targetVals elems [0 - 1048575 combos]) // [TYPE_USE, TYPE_PARAMETER]
* + (combinations where base or container has no Target [1024 combos]) final static Set<ElementType> jdk8 = EnumSet.range(TYPE_PARAMETER, TYPE_USE);
* + (no -1 even though 1st test is number 0 as last test is where both
* base and container have no target)
*/
int maxTestNum = (int)Math.pow(2, 2*targetVals.length) + 2*(int)Math.pow(2, targetVals.length); // List of test cases to run. This list is created in generate().
TestCaseGenerator tcg = new TestCaseGenerator(maxTestNum); // To run a specific test cases add case number in @run main line.
TargetAnnoCombo tac = new TargetAnnoCombo(); List<TestCase> testCases = new ArrayList<TestCase>();
int testCtr = 0; int errors = 0;
int testCase = -1;
while ( (testCase=tcg.getNextTestCase()) != -1 ) { // Identify test cases that fail.
tac.executeTestCase(testCase, maxTestNum); enum IgnoreKind {
testCtr++; RUN,
IGNORE
};
private class TestCase {
private Set<ElementType> baseAnnotations;
private Set<ElementType> containerAnnotations;
private IgnoreKind ignore;
public TestCase(Set<ElementType> baseAnnotations, Set<ElementType> containerAnnotations) {
this(baseAnnotations, containerAnnotations, IgnoreKind.RUN);
} }
System.out.println("Total tests run: " + testCtr); public TestCase(Set<ElementType> baseAnnotations, Set<ElementType> containerAnnotations,
if (tac.errors > 0) IgnoreKind ignoreKind) {
throw new Exception(tac.errors + " errors found"); this.baseAnnotations = baseAnnotations;
this.containerAnnotations = containerAnnotations;
this.ignore = ignoreKind;
} }
/* public Set getBaseAnnotations() {
* For given testCase, determine the base and container annotation Target sets, return baseAnnotations;
* get if testCase should compile, get test source file(s), get compilation result and verify.
*
*/
private void executeTestCase(int testCase, int maxTestNum) {
// Determine base and container annotation Target sets for the testCase
Set<String> baseAnnoTarget = null;
Set<String> conAnnoTarget = null;
//Number of base and container combinations [0 - 1048575 combos]
int baseContCombos = (int)Math.pow(2, 2*targetVals.length);
//Number of either base or container combinations when one of them has no @Target [1024 combos]
int targetValsCombos = (int)Math.pow(2, targetVals.length);
if (testCase >= baseContCombos) {
//Base annotation do not have @Target
if (testCase < baseContCombos + targetValsCombos) {
baseAnnoTarget = null;
conAnnoTarget = getSetFromBitVec(Integer.toBinaryString(testCase - baseContCombos));
} else if (testCase < baseContCombos + 2*targetValsCombos) {
//Container annotation do not have @Target
baseAnnoTarget = getSetFromBitVec(Integer.toBinaryString(testCase - baseContCombos - targetValsCombos));
conAnnoTarget = null;
} else {
//Both Base and Container annotation do not have @Target
baseAnnoTarget = null;
conAnnoTarget = null;
}
} else {
//TestCase number is represented as 10-bits for base followed by container bits
String bin = Integer.toBinaryString(testCase);
String base="", cont=bin;
if (bin.length() > targetVals.length){
base = bin.substring(0, bin.length() - targetVals.length);
cont = bin.substring(bin.length() - targetVals.length,bin.length());
}
baseAnnoTarget = getSetFromBitVec(base);
conAnnoTarget = getSetFromBitVec(cont);
} }
debugPrint("Test case number = " + testCase + " => binary = " + Integer.toBinaryString(testCase)); public Set getContainerAnnotations() {
debugPrint(" => baseAnnoTarget = " + baseAnnoTarget); return containerAnnotations;
debugPrint(" => containerAnnoTarget = " + conAnnoTarget);
// Determine if a testCase should compile or not
String className = "TC" + testCase;
boolean shouldCompile = isValidSubSet(baseAnnoTarget, conAnnoTarget);
// Get test source file(s)
Iterable<? extends JavaFileObject> files = getFileList(className, baseAnnoTarget,
conAnnoTarget, shouldCompile);
// Get result of compiling test src file(s)
boolean result = getCompileResult(className, shouldCompile, files);
// List test src code if test fails
if(!result) {
System.out.println("FAIL: Test " + testCase);
try {
for (JavaFileObject f: files) {
System.out.println("File: " + f.getName() + "\n" + f.getCharContent(true));
}
} catch (IOException ioe) {
System.out.println("Exception: " + ioe);
}
} else {
debugPrint("PASS: Test " + testCase);
}
} }
// Get a Set<String> based on bits that are set to 1 public boolean isIgnored() {
public Set<String> getSetFromBitVec(String bitVec) { return ignore == IgnoreKind.IGNORE;
Set<String> ret = new HashSet<>();
char[] bit = bitVec.toCharArray();
for (int i=bit.length-1, j=0; i>=0; i--, j++){
if (bit[i] == '1') {
ret.add(targetVals[j]);
}
}
return ret;
} }
// Compile the test source file(s) and return test result // Determine if a testCase should compile or not.
private boolean getCompileResult(String className, boolean shouldCompile, private boolean isValidSubSet() {
Iterable<? extends JavaFileObject> files) {
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
Helper.compileCode(diagnostics, files);
// Test case pass or fail
boolean ok = false;
String errMesg = "";
int numDiags = diagnostics.getDiagnostics().size();
if (numDiags == 0) {
if (shouldCompile) {
debugPrint("Test passed, compiled as expected.");
ok = true;
} else {
errMesg = "Test failed, compiled unexpectedly.";
ok = false;
}
} else {
if (shouldCompile) {
// did not compile
errMesg = "Test failed, did not compile.";
ok = false;
} else {
// Error in compilation as expected
String expectedErrKey = "compiler.err.invalid.repeatable." +
"annotation.incompatible.target";
for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
if((d.getKind() == Diagnostic.Kind.ERROR) &&
d.getCode().contains(expectedErrKey)) {
// Error message as expected
debugPrint("Error message as expected.");
ok = true;
break;
} else {
// error message is incorrect
ok = false;
}
}
if (!ok) {
errMesg = "Incorrect error received when compiling " +
className + ", expected: " + expectedErrKey;
}
}
}
if(!ok) {
error(errMesg);
for (Diagnostic<?> d : diagnostics.getDiagnostics())
System.out.println(" Diags: " + d);
}
return ok;
}
private void debugPrint(String string) {
if(DEBUG)
System.out.println(string);
}
// Create src code and corresponding JavaFileObjects
private Iterable<? extends JavaFileObject> getFileList(String className,
Set<String> baseAnnoTarget, Set<String> conAnnoTarget,
boolean shouldCompile) {
String srcContent = "";
String pkgInfoContent = "";
String template = Helper.template;
String baseTarget = "", conTarget = "";
String target = Helper.ContentVars.TARGET.getVal();
if(baseAnnoTarget != null) {
baseTarget = target.replace("#VAL", baseAnnoTarget.toString())
.replace("[", "{").replace("]", "}");
}
if(conAnnoTarget != null) {
conTarget = target.replace("#VAL", conAnnoTarget.toString())
.replace("[", "{").replace("]", "}");
}
String annoData = Helper.ContentVars.IMPORTSTMTS.getVal() +
conTarget +
Helper.ContentVars.CONTAINER.getVal() +
baseTarget +
Helper.ContentVars.REPEATABLE.getVal() +
Helper.ContentVars.BASE.getVal();
JavaFileObject pkgInfoFile = null;
/*
* If shouldCompile = true and no @Target is specified for container annotation,
* then all 8 ElementType enum constants are applicable as targets for
* container annotation.
*/
if(shouldCompile && conAnnoTarget == null) {
//conAnnoTarget = new HashSet<String>(Arrays.asList(targetVals));
conAnnoTarget = getDefaultTargetSet();
}
if(shouldCompile) {
boolean isPkgCasePresent = new ArrayList<String>(conAnnoTarget).contains("ElementType.PACKAGE");
String repeatableAnno = Helper.ContentVars.BASEANNO.getVal() + " " + Helper.ContentVars.BASEANNO.getVal();
for(String s: conAnnoTarget) {
s = s.replace("ElementType.","");
String replaceStr = "/*"+s+"*/";
if(s.equalsIgnoreCase("PACKAGE")) {
//Create packageInfo file
String pkgInfoName = TESTPKG + "." + "package-info";
pkgInfoContent = repeatableAnno + "\npackage " + TESTPKG + ";" + annoData;
pkgInfoFile = Helper.getFile(pkgInfoName, pkgInfoContent);
} else {
template = template.replace(replaceStr, repeatableAnno);
//srcContent = template.replace("#ClassName",className);
if(!isPkgCasePresent) {
srcContent = template.replace("/*ANNODATA*/", annoData).replace("#ClassName",className);
} else {
replaceStr = "/*PACKAGE*/";
srcContent = template.replace(replaceStr, "package " + TESTPKG + ";")
.replace("#ClassName", className);
}
}
}
} else {
// For invalid cases, compilation should fail at declaration site
template = "class #ClassName {}";
srcContent = annoData + template.replace("#ClassName",className);
}
JavaFileObject srcFile = Helper.getFile(className, srcContent);
Iterable<? extends JavaFileObject> files = null;
if(pkgInfoFile != null)
files = Arrays.asList(pkgInfoFile,srcFile);
else
files = Arrays.asList(srcFile);
return files;
}
private Set<String> getDefaultTargetSet() {
Set<String> defaultSet = new HashSet<>();
int ctr = 0;
for(String s : targetVals) {
if(ctr++ < DEFAULT_TARGET_CNT) {
defaultSet.add(s);
}
}
return defaultSet;
}
private boolean isValidSubSet(Set<String> baseAnnoTarget, Set<String> conAnnoTarget) {
/* /*
* RULE 1: conAnnoTarget should be a subset of baseAnnoTarget * RULE 1: conAnnoTarget should be a subset of baseAnnoTarget
* RULE 2: For empty @Target ({}) - annotation cannot be applied anywhere * RULE 2: For empty @Target ({}) - annotation cannot be applied anywhere
@ -410,61 +130,379 @@ public class TargetAnnoCombo {
*/ */
/* If baseAnno has no @Target, Foo can be either applied to @Target specified for container annotation /* If baseAnno has no @Target, Foo can be either applied to @Target specified
* else will be applicable for all default targets if no @Target is present for container annotation. * for container annotation else will be applicable for all default targets
* if no @Target is present for container annotation.
* In both cases, the set will be a valid set with no @Target for base annotation * In both cases, the set will be a valid set with no @Target for base annotation
*/ */
if(baseAnnoTarget == null) { if (baseAnnotations == null) {
if(conAnnoTarget == null) return true; if (containerAnnotations == null) {
return !(conAnnoTarget.contains("ElementType.TYPE_USE") || conAnnoTarget.contains("ElementType.TYPE_PARAMETER")); return true;
}
return !(containerAnnotations.contains(TYPE_USE) ||
containerAnnotations.contains(TYPE_PARAMETER));
} }
Set<String> tempBaseSet = new HashSet<>(baseAnnoTarget); Set<ElementType> tempBaseSet = EnumSet.noneOf(ElementType.class);
// If BaseAnno has TYPE, then ANNOTATION_TYPE is allowed by default tempBaseSet.addAll(baseAnnotations);
if(baseAnnoTarget.contains("ElementType.TYPE")) { // If BaseAnno has TYPE, then ANNOTATION_TYPE is allowed by default.
tempBaseSet.add("ElementType.ANNOTATION_TYPE"); if (baseAnnotations.contains(TYPE)) {
tempBaseSet.add(ANNOTATION_TYPE);
} }
/* // If containerAnno has no @Target, only valid case if baseAnnoTarget has
* If containerAnno has no @Target, only valid case if baseAnnoTarget has all targets defined // all targets defined else invalid set.
* else invalid set if (containerAnnotations == null) {
*/ return tempBaseSet.containsAll(jdk7);
if(conAnnoTarget == null) {
return (tempBaseSet.containsAll(getDefaultTargetSet()));
} }
// At this point, neither conAnnoTarget or baseAnnoTarget are null // At this point, neither conAnnoTarget or baseAnnoTarget are null.
if(conAnnoTarget.size() == 0) return true; if (containerAnnotations.isEmpty()) {
return true;
// At this point, conAnnoTarget is non-empty
if (baseAnnoTarget.size() == 0) return false;
// At this point, neither conAnnoTarget or baseAnnoTarget are empty
return tempBaseSet.containsAll(conAnnoTarget);
} }
void error(String msg) { // At this point, conAnnoTarget is non-empty.
if (baseAnnotations.isEmpty()) {
return false;
}
// At this point, neither conAnnoTarget or baseAnnoTarget are empty.
return tempBaseSet.containsAll(containerAnnotations);
}
}
public static void main(String args[]) throws Exception {
TargetAnnoCombo tac = new TargetAnnoCombo();
// Generates all test cases to be run.
tac.generate();
List<Integer> cases = new ArrayList<Integer>();
for (int i = 0; i < args.length; i++) {
cases.add(Integer.parseInt(args[i]));
}
if (cases.isEmpty()) {
tac.run();
} else {
for (int index : cases) {
tac.executeTestCase(tac.testCases.get(index), index);
}
}
}
private void generate() {
// Adding test cases to run.
testCases.addAll(Arrays.asList(
// No base target against no container target.
new TestCase(noSet, noSet),
// No base target against empty container target.
new TestCase(noSet, empty),
// No base target against TYPE_USE only container target.
new TestCase(noSet, less(jdk8, TYPE_PARAMETER)),
// No base target against TYPE_PARAMETER only container target.
new TestCase(noSet, less(jdk8, TYPE_USE)),
// No base target against TYPE_USE + TYPE_PARAMETER only container target.
new TestCase(noSet, jdk8),
// No base target against TYPE_USE + some selection of jdk7 targets.
new TestCase(noSet,
plus(EnumSet.range(TYPE, LOCAL_VARIABLE), TYPE_USE)),
// No base target against TYPE_PARAMETER + some selection of jdk7 targets.
new TestCase(noSet,
plus(EnumSet.range(TYPE, LOCAL_VARIABLE), TYPE_PARAMETER)),
// No base target against each jdk7 target alone as container target.
new TestCase(noSet, plus(empty, TYPE)),
new TestCase(noSet, plus(empty, PARAMETER)),
new TestCase(noSet, plus(empty, PACKAGE)),
new TestCase(noSet, plus(empty, METHOD)),
new TestCase(noSet, plus(empty, LOCAL_VARIABLE)),
new TestCase(noSet, plus(empty, FIELD)),
new TestCase(noSet, plus(empty, CONSTRUCTOR)),
new TestCase(noSet, plus(empty, ANNOTATION_TYPE)),
// Empty base target against no container target.
new TestCase(empty, noSet),
// Empty base target against empty container target.
new TestCase(empty, empty),
// Empty base target against any lone container target.
new TestCase(empty, plus(empty, TYPE)),
new TestCase(empty, plus(empty, PARAMETER)),
new TestCase(empty, plus(empty, PACKAGE)),
new TestCase(empty, plus(empty, METHOD)),
new TestCase(empty, plus(empty, LOCAL_VARIABLE)),
new TestCase(empty, plus(empty, FIELD)),
new TestCase(empty, plus(empty, CONSTRUCTOR)),
new TestCase(empty, plus(empty, ANNOTATION_TYPE)),
new TestCase(empty, less(jdk8, TYPE_USE)),
new TestCase(empty, less(jdk8, TYPE_PARAMETER)),
// No container target against all all-but one jdk7 targets.
new TestCase(less(jdk7, TYPE), noSet),
new TestCase(less(jdk7, PARAMETER), noSet),
new TestCase(less(jdk7, PACKAGE), noSet),
new TestCase(less(jdk7, METHOD), noSet),
new TestCase(less(jdk7, LOCAL_VARIABLE), noSet),
new TestCase(less(jdk7, FIELD), noSet),
new TestCase(less(jdk7, CONSTRUCTOR), noSet),
new TestCase(less(jdk7, ANNOTATION_TYPE), noSet),
// No container against all but TYPE and ANNOTATION_TYPE
new TestCase(less(jdk7, TYPE, ANNOTATION_TYPE), noSet),
// No container against jdk7 targets.
new TestCase(jdk7, noSet),
// No container against jdk7 targets plus one or both of TYPE_USE, TYPE_PARAMETER
new TestCase(plus(jdk7, TYPE_USE), noSet),
new TestCase(plus(jdk7, TYPE_PARAMETER), noSet),
new TestCase(allTargets, noSet),
// Empty container target against any lone target.
new TestCase(plus(empty, TYPE), empty),
new TestCase(plus(empty, PARAMETER), empty),
new TestCase(plus(empty, PACKAGE), empty),
new TestCase(plus(empty, METHOD), empty),
new TestCase(plus(empty, LOCAL_VARIABLE), empty),
new TestCase(plus(empty, FIELD), empty),
new TestCase(plus(empty, CONSTRUCTOR), empty),
new TestCase(plus(empty, ANNOTATION_TYPE), empty),
new TestCase(plus(empty, TYPE_USE), empty),
new TestCase(plus(empty, TYPE_PARAMETER), empty),
// All base targets against all container targets.
new TestCase(allTargets, allTargets),
// All base targets against all but one container targets.
new TestCase(allTargets, less(allTargets, TYPE)),
new TestCase(allTargets, less(allTargets, PARAMETER)),
new TestCase(allTargets, less(allTargets, PACKAGE)),
new TestCase(allTargets, less(allTargets, METHOD)),
new TestCase(allTargets, less(allTargets, LOCAL_VARIABLE)),
new TestCase(allTargets, less(allTargets, FIELD)),
new TestCase(allTargets, less(allTargets, CONSTRUCTOR)),
new TestCase(allTargets, less(allTargets, ANNOTATION_TYPE)),
new TestCase(allTargets, less(allTargets, TYPE_USE)),
new TestCase(allTargets, less(allTargets, TYPE_PARAMETER)),
// All container targets against all but one base targets.
new TestCase(less(allTargets, TYPE), allTargets),
new TestCase(less(allTargets, PARAMETER), allTargets),
new TestCase(less(allTargets, PACKAGE), allTargets),
new TestCase(less(allTargets, METHOD), allTargets),
new TestCase(less(allTargets, LOCAL_VARIABLE), allTargets),
new TestCase(less(allTargets, FIELD), allTargets),
new TestCase(less(allTargets, CONSTRUCTOR), allTargets),
new TestCase(less(allTargets, ANNOTATION_TYPE), allTargets),
new TestCase(less(allTargets, TYPE_USE), allTargets),
new TestCase(less(allTargets, TYPE_PARAMETER), allTargets)));
// Generates 100 test cases for any lone base target contained in Set
// allTargets against any lone container target.
for (ElementType b : allTargets) {
for (ElementType c : allTargets) {
testCases.add(new TestCase(plus(empty, b), plus(empty, c)));
}
}
}
void run() throws Exception {
int testCtr = 0;
for (TestCase tc : testCases) {
if (!tc.isIgnored()) {
executeTestCase(tc, testCases.indexOf(tc));
testCtr++;
}
}
System.out.println("Total tests run: " + testCtr);
if (errors > 0) {
throw new Exception(errors + " errors found");
}
}
private void executeTestCase(TestCase testCase, int index) {
debugPrint("Test case number = " + index);
debugPrint(" => baseAnnoTarget = " + testCase.getBaseAnnotations());
debugPrint(" => containerAnnoTarget = " + testCase.getContainerAnnotations());
String className = "TC" + index;
boolean shouldCompile = testCase.isValidSubSet();
Iterable<? extends JavaFileObject> files = getFileList(className, testCase, shouldCompile);
// Get result of compiling test src file(s).
boolean result = getCompileResult(className, shouldCompile, files);
// List test src code if test fails.
if (!result) {
System.out.println("FAIL: Test " + index);
try {
for (JavaFileObject f : files) {
System.out.println("File: " + f.getName() + "\n" + f.getCharContent(true));
}
} catch (IOException ioe) {
System.out.println("Exception: " + ioe);
}
} else {
debugPrint("PASS: Test " + index);
}
}
// Create src code and corresponding JavaFileObjects.
private Iterable<? extends JavaFileObject> getFileList(String className,
TestCase testCase, boolean shouldCompile) {
Set<ElementType> baseAnnoTarget = testCase.getBaseAnnotations();
Set<ElementType> conAnnoTarget = testCase.getContainerAnnotations();
String srcContent = "";
String pkgInfoContent = "";
String template = Helper.template;
String baseTarget = "", conTarget = "";
String target = Helper.ContentVars.TARGET.getVal();
if (baseAnnoTarget != null) {
String tmp = target.replace("#VAL", convertToString(baseAnnoTarget).toString());
baseTarget = tmp.replace("[", "{").replace("]", "}");
}
if (conAnnoTarget != null) {
String tmp = target.replace("#VAL", convertToString(conAnnoTarget).toString());
conTarget = tmp.replace("[", "{").replace("]", "}");
}
String annoData = Helper.ContentVars.IMPORTSTMTS.getVal()
+ conTarget
+ Helper.ContentVars.CONTAINER.getVal()
+ baseTarget
+ Helper.ContentVars.REPEATABLE.getVal()
+ Helper.ContentVars.BASE.getVal();
JavaFileObject pkgInfoFile = null;
// If shouldCompile = true and no @Target is specified for container annotation,
// then all 8 ElementType enum constants are applicable as targets for
// container annotation.
if (shouldCompile && conAnnoTarget == null) {
Set<ElementType> copySet = EnumSet.noneOf(ElementType.class);
copySet.addAll(jdk7);
conAnnoTarget = copySet;
}
if (shouldCompile) {
boolean isPkgCasePresent = conAnnoTarget.contains(PACKAGE);
String repeatableAnno = Helper.ContentVars.BASEANNO.getVal()
+ " " + Helper.ContentVars.BASEANNO.getVal();
for (ElementType s : conAnnoTarget) {
String replaceStr = "/*" + s.name() + "*/";
if (s.name().equalsIgnoreCase("PACKAGE")) {
//Create packageInfo file.
String pkgInfoName = TESTPKG + "." + "package-info";
pkgInfoContent = repeatableAnno + "\npackage " + TESTPKG + ";" + annoData;
pkgInfoFile = Helper.getFile(pkgInfoName, pkgInfoContent);
} else {
template = template.replace(replaceStr, repeatableAnno);
if (!isPkgCasePresent) {
srcContent = template.replace(
"/*ANNODATA*/", annoData).replace("#ClassName", className);
} else {
replaceStr = "/*PACKAGE*/";
String tmp = template.replace(replaceStr, "package " + TESTPKG + ";");
srcContent = tmp.replace("#ClassName", className);
}
}
}
} else {
// For invalid cases, compilation should fail at declaration site.
template = "class #ClassName {}";
srcContent = annoData + template.replace("#ClassName", className);
}
JavaFileObject srcFile = Helper.getFile(className, srcContent);
Iterable<? extends JavaFileObject> files = null;
if (pkgInfoFile != null) {
files = Arrays.asList(pkgInfoFile, srcFile);
} else {
files = Arrays.asList(srcFile);
}
return files;
}
// Compile the test source file(s) and return test result.
private boolean getCompileResult(String className, boolean shouldCompile,
Iterable<? extends JavaFileObject> files) {
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
Helper.compileCode(diagnostics, files);
// Test case pass or fail.
boolean ok = false;
String errMesg = "";
int numDiags = diagnostics.getDiagnostics().size();
if (numDiags == 0) {
if (shouldCompile) {
debugPrint("Test passed, compiled as expected.");
ok = true;
} else {
errMesg = "Test failed, compiled unexpectedly.";
ok = false;
}
} else {
if (shouldCompile) {
// did not compile.
errMesg = "Test failed, did not compile.";
ok = false;
} else {
// Error in compilation as expected.
String expectedErrKey = "compiler.err.invalid.repeatable."
+ "annotation.incompatible.target";
for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
if ((d.getKind() == Diagnostic.Kind.ERROR)
&& d.getCode().contains(expectedErrKey)) {
// Error message as expected.
debugPrint("Error message as expected.");
ok = true;
break;
} else {
// error message is incorrect.
ok = false;
}
}
if (!ok) {
errMesg = "Incorrect error received when compiling "
+ className + ", expected: " + expectedErrKey;
}
}
}
if (!ok) {
error(errMesg);
for (Diagnostic<?> d : diagnostics.getDiagnostics()) {
System.out.println(" Diags: " + d);
}
}
return ok;
}
private Set<ElementType> less(Set<ElementType> base, ElementType... sub) {
Set<ElementType> res = EnumSet.noneOf(ElementType.class);
res.addAll(base);
for (ElementType t : sub) {
res.remove(t);
}
return res;
}
private Set<ElementType> plus(Set<ElementType> base, ElementType... add) {
Set<ElementType> res = EnumSet.noneOf(ElementType.class);
res.addAll(base);
for (ElementType t : add) {
res.add(t);
}
return res;
}
// Iterate target set and add "ElementType." in front of every target type.
private List<String> convertToString(Set<ElementType> annoTarget) {
if (annoTarget == null) {
return null;
}
List<String> annoTargets = new ArrayList<String>();
for (ElementType e : annoTarget) {
annoTargets.add("ElementType." + e.name());
}
return annoTargets;
}
private void debugPrint(String string) {
if (DEBUG) {
System.out.println(string);
}
}
private void error(String msg) {
System.out.println("ERROR: " + msg); System.out.println("ERROR: " + msg);
errors++; errors++;
} }
// Lists the start and end range for the given set of target vals
void showGroups() {
//Group 1: All target set combinations ( 0 to 1048575 ) including empty @Target sets => @Target({})
int grpEnd1 = (int)Math.pow(2, 2*targetVals.length) - 1;
System.out.println("[Group 1]: 0 - " + grpEnd1);
//Group 2: @Target not defined for base annotation ( 1048576 - 1049599 ).
System.out.print("[Group 2]: " + (grpEnd1 + 1) + " - ");
int grpEnd2 = grpEnd1 + 1 + (int)Math.pow(2, targetVals.length) - 1;
System.out.println(grpEnd2);
//Group 3: @Target not defined for container annotation ( 1049600 - 1050623 ).
System.out.print("[Group 3]: " + (grpEnd2 + 1) + " - ");
int grpEnd3 = grpEnd2 + 1 + (int)Math.pow(2, targetVals.length) - 1;
System.out.println(grpEnd3);
//Group 4: @Target not defined for both base and container annotations ( 1050624 ).
System.out.println("[Group 4]: " + (grpEnd3 + 1));
}
} }

View File

@ -1,191 +0,0 @@
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
/* System properties:
* NumberOfTests, TestMode, and TestCaseNum are mutually exclusive
* TestSeed will be used only with NumberOfTests or TestMode, otherwise it will be ignored
* -DNumberOfTests=[0 to 2^20+2^11+1]
* -DTestMode=[FULL|DEFAULT]
* -DTestSeed=[seedNumber]
* -DTestCaseNum=[0 to 2^20+2^11+1]
*/
public class TestCaseGenerator {
// Total number of tests to be run
int numberOfTests = -1;
//Single test case
int testCaseNum = -1;
//Seed used to generate test cases
int testSeed;
int maxTestNum;
Random randNum;
// used in getNextTestCase
int curTestNum;
int testCompletedCount;
HashSet<Integer> uniqueTestSet;
static final int DEFAULT_TEST_COUNT = 250;
/*
* Get parameter values from command line to set numberOfTests, testCaseNum,
* and testSeed
*/
public TestCaseGenerator(int maxTestNum) {
this.maxTestNum = maxTestNum;
// Set values for variables based on input from command line
// TestMode system property
String testModeVal = System.getProperty("TestMode");
if(testModeVal != null && !testModeVal.isEmpty()) {
switch (testModeVal.toUpperCase()) {
case "FULL":
numberOfTests = maxTestNum;
break;
case "DEFAULT":
numberOfTests = DEFAULT_TEST_COUNT;
break;
default:
System.out.println("Invalid property value " + testModeVal +
" for numberOfTests. Possible range: 0 to " +
maxTestNum + ". Ignoring property");
numberOfTests = -1;
}
}
// NumberOfTests system property
String numTestsStr = System.getProperty("NumberOfTests");
if(numTestsStr != null && !numTestsStr.isEmpty()) {
int numTests = -1;
try {
numTests = Integer.parseInt(numTestsStr);
if (numTests < 0 || numTests > maxTestNum) {
throw new NumberFormatException();
}
} catch(NumberFormatException nfe) {
System.out.println("Invalid NumberOfTests property value " +
numTestsStr + ". Possible range: 0 to " + maxTestNum +
"Reset to default: " + DEFAULT_TEST_COUNT);
numTests = DEFAULT_TEST_COUNT;
}
if (numberOfTests != -1 && numTests != -1) {
System.out.println("TestMode and NumberOfTests cannot be set together. Ignoring TestMode.");
}
numberOfTests = numTests;
}
// TestSeed system property
String seedVal = System.getProperty("TestSeed");
if(seedVal != null && !seedVal.isEmpty()) {
try {
testSeed = Integer.parseInt(seedVal);
} catch(NumberFormatException nfe) {
Random srand = new Random();
testSeed = srand.nextInt();
}
} else {
Random srand = new Random();
testSeed = srand.nextInt();
}
// TestCaseNum system property
String testNumStr = System.getProperty("TestCaseNum");
if(testNumStr != null && !testNumStr.isEmpty()) {
try {
testCaseNum = Integer.parseInt(testNumStr);
if (testCaseNum < 0 || testCaseNum > maxTestNum) {
throw new NumberFormatException();
}
} catch(NumberFormatException nfe) {
System.out.println("Invalid TestCaseNumber property value " +
testNumStr + ". Possible value in range: 0 to " +
maxTestNum + ". Defaulting to last test case.");
testCaseNum = maxTestNum;
}
if ( numberOfTests != -1) {
System.out.println("TestMode or NumberOfTests cannot be set along with TestCaseNum. Ignoring TestCaseNumber.");
testCaseNum = -1;
}
}
if (numberOfTests == -1 && testCaseNum == -1) {
numberOfTests = DEFAULT_TEST_COUNT;
System.out.println("Setting TestMode to default, will run " + numberOfTests + "tests.");
}
/*
* By this point in code, we will have:
* - testSeed: as per TestSeed or a Random one
* - numberOfTests to run or -1 to denote not set
* - testCaseNum to run or -1 to denote not set
*/
/*
* If numberOfTests = maxTestNum, all tests are to be run,
* so no randNum will be required
*/
if (numberOfTests != -1 && numberOfTests < maxTestNum) {
System.out.println("Seed = " + testSeed);
randNum = new Random(testSeed);
uniqueTestSet = new HashSet<>();
}
testCompletedCount = 0;
// to be used to keep sequential count when running all tests
curTestNum = 0;
}
/*
* returns next test case number to run
* returns -1 when there are no more tests to run
*/
public int getNextTestCase() {
if (testCaseNum != -1) {
int nextTC = testCaseNum;
testCaseNum = -1;
return nextTC;
}
if (++testCompletedCount <= numberOfTests) {
if (numberOfTests == maxTestNum) {
//all the tests need to be run, so just return
//next test case sequentially
return curTestNum++;
} else {
int nextTC = -1;
// Ensuring unique test are run
while(!uniqueTestSet.add(nextTC = randNum.nextInt(maxTestNum))) {
}
return nextTC;
}
}
return -1;
}
}