Created
January 18, 2011 03:07
-
-
Save kimukou/783917 to your computer and use it in GitHub Desktop.
HamcrestTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// reference http://d.hatena.ne.jp/daisuke-m/20090710/1247181113 | |
// | |
// test using: Groovy Console Java Web Start | |
// http://dl.getdropbox.com/u/653108/groovy/console.jnlp | |
// | |
@Grab(group='org.hamcrest', module='hamcrest-all', version='1.1') | |
@Grab(group='junit', module='junit', version='4.8.2') | |
@Grab(group='xmlunit', module='xmlunit', version='1.3') | |
import static org.hamcrest.CoreMatchers.* | |
import static org.hamcrest.Matchers.* | |
import static org.junit.Assert.assertThat | |
import java.io.ByteArrayInputStream | |
import java.util.* | |
import javax.xml.parsers.* | |
import org.custommonkey.xmlunit.* | |
import org.hamcrest.Matcher | |
import org.hamcrest.Matchers | |
import org.junit.Test | |
import org.w3c.dom.Document | |
public class HamcrestTest { | |
@Test | |
public void testHamcrestBasic() { | |
String s = "foo" | |
String s2 = s | |
String s3 = "foobarbaz" | |
String s4 = "foo bar baz" | |
String n = null | |
Class<?> c = String.class | |
// --- Basic Case | |
assertThat(n, is(nullValue())) // assert that n is null value. | |
assertThat(s, is(notNullValue())) // assert that s is not null value. | |
assertThat(s, is("foo")) // asser that s is "foo". | |
assertThat(s, is(not(s3))) // assert that s is not f3. | |
assertThat(s, is(equalTo(s2))) // assert that s is equal to f2. [equals() compare] | |
assertThat(s, is(not(equalTo(s3)))) | |
assertThat(s, is(sameInstance(s2))) // assert that s is same instance of f2. [== compare] | |
assertThat(s, is(not(sameInstance(s3)))) | |
assertThat(s, is(instanceOf(String.class))) // assert that s is instance of String. | |
assertThat(c, is(typeCompatibleWith(CharSequence.class))) // assert that c is type (which is) compatible with CharSequence class | |
assertThat(c, is(not(typeCompatibleWith(Number.class)))) | |
// --- String Case | |
assertThat(s, is(equalToIgnoringCase("FOO"))) | |
assertThat(s4, is(equalToIgnoringWhiteSpace("foo bar baz"))) | |
assertThat(s3, startsWith("foo")) | |
assertThat(s3, endsWith("baz")) | |
assertThat(s3, containsString("bar")) | |
// --- Numeric Case | |
//def num = new java.math.BigDecimal(1.0) | |
//double num = 1.0 | |
def num = 1.0 | |
assertThat(num, is(greaterThan(0.5))) // 1.0 > 0.5 | |
assertThat(num, is(greaterThanOrEqualTo(1.0))) // 1.0 >= 1.0 | |
assertThat(num, is(lessThan(1.1))) // 1.0 < 1.1 | |
assertThat(num, is(lessThanOrEqualTo(1.0))) // 1.0 <= 1.0 | |
assertThat(num, is(closeTo(0.95, 0.1))) // 1.0 = 0.95±0.1 | |
assertThat(num, is(not(closeTo(0.95, 0.01)))) // 1.0 != 0.95±0.01 | |
} | |
@Test | |
public void testHamcrestCollection() { | |
String f = "foo" | |
String[] a = [ "foo", "foobar", "foobarbaz" ] | |
Collection<String> c = Arrays.asList("foo", "bar", "baz") | |
Map<String,String> map = new HashMap<String, String>() | |
map.put("A", "a") | |
map.put("B", "b") | |
// --- Collection Case | |
assertThat(c, hasItems("bar", "baz")) | |
assertThat(f, isIn(c)) | |
assertThat(f, isOneOf(a)) | |
assertThat(map, hasEntry("A", "a")) | |
assertThat(map, not(hasEntry("A", "b"))) | |
assertThat(map, not(hasEntry("Z", "z"))) | |
assertThat(map, Matchers.<String, String>hasKey("A")) | |
assertThat(map, not(Matchers.<String, String>hasKey("Z"))) | |
assertThat(map, Matchers.<String, String>hasValue("a")) | |
assertThat(map, not(Matchers.<String, String>hasValue("z"))) | |
} | |
@Test | |
public void testHamcrestEvent() throws Exception { | |
Object o = new Object() | |
EventObject ev = new EventObject(o) | |
Object o2 = new Object() | |
EventObject ev2 = new EventObject(o2) | |
assertThat(ev, is(eventFrom(o))) | |
assertThat(ev, is(not(eventFrom(o2)))) | |
assertThat(ev2, is(eventFrom(o2))) | |
assertThat(ev2, is(not(eventFrom(o)))) | |
} | |
@Test | |
public void testHamcrestXml() throws Exception { | |
//<?xml version="1.0" encoding="UTF-8" ?>\n | |
final String XML1 =""" | |
<foo hoge="hoge" fuga="fuga">\n | |
<bar>baz</bar>\n | |
</foo>\n | |
""" | |
//<?xml version="1.0" encoding="UTF-8" ?>\n | |
final String XML2 =""" | |
<foo fuga="fuga" hoge="hoge">\n | |
<bar>baz</bar>\n | |
</foo>\n | |
""" | |
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance() | |
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder() | |
Document document = documentBuilder.parse(new ByteArrayInputStream(XML1.getBytes("UTF-8"))) | |
assertThat(document, hasXPath("/foo/bar", is("baz"))) | |
// It is not hamcrest, and I introduce xmlunit. Incidentally because it was convenient. | |
DetailedDiff diff = new DetailedDiff(new Diff(XML1, XML2)) | |
assertThat(diff.getAllDifferences().toString(), diff.similar(), is(true)) | |
} | |
@Test | |
public void testHamcrestBean() throws Exception { | |
Hoge hoge = new Hoge() | |
assertThat(hoge, hasProperty("fuga")) | |
assertThat(hoge, not(hasProperty("ponyo"))) | |
hoge.setFuga("wooo") | |
assertThat(hoge, hasToString(equalTo("Hoge[wooo]"))) | |
// ↑ equals ↓ | |
assertThat(hoge.toString(), is(equalTo("Hoge[wooo]"))) | |
} | |
@Test | |
public void testHamcrestLogical() { | |
String f1 = "foobarbaz" | |
String f2 = "foo bar baz" | |
Collection<Matcher<? extends String>> matchers = new ArrayList<Matcher<? extends String>>() | |
matchers.add(containsString("foo")) | |
matchers.add(containsString("oob")) | |
matchers.add(containsString("arb")) | |
// f1 Enough all | |
assertThat(f1, is(allOf(matchers))) | |
// f1 Enough one of | |
assertThat(f2, is(anyOf(matchers))) | |
} | |
private class Hoge { | |
private String fuga | |
public String getFuga() { | |
return fuga | |
} | |
public void setFuga(String fuga) { | |
this.fuga = fuga | |
} | |
@Override | |
public String toString() { | |
return "Hoge[" + fuga + "]" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment