Skip to content

Commit 59d34c8

Browse files
committed
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.
1 parent fafb992 commit 59d34c8

File tree

6 files changed

+316
-25
lines changed

6 files changed

+316
-25
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.TypeSafeMatcher;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.Collection;
10+
import java.util.List;
11+
12+
import static org.hamcrest.core.IsEqual.equalTo;
13+
14+
/**
15+
* @deprecated As of release 2.1, replaced by {@link ArrayMatching}.
16+
*/
17+
@Deprecated
18+
public class IsArrayContainingInAnyOrder<E> extends TypeSafeMatcher<E[]> {
19+
private final IsIterableContainingInAnyOrder<E> iterableMatcher;
20+
private final Collection<Matcher<? super E>> matchers;
21+
22+
public IsArrayContainingInAnyOrder(Collection<Matcher<? super E>> matchers) {
23+
this.iterableMatcher = new IsIterableContainingInAnyOrder<E>(matchers);
24+
this.matchers = matchers;
25+
}
26+
27+
@Override
28+
public boolean matchesSafely(E[] item) {
29+
return iterableMatcher.matches(Arrays.asList(item));
30+
}
31+
32+
@Override
33+
public void describeMismatchSafely(E[] item, Description mismatchDescription) {
34+
iterableMatcher.describeMismatch(Arrays.asList(item), mismatchDescription);
35+
};
36+
37+
@Override
38+
public void describeTo(Description description) {
39+
description.appendList("[", ", ", "]", matchers)
40+
.appendText(" in any order");
41+
}
42+
43+
/**
44+
* Creates an order agnostic matcher for arrays that matches when each item in the
45+
* examined array satisfies one matcher anywhere in the specified matchers.
46+
* For a positive match, the examined array must be of the same length as the number of
47+
* specified matchers.
48+
* <p/>
49+
* N.B. each of the specified matchers will only be used once during a given examination, so be
50+
* careful when specifying matchers that may be satisfied by more than one entry in an examined
51+
* array.
52+
* <p>
53+
* For example:
54+
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(equalTo("bar"), equalTo("foo")))</pre>
55+
*
56+
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Matcher[])}.
57+
*
58+
* @param itemMatchers
59+
* a list of matchers, each of which must be satisfied by an entry in an examined array
60+
*/
61+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Matcher<? super E>... itemMatchers) {
62+
return arrayContainingInAnyOrder(Arrays.asList(itemMatchers));
63+
}
64+
65+
/**
66+
* Creates an order agnostic matcher for arrays that matches when each item in the
67+
* examined array satisfies one matcher anywhere in the specified collection of matchers.
68+
* For a positive match, the examined array must be of the same length as the specified collection
69+
* of matchers.
70+
* <p/>
71+
* N.B. each matcher in the specified collection will only be used once during a given
72+
* examination, so be careful when specifying matchers that may be satisfied by more than
73+
* one entry in an examined array.
74+
* <p>
75+
* For example:
76+
* <pre>assertThat(new String[]{"foo", "bar"}, arrayContainingInAnyOrder(Arrays.asList(equalTo("bar"), equalTo("foo"))))</pre>
77+
*
78+
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Collection)}.
79+
*
80+
* @param itemMatchers
81+
* a list of matchers, each of which must be satisfied by an item provided by an examined array
82+
*/
83+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(Collection<Matcher<? super E>> itemMatchers) {
84+
return new IsArrayContainingInAnyOrder<E>(itemMatchers);
85+
}
86+
87+
/**
88+
* Creates an order agnostic matcher for arrays that matches when each item in the
89+
* examined array is logically equal to one item anywhere in the specified items.
90+
* For a positive match, the examined array must be of the same length as the number of
91+
* specified items.
92+
* <p/>
93+
* N.B. each of the specified items will only be used once during a given examination, so be
94+
* careful when specifying items that may be equal to more than one entry in an examined
95+
* array.
96+
* <p>
97+
* For example:
98+
* <pre>assertThat(new String[]{"foo", "bar"}, containsInAnyOrder("bar", "foo"))</pre>
99+
*
100+
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContainingInAnyOrder(Object[])}.
101+
*
102+
* @param items
103+
* the items that must equal the entries of an examined array, in any order
104+
*/
105+
public static <E> Matcher<E[]> arrayContainingInAnyOrder(E... items) {
106+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
107+
for (E item : items) {
108+
matchers.add(equalTo(item));
109+
}
110+
return new IsArrayContainingInAnyOrder<E>(matchers);
111+
}
112+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Matcher;
5+
import org.hamcrest.TypeSafeMatcher;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.List;
10+
11+
import static java.util.Arrays.asList;
12+
import static org.hamcrest.core.IsEqual.equalTo;
13+
14+
/**
15+
* @deprecated As of release 2.1, replaced by {@link ArrayMatching}.
16+
*/
17+
public class IsArrayContainingInOrder<E> extends TypeSafeMatcher<E[]> {
18+
private final Collection<Matcher<? super E>> matchers;
19+
private final IsIterableContainingInOrder<E> iterableMatcher;
20+
21+
public IsArrayContainingInOrder(List<Matcher<? super E>> matchers) {
22+
this.iterableMatcher = new IsIterableContainingInOrder<E>(matchers);
23+
this.matchers = matchers;
24+
}
25+
26+
@Override
27+
public boolean matchesSafely(E[] item) {
28+
return iterableMatcher.matches(asList(item));
29+
}
30+
31+
@Override
32+
public void describeMismatchSafely(E[] item, Description mismatchDescription) {
33+
iterableMatcher.describeMismatch(asList(item), mismatchDescription);
34+
}
35+
36+
@Override
37+
public void describeTo(Description description) {
38+
description.appendList("[", ", ", "]", matchers);
39+
}
40+
41+
/**
42+
* Creates a matcher for arrays that matcheswhen each item in the examined array is
43+
* logically equal to the corresponding item in the specified items. For a positive match,
44+
* the examined array must be of the same length as the number of specified items.
45+
* <p/>
46+
* For example:
47+
* <pre>assertThat(new String[]{"foo", "bar"}, contains("foo", "bar"))</pre>
48+
*
49+
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(Object[])}.
50+
*
51+
* @param items
52+
* the items that must equal the items within an examined array
53+
*/
54+
public static <E> Matcher<E[]> arrayContaining(E... items) {
55+
List<Matcher<? super E>> matchers = new ArrayList<Matcher<? super E>>();
56+
for (E item : items) {
57+
matchers.add(equalTo(item));
58+
}
59+
return arrayContaining(matchers);
60+
}
61+
62+
/**
63+
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
64+
* corresponding matcher in the specified matchers. For a positive match, the examined array
65+
* must be of the same length as the number of specified matchers.
66+
* <p/>
67+
* For example:
68+
* <pre>assertThat(new String[]{"foo", "bar"}, contains(equalTo("foo"), equalTo("bar")))</pre>
69+
*
70+
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(Matcher[])}.
71+
*
72+
* @param itemMatchers
73+
* the matchers that must be satisfied by the items in the examined array
74+
*/
75+
public static <E> Matcher<E[]> arrayContaining(Matcher<? super E>... itemMatchers) {
76+
return arrayContaining(asList(itemMatchers));
77+
}
78+
79+
/**
80+
* Creates a matcher for arrays that matches when each item in the examined array satisfies the
81+
* corresponding matcher in the specified list of matchers. For a positive match, the examined array
82+
* must be of the same length as the specified list of matchers.
83+
* <p/>
84+
* For example:
85+
* <pre>assertThat(new String[]{"foo", "bar"}, contains(Arrays.asList(equalTo("foo"), equalTo("bar"))))</pre>
86+
*
87+
* @deprecated As of version 2.1, use {@link ArrayMatching#arrayContaining(List)}.
88+
*
89+
* @param itemMatchers
90+
* a list of matchers, each of which must be satisfied by the corresponding item in an examined array
91+
*/
92+
public static <E> Matcher<E[]> arrayContaining(List<Matcher<? super E>> itemMatchers) {
93+
return new IsArrayContainingInOrder<E>(itemMatchers);
94+
}
95+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
5+
6+
import static org.hamcrest.collection.ArrayMatching.arrayContainingInAnyOrder;
7+
import static org.hamcrest.core.IsEqual.equalTo;
8+
9+
public class ArrayMatchingInAnyOrderTest extends AbstractMatcherTest {
10+
11+
@SuppressWarnings("unchecked")
12+
@Override
13+
protected Matcher<?> createMatcher() {
14+
return ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2));
15+
}
16+
17+
@SuppressWarnings("unchecked")
18+
public void testHasAReadableDescription() {
19+
assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2)));
20+
assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(1, 2));
21+
}
22+
23+
public void testMatchesItemsInAnyOrder() {
24+
assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {1, 2, 3});
25+
assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {3, 2, 1});
26+
assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(1), new Integer[] {1});
27+
}
28+
29+
@SuppressWarnings("unchecked")
30+
public void testAppliesMatchersInAnyOrder() {
31+
assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3});
32+
assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {3, 2, 1});
33+
assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(equalTo(1)), new Integer[] {1});
34+
}
35+
36+
public void testMismatchesItemsInAnyOrder() {
37+
Matcher<Integer[]> matcher = ArrayMatching.arrayContainingInAnyOrder(1, 2, 3);
38+
assertMismatchDescription("was null", matcher, null);
39+
assertMismatchDescription("no item matches: <1>, <2>, <3> in []", matcher, new Integer[] {});
40+
assertMismatchDescription("no item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1});
41+
assertMismatchDescription("not matched: <4>", matcher, new Integer[] {4,3,2,1});
42+
}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.hamcrest.collection;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
5+
6+
import static org.hamcrest.collection.ArrayMatching.arrayContaining;
7+
import static org.hamcrest.core.IsEqual.equalTo;
8+
9+
public class ArrayMatchingInOrderTest extends AbstractMatcherTest {
10+
11+
@SuppressWarnings("unchecked")
12+
@Override
13+
protected Matcher<?> createMatcher() {
14+
return arrayContaining(equalTo(1), equalTo(2));
15+
}
16+
17+
@SuppressWarnings("unchecked")
18+
public void testHasAReadableDescription() {
19+
assertDescription("[<1>, <2>]", arrayContaining(equalTo(1), equalTo(2)));
20+
}
21+
22+
public void testMatchesItemsInOrder() {
23+
assertMatches("in order", arrayContaining(1, 2, 3), new Integer[] {1, 2, 3});
24+
assertMatches("single", arrayContaining(1), new Integer[] {1});
25+
}
26+
27+
@SuppressWarnings("unchecked")
28+
public void testAppliesMatchersInOrder() {
29+
assertMatches("in order", arrayContaining(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3});
30+
assertMatches("single", arrayContaining(equalTo(1)), new Integer[] {1});
31+
}
32+
33+
public void testMismatchesItemsInOrder() {
34+
Matcher<Integer[]> matcher = arrayContaining(1, 2, 3);
35+
assertMismatchDescription("was null", matcher, null);
36+
assertMismatchDescription("no item was <1>", matcher, new Integer[] {});
37+
assertMismatchDescription("no item was <2>", matcher, new Integer[] {1});
38+
assertMismatchDescription("item 0: was <4>", matcher, new Integer[] {4,3,2,1});
39+
assertMismatchDescription("item 2: was <4>", matcher, new Integer[] {1,2, 4});
40+
}
41+
42+
public void testCanHandleNullValuesInAnArray() {
43+
assertMatches("with nulls", arrayContaining(null, null), new Object[]{null, null});
44+
}
45+
}
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
package org.hamcrest.collection;
22

3+
import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder;
4+
import static org.hamcrest.core.IsEqual.equalTo;
5+
36
import org.hamcrest.AbstractMatcherTest;
47
import org.hamcrest.Matcher;
58

6-
import static org.hamcrest.collection.ArrayMatching.arrayContainingInAnyOrder;
7-
import static org.hamcrest.core.IsEqual.equalTo;
8-
99
public class IsArrayContainingInAnyOrderTest extends AbstractMatcherTest {
1010

1111
@SuppressWarnings("unchecked")
1212
@Override
1313
protected Matcher<?> createMatcher() {
14-
return ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2));
14+
return arrayContainingInAnyOrder(equalTo(1), equalTo(2));
1515
}
1616

