Skip to content

Commit 56c5bbb

Browse files
committed
Added null check to substring matching. Corrected javadoc.
1 parent 82e83c6 commit 56c5bbb

File tree

7 files changed

+47
-6
lines changed

7 files changed

+47
-6
lines changed

hamcrest-core/src/main/java/org/hamcrest/core/StringContains.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static Matcher<String> containsString(String substring) {
3333
* Creates a matcher that matches if the examined {@link String} contains the specified
3434
* {@link String} anywhere, ignoring case.
3535
* For example:
36-
* <pre>assertThat("myStringOfNote", containsString("ring"))</pre>
36+
* <pre>assertThat("myStringOfNote", containsStringIgnoringCase("Ring"))</pre>
3737
*
3838
* @param substring
3939
* the substring that the returned matcher will expect to find within any examined string

hamcrest-core/src/main/java/org/hamcrest/core/StringEndsWith.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static Matcher<String> endsWith(String suffix) {
3030
* Creates a matcher that matches if the examined {@link String} ends with the specified
3131
* {@link String}, ignoring case.
3232
* For example:
33-
* <pre>assertThat("myStringOfNote", endsWith("Note"))</pre>
33+
* <pre>assertThat("myStringOfNote", endsWithIgnoringCase("note"))</pre>
3434
*
3535
* @param suffix
3636
* the substring that the returned matcher will expect at the end of any examined string

hamcrest-core/src/main/java/org/hamcrest/core/StringStartsWith.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class StringStartsWith extends SubstringMatcher {
3030
* {@link String}, ignoring case
3131
* </p>
3232
* For example:
33-
* <pre>assertThat("myStringOfNote", startsWith("my"))</pre>
33+
* <pre>assertThat("myStringOfNote", startsWithIgnoringCase("My"))</pre>
3434
*
3535
* @param prefix
3636
* the substring that the returned matcher will expect at the start of any examined string

hamcrest-core/src/main/java/org/hamcrest/core/SubstringMatcher.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ protected SubstringMatcher(String relationship, boolean ignoringCase, String sub
1616
this.relationship = relationship;
1717
this.ignoringCase = ignoringCase;
1818
this.substring = substring;
19+
if (null == substring) {
20+
throw new IllegalArgumentException("missing substring");
21+
}
1922
}
2023

2124
@Override

hamcrest-core/src/test/java/org/hamcrest/core/StringContainsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ public void testMatchesSubstringsIgnoringCase() {
4040
assertMismatchDescription("was \"Something else\"", ignoringCase, "Something else");
4141
assertDescription("a string containing \"ExCert\" ignoring case", ignoringCase);
4242
}
43+
44+
4345
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.hamcrest.core;
2+
3+
import org.junit.Rule;
4+
import org.junit.Test;
5+
import org.junit.rules.ExpectedException;
6+
7+
import static org.hamcrest.core.StringContains.containsString;
8+
import static org.hamcrest.core.StringEndsWith.endsWith;
9+
import static org.hamcrest.core.StringStartsWith.startsWith;
10+
11+
/**
12+
* @author Steve Freeman 2016 http://www.hamcrest.com
13+
*/
14+
public class StringMatchingTest {
15+
@Rule public final ExpectedException thrown = ExpectedException.none();
16+
17+
@Test public void
18+
startsWithFailsWithNullSubstring() {
19+
thrown.expect(IllegalArgumentException.class);
20+
startsWith(null);
21+
}
22+
23+
@Test public void
24+
endWithFailsWithNullSubstring() {
25+
thrown.expect(IllegalArgumentException.class);
26+
endsWith(null);
27+
}
28+
29+
@Test public void
30+
containsFailsWithNullSubstring() {
31+
thrown.expect(IllegalArgumentException.class);
32+
containsString(null);
33+
}
34+
35+
}

hamcrest-library/src/main/java/org/hamcrest/Matchers.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ public static org.hamcrest.Matcher<java.lang.String> containsString(java.lang.St
471471
* Creates a matcher that matches if the examined {@link String} contains the specified
472472
* {@link String} anywhere, ignoring case.
473473
* For example:
474-
* <pre>assertThat("myStringOfNote", containsString("ring"))</pre>
474+
* <pre>assertThat("myStringOfNote", containsStringIgnoringCase("Ring"))</pre>
475475
*
476476
* @param substring
477477
* the substring that the returned matcher will expect to find within any examined string
@@ -501,7 +501,7 @@ public static org.hamcrest.Matcher<java.lang.String> startsWith(java.lang.String
501501
* {@link String}, ignoring case
502502
* </p>
503503
* For example:
504-
* <pre>assertThat("myStringOfNote", startsWith("my"))</pre>
504+
* <pre>assertThat("myStringOfNote", startsWithIgnoringCase("My"))</pre>
505505
*
506506
* @param prefix
507507
* the substring that the returned matcher will expect at the start of any examined string
@@ -527,7 +527,7 @@ public static org.hamcrest.Matcher<java.lang.String> endsWith(java.lang.String s
527527
* Creates a matcher that matches if the examined {@link String} ends with the specified
528528
* {@link String}, ignoring case.
529529
* For example:
530-
* <pre>assertThat("myStringOfNote", endsWith("Note"))</pre>
530+
* <pre>assertThat("myStringOfNote", endsWithIgnoringCase("note"))</pre>
531531
*
532532
* @param suffix
533533
* the substring that the returned matcher will expect at the end of any examined string
@@ -1633,4 +1633,5 @@ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String x
16331633
return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext);
16341634
}
16351635

1636+
16361637
}

0 commit comments

Comments
 (0)