Skip to content

Commit 898d274

Browse files
committed
convert to JUnit4
1 parent efe363e commit 898d274

1 file changed

Lines changed: 63 additions & 59 deletions

File tree

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,112 @@
11
package org.hamcrest.core;
22

3-
import org.hamcrest.AbstractMatcherTest;
4-
import org.hamcrest.Description;
5-
import org.hamcrest.Matcher;
6-
import org.hamcrest.TypeSafeDiagnosingMatcher;
3+
import static java.util.Arrays.asList;
4+
import static org.hamcrest.AbstractMatcherTest.assertDescription;
5+
import static org.hamcrest.AbstractMatcherTest.assertDoesNotMatch;
6+
import static org.hamcrest.AbstractMatcherTest.assertMatches;
7+
import static org.hamcrest.AbstractMatcherTest.assertMismatchDescription;
8+
import static org.hamcrest.AbstractMatcherTest.assertNullSafe;
9+
import static org.hamcrest.AbstractMatcherTest.assertUnknownTypeSafe;
10+
import static org.hamcrest.core.IsCollectionContaining.hasItem;
11+
import static org.hamcrest.core.IsCollectionContaining.hasItems;
12+
import static org.hamcrest.core.IsEqual.equalTo;
713

814
import java.util.ArrayList;
915
import java.util.HashSet;
1016
import java.util.Set;
1117

12-
import static java.util.Arrays.asList;
13-
import static org.hamcrest.MatcherAssert.assertThat;
14-
import static org.hamcrest.core.IsCollectionContaining.hasItem;
15-
import static org.hamcrest.core.IsCollectionContaining.hasItems;
16-
import static org.hamcrest.core.IsEqual.equalTo;
18+
import org.hamcrest.Description;
19+
import org.hamcrest.Matcher;
20+
import org.hamcrest.TypeSafeDiagnosingMatcher;
21+
import org.junit.Test;
1722

