| 1 | |
package net.sf.cotta.test.assertion; |
| 2 | |
|
| 3 | |
import org.hamcrest.BaseMatcher; |
| 4 | |
import org.hamcrest.Description; |
| 5 | |
import org.hamcrest.Matcher; |
| 6 | |
import org.hamcrest.core.AllOf; |
| 7 | |
import org.junit.matchers.JUnitMatchers; |
| 8 | |
|
| 9 | |
import java.util.ArrayList; |
| 10 | |
import java.util.List; |
| 11 | |
|
| 12 | |
public class StringAssert extends BaseAssert<String, StringAssert> { |
| 13 | |
public StringAssert(String value) { |
| 14 | 4 | super(value); |
| 15 | 4 | } |
| 16 | |
|
| 17 | |
public StringAssert(byte[] value) { |
| 18 | 1 | this(value == null ? null : new String(value)); |
| 19 | 1 | } |
| 20 | |
|
| 21 | |
public StringAssert isNotEmpty() { |
| 22 | 0 | not(empty()); |
| 23 | 0 | return this; |
| 24 | |
} |
| 25 | |
|
| 26 | |
public StringAssert isEmpty() { |
| 27 | 0 | matches(empty()); |
| 28 | 0 | return this; |
| 29 | |
} |
| 30 | |
|
| 31 | |
private BaseMatcher<String> empty() { |
| 32 | 0 | return new BaseMatcher<String>() { |
| 33 | |
public boolean matches(Object o) { |
| 34 | 0 | String string = (String) o; |
| 35 | 0 | return string != null && string.length() == 0; |
| 36 | |
} |
| 37 | |
|
| 38 | |
public void describeTo(Description description) { |
| 39 | 0 | description.appendText("empty string"); |
| 40 | 0 | } |
| 41 | |
}; |
| 42 | |
} |
| 43 | |
|
| 44 | |
public StringAssert contains(String... expectedValues) { |
| 45 | 3 | Matcher<String> matcher = expectedValues.length == 1 ? |
| 46 | |
JUnitMatchers.containsString(expectedValues[0]) : |
| 47 | |
allof(expectedValues); |
| 48 | 3 | matches(matcher); |
| 49 | 3 | return this; |
| 50 | |
} |
| 51 | |
|
| 52 | |
private Matcher<String> allof(String[] expectedValues) { |
| 53 | 0 | List<Matcher<? extends String>> matchers = new ArrayList<Matcher<? extends String>>(expectedValues.length); |
| 54 | 0 | for (String item : expectedValues) { |
| 55 | 0 | matchers.add(JUnitMatchers.containsString(item)); |
| 56 | |
} |
| 57 | 0 | return new AllOf<String>(matchers); |
| 58 | |
} |
| 59 | |
|
| 60 | |
} |