Browse Source

Invalid path detection improved..

pull/57/head
Kalle Stenflo 10 years ago
parent
commit
e3c1673f19
  1. 25
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  2. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java

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

@ -1,5 +1,9 @@
package com.jayway.jsonassert; package com.jayway.jsonassert;
import com.jayway.jsonpath.InvalidPathException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.PathCompiler;
import org.junit.Test; import org.junit.Test;
import java.io.InputStream; import java.io.InputStream;
@ -146,15 +150,6 @@ public class JsonAssertTest {
.assertThat("$['store'].book[0].title", equalTo("Sayings of the Century")); .assertThat("$['store'].book[0].title", equalTo("Sayings of the Century"));
} }
/*
@Test
public void no_hit_returns_null() throws Exception {
with(JSON).assertThat("$.store.book[1000].title", Matchers.<Object>nullValue());
}
*/
@Test @Test
public void path_including_wildcard_path_followed_by_another_path_concatenates_results_to_list() throws Exception { public void path_including_wildcard_path_followed_by_another_path_concatenates_results_to_list() throws Exception {
with(getResourceAsStream("lotto.json")).assertThat("lotto.winners[*].winnerId", hasItems(23, 54)); with(getResourceAsStream("lotto.json")).assertThat("lotto.winners[*].winnerId", hasItems(23, 54));
@ -163,13 +158,17 @@ public class JsonAssertTest {
@Test @Test
public void testNotDefined() throws Exception { public void testNotDefined() throws Exception {
JsonAsserter asserter = JsonAssert.with("{\"foo\":\"bar\"}"); JsonAsserter asserter = JsonAssert.with("{\"foo\":\"bar\"}");
asserter.assertEquals("$.foo", "bar"); // pass asserter.assertNotDefined("$.xxx");
asserter.assertEquals("$foo", "bar"); // pass
asserter.assertNotDefined("$.xxx"); // fail but should be pass
asserter.assertNotDefined("$xxx"); // fail but should be pass
} }
@Test(expected = InvalidPathException.class)
public void assert_that_invalid_path_is_thrown() {
JsonAsserter asserter = JsonAssert.with("{\"foo\":\"bar\"}");
asserter.assertEquals("$foo", "bar");
}
private InputStream getResourceAsStream(String resourceName) { private InputStream getResourceAsStream(String resourceName) {
return getClass().getClassLoader().getResourceAsStream(resourceName); return getClass().getClassLoader().getResourceAsStream(resourceName);

5
json-path/src/main/java/com/jayway/jsonpath/internal/PathCompiler.java

@ -70,6 +70,11 @@ public class PathCompiler {
path = "$" + path.substring(1); path = "$" + path.substring(1);
} }
if(path.length() > 1 &&
path.charAt(1) != '.' &&
path.charAt(1) != '['){
throw new InvalidPathException("Invalid path " + path);
}
String cacheKey = path + isRootPath + filterList.toString(); String cacheKey = path + isRootPath + filterList.toString();
Path p = cache.get(cacheKey); Path p = cache.get(cacheKey);

Loading…
Cancel
Save