From 18086cd0f00a9fe6d768060092d3affecfd12ac7 Mon Sep 17 00:00:00 2001 From: Joe Schmetzer Date: Tue, 4 Dec 2018 08:04:02 +0000 Subject: [PATCH 1/5] Restore deprecated class IsCollectionContaining --- .../hamcrest/core/IsCollectionContaining.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 hamcrest/src/main/java/org/hamcrest/core/IsCollectionContaining.java diff --git a/hamcrest/src/main/java/org/hamcrest/core/IsCollectionContaining.java b/hamcrest/src/main/java/org/hamcrest/core/IsCollectionContaining.java new file mode 100644 index 00000000..b54b6aa9 --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/core/IsCollectionContaining.java @@ -0,0 +1,100 @@ +package org.hamcrest.core; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +/** + * @deprecated As of release 2.1, replaced by {@link IsIterableContaining}. + */ +@Deprecated +public class IsCollectionContaining extends TypeSafeDiagnosingMatcher> { + private final IsIterableContaining delegate; + + public IsCollectionContaining(Matcher elementMatcher) { + this.delegate = new IsIterableContaining<>(elementMatcher); + } + + @Override + protected boolean matchesSafely(Iterable collection, Description mismatchDescription) { + return delegate.matchesSafely(collection, mismatchDescription); + } + + @Override + public void describeTo(Description description) { + delegate.describeTo(description); + } + + + /** + * Creates a matcher for {@link Iterable}s that only matches when a single pass over the + * examined {@link Iterable} yields at least one item that is matched by the specified + * itemMatcher. Whilst matching, the traversal of the examined {@link Iterable} + * will stop as soon as a matching item is found. + * For example: + *
assertThat(Arrays.asList("foo", "bar"), hasItem(startsWith("ba")))
+ * + * @deprecated As of version 2.1, use {@link IsIterableContaining#hasItem(Matcher)}. + * + * @param itemMatcher + * the matcher to apply to items provided by the examined {@link Iterable} + */ + public static Matcher> hasItem(Matcher itemMatcher) { + return IsIterableContaining.hasItem(itemMatcher); + } + + /** + * Creates a matcher for {@link Iterable}s that only matches when a single pass over the + * examined {@link Iterable} yields at least one item that is equal to the specified + * item. Whilst matching, the traversal of the examined {@link Iterable} + * will stop as soon as a matching item is found. + * For example: + *
assertThat(Arrays.asList("foo", "bar"), hasItem("bar"))
+ * + * @deprecated As of version 2.1, use {@link IsIterableContaining#hasItem(Object)}. + * + * @param item + * the item to compare against the items provided by the examined {@link Iterable} + */ + public static Matcher> hasItem(T item) { + // Doesn't forward to hasItem() method so compiler can sort out generics. + return IsIterableContaining.hasItem(item); + } + + /** + * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the + * examined {@link Iterable} yield at least one item that is matched by the corresponding + * matcher from the specified itemMatchers. Whilst matching, each traversal of + * the examined {@link Iterable} will stop as soon as a matching item is found. + * For example: + *
assertThat(Arrays.asList("foo", "bar", "baz"), hasItems(endsWith("z"), endsWith("o")))
+ * + * @deprecated As of version 2.1, use {@link IsIterableContaining#hasItems(Matcher[])}}. + * + * @param itemMatchers + * the matchers to apply to items provided by the examined {@link Iterable} + */ + @SafeVarargs + public static Matcher> hasItems(Matcher... itemMatchers) { + return IsIterableContaining.hasItems(itemMatchers); + } + + /** + * Creates a matcher for {@link Iterable}s that matches when consecutive passes over the + * examined {@link Iterable} yield at least one item that is equal to the corresponding + * item from the specified items. Whilst matching, each traversal of the + * examined {@link Iterable} will stop as soon as a matching item is found. + * For example: + *
assertThat(Arrays.asList("foo", "bar", "baz"), hasItems("baz", "foo"))
+ * + * @deprecated As of version 2.1, use {@link IsIterableContaining#hasItems(Object[])}}. + * + * @param items + * the items to compare against the items provided by the examined {@link Iterable} + */ + @SafeVarargs + public static Matcher> hasItems(T... items) { + return IsIterableContaining.hasItems(items); + } + +} From e9d7b17b1feac40edd2b24abbfd73bcffa7222d2 Mon Sep 17 00:00:00 2001 From: Joe Schmetzer Date: Mon, 10 Dec 2018 17:52:26 +0000 Subject: [PATCH 2/5] Restore deprecated classes IsArrayContainingXXX. IsArrayContainingInOrder and IsArrayContainerInAnyOrder were both removed in version 2.1, however they are public classes that were not previously deprecated. This commit temporarily restores these classes (with deprecations this time). The next major version upgrade of Hamcrest will remove these classes at that time. --- .../IsArrayContainingInAnyOrder.java | 112 ++++++++++++++++++ .../collection/IsArrayContainingInOrder.java | 95 +++++++++++++++ .../ArrayMatchingInAnyOrderTest.java | 43 +++++++ .../collection/ArrayMatchingInOrderTest.java | 45 +++++++ .../IsArrayContainingInAnyOrderTest.java | 32 ++--- .../IsArrayContainingInOrderTest.java | 14 +-- 6 files changed, 316 insertions(+), 25 deletions(-) create mode 100644 hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInAnyOrder.java create mode 100644 hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java create mode 100644 hamcrest/src/test/java/org/hamcrest/collection/ArrayMatchingInAnyOrderTest.java create mode 100644 hamcrest/src/test/java/org/hamcrest/collection/ArrayMatchingInOrderTest.java diff --git a/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInAnyOrder.java b/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInAnyOrder.java new file mode 100644 index 00000000..d6cb637e --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInAnyOrder.java @@ -0,0 +1,112 @@ +package org.hamcrest.collection; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import static org.hamcrest.core.IsEqual.equalTo; + +/** + * @deprecated As of release 2.1, replaced by {@link ArrayMatching}. + */ +@Deprecated +public class IsArrayContainingInAnyOrder extends TypeSafeMatcher { + private final IsIterableContainingInAnyOrder iterableMatcher; + private final Collection> matchers; + + public IsArrayContainingInAnyOrder(Collection> matchers) { + this.iterableMatcher = new IsIterableContainingInAnyOrder(matchers); + this.matchers = matchers; + } + + @Override + public boolean matchesSafely(E[] item) { + return iterableMatcher.matches(Arrays.asList(item)); + } + + @Override + public void describeMismatchSafely(E[] item, Description mismatchDescription) { + iterableMatcher.describeMismatch(Arrays.asList(item), mismatchDescription); + }; + + @Override + public void describeTo(Description description) { + description.appendList("[", ", ", "]", matchers) + .appendText(" in any order"); + } + + /** + * Creates an order agnostic matcher for arrays that matches when each item in the + * examined array satisfies one matcher anywhere in the specified matchers. + * For a positive match, the examined array must be of the same length as the number of + * specified matchers. + *

+ * N.B. each of the specified matchers will only be used once during a given examination, so be + * careful when specifying matchers that may be satisfied by more than one entry in an examined + * array. + *

+ * For example: + *

assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))
+ * + * @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Matcher[])}. + * + * @param itemMatchers + * a list of matchers, each of which must be satisfied by an entry in an examined array + */ + public static Matcher arrayContainingInAnyOrder(Matcher... itemMatchers) { + return arrayContainingInAnyOrder(Arrays.asList(itemMatchers)); + } + + /** + * Creates an order agnostic matcher for arrays that matches when each item in the + * examined array satisfies one matcher anywhere in the specified collection of matchers. + * For a positive match, the examined array must be of the same length as the specified collection + * of matchers. + *

+ * N.B. each matcher in the specified collection will only be used once during a given + * examination, so be careful when specifying matchers that may be satisfied by more than + * one entry in an examined array. + *

+ * For example: + *

assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))
+ * + * @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Collection)}. + * + * @param itemMatchers + * a list of matchers, each of which must be satisfied by an item provided by an examined array + */ + public static Matcher arrayContainingInAnyOrder(Collection> itemMatchers) { + return new IsArrayContainingInAnyOrder(itemMatchers); + } + + /** + * Creates an order agnostic matcher for arrays that matches when each item in the + * examined array is logically equal to one item anywhere in the specified items. + * For a positive match, the examined array must be of the same length as the number of + * specified items. + *

+ * N.B. each of the specified items will only be used once during a given examination, so be + * careful when specifying items that may be equal to more than one entry in an examined + * array. + *

+ * For example: + *

assertThat(new String[]{"foo", "bar"}, containsInAnyOrder("bar", "foo"))
+ * + * @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Object[])}. + * + * @param items + * the items that must equal the entries of an examined array, in any order + */ + public static Matcher arrayContainingInAnyOrder(E... items) { + List> matchers = new ArrayList>(); + for (E item : items) { + matchers.add(equalTo(item)); + } + return new IsArrayContainingInAnyOrder(matchers); + } +} diff --git a/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java b/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java new file mode 100644 index 00000000..39c44ecd --- /dev/null +++ b/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java @@ -0,0 +1,95 @@ +package org.hamcrest.collection; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static java.util.Arrays.asList; +import static org.hamcrest.core.IsEqual.equalTo; + +/** + * @deprecated As of release 2.1, replaced by {@link ArrayMatching}. + */ +public class IsArrayContainingInOrder extends TypeSafeMatcher { + private final Collection> matchers; + private final IsIterableContainingInOrder iterableMatcher; + + public IsArrayContainingInOrder(List> matchers) { + this.iterableMatcher = new IsIterableContainingInOrder(matchers); + this.matchers = matchers; + } + + @Override + public boolean matchesSafely(E[] item) { + return iterableMatcher.matches(asList(item)); + } + + @Override + public void describeMismatchSafely(E[] item, Description mismatchDescription) { + iterableMatcher.describeMismatch(asList(item), mismatchDescription); + } + + @Override + public void describeTo(Description description) { + description.appendList("[", ", ", "]", matchers); + } + + /** + * Creates a matcher for arrays that matcheswhen each item in the examined array is + * logically equal to the corresponding item in the specified items. For a positive match, + * the examined array must be of the same length as the number of specified items. + *

+ * For example: + *

assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))
+ * + * @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(Object[])}. + * + * @param items + * the items that must equal the items within an examined array + */ + public static Matcher arrayContaining(E... items) { + List> matchers = new ArrayList>(); + for (E item : items) { + matchers.add(equalTo(item)); + } + return arrayContaining(matchers); + } + + /** + * Creates a matcher for arrays that matches when each item in the examined array satisfies the + * corresponding matcher in the specified matchers. For a positive match, the examined array + * must be of the same length as the number of specified matchers. + *

+ * For example: + *

assertThat(new String[]{"foo", "bar"}, contains(equalTo("foo"), equalTo("bar")))
+ * + * @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(Matcher[])}. + * + * @param itemMatchers + * the matchers that must be satisfied by the items in the examined array + */ + public static Matcher arrayContaining(Matcher... itemMatchers) { + return arrayContaining(asList(itemMatchers)); + } + + /** + * Creates a matcher for arrays that matches when each item in the examined array satisfies the + * corresponding matcher in the specified list of matchers. For a positive match, the examined array + * must be of the same length as the specified list of matchers. + *

+ * For example: + *

assertThat(new String[]{"foo", "bar"}, contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))
+ * + * @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(List)}. + * + * @param itemMatchers + * a list of matchers, each of which must be satisfied by the corresponding item in an examined array + */ + public static Matcher arrayContaining(List> itemMatchers) { + return new IsArrayContainingInOrder(itemMatchers); + } +} diff --git a/hamcrest/src/test/java/org/hamcrest/collection/ArrayMatchingInAnyOrderTest.java b/hamcrest/src/test/java/org/hamcrest/collection/ArrayMatchingInAnyOrderTest.java new file mode 100644 index 00000000..572bf63d --- /dev/null +++ b/hamcrest/src/test/java/org/hamcrest/collection/ArrayMatchingInAnyOrderTest.java @@ -0,0 +1,43 @@ +package org.hamcrest.collection; + +import org.hamcrest.AbstractMatcherTest; +import org.hamcrest.Matcher; + +import static org.hamcrest.collection.ArrayMatching.arrayContainingInAnyOrder; +import static org.hamcrest.core.IsEqual.equalTo; + +public class ArrayMatchingInAnyOrderTest extends AbstractMatcherTest { + + @SuppressWarnings("unchecked") + @Override + protected Matcher createMatcher() { + return ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2)); + } + + @SuppressWarnings("unchecked") + public void testHasAReadableDescription() { + assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2))); + assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(1, 2)); + } + + public void testMatchesItemsInAnyOrder() { + assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {1, 2, 3}); + assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {3, 2, 1}); + assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(1), new Integer[] {1}); + } + + @SuppressWarnings("unchecked") + public void testAppliesMatchersInAnyOrder() { + assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3}); + assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {3, 2, 1}); + assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(equalTo(1)), new Integer[] {1}); + } + + public void testMismatchesItemsInAnyOrder() { + Matcher matcher = ArrayMatching.arrayContainingInAnyOrder(1, 2, 3); + assertMismatchDescription("was null", matcher, null); + assertMismatchDescription("no item matches: <1>, <2>, <3> in []", matcher, new Integer[] {}); + assertMismatchDescription("no item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1}); + assertMismatchDescription("not matched: <4>", matcher, new Integer[] {4,3,2,1}); + } +} diff --git a/hamcrest/src/test/java/org/hamcrest/collection/ArrayMatchingInOrderTest.java b/hamcrest/src/test/java/org/hamcrest/collection/ArrayMatchingInOrderTest.java new file mode 100644 index 00000000..784817a0 --- /dev/null +++ b/hamcrest/src/test/java/org/hamcrest/collection/ArrayMatchingInOrderTest.java @@ -0,0 +1,45 @@ +package org.hamcrest.collection; + +import org.hamcrest.AbstractMatcherTest; +import org.hamcrest.Matcher; + +import static org.hamcrest.collection.ArrayMatching.arrayContaining; +import static org.hamcrest.core.IsEqual.equalTo; + +public class ArrayMatchingInOrderTest extends AbstractMatcherTest { + + @SuppressWarnings("unchecked") + @Override + protected Matcher createMatcher() { + return arrayContaining(equalTo(1), equalTo(2)); + } + + @SuppressWarnings("unchecked") + public void testHasAReadableDescription() { + assertDescription("[<1>, <2>]", arrayContaining(equalTo(1), equalTo(2))); + } + + public void testMatchesItemsInOrder() { + assertMatches("in order", arrayContaining(1, 2, 3), new Integer[] {1, 2, 3}); + assertMatches("single", arrayContaining(1), new Integer[] {1}); + } + + @SuppressWarnings("unchecked") + public void testAppliesMatchersInOrder() { + assertMatches("in order", arrayContaining(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3}); + assertMatches("single", arrayContaining(equalTo(1)), new Integer[] {1}); + } + + public void testMismatchesItemsInOrder() { + Matcher matcher = arrayContaining(1, 2, 3); + assertMismatchDescription("was null", matcher, null); + assertMismatchDescription("no item was <1>", matcher, new Integer[] {}); + assertMismatchDescription("no item was <2>", matcher, new Integer[] {1}); + assertMismatchDescription("item 0: was <4>", matcher, new Integer[] {4,3,2,1}); + assertMismatchDescription("item 2: was <4>", matcher, new Integer[] {1,2, 4}); + } + + public void testCanHandleNullValuesInAnArray() { + assertMatches("with nulls", arrayContaining(null, null), new Object[]{null, null}); + } +} diff --git a/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInAnyOrderTest.java b/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInAnyOrderTest.java index 4a1982c7..a29e99b2 100644 --- a/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInAnyOrderTest.java +++ b/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInAnyOrderTest.java @@ -1,43 +1,43 @@ package org.hamcrest.collection; +import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder; +import static org.hamcrest.core.IsEqual.equalTo; + import org.hamcrest.AbstractMatcherTest; import org.hamcrest.Matcher; -import static org.hamcrest.collection.ArrayMatching.arrayContainingInAnyOrder; -import static org.hamcrest.core.IsEqual.equalTo; - public class IsArrayContainingInAnyOrderTest extends AbstractMatcherTest { @SuppressWarnings("unchecked") @Override protected Matcher createMatcher() { - return ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2)); + return arrayContainingInAnyOrder(equalTo(1), equalTo(2)); } @SuppressWarnings("unchecked") public void testHasAReadableDescription() { - assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2))); - assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(1, 2)); + assertDescription("[<1>, <2>] in any order", arrayContainingInAnyOrder(equalTo(1), equalTo(2))); + assertDescription("[<1>, <2>] in any order", arrayContainingInAnyOrder(1, 2)); } public void testMatchesItemsInAnyOrder() { - assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {1, 2, 3}); - assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {3, 2, 1}); - assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(1), new Integer[] {1}); + assertMatches("in order", arrayContainingInAnyOrder(1, 2, 3), new Integer[] {1, 2, 3}); + assertMatches("out of order", arrayContainingInAnyOrder(1, 2, 3), new Integer[] {3, 2, 1}); + assertMatches("single", arrayContainingInAnyOrder(1), new Integer[] {1}); } @SuppressWarnings("unchecked") public void testAppliesMatchersInAnyOrder() { - assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3}); - assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {3, 2, 1}); - assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(equalTo(1)), new Integer[] {1}); + assertMatches("in order", arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3}); + assertMatches("out of order", arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {3, 2, 1}); + assertMatches("single", arrayContainingInAnyOrder(equalTo(1)), new Integer[] {1}); } public void testMismatchesItemsInAnyOrder() { - Matcher matcher = ArrayMatching.arrayContainingInAnyOrder(1, 2, 3); + Matcher matcher = arrayContainingInAnyOrder(1, 2, 3); assertMismatchDescription("was null", matcher, null); - assertMismatchDescription("no item matches: <1>, <2>, <3> in []", matcher, new Integer[] {}); - assertMismatchDescription("no item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1}); - assertMismatchDescription("not matched: <4>", matcher, new Integer[] {4,3,2,1}); + assertMismatchDescription("No item matches: <1>, <2>, <3> in []", matcher, new Integer[] {}); + assertMismatchDescription("No item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1}); + assertMismatchDescription("Not matched: <4>", matcher, new Integer[] {4,3,2,1}); } } diff --git a/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java b/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java index f0faa5c8..ac651b1f 100644 --- a/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java +++ b/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java @@ -1,11 +1,11 @@ package org.hamcrest.collection; +import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; +import static org.hamcrest.core.IsEqual.equalTo; + import org.hamcrest.AbstractMatcherTest; import org.hamcrest.Matcher; -import static org.hamcrest.collection.ArrayMatching.arrayContaining; -import static org.hamcrest.core.IsEqual.equalTo; - public class IsArrayContainingInOrderTest extends AbstractMatcherTest { @SuppressWarnings("unchecked") @@ -33,13 +33,9 @@ public void testAppliesMatchersInOrder() { public void testMismatchesItemsInOrder() { Matcher matcher = arrayContaining(1, 2, 3); assertMismatchDescription("was null", matcher, null); - assertMismatchDescription("no item was <1>", matcher, new Integer[] {}); - assertMismatchDescription("no item was <2>", matcher, new Integer[] {1}); + assertMismatchDescription("No item matched: <1>", matcher, new Integer[] {}); + assertMismatchDescription("No item matched: <2>", matcher, new Integer[] {1}); assertMismatchDescription("item 0: was <4>", matcher, new Integer[] {4,3,2,1}); assertMismatchDescription("item 2: was <4>", matcher, new Integer[] {1,2, 4}); } - - public void testCanHandleNullValuesInAnArray() { - assertMatches("with nulls", arrayContaining(null, null), new Object[]{null, null}); - } } From 3e70fba80fd8d08f488a7cfe88ffb3d735404666 Mon Sep 17 00:00:00 2001 From: Joe Schmetzer Date: Mon, 10 Dec 2018 22:06:50 +0000 Subject: [PATCH 3/5] Fix failing tests. --- .../collection/IsArrayContainingInAnyOrderTest.java | 6 +++--- .../hamcrest/collection/IsArrayContainingInOrderTest.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInAnyOrderTest.java b/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInAnyOrderTest.java index a29e99b2..01d9e3de 100644 --- a/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInAnyOrderTest.java +++ b/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInAnyOrderTest.java @@ -36,8 +36,8 @@ public void testAppliesMatchersInAnyOrder() { public void testMismatchesItemsInAnyOrder() { Matcher matcher = arrayContainingInAnyOrder(1, 2, 3); assertMismatchDescription("was null", matcher, null); - assertMismatchDescription("No item matches: <1>, <2>, <3> in []", matcher, new Integer[] {}); - assertMismatchDescription("No item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1}); - assertMismatchDescription("Not matched: <4>", matcher, new Integer[] {4,3,2,1}); + assertMismatchDescription("no item matches: <1>, <2>, <3> in []", matcher, new Integer[] {}); + assertMismatchDescription("no item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1}); + assertMismatchDescription("not matched: <4>", matcher, new Integer[] {4,3,2,1}); } } diff --git a/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java b/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java index ac651b1f..b719dd14 100644 --- a/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java +++ b/hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java @@ -33,8 +33,8 @@ public void testAppliesMatchersInOrder() { public void testMismatchesItemsInOrder() { Matcher matcher = arrayContaining(1, 2, 3); assertMismatchDescription("was null", matcher, null); - assertMismatchDescription("No item matched: <1>", matcher, new Integer[] {}); - assertMismatchDescription("No item matched: <2>", matcher, new Integer[] {1}); + assertMismatchDescription("no item was <1>", matcher, new Integer[] {}); + assertMismatchDescription("no item was <2>", matcher, new Integer[] {1}); assertMismatchDescription("item 0: was <4>", matcher, new Integer[] {4,3,2,1}); assertMismatchDescription("item 2: was <4>", matcher, new Integer[] {1,2, 4}); } From 76a20f2dcdb78558adffa7af08852b80226d39bb Mon Sep 17 00:00:00 2001 From: Joe Schmetzer Date: Mon, 10 Dec 2018 22:08:49 +0000 Subject: [PATCH 4/5] Bring back test for IsCollectionContaining. --- .../core/IsCollectionContainingTest.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 hamcrest/src/test/java/org/hamcrest/core/IsCollectionContainingTest.java diff --git a/hamcrest/src/test/java/org/hamcrest/core/IsCollectionContainingTest.java b/hamcrest/src/test/java/org/hamcrest/core/IsCollectionContainingTest.java new file mode 100644 index 00000000..db76bb1d --- /dev/null +++ b/hamcrest/src/test/java/org/hamcrest/core/IsCollectionContainingTest.java @@ -0,0 +1,103 @@ +package org.hamcrest.core; + +import org.hamcrest.AbstractMatcherTest; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsCollectionContaining.hasItem; +import static org.hamcrest.core.IsCollectionContaining.hasItems; +import static org.hamcrest.core.IsEqual.equalTo; + +public class IsCollectionContainingTest extends AbstractMatcherTest { + @Override + protected Matcher createMatcher() { + return hasItem(equalTo("irrelevant")); + } + + public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher() { + Matcher> itemMatcher = hasItem(equalTo("a")); + + assertMatches("should match list that contains 'a'", + itemMatcher, asList("a", "b", "c")); + } + + public void testDoesNotMatchCollectionThatDoesntContainAnElementMatchingTheGivenMatcher() { + final Matcher> matcher1 = hasItem(mismatchable("a")); + assertMismatchDescription("mismatches were: [mismatched: b, mismatched: c]", matcher1, asList("b", "c")); + + + final Matcher> matcher2 = hasItem(equalTo("a")); + assertMismatchDescription("was empty", matcher2, new ArrayList()); + } + + public void testDoesNotMatchNull() { + assertDoesNotMatch("should not matches null", hasItem(equalTo("a")), null); + } + + public void testHasAReadableDescription() { + assertDescription("a collection containing \"a\"", hasItem(equalTo("a"))); + } + + public void testCanMatchItemWhenCollectionHoldsSuperclass() // Issue 24 + { + final Set s = new HashSet(); + s.add(Integer.valueOf(2)); + assertThat(s, new IsCollectionContaining(new IsEqual(Integer.valueOf(2)))); + assertThat(s, IsCollectionContaining.hasItem(Integer.valueOf(2))); + } + + @SuppressWarnings("unchecked") + public void testMatchesAllItemsInCollection() { + final Matcher> matcher1 = hasItems(equalTo("a"), equalTo("b"), equalTo("c")); + assertMatches("should match list containing all items", + matcher1, + asList("a", "b", "c")); + + final Matcher> matcher2 = hasItems("a", "b", "c"); + assertMatches("should match list containing all items (without matchers)", + matcher2, + asList("a", "b", "c")); + + final Matcher> matcher3 = hasItems(equalTo("a"), equalTo("b"), equalTo("c")); + assertMatches("should match list containing all items in any order", + matcher3, + asList("c", "b", "a")); + + final Matcher> matcher4 = hasItems(equalTo("a"), equalTo("b"), equalTo("c")); + assertMatches("should match list containing all items plus others", + matcher4, + asList("e", "c", "b", "a", "d")); + + final Matcher> matcher5 = hasItems(equalTo("a"), equalTo("b"), equalTo("c")); + assertDoesNotMatch("should not match list unless it contains all items", + matcher5, + asList("e", "c", "b", "d")); // 'a' missing + } + + + private static Matcher mismatchable(final String string) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(String item, Description mismatchDescription) { + if (string.equals(item)) + return true; + + mismatchDescription.appendText("mismatched: " + item); + return false; + } + + @Override + public void describeTo(Description description) { + description.appendText("mismatchable: " + string); + } + }; + } +} + From 32d1fa59557479e68865d14e23c219b11300a9d8 Mon Sep 17 00:00:00 2001 From: Joe Schmetzer Date: Mon, 10 Dec 2018 22:28:43 +0000 Subject: [PATCH 5/5] Fix javadoc error when using Oracle. --- .../hamcrest/collection/IsArrayContainingInAnyOrder.java | 6 +++--- .../org/hamcrest/collection/IsArrayContainingInOrder.java | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInAnyOrder.java b/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInAnyOrder.java index d6cb637e..7e72a622 100644 --- a/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInAnyOrder.java +++ b/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInAnyOrder.java @@ -45,7 +45,7 @@ public void describeTo(Description description) { * examined array satisfies one matcher anywhere in the specified matchers. * For a positive match, the examined array must be of the same length as the number of * specified matchers. - *

