Browse Source

Merge remote-tracking branch 'upstream/master' into add_test_for_issue_29_part_1

pull/20/head
Jochen Berger 12 years ago
parent
commit
76b716e337
  1. 16
      json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java
  2. 39
      json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java
  3. 11
      json-path/pom.xml
  4. 4
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  5. 7
      json-path/src/main/java/com/jayway/jsonpath/Filter.java
  6. 2
      json-path/src/main/java/com/jayway/jsonpath/JsonModel.java
  7. 4
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  8. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java
  9. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java
  10. 2
      json-path/src/main/java/com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java
  11. 18
      json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java
  12. 2
      json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java
  13. 2
      json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java
  14. 15
      pom.xml

16
json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java

@ -26,6 +26,15 @@ public interface JsonAsserter {
*/ */
<T> JsonAsserter assertThat(String path, Matcher<T> matcher); <T> JsonAsserter assertThat(String path, Matcher<T> matcher);
/**
* @param path
* @param matcher
* @param message
* @param <T>
* @return
*/
<T> JsonAsserter assertThat(String path, Matcher<T> matcher, String message);
/** /**
* Asserts that object specified by path is equal to the expected value. * Asserts that object specified by path is equal to the expected value.
* If they are not, an AssertionError is thrown with the given message. * If they are not, an AssertionError is thrown with the given message.
@ -37,6 +46,8 @@ public interface JsonAsserter {
*/ */
<T> JsonAsserter assertEquals(String path, T expected); <T> JsonAsserter assertEquals(String path, T expected);
<T> JsonAsserter assertEquals(String path, T expected, String message);
/** /**
* Checks that a path is not defined within a document. If the document contains the * Checks that a path is not defined within a document. If the document contains the
* given path, an AssertionError is thrown * given path, an AssertionError is thrown
@ -46,6 +57,8 @@ public interface JsonAsserter {
*/ */
JsonAsserter assertNotDefined(String path); JsonAsserter assertNotDefined(String path);
JsonAsserter assertNotDefined(String path, String message);
/** /**
* Asserts that object specified by path is null. If it is not, an AssertionError * Asserts that object specified by path is null. If it is not, an AssertionError
@ -55,6 +68,7 @@ public interface JsonAsserter {
* @return this to allow fluent assertion chains * @return this to allow fluent assertion chains
*/ */
JsonAsserter assertNull(String path); JsonAsserter assertNull(String path);
JsonAsserter assertNull(String path, String message);
/** /**
* Asserts that object specified by path is NOT null. If it is, an AssertionError * Asserts that object specified by path is NOT null. If it is, an AssertionError
@ -65,6 +79,8 @@ public interface JsonAsserter {
*/ */
<T> JsonAsserter assertNotNull(String path); <T> JsonAsserter assertNotNull(String path);
<T> JsonAsserter assertNotNull(String path, String message);
/** /**
* Syntactic sugar to allow chaining assertions with a separating and() statement * Syntactic sugar to allow chaining assertions with a separating and() statement
* <p/> * <p/>

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

@ -38,7 +38,19 @@ public class JsonAsserterImpl implements JsonAsserter {
T obj = JsonPath.<T>read(jsonObject, path); T obj = JsonPath.<T>read(jsonObject, path);
if (!matcher.matches(obj)) { if (!matcher.matches(obj)) {
throw new AssertionError(String.format("JSON doesn't match.\nExpected:\n%s\nActual:\n%s", matcher.toString(), obj)); throw new AssertionError(String.format("JSON path [%s] doesn't match.\nExpected:\n%s\nActual:\n%s", path, matcher.toString(), obj));
}
return this;
}
/**
* {@inheritDoc}
*/
@SuppressWarnings("unchecked")
public <T> JsonAsserter assertThat(String path, Matcher<T> matcher, String message) {
T obj = JsonPath.<T>read(jsonObject, path);
if (!matcher.matches(obj)) {
throw new AssertionError(String.format("JSON Assert Error: %s\nExpected:\n%s\nActual:\n%s", message, matcher.toString(), obj));
} }
return this; return this;
} }
@ -63,6 +75,16 @@ public class JsonAsserterImpl implements JsonAsserter {
return this; return this;
} }
@Override
public JsonAsserter assertNotDefined(String path, String message) {
try {
Object o = JsonPath.read(jsonObject, path);
throw new AssertionError(format("Document contains the path <%s> but was expected not to.", path));
} catch (InvalidPathException e) {
}
return this;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -70,6 +92,16 @@ public class JsonAsserterImpl implements JsonAsserter {
return assertThat(path, nullValue()); return assertThat(path, nullValue());
} }
@Override
public JsonAsserter assertNull(String path, String message) {
return assertThat(path, nullValue(), message);
}
@Override
public <T> JsonAsserter assertEquals(String path, T expected, String message) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -77,6 +109,11 @@ public class JsonAsserterImpl implements JsonAsserter {
return assertThat(path, notNullValue()); return assertThat(path, notNullValue());
} }
@Override
public <T> JsonAsserter assertNotNull(String path, String message) {
return assertThat(path, notNullValue(), message);
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

11
json-path/pom.xml

@ -34,8 +34,8 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang3</artifactId>
</dependency> </dependency>
<dependency> <dependency>
@ -60,6 +60,13 @@
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>

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

@ -18,8 +18,8 @@ import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.apache.commons.lang.Validate.notEmpty; import static org.apache.commons.lang3.Validate.notEmpty;
import static org.apache.commons.lang.Validate.notNull; import static org.apache.commons.lang3.Validate.notNull;
/** /**
* @author Kalle Stenflo * @author Kalle Stenflo

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

@ -16,6 +16,8 @@ package com.jayway.jsonpath;
import java.util.*; import java.util.*;
import com.jayway.jsonpath.spi.JsonProvider;
/** /**
* A filter is used to filter the content of a JSON array in a JSONPath. * A filter is used to filter the content of a JSON array in a JSONPath.
* *
@ -45,10 +47,11 @@ public abstract class Filter<T> {
/** /**
* Filters the provided list based on this filter configuration * Filters the provided list based on this filter configuration
* @param filterItems items to filter * @param filterItems items to filter
* @param jsonProvider the json provider that is used to create the result list
* @return the filtered list * @return the filtered list
*/ */
public List<T> doFilter(List<T> filterItems) { public List doFilter(List<T> filterItems, JsonProvider jsonProvider) {
List<T> result = new ArrayList<T>(); List result = jsonProvider.createList();;
for (T filterItem : filterItems) { for (T filterItem : filterItems) {
if (accept(filterItem)) { if (accept(filterItem)) {
result.add(filterItem); result.add(filterItem);

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

@ -28,7 +28,7 @@ import java.net.URL;
import java.util.*; import java.util.*;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.apache.commons.lang.Validate.*; import static org.apache.commons.lang3.Validate.*;
/** /**
* A JsonModel holds a parsed JSON document and provides easy read and write operations. In contrast to the * A JsonModel holds a parsed JSON document and provides easy read and write operations. In contrast to the

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

@ -22,7 +22,7 @@ import com.jayway.jsonpath.internal.filter.PathTokenFilter;
import com.jayway.jsonpath.spi.HttpProviderFactory; import com.jayway.jsonpath.spi.HttpProviderFactory;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import com.jayway.jsonpath.spi.JsonProviderFactory; import com.jayway.jsonpath.spi.JsonProviderFactory;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.io.*; import java.io.*;
import java.net.URL; import java.net.URL;
@ -32,7 +32,7 @@ import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.apache.commons.lang.Validate.*; import static org.apache.commons.lang3.Validate.*;
/** /**
* <p/> * <p/>

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java

@ -34,7 +34,7 @@ public class ArrayQueryFilter extends PathTokenFilter {
Filter filter = filters.poll(); Filter filter = filters.poll();
return filter.doFilter((List)obj); return filter.doFilter(jsonProvider.toList(obj), jsonProvider);
} }

9
json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java

@ -90,6 +90,15 @@ public class ExpressionEvaluator {
} else if ("!=".equals(comparator) || "<>".equals(comparator)) { } else if ("!=".equals(comparator) || "<>".equals(comparator)) {
return !a.equals(expected); return !a.equals(expected);
} }
} else if (actual instanceof Boolean) {
Boolean a = (Boolean) actual;
Boolean e = Boolean.valueOf(expected);
if ("==".equals(comparator)) {
return a.equals(e);
} else if ("!=".equals(comparator) || "<>".equals(comparator)) {
return !a.equals(e);
}
} }
return false; return false;

2
json-path/src/main/java/com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java

@ -15,7 +15,7 @@
package com.jayway.jsonpath.spi.impl; package com.jayway.jsonpath.spi.impl;
import com.jayway.jsonpath.spi.JsonProvider; import com.jayway.jsonpath.spi.JsonProvider;
import org.apache.commons.lang.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;

18
json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java

@ -60,5 +60,23 @@ public class ExpressionEvalTest {
} }
@Test
public void boolean_eval() throws Exception {
assertTrue(ExpressionEvaluator.eval(true, "==", "true"));
assertTrue(ExpressionEvaluator.eval(false, "==", "false"));
assertTrue(ExpressionEvaluator.eval(true, "!=", "false"));
assertTrue(ExpressionEvaluator.eval(true, "<>", "false"));
assertTrue(ExpressionEvaluator.eval(false, "!=", "true"));
assertTrue(ExpressionEvaluator.eval(false, "<>", "true"));
assertFalse(ExpressionEvaluator.eval(true, "==", "false"));
assertFalse(ExpressionEvaluator.eval(false, "==", "true"));
assertFalse(ExpressionEvaluator.eval(true, "!=", "true"));
assertFalse(ExpressionEvaluator.eval(true, "<>", "true"));
assertFalse(ExpressionEvaluator.eval(false, "!=", "false"));
assertFalse(ExpressionEvaluator.eval(false, "<>", "false"));
}
} }

2
json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java

@ -33,7 +33,7 @@ public class HttpProviderTest {
try { try {
inputStream = HttpProviderFactory.getProvider().get(url); inputStream = HttpProviderFactory.getProvider().get(url);
byte[] bytes = sun.misc.IOUtils.readFully(inputStream, -1, true); byte[] bytes = org.apache.commons.io.IOUtils.toByteArray(inputStream);
String json = new String(bytes).trim(); String json = new String(bytes).trim();

2
json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java

@ -1,7 +1,7 @@
package com.jayway.jsonpath; package com.jayway.jsonpath;
import com.jayway.jsonpath.spi.impl.JacksonProvider; import com.jayway.jsonpath.spi.impl.JacksonProvider;
import org.apache.commons.lang.SerializationUtils; import org.apache.commons.lang3.SerializationUtils;
import org.junit.Test; import org.junit.Test;
import java.io.Serializable; import java.io.Serializable;

15
pom.xml

@ -24,7 +24,7 @@
<groupId>com.jayway.jsonpath</groupId> <groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-parent</artifactId> <artifactId>json-path-parent</artifactId>
<packaging>pom</packaging> <packaging>pom</packaging>
<version>0.8.2-SNAPSHOT</version> <version>0.8.1-HBO</version>
<url>https://github.com/jayway/JsonPath</url> <url>https://github.com/jayway/JsonPath</url>
<name>json-path-parent-pom</name> <name>json-path-parent-pom</name>
<description>Java JsonPath implementation</description> <description>Java JsonPath implementation</description>
@ -215,9 +215,9 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>commons-lang</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang</artifactId> <artifactId>commons-lang3</artifactId>
<version>2.6</version> <version>3.1</version>
</dependency> </dependency>
<dependency> <dependency>
@ -245,6 +245,13 @@
<version>4.10</version> <version>4.10</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
</project> </project>
Loading…
Cancel
Save