Skip to content

Commit 0d7077d

Browse files
author
joeretro
committed
Added null tests to all matcher test cases.
1 parent 915c3ae commit 0d7077d

30 files changed

Lines changed: 158 additions & 17 deletions

‎src/library/org/hamcrest/object/HasToString.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public HasToString(Matcher<String> toStringMatcher) {
1313
}
1414

1515
public boolean matches(Object item) {
16-
return toStringMatcher.matches(item.toString());
16+
return item != null && toStringMatcher.matches(item.toString());
1717
}
1818

1919
public void describeTo(Description description) {

‎src/unit-test/org/hamcrest/AbstractMatcherTest.java‎

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22
*/
33
package org.hamcrest;
44

5-
import junit.framework.AssertionFailedError;
5+
import static org.hamcrest.core.Always.alwaysPasses;
6+
import static org.hamcrest.core.Always.alwaysFails;
67
import junit.framework.TestCase;
7-
import org.hamcrest.core.Always;
88

99
public abstract class AbstractMatcherTest extends TestCase {
10-
protected static final Matcher TRUE_MATCHER = new Always(true);
11-
protected static final Matcher FALSE_MATCHER = new Always(false);
1210

13-
protected static final Matcher NEVER_EVALUATED = new Matcher() {
14-
public boolean matches(Object o) {
15-
throw new AssertionFailedError("matcher should not have been evaluated");
16-
}
11+
protected static final Matcher<Boolean> TRUE_MATCHER = alwaysPasses();
12+
protected static final Matcher<Boolean> FALSE_MATCHER = alwaysFails();
1713

18-
public void describeTo(Description description) {
19-
description.appendText("NEVER_EVALUATED");
20-
}
21-
};
14+
/**
15+
* Create an instance of the Matcher so some generic safety-net tests can be run on it.
16+
*/
17+
protected abstract Matcher<?> createMatcher();
2218

2319
protected static final Object ARGUMENT_IGNORED = new Object();
2420
protected static final Object ANY_NON_NULL_ARGUMENT = new Object();
@@ -36,4 +32,10 @@ public void assertDescription(String expected, Matcher matcher) {
3632
matcher.describeTo(description);
3733
assertEquals(expected, description.toString());
3834
}
35+
36+
public void testIsNullSafe() {
37+
// should not throw a NullPointerException
38+
createMatcher().matches(null);
39+
}
40+
3941
}

‎src/unit-test/org/hamcrest/beans/HasPropertyTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.hamcrest.core.IsNot.not;
88

99
import org.hamcrest.AbstractMatcherTest;
10+
import org.hamcrest.Matcher;
1011

1112
/**
1213
* @author Iain McGinniss
@@ -19,6 +20,10 @@ public class HasPropertyTest extends AbstractMatcherTest {
1920
private final HasPropertyWithValueTest.BeanWithoutInfo bean
2021
= new HasPropertyWithValueTest.BeanWithoutInfo("a bean");
2122

23+
protected Matcher<?> createMatcher() {
24+
return hasProperty("irrelevant");
25+
}
26+
2227
public void testReturnsTrueIfPropertyExists() {
2328
assertThat(bean, hasProperty("writeOnlyProperty"));
2429
}

‎src/unit-test/org/hamcrest/beans/HasPropertyWithValueTest.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public class HasPropertyWithValueTest extends AbstractMatcherTest {
2929
private final BeanWithoutInfo shouldNotMatch = new BeanWithoutInfo("not expected");
3030
private final BeanWithInfo beanWithInfo = new BeanWithInfo("with info");
3131

32+
protected Matcher<?> createMatcher() {
33+
return hasProperty("irrelevant", anything());
34+
}
35+
3236
public void testMatchesInfolessBeanWithMatchedNamedProperty() {
3337
assertThat(shouldMatch, hasProperty("property", eq("is expected")));
3438
assertThat(shouldNotMatch, not(hasProperty("property", eq("is expected"))));

‎src/unit-test/org/hamcrest/collection/IsArrayContainingTest.java‎

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

33
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
45
import static org.hamcrest.collection.IsArrayContaining.arrayContaining;
56

67
public class IsArrayContainingTest extends AbstractMatcherTest {
78

9+
protected Matcher<?> createMatcher() {
10+
return arrayContaining("irrelevant");
11+
}
12+
813
public void testMatchesAnArrayThatContainsAnElementMatchingTheGivenMatcher() {
914
assertMatches("should matches array that contains 'a'",
1015
arrayContaining("a"), new String[]{"a", "b", "c"});

‎src/unit-test/org/hamcrest/collection/IsArrayTest.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
import static org.hamcrest.core.IsEqual.eq;
55

66
import org.hamcrest.AbstractMatcherTest;
7+
import org.hamcrest.Matcher;
78

89
public class IsArrayTest extends AbstractMatcherTest {
10+
11+
protected Matcher<?> createMatcher() {
12+
return array(eq("irrelevant"));
13+
}
14+
915
public void testMatchesAnArrayThatMatchingesAllTheElementMatchers() {
1016
assertMatches("should matches array that contains 'a'",
1117
array(eq("a"), eq("b"), eq("c")), new String[]{"a", "b", "c"});

‎src/unit-test/org/hamcrest/collection/IsCollectionContainingTest.java‎

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

33
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
45
import static org.hamcrest.collection.IsCollectionContaining.collectionContaining;
56
import static org.hamcrest.core.IsEqual.eq;
67

@@ -9,6 +10,10 @@
910

1011
public class IsCollectionContainingTest extends AbstractMatcherTest {
1112

13+
protected Matcher<?> createMatcher() {
14+
return collectionContaining(eq("irrelevant"));
15+
}
16+
1217
public void testMatchesACollectionThatContainsAnElementMatchingTheGivenMatcher() {
1318
assertMatches("should matches list that contains 'a'",
1419
collectionContaining(eq("a")), asList("a", "b", "c"));

‎src/unit-test/org/hamcrest/collection/IsMapContainingTest.java‎

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

33
import org.hamcrest.AbstractMatcherTest;
4+
import org.hamcrest.Matcher;
45
import static org.hamcrest.collection.IsMapContaining.mapContaining;
56
import static org.hamcrest.core.IsAnything.anything;
67
import static org.hamcrest.core.IsEqual.eq;
@@ -10,6 +11,10 @@
1011

1112
public class IsMapContainingTest extends AbstractMatcherTest {
1213

14+
protected Matcher<?> createMatcher() {
15+
return mapContaining("irrelevant", "irrelevant");
16+
}
17+
1318
public void testMatchesMapContainingMatchingKeyAndValue() {
1419
Map<String,Integer> map = new HashMap<String,Integer>();
1520
map.put("a", 1);

‎src/unit-test/org/hamcrest/core/AlwaysTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
package org.hamcrest.core;
44

55
import org.hamcrest.AbstractMatcherTest;
6+
import org.hamcrest.Matcher;
67
import static org.hamcrest.MatcherAssert.assertThat;
78
import static org.hamcrest.core.Always.alwaysFails;
89
import static org.hamcrest.core.Always.alwaysPasses;
910
import static org.hamcrest.core.IsNot.not;
1011

1112
public class AlwaysTest extends AbstractMatcherTest {
1213

14+
protected Matcher<?> createMatcher() {
15+
return alwaysPasses();
16+
}
17+
1318
public void testAlwaysReturnsFixedBooleanValueFromMatchesMethod() {
1419
assertThat("something", alwaysPasses());
1520
assertThat("something else", alwaysPasses());

‎src/unit-test/org/hamcrest/core/AndTest.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package org.hamcrest.core;
44

55
import org.hamcrest.AbstractMatcherTest;
6+
import org.hamcrest.Matcher;
67
import static org.hamcrest.core.And.and;
78
import static org.hamcrest.core.IsEqual.eq;
89
import static org.hamcrest.core.IsNot.not;
@@ -11,6 +12,10 @@
1112

1213
public class AndTest extends AbstractMatcherTest {
1314

15+
protected Matcher<?> createMatcher() {
16+
return and(eq("irrelevant"), eq("irrelevant"));
17+
}
18+
1419
public void testEvaluatesToTheTheLogicalConjunctionOfTwoOtherMatchers() {
1520
assertThat("good", and(eq("good"), eq("good")));
1621

0 commit comments

Comments
 (0)