Browse Source

Fixed exception catching in JsonModel hasPath().

pull/32/head
Kalle Stenflo 11 years ago
parent
commit
5f2879f1ed
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  2. 23
      json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

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

@ -155,7 +155,7 @@ public class JsonModel {
try {
get(jsonPath);
} catch (InvalidPathException e) {
} catch (PathNotFoundException e) {
return false;
}
return true;

23
json-path/src/test/java/com/jayway/jsonpath/JsonModelTest.java

@ -54,6 +54,18 @@ public class JsonModelTest {
" }\n" +
"}";
public final static String INVALID_DOCUMENT = "{?\\?\\?!!?~q`}}}}}\"\" \"store\": {\n";
@Test(expected = InvalidJsonException.class)
public void invalid_json_throws() throws Exception {
JsonModel.model(INVALID_DOCUMENT).get("store.id");
}
@Test(expected = InvalidPathException.class)
public void invalid_path_throws() throws Exception {
JsonModel.model(DOCUMENT).get("a(");
}
@Test
public void a_model_can_be_pretty_printed() throws Exception {
@ -65,10 +77,10 @@ public class JsonModelTest {
@Test
public void has_path_validates() throws Exception {
assertFalse(JsonModel.model(DOCUMENT).hasPath("store.invalid"));
assertFalse( JsonModel.model(DOCUMENT).hasPath("store.book[0].foo"));
assertFalse(JsonModel.model(DOCUMENT).hasPath("store.book[0].foo"));
assertTrue( JsonModel.model(DOCUMENT).hasPath("store.book"));
assertTrue( JsonModel.model(DOCUMENT).hasPath("store.book[0].title"));
assertTrue(JsonModel.model(DOCUMENT).hasPath("store.book"));
assertTrue(JsonModel.model(DOCUMENT).hasPath("store.book[0].title"));
}
@Test
@ -84,7 +96,6 @@ public class JsonModelTest {
}
@Test
public void maps_and_list_can_queried() throws Exception {
Map<String, Object> doc = new HashMap<String, Object>();
@ -99,10 +110,6 @@ public class JsonModelTest {
}
@Test(expected = InvalidPathException.class)
public void invalid_path_throws() throws Exception {
JsonModel.model(DOCUMENT).get("store.invalid");
}
@Test
public void query_for_null_property_returns_null() {

Loading…
Cancel
Save