Browse Source

more commit fun

pull/1/head
David Baldwin 14 years ago
parent
commit
72662b40c0
  1. 3
      json-path-assert/src/main/java/com/jayway/jsonassert/JsonAssert.java
  2. 9
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
  3. 20
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  4. 4
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  5. 6
      json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java
  6. 2
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java
  7. 1
      json-path/src/main/java/com/jayway/jsonpath/filter/ListWildcardFilter.java
  8. 7
      json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java
  9. 9
      json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java
  10. 17
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

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

@ -50,7 +50,8 @@ public class JsonAssert {
*/
public static JsonAsserter with(String json) throws com.jayway.jsonpath.json.ParseException {
try {
return new JsonAsserterImpl(JSON_PARSER.parse(json));
return new JsonAsserterImpl(JSON_PARSER.parse(json));
} catch (IOException e) {
throw new RuntimeException(e);
}

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

@ -43,14 +43,13 @@ public class JsonAsserterImpl implements JsonAsserter {
String reason = "When processing json path: " + path;
JsonElement je = JsonPath.read(jsonObject, path);
if (!( (je == null && matcher.matches(je)) || (je.isContainer() && matcher.matches(je)) || matcher.matches(je.toObject()) ) ) {
if (! (matcher.matches(je)||matcher.matches(je.toObject()))){
System.out.println(JsonPath.read(jsonObject, path).toString());
throw new AssertionError(reason + matcher.toString());
}
/*
*
if (PathUtil.isPathDefinite(path)) {
if (!matcher.matches(JsonPath.<T>readOne(jsonObject, path))) {
throw new AssertionError(reason + matcher.toString());
@ -80,8 +79,8 @@ public class JsonAsserterImpl implements JsonAsserter {
*/
public JsonAsserter assertNotDefined(String path) throws JsonException {
JsonElement o = JsonPath.read(jsonObject, path);
if (!o.isJsonNull()) {
if (o!=null && !o.isJsonNull()) {
throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path));
}
return this;

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

@ -12,6 +12,7 @@ import com.jayway.jsonpath.json.JsonFactory;
import java.io.InputStream;
import static com.jayway.jsonassert.JsonAssert.*;
import static org.hamcrest.Matchers.*;
@ -85,7 +86,7 @@ public abstract class JsonAssertTest {
@Test
public void a_value_can_asserted_to_be_null() throws Exception {
with(JSON).assertNull("$.store.bicycle.nullValue");
with(JSON).assertEquals("$.store.bicycle.nullValue",factory.createJsonPrimitive(null));
}
@Test
@ -96,8 +97,8 @@ public abstract class JsonAssertTest {
@Test
public void a_path_can_be_asserted_with_matcher() throws Exception {
with(JSON).assertThat("$.store.bicycle.color", equalTo("red"))
.assertThat("$.store.bicycle.price", equalTo(19.95D));
with(JSON).assertThat("$.store.bicycle.color", equalTo(w("red")))
.assertThat("$.store.bicycle.price", equalTo(w(19.95D)));
}
@Test
@ -158,21 +159,22 @@ public abstract class JsonAssertTest {
@Test
public void a_path_can_be_asserted_equal_to() throws Exception {
with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century")
.assertThat("$.store.book[0].title", equalTo("Sayings of the Century"));
with(JSON).assertEquals("$.store.book[0].title", w("Sayings of the Century"))
.assertThat("$.store.book[0].title", equalTo(w("Sayings of the Century")));
with(JSON).assertEquals("$['store']['book'][0].['title']", "Shttp://www.theregister.co.uk/public_sector/government/ayings of the Century")
.assertThat("$['store'].book[0].title", equalTo("Sayings of the Century"));
with(JSON).assertEquals("$['store']['book'][0].['title']", w("Sayings of the Century"))
.assertThat("$['store'].book[0].title", equalTo(w("Sayings of the Century")));
}
@Test
public void no_hit_returns_null() throws Exception {
with(JSON).assertThat("$.store.book[1000].title", Matchers.<Object>nullValue());
with(JSON).assertThat("$.store.book[1000]", equalTo(null));
}
@Test
public void invalid_path() throws Exception {
with(JSON).assertThat("$.store.book[*].fooBar.(value)", Matchers.<Object>nullValue());
with(JSON).assertThat("$.store.book[*].fooBar", Matchers.<Object>nullValue());
}
@Test

4
json-path/src/main/java/com/jayway/jsonpath/JsonPath.java

@ -135,13 +135,15 @@ public class JsonPath {
FilterOutput filterOutput = filters.filter(json);
if (filterOutput == null || filterOutput.getResult() == null) {
throw new JsonException("Element not found");
return null;
}
return filterOutput.getResult();
}
/**
* Applies this json path to the provided object
*

6
json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java

@ -36,6 +36,11 @@ public class FilterOutput {
public JsonElement getResult() throws JsonException {
for(int i=result.size()-1;i>=0;i--){
if(result.get(i).isJsonNull())
result.remove(i);
}
if(result.size()==0){
return null;
}
@ -51,6 +56,7 @@ public class FilterOutput {
}
}
public JsonArray getResultAsList() throws JsonException {
return getResult().toJsonArray();
}

2
json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java

@ -51,6 +51,8 @@ public class JsonPathFilterChain {
if(out.getList() == null){
return null;
}
out = filter.apply(out);
if(out.getResult()!=null)
log.info(out.getResult().toString());

1
json-path/src/main/java/com/jayway/jsonpath/filter/ListWildcardFilter.java

@ -26,6 +26,7 @@ public class ListWildcardFilter extends JsonPathFilterBase{
}
}
return result;
}

7
json-path/src/main/java/com/jayway/jsonpath/json/JsonNull.java

@ -60,5 +60,10 @@ public class JsonNull extends JsonElement {
}
@Override
public boolean equals(Object o1){
if(o1==null) return false;
return o1 instanceof JsonNull;
}
}

9
json-path/src/main/java/com/jayway/jsonpath/json/gson/GsonJsonPrimitive.java

@ -93,6 +93,13 @@ public class GsonJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{
@Override
public boolean equals(Object o1){
if (o1==null) return false;
if(o1 instanceof GsonJsonPrimitive){
if(value == null)
return ((GsonJsonPrimitive) o1).value == null;
else
return value.equals(((GsonJsonPrimitive) o1).value);
}
return (o1!=null) && o1.equals(wrappit(value));
}
@Override
@ -101,6 +108,7 @@ public class GsonJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{
}
@Override
public String toString(){
if(value == null) return "null";
return wrappit(value).toString();
}
@ -109,6 +117,7 @@ public class GsonJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{
return true;
}
@Override
public Object toObject() throws JsonException {
return wrappit(value);

17
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -149,13 +149,10 @@ public abstract class JsonPathTest {
String doc = "{foo:{biz:{id:1}}}";
assertEquals(JsonPath.read(doc, "$.foo.biz.(object)").toString(), "{\"id\":1}");
try{
JsonPath.read(doc, "$.foo.biz.(collection)");
fail();
}
catch(JsonException e){
}
doc = "{foo:{biz:[{Id:1},{Id:2},{Id:4,foo:1234}]}}";
@ -294,13 +291,9 @@ public abstract class JsonPathTest {
@Test
public void access_index_out_of_bounds_does_not_throw_exception() throws Exception {
try{
JsonElement res = JsonPath.read(DOCUMENT, "$.store.book[100].author");
fail();
}
catch(JsonException je){
}

Loading…
Cancel
Save