Browse Source

Fixed issues

pull/40/head
Kalle Stenflo 10 years ago
parent
commit
c88638c4ef
  1. 8
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
  2. 10
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  3. 7
      json-path/src/main/java/com/jayway/jsonpath/Configuration.java
  4. 9
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  5. 28
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/PathCompiler.java
  6. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/PathToken.java
  7. 78
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  8. 5
      json-path/src/test/java/com/jayway/jsonpath/NullHandlingTest.java
  9. 14
      json-path/src/test/java/com/jayway/jsonpath/internal/PropertyPathTokenTest.java

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

@ -65,9 +65,7 @@ public class JsonAsserterImpl implements JsonAsserter {
public JsonAsserter assertNotDefined(String path) { public JsonAsserter assertNotDefined(String path) {
try { try {
//Object o = JsonPath.read(jsonObject, path); Configuration c = Configuration.defaultConfiguration();
Configuration c = Configuration.builder().options(Option.THROW_ON_MISSING_PROPERTY).build();
JsonPath.using(c).parse(jsonObject).read(path); JsonPath.using(c).parse(jsonObject).read(path);
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));
@ -79,9 +77,7 @@ public class JsonAsserterImpl implements JsonAsserter {
@Override @Override
public JsonAsserter assertNotDefined(String path, String message) { public JsonAsserter assertNotDefined(String path, String message) {
try { try {
//Object o = JsonPath.read(jsonObject, path); Configuration c = Configuration.defaultConfiguration();
Configuration c = Configuration.builder().options(Option.THROW_ON_MISSING_PROPERTY).build();
JsonPath.using(c).parse(jsonObject).read(path); JsonPath.using(c).parse(jsonObject).read(path);

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

@ -55,20 +55,20 @@ public class JsonAssertTest {
} }
@Test @Test
@Ignore //TODO: finalize behaviour //@Ignore //TODO: finalize behaviour
public void links_document() throws Exception { public void links_document() throws Exception {
with(getResourceAsStream("links.json")).assertEquals("count", 2) with(getResourceAsStream("links.json")).assertEquals("count", 2)
.assertThat("links.gc:this.href", endsWith("?pageNumber=1&pageSize=2")) .assertThat("links['gc:this']href", endsWith("?pageNumber=1&pageSize=2"))
.assertNotDefined("links.gc:prev") .assertNotDefined("links['gc:prev']")
.assertNotDefined("links.gc:next") .assertNotDefined("links['gc:next']")
.assertThat("rows", collectionWithSize(equalTo(2))); .assertThat("rows", collectionWithSize(equalTo(2)));
} }
@Test @Test
@Ignore //TODO: finalize behaviour //@Ignore //TODO: finalize behaviour
public void a_document_can_be_expected_not_to_contain_a_path() throws Exception { public void a_document_can_be_expected_not_to_contain_a_path() throws Exception {
with(JSON).assertNotDefined("$.store.bicycle.cool"); with(JSON).assertNotDefined("$.store.bicycle.cool");
} }

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

@ -55,9 +55,14 @@ public class Configuration {
} }
public Set<Option> getOptions() { public Set<Option> getOptions() {
return Collections.unmodifiableSet(options); return options;
} }
public boolean containsOption(Option option){
return options.contains(option);
}
public static Configuration defaultConfiguration() { public static Configuration defaultConfiguration() {
return new Configuration(JsonProviderFactory.createProvider(), EnumSet.noneOf(Option.class)); return new Configuration(JsonProviderFactory.createProvider(), EnumSet.noneOf(Option.class));
} }

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

@ -165,12 +165,11 @@ public class JsonPath {
* @param <T> expected return type * @param <T> expected return type
* @return object(s) matched by the given path * @return object(s) matched by the given path
*/ */
public <T> T read(Object jsonObject, Configuration configuration) { public <T> T read(Object jsonObject, Configuration configuration) {
boolean optAsPathList = configuration.getOptions().contains(Option.AS_PATH_LIST); boolean optAsPathList = configuration.containsOption(Option.AS_PATH_LIST);
boolean optAlwaysReturnList = configuration.getOptions().contains(Option.ALWAYS_RETURN_LIST); boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
boolean optSuppressExceptions = configuration.getOptions().contains(Option.SUPPRESS_EXCEPTIONS); boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
boolean optThrowOnMissingProperty = configuration.getOptions().contains(Option.THROW_ON_MISSING_PROPERTY); boolean optThrowOnMissingProperty = configuration.containsOption(Option.THROW_ON_MISSING_PROPERTY);
try { try {
if(optAsPathList){ if(optAsPathList){

28
json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/PathCompiler.java

@ -252,20 +252,13 @@ public class PathCompiler {
boolean functionBracketOpened = false; boolean functionBracketOpened = false;
boolean functionBracketClosed = false; boolean functionBracketClosed = false;
boolean propertyOpen = false;
current = chars[++i]; //skip the '?' current = chars[++i]; //skip the '?'
while (current != ']' || bracketCount != 0) { while (current != ']' || bracketCount != 0) {
switch (current) { switch (current) {
case '(':
functionBracketOpened = true;
break;
case ')':
functionBracketClosed = true;
break;
case '[': case '[':
bracketCount++; bracketCount++;
pathBuffer.append(current); pathBuffer.append(current);
@ -280,7 +273,26 @@ public class PathCompiler {
pathBuffer.append('$'); pathBuffer.append('$');
break; break;
case '(':
if(!propertyOpen) {
functionBracketOpened = true;
break;
}
case ')':
if(!propertyOpen) {
functionBracketClosed = true;
break;
}
default: default:
if('\'' == current){
if (propertyOpen){
propertyOpen = false;
} else {
propertyOpen = true;
}
}
if (bracketCount == 0 && isOperatorChar(current)) { if (bracketCount == 0 && isOperatorChar(current)) {
operatorBuffer.append(current); operatorBuffer.append(current);

2
json-path/src/main/java/com/jayway/jsonpath/internal/spi/compiler/PathToken.java

@ -39,7 +39,7 @@ abstract class PathToken {
throw new InvalidPathException("Multi properties can only be used as path leafs: " + evalPath); throw new InvalidPathException("Multi properties can only be used as path leafs: " + evalPath);
} }
if(ctx.configuration().getOptions().contains(Option.MERGE_MULTI_PROPS)) { if(ctx.configuration().containsOption(Option.MERGE_MULTI_PROPS)) {
Object map = ctx.jsonProvider().createMap(); Object map = ctx.jsonProvider().createMap();
for (String property : properties) { for (String property : properties) {
Object propertyVal = readObjectProperty(property, model, ctx); Object propertyVal = readObjectProperty(property, model, ctx);

78
json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java

@ -3,14 +3,18 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.Utils; import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.json.JsonProvider; import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.json.JsonProviderFactory; import com.jayway.jsonpath.spi.json.JsonProviderFactory;
import org.assertj.core.api.Assertions;
import org.assertj.core.data.MapEntry;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Test; import org.junit.Test;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static junit.framework.Assert.*; import static junit.framework.Assert.*;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -214,7 +218,8 @@ public class IssuesTest {
} }
@Test @Test
public void issue_22c() throws Exception { public void issue_22c() throws Exception {
Configuration configuration = Configuration.builder().build(); //Configuration configuration = Configuration.builder().build();
Configuration configuration = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
String json = "{\"a\":{\"b\":1,\"c\":2}}"; String json = "{\"a\":{\"b\":1,\"c\":2}}";
assertNull(JsonPath.parse(json, configuration).read("a.d")); assertNull(JsonPath.parse(json, configuration).read("a.d"));
@ -335,4 +340,75 @@ public class IssuesTest {
Object read = JsonPath.read(json, "$.data.passes[0].id"); Object read = JsonPath.read(json, "$.data.passes[0].id");
} }
@Test
public void issue_42() {
String json = "{" +
" \"list\": [{" +
" \"name\": \"My (String)\" " +
" }] " +
" }";
List<Map<String, String>> result = JsonPath.read(json, "$.list[?(@.name == 'My (String)')]");
Assertions.assertThat(result).containsExactly(Collections.singletonMap("name", "My (String)"));
}
@Test
public void issue_43() {
String json = "{\"test\":null}";
Assertions.assertThat(JsonPath.read(json, "test")).isNull();
Assertions.assertThat(JsonPath.using(Configuration.defaultConfiguration().options(Option.SUPPRESS_EXCEPTIONS)).parse(json).read("nonExistingProperty")).isNull();
try {
JsonPath.read(json, "nonExistingProperty");
failBecauseExceptionWasNotThrown(PathNotFoundException.class);
} catch (PathNotFoundException e){
Assertions.assertThat(e).hasMessage("No results for path: $['nonExistingProperty']");
}
try {
JsonPath.read(json, "nonExisting.property");
failBecauseExceptionWasNotThrown(PathNotFoundException.class);
} catch (PathNotFoundException e){
Assertions.assertThat(e).hasMessage("No results for path: $['nonExisting']['property']");
}
}
@Test
public void issue_45() {
String json = "{\"rootkey\":{\"sub.key\":\"value\"}}";
Assertions.assertThat(JsonPath.read(json, "rootkey['sub.key']")).isEqualTo("value");
}
@Test
public void issue_46() {
String json = "{\"a\": {}}";
Configuration configuration = Configuration.defaultConfiguration().options(Option.SUPPRESS_EXCEPTIONS);
Assertions.assertThat(JsonPath.using(configuration).parse(json).read("a.x")).isNull();
try {
JsonPath.read(json, "a.x");
failBecauseExceptionWasNotThrown(PathNotFoundException.class);
} catch (PathNotFoundException e){
Assertions.assertThat(e).hasMessage("No results for path: $['a']['x']");
}
}
} }

5
json-path/src/test/java/com/jayway/jsonpath/NullHandlingTest.java

@ -42,6 +42,11 @@ public class NullHandlingTest {
@Test @Test
public void last_token_defaults_to_null() { public void last_token_defaults_to_null() {
//Configuration configuration = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
//assertNull(JsonPath.parse(DOCUMENT, configuration).read("$.children[2].age"));
assertNull(JsonPath.read(DOCUMENT, "$.children[2].age")); assertNull(JsonPath.read(DOCUMENT, "$.children[2].age"));
} }

14
json-path/src/test/java/com/jayway/jsonpath/internal/PropertyPathTokenTest.java

@ -8,8 +8,11 @@ import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import static junit.framework.Assert.assertNull;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class PropertyPathTokenTest { public class PropertyPathTokenTest {
@ -39,9 +42,16 @@ public class PropertyPathTokenTest {
@Test @Test
public void property_not_found() { public void property_not_found() {
String result = JsonPath.read(SIMPLE_MAP, "$.not-found"); //String result = JsonPath.read(SIMPLE_MAP, "$.not-found");
//assertThat(result).isNull();
Configuration configuration = Configuration.builder().options(Option.SUPPRESS_EXCEPTIONS).build();
String json = "{\"a\":{\"b\":1,\"c\":2}}";
assertNull(JsonPath.parse(SIMPLE_MAP, configuration).read("$.not-found"));
assertThat(result).isNull();
} }
@Test(expected = PathNotFoundException.class) @Test(expected = PathNotFoundException.class)

Loading…
Cancel
Save