18-
public class IsCollectionContainingTest extends AbstractMatcherTest {
19-
@Override
20-
protected Matcher<?> createMatcher() {
21-
return hasItem(equalTo("irrelevant"));
23+
public final class IsCollectionContainingTest {
24+
25+
@Test public void
26+
copesWithNullsAndUnknownTypes() {
27+
Matcher<?> matcher = hasItem(equalTo("irrelevant"));
28+
29+
assertNullSafe(matcher);
30+
assertUnknownTypeSafe(matcher);
2231
}
2332

24-
public void testMatchesACollectionThatContainsAnElementForTheGivenMatcher() {
33+
@Test public void
34+
matchesACollectionThatContainsAnElementForTheGivenMatcher() {
2535
final Matcher<Iterable<? super String>> itemMatcher = hasItem(equalTo("a"));
2636

27-
assertMatches("list containing 'a'",
28-
itemMatcher, asList("a", "b", "c"));
37+
assertMatches("list containing 'a'", itemMatcher, asList("a", "b", "c"));
2938
}
3039

31-
public void testDoesNotMatchCollectionWithoutAnElementForGivenMatcher() {
40+
@Test public void
41+
doesNotMatchCollectionWithoutAnElementForGivenMatcher() {
3242
final Matcher<Iterable<? super String>> matcher = hasItem(mismatchable("a"));
33-
assertMismatchDescription("mismatches were: [mismatched: b, mismatched: c]",
34-
matcher, asList("b", "c"));
3543

44+
assertMismatchDescription("mismatches were: [mismatched: b, mismatched: c]", matcher, asList("b", "c"));
3645
assertMismatchDescription("was empty", matcher, new ArrayList<String>());
3746
}
3847

39-
public void testDoesNotMatchNull() {
48+
@Test public void
49+
doesNotMatchNull() {
4050
assertDoesNotMatch("doesn't match null", hasItem(equalTo("a")), null);
4151
}
4252

43-
public void testHasAReadableDescription() {
53+
@Test public void
54+
hasAReadableDescription() {
4455
assertDescription("a collection containing mismatchable: a", hasItem(mismatchable("a")));
4556
}
4657

47-
public void testCanMatchItemWhenCollectionHoldsSuperclass() // Issue 24
48-
{
49-
final Set<Number> s = new HashSet<Number>();
50-
s.add(2);
51-
assertThat(s, new IsCollectionContaining<Number>(new IsEqual<Number>(2)));
52-
assertThat(s, IsCollectionContaining.hasItem(2));
58+
@Test public void
59+
canMatchItemWhenCollectionHoldsSuperclass() { // Issue 24
60+
final Set<Number> s = new HashSet<Number>();
61+
s.add(2);
62+
63+
assertMatches("didn't match", new IsCollectionContaining<Number>(new IsEqual<Number>(2)), s);
64+
assertMatches("didn't match", IsCollectionContaining.hasItem(2), s);
5365
}
5466

5567
@SuppressWarnings("unchecked")
56-
public void testMatchesMultipleItemsInCollection() {
68+
@Test public void
69+
matchesMultipleItemsInCollection() {
5770
final Matcher<Iterable<String>> matcher1 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));
58-
assertMatches("list containing all items",
59-
matcher1,
60-
asList("a", "b", "c"));
71+
assertMatches("list containing all items", matcher1, asList("a", "b", "c"));
6172

6273
final Matcher<Iterable<String>> matcher2 = hasItems("a", "b", "c");
63-
assertMatches("list containing all items (without matchers)",
64-
matcher2,
65-
asList("a", "b", "c"));
74+
assertMatches("list containing all items (without matchers)", matcher2, asList("a", "b", "c"));
6675

6776
final Matcher<Iterable<String>> matcher3 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));
68-
assertMatches("list containing all items in any order",
69-
matcher3,
70-
asList("c", "b", "a"));
77+
assertMatches("list containing all items in any order", matcher3, asList("c", "b", "a"));
7178

7279
final Matcher<Iterable<String>> matcher4 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));
73-
assertMatches("list containing all items plus others",
74-
matcher4,
75-
asList("e", "c", "b", "a", "d"));
80+
assertMatches("list containing all items plus others", matcher4, asList("e", "c", "b", "a", "d"));
7681

7782
final Matcher<Iterable<String>> matcher5 = hasItems(equalTo("a"), equalTo("b"), equalTo("c"));
78-
assertDoesNotMatch("not match list unless it contains all items",
79-
matcher5,
80-
asList("e", "c", "b", "d")); // 'a' missing
83+
assertDoesNotMatch("not match list unless it contains all items", matcher5, asList("e", "c", "b", "d")); // 'a' missing
8184
}
8285

83-
public void testReportsMismatchWithAReadableDescriptionForMultipleItems() {
86+
@Test public void
87+
reportsMismatchWithAReadableDescriptionForMultipleItems() {
8488
final Matcher<Iterable<Integer>> matcher = hasItems(3, 4);
8589

8690
assertMismatchDescription("a collection containing <4> mismatches were: [was <1>, was <2>, was <3>]",
8791
matcher, asList(1, 2, 3));
8892
}
89-
93+
9094
private static Matcher<? super String> mismatchable(final String string) {
91-
return new TypeSafeDiagnosingMatcher<String>() {
92-
@Override
93-
protected boolean matchesSafely(String item, Description mismatchDescription) {
94-
if (string.equals(item))
95-
return true;
96-
97-
mismatchDescription.appendText("mismatched: " + item);
98-
return false;
99-
}
95+
return new TypeSafeDiagnosingMatcher<String>() {
96+
@Override
97+
protected boolean matchesSafely(String item, Description mismatchDescription) {
98+
if (string.equals(item))
99+
return true;
100+
101+
mismatchDescription.appendText("mismatched: " + item);
102+
return false;
103+
}
100104

101-
@Override
102-
public void describeTo(Description description) {
103-
description.appendText("mismatchable: " + string);
104-
}
105-
};
105+
@Override
106+
public void describeTo(Description description) {
107+
description.appendText("mismatchable: " + string);
108+
}
109+
};
106110
}
107111
}
108112

0 commit comments

Comments
 (0)