1 package org.hamcrest.text; 2 3 import org.hamcrest.Description; 4 import org.hamcrest.Matcher; 5 import org.hamcrest.TypeSafeMatcher; 6 7 /** 8 * Tests if a string is equal to another string, regardless of the case. 9 */ 10 public class IsEqualIgnoringCase extends TypeSafeMatcher<String> { 11 12 // TODO: Replace String with CharSequence to allow for easy interoperability between 13 // String, StringBuffer, StringBuilder, CharBuffer, etc (joe). 14 15 private final String string; 16 17 public IsEqualIgnoringCase(String string) { 18 if (string == null) { 19 throw new IllegalArgumentException("Non-null value required by IsEqualIgnoringCase()"); 20 } 21 this.string = string; 22 } 23 24 @Override 25 public boolean matchesSafely(String item) { 26 return string.equalsIgnoreCase(item); 27 } 28 29 @Override 30 public void describeMismatchSafely(String item, Description mismatchDescription) { 31 mismatchDescription.appendText("was ").appendValue(item); 32 } 33 34 @Override 35 public void describeTo(Description description) { 36 description.appendText("equalToIgnoringCase(") 37 .appendValue(string) 38 .appendText(")"); 39 } 40 41 /** 42 * Creates a matcher of {@link String} that matches when the examined string is equal to 43 * the specified expectedString, ignoring case. 44 * For example: 45 * <pre>assertThat("Foo", equalToIgnoringCase("FOO"))</pre> 46 * 47 * @param expectedString 48 * the expected value of matched strings 49 */ 50 public static Matcher<String> equalToIgnoringCase(String expectedString) { 51 return new IsEqualIgnoringCase(expectedString); 52 } 53 54 } 55