1717
@SuppressWarnings("unchecked")
1818
public void testHasAReadableDescription() {
19-
assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2)));
20-
assertDescription("[<1>, <2>] in any order", ArrayMatching.arrayContainingInAnyOrder(1, 2));
19+
assertDescription("[<1>, <2>] in any order", arrayContainingInAnyOrder(equalTo(1), equalTo(2)));
20+
assertDescription("[<1>, <2>] in any order", arrayContainingInAnyOrder(1, 2));
2121
}
2222

2323
public void testMatchesItemsInAnyOrder() {
24-
assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {1, 2, 3});
25-
assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(1, 2, 3), new Integer[] {3, 2, 1});
26-
assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(1), new Integer[] {1});
24+
assertMatches("in order", arrayContainingInAnyOrder(1, 2, 3), new Integer[] {1, 2, 3});
25+
assertMatches("out of order", arrayContainingInAnyOrder(1, 2, 3), new Integer[] {3, 2, 1});
26+
assertMatches("single", arrayContainingInAnyOrder(1), new Integer[] {1});
2727
}
2828

2929
@SuppressWarnings("unchecked")
3030
public void testAppliesMatchersInAnyOrder() {
31-
assertMatches("in order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3});
32-
assertMatches("out of order", ArrayMatching.arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {3, 2, 1});
33-
assertMatches("single", ArrayMatching.arrayContainingInAnyOrder(equalTo(1)), new Integer[] {1});
31+
assertMatches("in order", arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {1, 2, 3});
32+
assertMatches("out of order", arrayContainingInAnyOrder(equalTo(1), equalTo(2), equalTo(3)), new Integer[] {3, 2, 1});
33+
assertMatches("single", arrayContainingInAnyOrder(equalTo(1)), new Integer[] {1});
3434
}
3535

3636
public void testMismatchesItemsInAnyOrder() {
37-
Matcher<Integer[]> matcher = ArrayMatching.arrayContainingInAnyOrder(1, 2, 3);
37+
Matcher<Integer[]> matcher = arrayContainingInAnyOrder(1, 2, 3);
3838
assertMismatchDescription("was null", matcher, null);
39-
assertMismatchDescription("no item matches: <1>, <2>, <3> in []", matcher, new Integer[] {});
40-
assertMismatchDescription("no item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1});
41-
assertMismatchDescription("not matched: <4>", matcher, new Integer[] {4,3,2,1});
39+
assertMismatchDescription("No item matches: <1>, <2>, <3> in []", matcher, new Integer[] {});
40+
assertMismatchDescription("No item matches: <2>, <3> in [<1>]", matcher, new Integer[] {1});
41+
assertMismatchDescription("Not matched: <4>", matcher, new Integer[] {4,3,2,1});
4242
}
4343
}

hamcrest/src/test/java/org/hamcrest/collection/IsArrayContainingInOrderTest.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.hamcrest.collection;
22

3+
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
4+
import static org.hamcrest.core.IsEqual.equalTo;
5+
36
import org.hamcrest.AbstractMatcherTest;
47
import org.hamcrest.Matcher;
58

6-
import static org.hamcrest.collection.ArrayMatching.arrayContaining;
7-
import static org.hamcrest.core.IsEqual.equalTo;
8-
99
public class IsArrayContainingInOrderTest extends AbstractMatcherTest {
1010

1111
@SuppressWarnings("unchecked")
@@ -33,13 +33,9 @@ public void testAppliesMatchersInOrder() {
3333
public void testMismatchesItemsInOrder() {
3434
Matcher<Integer[]> matcher = arrayContaining(1, 2, 3);
3535
assertMismatchDescription("was null", matcher, null);
36-
assertMismatchDescription("no item was <1>", matcher, new Integer[] {});
37-
assertMismatchDescription("no item was <2>", matcher, new Integer[] {1});
36+
assertMismatchDescription("No item matched: <1>", matcher, new Integer[] {});
37+
assertMismatchDescription("No item matched: <2>", matcher, new Integer[] {1});
3838
assertMismatchDescription("item 0: was <4>", matcher, new Integer[] {4,3,2,1});
3939
assertMismatchDescription("item 2: was <4>", matcher, new Integer[] {1,2, 4});
4040
}
41-
42-
public void testCanHandleNullValuesInAnArray() {
43-
assertMatches("with nulls", arrayContaining(null, null), new Object[]{null, null});
44-
}
4541
}

0 commit comments

Comments
 (0)