diff options
author | Pete Bentley <prb@google.com> | 2021-03-03 15:09:19 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-03-03 15:09:19 +0000 |
commit | 4ea71685c0a1bda87567b2d1ef5ed651d9c2608b (patch) | |
tree | 10923530939981dd1dc5f44ceb6316fb9442428a /src/main/java/org/junit/experimental | |
parent | 5cb7d97d73027edb2274ac39e8a958bab5e2e7b3 (diff) | |
parent | a9a7715d84046efd231e038b0dc2f551daf61701 (diff) | |
download | junit-4ea71685c0a1bda87567b2d1ef5ed651d9c2608b.tar.gz |
Merge changes I578a2676,I4b37c2d0,Id1e2d638,I1ebe37da,I6135799c am: b6446bec0a am: fcd81b3e3f am: c7a6d4ec7d am: a9a7715d84
Original change: https://android-review.googlesource.com/c/platform/external/junit/+/1613132
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: I56f45d05317bd1060b5838f1ada3a90bb2985d55
Diffstat (limited to 'src/main/java/org/junit/experimental')
8 files changed, 119 insertions, 78 deletions
diff --git a/src/main/java/org/junit/experimental/categories/Categories.java b/src/main/java/org/junit/experimental/categories/Categories.java index 290c180..0c73ed8 100644 --- a/src/main/java/org/junit/experimental/categories/Categories.java +++ b/src/main/java/org/junit/experimental/categories/Categories.java @@ -2,8 +2,10 @@ 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; @@ -76,7 +78,7 @@ import org.junit.runners.model.RunnerBuilder; * </pre> * * @version 4.12 - * @see <a href="https://github.com/junit-team/junit/wiki/Categories">Categories at JUnit wiki</a> + * @see <a href="https://github.com/junit-team/junit4/wiki/Categories">Categories at JUnit wiki</a> */ public class Categories extends Suite { @@ -86,13 +88,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}. */ - public Class<?>[] value() default {}; + 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. */ - public boolean matchAny() default true; + boolean matchAny() default true; } @Retention(RetentionPolicy.RUNTIME) @@ -101,13 +103,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()}. */ - public Class<?>[] value() default {}; + 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. */ - public boolean matchAny() default true; + boolean matchAny() default true; } public static class CategoryFilter extends Filter { @@ -117,10 +119,7 @@ public class Categories extends Suite { private final boolean excludedAny; public static CategoryFilter include(boolean matchAny, Class<?>... categories) { - if (hasNull(categories)) { - throw new NullPointerException("has null category"); - } - return categoryFilter(matchAny, createSet(categories), true, null); + return new CategoryFilter(matchAny, categories, true, null); } public static CategoryFilter include(Class<?> category) { @@ -132,10 +131,7 @@ public class Categories extends Suite { } public static CategoryFilter exclude(boolean matchAny, Class<?>... categories) { - if (hasNull(categories)) { - throw new NullPointerException("has null category"); - } - return categoryFilter(true, null, matchAny, createSet(categories)); + return new CategoryFilter(true, null, matchAny, categories); } public static CategoryFilter exclude(Class<?> category) { @@ -151,14 +147,30 @@ 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() */ @@ -284,23 +296,13 @@ public class Categories extends Suite { } private static Set<Class<?>> copyAndRefine(Set<Class<?>> classes) { - HashSet<Class<?>> c= new HashSet<Class<?>>(); + Set<Class<?>> c= new LinkedHashSet<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 { @@ -315,7 +317,6 @@ public class Categories extends Suite { } catch (NoTestsRemainException e) { throw new InitializationError(e); } - assertNoCategorizedDescendentsOfUncategorizeableParents(getDescription()); } private static Set<Class<?>> getIncludedCategory(Class<?> klass) { @@ -338,34 +339,6 @@ 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)) { @@ -375,11 +348,28 @@ public class Categories extends Suite { return false; } - private static Set<Class<?>> createSet(Class<?>... t) { - final Set<Class<?>> set= new HashSet<Class<?>>(); - if (t != null) { - Collections.addAll(set, t); + 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"); + } } - return set; + + 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); } } diff --git a/src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java b/src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java index cee1ae7..e9bdab7 100644 --- a/src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java +++ b/src/main/java/org/junit/experimental/categories/CategoryFilterFactory.java @@ -37,7 +37,11 @@ abstract class CategoryFilterFactory implements FilterFactory { List<Class<?>> categoryClasses = new ArrayList<Class<?>>();
for (String category : categories.split(",")) {
- Class<?> categoryClass = Classes.getClass(category);
+ /*
+ * 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());
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 45a4033..ab7443f 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/junit/issues/976 + * See https://github.com/junit-team/junit4/issues/976 */ private final Map<String, Long> fDurations = new HashMap<String, Long>(); private final Map<String, Long> fFailureTimestamps = new HashMap<String, Long>(); @@ -75,10 +75,15 @@ public class MaxHistory implements Serializable { } private void save() throws IOException { - ObjectOutputStream stream = new ObjectOutputStream(new FileOutputStream( - fHistoryStore)); - stream.writeObject(this); - stream.close(); + ObjectOutputStream stream = null; + try { + stream = new ObjectOutputStream(new FileOutputStream(fHistoryStore)); + stream.writeObject(this); + } finally { + if (stream != null) { + 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 ffe22f0..0f67766 100644 --- a/src/main/java/org/junit/experimental/results/PrintableResult.java +++ b/src/main/java/org/junit/experimental/results/PrintableResult.java @@ -54,6 +54,15 @@ 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 cf58f1b..92f2e6b 100644 --- a/src/main/java/org/junit/experimental/results/ResultMatchers.java +++ b/src/main/java/org/junit/experimental/results/ResultMatchers.java @@ -14,6 +14,15 @@ 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 */ @@ -53,13 +62,33 @@ 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 BaseMatcher<PrintableResult>() { - public boolean matches(Object item) { - return item.toString().contains(string); + return new TypeSafeMatcher<PrintableResult>() { + @Override + public boolean matchesSafely(PrintableResult item) { + return item.failureCount() > 0 && 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 15b5d95..846a39e 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 heirarchy such + * annotations on parameters are detected by searching up this hierarchy 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 817f553..ac88a36 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 user's filename should be included in the config file name, + * This makes it clear that the username 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. + * assumptions pass, but an assertion fails, the test fails. If no parameters can be found that satisfy all assumptions, 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,6 +73,11 @@ 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); @@ -215,7 +220,7 @@ public class Theories extends BlockJUnit4ClassRunner { protected void runWithCompleteAssignment(final Assignments complete) throws Throwable { - new BlockJUnit4ClassRunner(getTestClass().getJavaClass()) { + new BlockJUnit4ClassRunner(getTestClass()) { @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 a94c8a5..6626797 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.size() == 0; + return unassigned.isEmpty(); } public ParameterSignature nextUnassigned() { @@ -55,11 +55,10 @@ public class Assignments { } public Assignments assignNext(PotentialAssignment source) { - List<PotentialAssignment> assigned = new ArrayList<PotentialAssignment>( - this.assigned); - assigned.add(source); + List<PotentialAssignment> potentialAssignments = new ArrayList<PotentialAssignment>(assigned); + potentialAssignments.add(source); - return new Assignments(assigned, unassigned.subList(1, + return new Assignments(potentialAssignments, unassigned.subList(1, unassigned.size()), clazz); } @@ -77,7 +76,7 @@ public class Assignments { ParameterSignature unassigned = nextUnassigned(); List<PotentialAssignment> assignments = getSupplier(unassigned).getValueSources(unassigned); - if (assignments.size() == 0) { + if (assignments.isEmpty()) { assignments = generateAssignmentsFromTypeAlone(unassigned); } |