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) {
try {
//Object o = JsonPath.read(jsonObject, path);
Configuration c = Configuration.builder().options(Option.THROW_ON_MISSING_PROPERTY).build();
Configuration c = Configuration.defaultConfiguration();
JsonPath.using(c).parse(jsonObject).read(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
public JsonAsserter assertNotDefined(String path, String message) {
try {
//Object o = JsonPath.read(jsonObject, path);
Configuration c = Configuration.builder().options(Option.THROW_ON_MISSING_PROPERTY).build();
Configuration c = Configuration.defaultConfiguration();
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
@Ignore //TODO: finalize behaviour
//@Ignore //TODO: finalize behaviour
public void links_document() throws Exception {
with(getResourceAsStream("links.json")).assertEquals("count", 2)
.assertThat("links.gc:this.href", endsWith("?pageNumber=1&pageSize=2"))
.assertNotDefined("links.gc:prev")
.assertNotDefined("links.gc:next")
.assertThat("links['gc:this']href", endsWith("?pageNumber=1&pageSize=2"))
.assertNotDefined("links['gc:prev']")
.assertNotDefined("links['gc:next']")
.assertThat("rows", collectionWithSize(equalTo(2)));
}
@Test
@Ignore //TODO: finalize behaviour
//@Ignore //TODO: finalize behaviour
public void a_document_can_be_expected_not_to_contain_a_path() throws Exception {
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() {
return Collections.unmodifiableSet(options);
return options;
}
public boolean containsOption(Option option){
return options.contains(option);
}
public static Configuration defaultConfiguration() {
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
* @return object(s) matched by the given path
*/
public <T> T read(Object jsonObject, Configuration configuration) {
boolean optAsPathList = configuration.getOptions().contains(Option.AS_PATH_LIST);
boolean optAlwaysReturnList = configuration.getOptions().contains(Option.ALWAYS_RETURN_LIST);
boolean optSuppressExceptions = configuration.getOptions().contains(Option.SUPPRESS_EXCEPTIONS);
boolean optThrowOnMissingProperty = configuration.getOptions().contains(Option.THROW_ON_MISSING_PROPERTY);
boolean optAsPathList = configuration.containsOption(Option.AS_PATH_LIST);
boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
boolean optThrowOnMissingProperty = configuration.containsOption(Option.THROW_ON_MISSING_PROPERTY);
try {
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 functionBracketClosed = false;
boolean propertyOpen = false;
current = chars[++i]; //skip the '?'
while (current != ']' || bracketCount != 0) {
switch (current) {
case '(':
functionBracketOpened = true;
break;
case ')':
functionBracketClosed = true;
break;
case '[':
bracketCount++;
pathBuffer.append(current);
@ -280,7 +273,26 @@ public class PathCompiler {
pathBuffer.append('$');
break;
case '(':
if(!propertyOpen) {
functionBracketOpened = true;
break;
}
case ')':
if(!propertyOpen) {
functionBracketClosed = true;
break;
}
default:
if('\'' == current){
if (propertyOpen){
propertyOpen = false;
} else {
propertyOpen = true;
}
}
if (bracketCount == 0 && isOperatorChar(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);
}
if(ctx.configuration().getOptions().contains(Option.MERGE_MULTI_PROPS)) {
if(ctx.configuration().containsOption(Option.MERGE_MULTI_PROPS)) {
Object map = ctx.jsonProvider().createMap();
for (String property : properties) {
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.spi.json.JsonProvider;
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.junit.Test;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static junit.framework.Assert.*;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@ -214,7 +218,8 @@ public class IssuesTest {
}
@Test
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}}";
assertNull(JsonPath.parse(json, configuration).read("a.d"));
@ -335,4 +340,75 @@ public class IssuesTest {
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
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"));
}

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.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static junit.framework.Assert.assertNull;
import static org.assertj.core.api.Assertions.assertThat;
public class PropertyPathTokenTest {
@ -39,9 +42,16 @@ public class PropertyPathTokenTest {
@Test
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)

Loading…
Cancel
Save