Browse Source

added some hamcrest matchers

pull/1/merge
kalle 14 years ago
parent
commit
bc605f738d
  1. 22
      json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
  2. 7
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
  3. 17
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/CollectionMatcher.java
  4. 50
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java
  5. 30
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsEmptyCollection.java
  6. 41
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java
  7. 40
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java
  8. 14
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/MapTypeSafeMatcher.java
  9. 58
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  10. 1
      pom.xml

22
json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java

@ -2,6 +2,8 @@ package com.jayway.jsonassert;
import com.jayway.jsonassert.impl.JsonAsserterImpl; import com.jayway.jsonassert.impl.JsonAsserterImpl;
import com.jayway.jsonassert.impl.matcher.*;
import org.hamcrest.Matcher;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import java.io.IOException; import java.io.IOException;
@ -9,6 +11,8 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.Reader; import java.io.Reader;
import java.text.ParseException; import java.text.ParseException;
import java.util.Collection;
import java.util.Map;
/** /**
* User: kalle stenflo * User: kalle stenflo
@ -63,4 +67,22 @@ public class JsonAssert {
return with(reader); return with(reader);
} }
//Matchers
public static CollectionMatcher collectionWithSize(Matcher<? super Integer> sizeMatcher) {
return new IsCollectionWithSize(sizeMatcher);
}
public static Matcher<Map<String, ?>> mapContainingKey(Matcher<String> keyMatcher) {
return new IsMapContainingKey(keyMatcher);
}
public static <V> Matcher<? super Map<?, V>> mapContainingValue(Matcher<? super V> valueMatcher) {
return new IsMapContainingValue<V>(valueMatcher);
}
public static Matcher<Collection<Object>> emptyCollection() {
return new IsEmptyCollection<Object>();
}
} }

7
json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java

@ -4,6 +4,7 @@ package com.jayway.jsonassert.impl;
import com.jayway.jsonassert.JsonAsserter; import com.jayway.jsonassert.JsonAsserter;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathUtil; import com.jayway.jsonpath.PathUtil;
import org.hamcrest.Description;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert; import org.hamcrest.MatcherAssert;
@ -41,11 +42,13 @@ public class JsonAsserterImpl implements JsonAsserter {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> JsonAsserter assertThat(String path, Matcher<T> matcher) { public <T> JsonAsserter assertThat(String path, Matcher<T> matcher) {
String reason = "When processing json path: " + path;
if(PathUtil.isPathDefinite(path)){ if(PathUtil.isPathDefinite(path)){
MatcherAssert.assertThat(JsonPath.<T>readOne(jsonObject, path), matcher); MatcherAssert.assertThat(reason, JsonPath.<T>readOne(jsonObject, path), matcher);
} }
else { else {
MatcherAssert.assertThat((T) JsonPath.<T>read(jsonObject, path), matcher); MatcherAssert.assertThat(reason, (T) JsonPath.<T>read(jsonObject, path), matcher);
} }
return this; return this;
} }

17
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/CollectionMatcher.java

@ -0,0 +1,17 @@
package com.jayway.jsonassert.impl.matcher;
import org.hamcrest.BaseMatcher;
import java.util.Collection;
public abstract class CollectionMatcher<C extends Collection<?>> extends BaseMatcher<C> {
@SuppressWarnings("unchecked")
public boolean matches(Object item) {
if (!(item instanceof Collection)) {
return false;
}
return matchesSafely((C)item);
}
protected abstract boolean matchesSafely(C collection);
}

50
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsCollectionWithSize.java

@ -0,0 +1,50 @@
package com.jayway.jsonassert.impl.matcher;
import static org.hamcrest.core.IsEqual.equalTo;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import java.util.Collection;
/**
* Matches if collection size satisfies a nested matcher.
*/
public class IsCollectionWithSize<E> extends CollectionMatcher<Collection<? extends E>> {
private final Matcher<? super Integer> sizeMatcher;
public IsCollectionWithSize(Matcher<? super Integer> sizeMatcher) {
this.sizeMatcher = sizeMatcher;
}
@Override
public boolean matchesSafely(Collection<? extends E> item) {
return sizeMatcher.matches(item.size());
}
public void describeTo(Description description) {
description.appendText("a collection with size ")
.appendDescriptionOf(sizeMatcher);
}
/**
* Does collection size satisfy a given matcher?
*/
@Factory
public static <E> Matcher<? super Collection<? extends E>> hasSize(Matcher<? super Integer> size) {
return new IsCollectionWithSize<E>(size);
}
/**
* This is a shortcut to the frequently used hasSize(equalTo(x)).
*
* For example, assertThat(hasSize(equal_to(x)))
* vs. assertThat(hasSize(x))
*/
@Factory
public static <E> Matcher<? super Collection<? extends E>> hasSize(int size) {
Matcher<? super Integer> matcher = equalTo(size);
return IsCollectionWithSize.<E>hasSize(matcher);
}
}

30
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsEmptyCollection.java

@ -0,0 +1,30 @@
package com.jayway.jsonassert.impl.matcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import java.util.Collection;
/**
* Tests if collection is empty.
*/
public class IsEmptyCollection<E> extends CollectionMatcher<Collection<E>> {
@Override
public boolean matchesSafely(Collection<E> item) {
return item.isEmpty();
}
public void describeTo(Description description) {
description.appendText("an empty collection");
}
/**
* Matches an empty collection.
*/
@Factory
public static <E> Matcher<Collection<E>> empty() {
return new IsEmptyCollection<E>();
}
}

41
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingKey.java

