diff options
Diffstat (limited to 'src/main/java/org/junit/experimental')
8 files changed, 78 insertions, 119 deletions
diff --git a/src/main/java/org/junit/experimental/categories/Categories.java b/src/main/java/org/junit/experimental/categories/Categories.java index 0c73ed8..290c180 100644 --- a/src/main/java/org/junit/experimental/categories/Categories.java +++ b/src/main/java/org/junit/experimental/categories/Categories.java @@ -2,10 +2,8 @@ package org.junit.experimental.categories; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.util.Arrays; import java.util.Collections; import java.util.HashSet; -import java.util.LinkedHashSet; import java.util.Set; import org.junit.runner.Description; @@ -78,7 +76,7 @@ import org.junit.runners.model.RunnerBuilder; * </pre> * * @version 4.12 - * @see <a href="https://github.com/junit-team/junit4/wiki/Categories">Categories at JUnit wiki</a> + * @see <a href="https://github.com/junit-team/junit/wiki/Categories">Categories at JUnit wiki</a> */ public class Categories extends Suite { @@ -88,13 +86,13 @@ public class Categories extends Suite { * Determines the tests to run that are annotated with categories specified in * the value of this annotation or their subtypes unless excluded with {@link ExcludeCategory}. */ - Class<?>[] value() default {}; + public Class<?>[] value() default {}; /** * If <tt>true</tt>, runs tests annotated with <em>any</em> of the categories in * {@link IncludeCategory#value()}. Otherwise, runs tests only if annotated with <em>all</em> of the categories. */ - boolean matchAny() default true; + public boolean matchAny() default true; } @Retention(RetentionPolicy.RUNTIME) @@ -103,13 +101,13 @@ public class Categories extends Suite { * Determines the tests which do not run if they are annotated with categories specified in the * value of this annotation or their subtypes regardless of being included in {@link IncludeCategory#value()}. */ - Class<?>[] value() default {}; + public Class<?>[] value() default {}; /** * If <tt>true</tt>, the tests annotated with <em>any</em> of the categories in {@link ExcludeCategory#value()} * do not run. Otherwise, the tests do not run if and only if annotated with <em>all</em> categories. */ - boolean matchAny() default true; + public boolean matchAny() default true; } public static class CategoryFilter extends Filter { @@ -119,7 +117,10 @@ public class Categories extends Suite { private final boolean excludedAny; public static CategoryFilter include(boolean matchAny, Class<?>... categories) { - return new CategoryFilter(matchAny, categories, true, null); + if (hasNull(categories)) { + throw new NullPointerException("has null category"); + } + return categoryFilter(matchAny, createSet(categories), true, null); } public static CategoryFilter include(Class<?> category) { @@ -131,7 +132,10 @@ public class Categories extends Suite { } public static CategoryFilter exclude(boolean matchAny, Class<?>... categories) { - return new CategoryFilter(true, null, matchAny, categories); + if (hasNull(categories)) { + throw new NullPointerException("has null category"); + } + return categoryFilter(true, null, matchAny, createSet(categories)); } public static CategoryFilter exclude(Class<?> category) { @@ -147,30 +151,14 @@ public class Categories extends Suite { return new CategoryFilter(matchAnyInclusions, inclusions, matchAnyExclusions, exclusions); } - @Deprecated - public CategoryFilter(Class<?> includedCategory, Class<?> excludedCategory) { - includedAny = true; - excludedAny = true; - included = nullableClassToSet(includedCategory); - excluded = nullableClassToSet(excludedCategory); - } - protected CategoryFilter(boolean matchAnyIncludes, Set<Class<?>> includes, - boolean matchAnyExcludes, Set<Class<?>> excludes) { + boolean matchAnyExcludes, Set<Class<?>> excludes) { includedAny = matchAnyIncludes; excludedAny = matchAnyExcludes; included = copyAndRefine(includes); excluded = copyAndRefine(excludes); } - private CategoryFilter(boolean matchAnyIncludes, Class<?>[] inclusions, - boolean matchAnyExcludes, Class<?>[] exclusions) { - includedAny = matchAnyIncludes; - excludedAny = matchAnyExcludes; - included = createSet(inclusions); - excluded = createSet(exclusions); - } - /** * @see #toString() */ @@ -296,13 +284,23 @@ public class Categories extends Suite { } private static Set<Class<?>> copyAndRefine(Set<Class<?>> classes) { - Set<Class<?>> c= new LinkedHashSet<Class<?>>(); + HashSet<Class<?>> c= new HashSet<Class<?>>(); if (classes != null) { c.addAll(classes); } c.remove(null); return c; } + + private static boolean hasNull(Class<?>... classes) { + if (classes == null) return false; + for (Class<?> clazz : classes) { + if (clazz == null) { + return true; + } + } + return false; + } } public Categories(Class<?> klass, RunnerBuilder builder) throws InitializationError { @@ -317,6 +315,7 @@ public class Categories extends Suite { } catch (NoTestsRemainException e) { throw new InitializationError(e); } + assertNoCategorizedDescendentsOfUncategorizeableParents(getDescription()); } private static Set<Class<?>> getIncludedCategory(Class<?> klass) { @@ -339,6 +338,34 @@ public class Categories extends Suite { return annotation == null || annotation.matchAny(); } + private static void assertNoCategorizedDescendentsOfUncategorizeableParents(Description description) throws InitializationError { + if (!canHaveCategorizedChildren(description)) { + assertNoDescendantsHaveCategoryAnnotations(description); + } + for (Description each : description.getChildren()) { + assertNoCategorizedDescendentsOfUncategorizeableParents(each); + } + } + + private static void assertNoDescendantsHaveCategoryAnnotations(Description description) throws InitializationError { + for (Description each : description.getChildren()) { + if (each.getAnnotation(Category.class) != null) { + throw new InitializationError("Category annotations on Parameterized classes are not supported on individual methods."); + } + assertNoDescendantsHaveCategoryAnnotations(each); + } + } + + // If children have names like [0], our current magical category code can't determine their parentage. + private static boolean canHaveCategorizedChildren(Description description) { + for (Description each : description.getChildren()) { + if (each.getTestClass() == null) { + return false; + } + } + return true; + } + private static boolean hasAssignableTo(Set<Class<?>> assigns, Class<?> to) { for (final Class<?> from : assigns) { if (to.isAssignableFrom(from)) { @@ -348,28 +375,11 @@ public class Categories extends Suite { return false; } - private static Set<Class<?>> createSet(Class<?>[] classes) { - // Not throwing a NPE if t is null is a bad idea, but it's the behavior from JUnit 4.12 - // for include(boolean, Class<?>...) and exclude(boolean, Class<?>...) - if (classes == null || classes.length == 0) { - return Collections.emptySet(); - } - for (Class<?> category : classes) { - if (category == null) { - throw new NullPointerException("has null category"); - } + private static Set<Class<?>> createSet(Class<?>... t) { + final Set<Class<?>> set= new HashSet<Class<?>>(); + if (t != null) { + Collections.addAll(set, t); } - - return classes.length == 1 - ? Collections.<Class<?>>singleton(classes[0]) - : new LinkedHashSet<Class<?>>(Arrays.asList(classes)); - } - - private static Set<Class<?>> nullableClassToSet(Class<?> nullableClass) { - // Not throwing a NPE if t is null is a bad idea, but it's the behavior from JUnit 4.11 - // for CategoryFilter(Class<?> includedCategory, Class<?> excludedCategory) - return nullableClass == null - ? Collections.<Class<?>>emptySet() - : Collections.<Class<?>>singleton(nullableClass); + return set; } } diff --git a/src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java b/src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java index e9bdab7..cee1ae7 100644 --- a/src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java +++ b/src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java @@ -37,11 +37,7 @@ abstract class CategoryFilterFactory implements FilterFactory { List<Class<?>> categoryClasses = new ArrayList<Class<?>>();
for (String category : categories.split(",")) {
- /*
- * Load the category class using the context class loader.
- * If there is no context class loader, use the class loader for this class.
- */
- Class<?> categoryClass = Classes.getClass(category, getClass());
+ Class<?> categoryClass = Classes.getClass(category);
categoryClasses.add(categoryClass);
}
diff --git a/src/main/java/org/junit/experimental/max/MaxHistory.java b/src/main/java/org/junit/experimental/max/MaxHistory.java index ab7443f..45a4033 100644 --- a/src/main/java/org/junit/experimental/max/MaxHistory.java +++ b/src/main/java/org/junit/experimental/max/MaxHistory.java @@ -64,7 +64,7 @@ public class MaxHistory implements Serializable { /* * We have to use the f prefix until the next major release to ensure * serialization compatibility. - * See https://github.com/junit-team/junit4/issues/976 + * See https://github.com/junit-team/junit/issues/976 */ private final Map<String, Long> fDurations = new HashMap<String, Long>(); private final Map<String, Long> fFailureTimestamps = new HashMap<String, Long>(); @@ -75,15 +75,10 @@ public class MaxHistory implements Serializable { } private void save() throws IOException { - ObjectOutputStream stream = null; - try { - stream = new ObjectOutputStream(new FileOutputStream(fHistoryStore)); - stream.writeObject(this); - } finally { - if (stream != null) { - stream.close(); - } - } + ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream( + fHistoryStore)); + stream.writeObject(this); + stream.close(); } Long getFailureTimestamp(Description key) { diff --git a/src/main/java/org/junit/experimental/results/PrintableResult.java b/src/main/java/org/junit/experimental/results/PrintableResult.java index 0f67766..ffe22f0 100644 --- a/src/main/java/org/junit/experimental/results/PrintableResult.java +++ b/src/main/java/org/junit/experimental/results/PrintableResult.java @@ -54,15 +54,6 @@ public class PrintableResult { return result.getFailures().size(); } - /** - * Returns the failures in this result. - * - * @since 4.13 - */ - public List<Failure> failures() { - return result.getFailures(); - } - @Override public String toString() { ByteArrayOutputStream stream = new ByteArrayOutputStream(); diff --git a/src/main/java/org/junit/experimental/results/ResultMatchers.java b/src/main/java/org/junit/experimental/results/ResultMatchers.java index 92f2e6b..cf58f1b 100644 --- a/src/main/java/org/junit/experimental/results/ResultMatchers.java +++ b/src/main/java/org/junit/experimental/results/ResultMatchers.java @@ -14,15 +14,6 @@ import org.hamcrest.TypeSafeMatcher; * </pre> */ public class ResultMatchers { - - /** - * Do not instantiate. - * @deprecated will be private soon. - */ - @Deprecated - public ResultMatchers() { - } - /** * Matches if the tests are all successful */ @@ -62,33 +53,13 @@ public class ResultMatchers { } /** - * Matches if the result has exactly one failure matching the given matcher. - * - * @since 4.13 - */ - public static Matcher<PrintableResult> hasSingleFailureMatching(final Matcher<Throwable> matcher) { - return new TypeSafeMatcher<PrintableResult>() { - @Override - public boolean matchesSafely(PrintableResult item) { - return item.failureCount() == 1 && matcher.matches(item.failures().get(0).getException()); - } - - public void describeTo(Description description) { - description.appendText("has failure with exception matching "); - matcher.describeTo(description); - } - }; - } - - /** * Matches if the result has one or more failures, and at least one of them * contains {@code string} */ public static Matcher<PrintableResult> hasFailureContaining(final String string) { - return new TypeSafeMatcher<PrintableResult>() { - @Override - public boolean matchesSafely(PrintableResult item) { - return item.failureCount() > 0 && item.toString().contains(string); + return new BaseMatcher<PrintableResult>() { + public boolean matches(Object item) { + return item.toString().contains(string); } public void describeTo(Description description) { diff --git a/src/main/java/org/junit/experimental/theories/ParametersSuppliedBy.java b/src/main/java/org/junit/experimental/theories/ParametersSuppliedBy.java index 846a39e..15b5d95 100644 --- a/src/main/java/org/junit/experimental/theories/ParametersSuppliedBy.java +++ b/src/main/java/org/junit/experimental/theories/ParametersSuppliedBy.java @@ -17,7 +17,7 @@ import java.lang.annotation.Target; * * In addition, annotations themselves can be annotated with * @ParametersSuppliedBy, and then used similarly. ParameterSuppliedBy - * annotations on parameters are detected by searching up this hierarchy such + * annotations on parameters are detected by searching up this heirarchy such * that these act as syntactic sugar, making: * * <pre> diff --git a/src/main/java/org/junit/experimental/theories/Theories.java b/src/main/java/org/junit/experimental/theories/Theories.java index ac88a36..817f553 100644 --- a/src/main/java/org/junit/experimental/theories/Theories.java +++ b/src/main/java/org/junit/experimental/theories/Theories.java @@ -51,11 +51,11 @@ import org.junit.runners.model.TestClass; * } * } * </pre> - * This makes it clear that the username should be included in the config file name, + * This makes it clear that the user's filename should be included in the config file name, * only if it doesn't contain a slash. Another test or theory might define what happens when a username does contain * a slash. <code>UserTest</code> will attempt to run <code>filenameIncludesUsername</code> on every compatible data * point defined in the class. If any of the assumptions fail, the data point is silently ignored. If all of the - * assumptions pass, but an assertion fails, the test fails. If no parameters can be found that satisfy all assumptions, the test fails. + * assumptions pass, but an assertion fails, the test fails. * <p> * Defining general statements as theories allows data point reuse across a bunch of functionality tests and also * allows automated tools to search for new, unexpected data points that expose bugs. @@ -73,11 +73,6 @@ public class Theories extends BlockJUnit4ClassRunner { super(klass); } - /** @since 4.13 */ - protected Theories(TestClass testClass) throws InitializationError { - super(testClass); - } - @Override protected void collectInitializationErrors(List<Throwable> errors) { super.collectInitializationErrors(errors); @@ -220,7 +215,7 @@ public class Theories extends BlockJUnit4ClassRunner { protected void runWithCompleteAssignment(final Assignments complete) throws Throwable { - new BlockJUnit4ClassRunner(getTestClass()) { + new BlockJUnit4ClassRunner(getTestClass().getJavaClass()) { @Override protected void collectInitializationErrors( List<Throwable> errors) { diff --git a/src/main/java/org/junit/experimental/theories/internal/Assignments.java b/src/main/java/org/junit/experimental/theories/internal/Assignments.java index 6626797..a94c8a5 100644 --- a/src/main/java/org/junit/experimental/theories/internal/Assignments.java +++ b/src/main/java/org/junit/experimental/theories/internal/Assignments.java @@ -47,7 +47,7 @@ public class Assignments { } public boolean isComplete() { - return unassigned.isEmpty(); + return unassigned.size() == 0; } public ParameterSignature nextUnassigned() { @@ -55,10 +55,11 @@ public class Assignments { } public Assignments assignNext(PotentialAssignment source) { - List<PotentialAssignment> potentialAssignments = new ArrayList<PotentialAssignment>(assigned); - potentialAssignments.add(source); + List<PotentialAssignment> assigned = new ArrayList<PotentialAssignment>( + this.assigned); + assigned.add(source); - return new Assignments(potentialAssignments, unassigned.subList(1, + return new Assignments(assigned, unassigned.subList(1, unassigned.size()), clazz); } @@ -76,7 +77,7 @@ public class Assignments { ParameterSignature unassigned = nextUnassigned(); List<PotentialAssignment> assignments = getSupplier(unassigned).getValueSources(unassigned); - if (assignments.isEmpty()) { + if (assignments.size() == 0) { assignments = generateAssignmentsFromTypeAlone(unassigned); } |