Browse Source

more commit fun

pull/1/head
David Baldwin 14 years ago
parent
commit
72662b40c0
  1. 1
      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. 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. 5
      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. 13
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

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

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

7
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; String reason = "When processing json path: " + path;
JsonElement je = JsonPath.read(jsonObject, 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()); System.out.println(JsonPath.read(jsonObject, path).toString());
throw new AssertionError(reason + matcher.toString()); throw new AssertionError(reason + matcher.toString());
} }
/* /*
*
if (PathUtil.isPathDefinite(path)) { if (PathUtil.isPathDefinite(path)) {
if (!matcher.matches(JsonPath.<T>readOne(jsonObject, path))) { if (!matcher.matches(JsonPath.<T>readOne(jsonObject, path))) {
throw new AssertionError(reason + matcher.toString()); throw new AssertionError(reason + matcher.toString());
@ -81,7 +80,7 @@ public class JsonAsserterImpl implements JsonAsserter {
public JsonAsserter assertNotDefined(String path) throws JsonException { public JsonAsserter assertNotDefined(String path) throws JsonException {
JsonElement o = JsonPath.read(jsonObject, path); 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)); throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path));
} }
return this; 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 java.io.InputStream;
import static com.jayway.jsonassert.JsonAssert.*; import static com.jayway.jsonassert.JsonAssert.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@ -85,7 +86,7 @@ public abstract class JsonAssertTest {
@Test @Test
public void a_value_can_asserted_to_be_null() throws Exception { 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 @Test
@ -96,8 +97,8 @@ public abstract class JsonAssertTest {
@Test @Test
public void a_path_can_be_asserted_with_matcher() throws Exception { public void a_path_can_be_asserted_with_matcher() throws Exception {
with(JSON).assertThat("$.store.bicycle.color", equalTo("red")) with(JSON).assertThat("$.store.bicycle.color", equalTo(w("red")))
.assertThat("$.store.bicycle.price", equalTo(19.95D)); .assertThat("$.store.bicycle.price", equalTo(w(19.95D)));
} }
@Test @Test
@ -158,21 +159,22 @@ public abstract class JsonAssertTest {
@Test @Test
public void a_path_can_be_asserted_equal_to() throws Exception { public void a_path_can_be_asserted_equal_to() throws Exception {
with(JSON).assertEquals("$.store.book[0].title", "Sayings of the Century") with(JSON).assertEquals("$.store.book[0].title", w("Sayings of the Century"))
.assertThat("$.store.book[0].title", equalTo("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") with(JSON).assertEquals("$['store']['book'][0].['title']", w("Sayings of the Century"))
.assertThat("$['store'].book[0].title", equalTo("Sayings of the Century")); .assertThat("$['store'].book[0].title", equalTo(w("Sayings of the Century")));
} }
@Test @Test
public void no_hit_returns_null() throws Exception { 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 @Test
public void invalid_path() throws Exception { 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 @Test

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

@ -135,13 +135,15 @@ public class JsonPath {
FilterOutput filterOutput = filters.filter(json); FilterOutput filterOutput = filters.filter(json);
if (filterOutput == null || filterOutput.getResult() == null) { if (filterOutput == null || filterOutput.getResult() == null) {
throw new JsonException("Element not found"); return null;
} }
return filterOutput.getResult(); return filterOutput.getResult();
} }
/** /**
* Applies this json path to the provided object * 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 { 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){ if(result.size()==0){
return null; return null;
} }
@ -51,6 +56,7 @@ public class FilterOutput {
} }
} }
public JsonArray getResultAsList() throws JsonException { public JsonArray getResultAsList() throws JsonException {
return getResult().toJsonArray(); 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){ if(out.getList() == null){
return null; return null;
} }
out = filter.apply(out); out = filter.apply(out);
if(out.getResult()!=null) if(out.getResult()!=null)
log.info(out.getResult().toString()); 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; return result;
} }

5
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 @Override
public boolean equals(Object o1){ 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)); return (o1!=null) && o1.equals(wrappit(value));
} }
@Override @Override
@ -101,6 +108,7 @@ public class GsonJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{
} }
@Override @Override
public String toString(){ public String toString(){
if(value == null) return "null";
return wrappit(value).toString(); return wrappit(value).toString();
} }
@ -109,6 +117,7 @@ public class GsonJsonPrimitive extends com.jayway.jsonpath.json.JsonPrimitive{
return true; return true;
} }
@Override @Override
public Object toObject() throws JsonException { public Object toObject() throws JsonException {
return wrappit(value); return wrappit(value);

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

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

Loading…
Cancel
Save