@ -0,0 +1,41 @@
package com.jayway.jsonassert.impl.matcher;
import java.util.Map;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import static org.hamcrest.core.IsEqual.equalTo;
public class IsMapContainingKey<K> extends MapTypeSafeMatcher<Map<K,?>> {
private final Matcher<K> keyMatcher;
public IsMapContainingKey(Matcher<K> keyMatcher) {
this.keyMatcher = keyMatcher;
}
@Override
public boolean matchesSafely(Map<K, ?> item) {
for (K key : item.keySet()) {
if (keyMatcher.matches(key)) {
return true;
}
}
return false;
}
public void describeTo(Description description) {
description.appendText("map with key ")
.appendDescriptionOf(keyMatcher);
}
@Factory
public static <K> Matcher<Map<K,?>> hasKey(K key) {
return hasKey(equalTo(key));
}
@Factory
public static <K> Matcher<Map<K,?>> hasKey(Matcher<K> keyMatcher) {
return new IsMapContainingKey<K>(keyMatcher);
}
}

40
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/IsMapContainingValue.java

@ -0,0 +1,40 @@
package com.jayway.jsonassert.impl.matcher;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import static org.hamcrest.core.IsEqual.equalTo;
import java.util.Map;
public class IsMapContainingValue<V> extends MapTypeSafeMatcher<Map<?,V>>{
private final Matcher<? super V> valueMatcher;
public IsMapContainingValue(Matcher<? super V> valueMatcher) {
this.valueMatcher = valueMatcher;
}
@Override
public boolean matchesSafely(Map<?, V> item) {
for (V value : item.values()) {
if (valueMatcher.matches(value)) {
return true;
}
}
return false;
}
public void describeTo(Description description) {
description.appendText("map with value ")
.appendDescriptionOf(valueMatcher);
}
@Factory
public static <V> Matcher<? super Map<?,V>> hasValue(V value) {
return IsMapContainingValue.<V>hasValue(equalTo(value));
}
@Factory
public static <V> Matcher<? super Map<?,V>> hasValue(Matcher<? super V> valueMatcher) {
return new IsMapContainingValue<V>(valueMatcher);
}
}

14
json-path-assert/src/main/java/com/jayway/jsonassert/impl/matcher/MapTypeSafeMatcher.java

@ -0,0 +1,14 @@
package com.jayway.jsonassert.impl.matcher;
import org.hamcrest.BaseMatcher;
import java.util.Map;
public abstract class MapTypeSafeMatcher<M extends Map<?, ?>> extends BaseMatcher<M> {
@SuppressWarnings("unchecked")
public boolean matches(Object item) {
return item instanceof Map && matchesSafely((M) item);
}
protected abstract boolean matchesSafely(M map);
}

58
json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java

@ -2,7 +2,7 @@ package com.jayway.jsonassert;
import org.junit.Test; import org.junit.Test;
import static com.jayway.jsonassert.JsonAssert.with; import static com.jayway.jsonassert.JsonAssert.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
/** /**
@ -57,7 +57,8 @@ public class JsonAssertTest {
with(JSON).assertThat("$..book[*].author", hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); with(JSON).assertThat("$..book[*].author", hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"));
with(JSON).assertThat("$..author", hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien")); with(JSON).assertThat("$..author", hasItems("Nigel Rees", "Evelyn Waugh", "Herman Melville", "J. R. R. Tolkien"))
.assertThat("$..author", is(collectionWithSize(equalTo(4))));
} }
@Test @Test
@ -69,56 +70,25 @@ public class JsonAssertTest {
public void map_content_can_be_asserted_with_matcher() throws Exception { public void map_content_can_be_asserted_with_matcher() throws Exception {
with(JSON).assertThat("$.store.book[0]", hasEntry("category", "reference")) with(JSON).assertThat("$.store.book[0]", hasEntry("category", "reference"))
.assertThat("$.store.book[0]", hasEntry("title", "Sayings of the Century")); .assertThat("$.store.book[0]", hasEntry("title", "Sayings of the Century"))
.and()
with(JSON).assertThat("$..book[0]", hasItems(hasEntry("category", "reference"))); .assertThat("$..book[0]", hasItems(hasEntry("category", "reference")))
} .and()
.assertThat("$.store.book[0]", mapContainingKey(equalTo("category")))
@Test .and()
public void a_path_can_be_asserted_equal_to() throws Exception { .assertThat("$.store.book[0]", mapContainingValue(equalTo("reference")));
with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century")
.assertThat("$.store.book[0].title", equalTo("Sayings of the Century"));
} }
/*
@Test @Test
public void a_sub_document_can_asserted_on__by_path() throws Exception { public void an_empty_collection() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("subDocument.subField", is(equalTo("sub-field"))); with(JSON).assertThat("$.store.book[?(@.category = 'x')]", emptyCollection());
} }
@Test @Test
public void a_path_can_be_asserted_equal_to() throws Exception { public void a_path_can_be_asserted_equal_to() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertEquals("stringField", "string-field"); with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century")
} .assertThat("$.store.book[0].title", equalTo("Sayings of the Century"));
@Test
public void a_path_can_be_asserted_is_null() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertNull("nullField");
}
@Test(expected = AssertionError.class)
public void failed_assert_throws() throws Exception {
JsonAssert.with(TEST_DOCUMENT).assertThat("stringField", equalTo("SOME CRAP"));
}
@Test
public void multiple_asserts_can_be_chained() throws Exception {
JsonAssert.with(TEST_DOCUMENT)
.assertThat("stringField", equalTo("string-field"))
.assertThat("numberField", is(notNullValue()))
.and()
.assertNull("nullField")
.and()
.assertEquals("stringField", "string-field");
} }
*/
} }

1
pom.xml

@ -72,7 +72,6 @@
<modules> <modules>
<module>json-path</module> <module>json-path</module>
<module>examples</module>
<module>json-path-assert</module> <module>json-path-assert</module>
</modules> </modules>

Loading…
Cancel
Save