+ *

* N.B. each of the specified matchers will only be used once during a given examination, so be * careful when specifying matchers that may be satisfied by more than one entry in an examined * array. @@ -67,7 +67,7 @@ public static Matcher arrayContainingInAnyOrder(Matcher... i * examined array satisfies one matcher anywhere in the specified collection of matchers. * For a positive match, the examined array must be of the same length as the specified collection * of matchers. - *

+ *

* N.B. each matcher in the specified collection will only be used once during a given * examination, so be careful when specifying matchers that may be satisfied by more than * one entry in an examined array. @@ -89,7 +89,7 @@ public static Matcher arrayContainingInAnyOrder(Collection + *

* N.B. each of the specified items will only be used once during a given examination, so be * careful when specifying items that may be equal to more than one entry in an examined * array. diff --git a/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java b/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java index 39c44ecd..c046914f 100644 --- a/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java +++ b/hamcrest/src/main/java/org/hamcrest/collection/IsArrayContainingInOrder.java @@ -42,7 +42,7 @@ public void describeTo(Description description) { * Creates a matcher for arrays that matcheswhen each item in the examined array is * logically equal to the corresponding item in the specified items. For a positive match, * the examined array must be of the same length as the number of specified items. - *

+ *

* For example: *

assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))
* @@ -63,7 +63,7 @@ public static Matcher arrayContaining(E... items) { * Creates a matcher for arrays that matches when each item in the examined array satisfies the * corresponding matcher in the specified matchers. For a positive match, the examined array * must be of the same length as the number of specified matchers. - *

+ *

* For example: *

assertThat(new String[]{"foo", "bar"}, contains(equalTo("foo"), equalTo("bar")))
* @@ -80,7 +80,7 @@ public static Matcher arrayContaining(Matcher... itemMatcher * Creates a matcher for arrays that matches when each item in the examined array satisfies the * corresponding matcher in the specified list of matchers. For a positive match, the examined array * must be of the same length as the specified list of matchers. - *

+ *

* For example: *

assertThat(new String[]{"foo", "bar"}, contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))
*