diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java index 9cff5bec..50092234 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/JsonAsserter.java @@ -26,6 +26,15 @@ public interface JsonAsserter { */ JsonAsserter assertThat(String path, Matcher matcher); + /** + * @param path + * @param matcher + * @param message + * @param + * @return + */ + JsonAsserter assertThat(String path, Matcher matcher, String message); + /** * Asserts that object specified by path is equal to the expected value. * If they are not, an AssertionError is thrown with the given message. @@ -37,6 +46,8 @@ public interface JsonAsserter { */ JsonAsserter assertEquals(String path, T expected); + JsonAsserter assertEquals(String path, T expected, String message); + /** * Checks that a path is not defined within a document. If the document contains the * given path, an AssertionError is thrown @@ -46,6 +57,8 @@ public interface JsonAsserter { */ JsonAsserter assertNotDefined(String path); + JsonAsserter assertNotDefined(String path, String message); + /** * 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 */ JsonAsserter assertNull(String path); + JsonAsserter assertNull(String path, String message); /** * Asserts that object specified by path is NOT null. If it is, an AssertionError @@ -65,6 +79,8 @@ public interface JsonAsserter { */ JsonAsserter assertNotNull(String path); + JsonAsserter assertNotNull(String path, String message); + /** * Syntactic sugar to allow chaining assertions with a separating and() statement *

diff --git a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java index 2342336a..54e86f14 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java +++ b/json-path-assert/src/main/java/com/jayway/jsonassert/impl/JsonAsserterImpl.java @@ -38,7 +38,19 @@ public class JsonAsserterImpl implements JsonAsserter { T obj = JsonPath.read(jsonObject, path); 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 JsonAsserter assertThat(String path, Matcher matcher, String message) { + T obj = JsonPath.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; } @@ -63,6 +75,16 @@ public class JsonAsserterImpl implements JsonAsserter { 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} */ @@ -70,6 +92,16 @@ public class JsonAsserterImpl implements JsonAsserter { return assertThat(path, nullValue()); } + @Override + public JsonAsserter assertNull(String path, String message) { + return assertThat(path, nullValue(), message); + } + + @Override + public JsonAsserter assertEquals(String path, T expected, String message) { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + /** * {@inheritDoc} */ @@ -77,6 +109,11 @@ public class JsonAsserterImpl implements JsonAsserter { return assertThat(path, notNullValue()); } + @Override + public JsonAsserter assertNotNull(String path, String message) { + return assertThat(path, notNullValue(), message); + } + /** * {@inheritDoc} */ diff --git a/json-path/pom.xml b/json-path/pom.xml index 8f06999d..6e1edb9e 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -34,8 +34,8 @@ - commons-lang - commons-lang + org.apache.commons + commons-lang3 @@ -60,6 +60,13 @@ junit test + + + commons-io + commons-io + test + + diff --git a/json-path/src/main/java/com/jayway/jsonpath/Criteria.java b/json-path/src/main/java/com/jayway/jsonpath/Criteria.java index 222c9dfa..8e67cf10 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Criteria.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Criteria.java @@ -18,8 +18,8 @@ import java.util.*; import java.util.regex.Pattern; import static java.util.Arrays.asList; -import static org.apache.commons.lang.Validate.notEmpty; -import static org.apache.commons.lang.Validate.notNull; +import static org.apache.commons.lang3.Validate.notEmpty; +import static org.apache.commons.lang3.Validate.notNull; /** * @author Kalle Stenflo diff --git a/json-path/src/main/java/com/jayway/jsonpath/Filter.java b/json-path/src/main/java/com/jayway/jsonpath/Filter.java index 9de183bf..2b81e8b6 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Filter.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Filter.java @@ -16,6 +16,8 @@ package com.jayway.jsonpath; import java.util.*; +import com.jayway.jsonpath.spi.JsonProvider; + /** * A filter is used to filter the content of a JSON array in a JSONPath. * @@ -45,10 +47,11 @@ public abstract class Filter { /** * Filters the provided list based on this filter configuration * @param filterItems items to filter + * @param jsonProvider the json provider that is used to create the result list * @return the filtered list */ - public List doFilter(List filterItems) { - List result = new ArrayList(); + public List doFilter(List filterItems, JsonProvider jsonProvider) { + List result = jsonProvider.createList();; for (T filterItem : filterItems) { if (accept(filterItem)) { result.add(filterItem); diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java index 5981c760..e9971b0a 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonModel.java @@ -28,7 +28,7 @@ import java.net.URL; import java.util.*; 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 diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index 5af8a171..cb708788 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/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.JsonProvider; import com.jayway.jsonpath.spi.JsonProviderFactory; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import java.io.*; import java.net.URL; @@ -32,7 +32,7 @@ import java.util.Map; import java.util.regex.Pattern; import static java.util.Arrays.asList; -import static org.apache.commons.lang.Validate.*; +import static org.apache.commons.lang3.Validate.*; /** *

diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java index 80892f78..35044a45 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java +++ b/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(); - return filter.doFilter((List)obj); + return filter.doFilter(jsonProvider.toList(obj), jsonProvider); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java index 36bf57c8..e6077537 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/eval/ExpressionEvaluator.java +++ b/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)) { 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; diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java index 10951b84..c0abb31e 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java @@ -15,7 +15,7 @@ package com.jayway.jsonpath.spi.impl; import com.jayway.jsonpath.spi.JsonProvider; -import org.apache.commons.lang.SerializationUtils; +import org.apache.commons.lang3.SerializationUtils; import java.io.Serializable; import java.util.List; diff --git a/json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java b/json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java index 8ee09297..be23e647 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/ExpressionEvalTest.java +++ b/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")); + + } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java b/json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java index 1b732ef6..5e238637 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java @@ -33,7 +33,7 @@ public class HttpProviderTest { try { 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(); diff --git a/json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java b/json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java index c87d56b0..97949795 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/JsonProviderTest.java @@ -1,7 +1,7 @@ package com.jayway.jsonpath; import com.jayway.jsonpath.spi.impl.JacksonProvider; -import org.apache.commons.lang.SerializationUtils; +import org.apache.commons.lang3.SerializationUtils; import org.junit.Test; import java.io.Serializable; diff --git a/pom.xml b/pom.xml index e3c9bb00..b8cdc020 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ com.jayway.jsonpath json-path-parent pom - 0.8.2-SNAPSHOT + 0.8.1-HBO https://github.com/jayway/JsonPath json-path-parent-pom Java JsonPath implementation @@ -215,9 +215,9 @@ - commons-lang - commons-lang - 2.6 + org.apache.commons + commons-lang3 + 3.1 @@ -245,6 +245,13 @@ 4.10 test + + + commons-io + commons-io + 2.4 + test + \ No newline at end of file