From 69e55edfc82ba0351641d0f0f9084714522067fe Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Tue, 21 May 2013 13:56:16 +0200 Subject: [PATCH 1/7] update commons-lang to version 3.1 --- json-path/pom.xml | 4 ++-- json-path/src/main/java/com/jayway/jsonpath/Criteria.java | 4 ++-- json-path/src/main/java/com/jayway/jsonpath/JsonModel.java | 2 +- json-path/src/main/java/com/jayway/jsonpath/JsonPath.java | 4 ++-- .../com/jayway/jsonpath/spi/impl/AbstractJsonProvider.java | 2 +- .../src/test/java/com/jayway/jsonpath/JsonProviderTest.java | 2 +- pom.xml | 6 +++--- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/json-path/pom.xml b/json-path/pom.xml index 8f06999d..4d534b4e 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -34,8 +34,8 @@ - commons-lang - commons-lang + org.apache.commons + commons-lang3 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/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/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/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..87ea1607 100644 --- a/pom.xml +++ b/pom.xml @@ -215,9 +215,9 @@ - commons-lang - commons-lang - 2.6 + org.apache.commons + commons-lang3 + 3.1 From 7ec75fa147fbb1074dfeb945868956e239b053c8 Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Tue, 21 May 2013 14:04:33 +0200 Subject: [PATCH 2/7] replace usage of restricted class sun.misc.IOUtils by org.apache.commons.io.IOUtils --- json-path/pom.xml | 7 +++++++ .../test/java/com/jayway/jsonpath/HttpProviderTest.java | 2 +- pom.xml | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/json-path/pom.xml b/json-path/pom.xml index 8f06999d..6b3abd66 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -60,6 +60,13 @@ junit test + + + org.apache.commons + commons-io + test + + 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/pom.xml b/pom.xml index e3c9bb00..3dee7bac 100644 --- a/pom.xml +++ b/pom.xml @@ -245,6 +245,13 @@ 4.10 test + + + org.apache.commons + commons-io + 1.3.2 + test + \ No newline at end of file From 5450ed4d110c8858135c33a20653279a80743cdd Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Tue, 21 May 2013 14:54:39 +0200 Subject: [PATCH 3/7] update commons-io to 2.4 --- json-path/pom.xml | 2 +- pom.xml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/json-path/pom.xml b/json-path/pom.xml index 6b3abd66..384c1668 100644 --- a/json-path/pom.xml +++ b/json-path/pom.xml @@ -62,7 +62,7 @@ - org.apache.commons + commons-io commons-io test diff --git a/pom.xml b/pom.xml index 3dee7bac..8556a67b 100644 --- a/pom.xml +++ b/pom.xml @@ -247,9 +247,9 @@ - org.apache.commons + commons-io commons-io - 1.3.2 + 2.4 test From 7c848382399dbf6260c155c66937da44f2ce554e Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Thu, 23 May 2013 09:46:16 +0200 Subject: [PATCH 4/7] use the json provider to create a new list for the results --- json-path/src/main/java/com/jayway/jsonpath/Filter.java | 7 +++++-- .../jayway/jsonpath/internal/filter/ArrayQueryFilter.java | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) 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/internal/filter/ArrayQueryFilter.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java index 80892f78..8ded0836 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((List)obj, jsonProvider); } From 3cd4c3c2aab4c87388ee0e609282eace6fd5c7d8 Mon Sep 17 00:00:00 2001 From: Jochen Berger Date: Fri, 24 May 2013 13:54:43 +0200 Subject: [PATCH 5/7] convert rather than cast to List --- .../com/jayway/jsonpath/internal/filter/ArrayQueryFilter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8ded0836..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, jsonProvider); + return filter.doFilter(jsonProvider.toList(obj), jsonProvider); } From a3b2684b6f84d7b2ebc246939bdfd2e8ead84a75 Mon Sep 17 00:00:00 2001 From: Dmytro Kostiuchenko Date: Sun, 26 May 2013 00:18:57 +0300 Subject: [PATCH 6/7] Support for Boolean values in ExpressionEvaluator --- .../filter/eval/ExpressionEvaluator.java | 9 +++++++++ .../jayway/jsonpath/ExpressionEvalTest.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) 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/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")); + + } } From f466a0b6c5c651df785c3965b8f0cd60dd46289f Mon Sep 17 00:00:00 2001 From: Sarath Chandra Date: Tue, 25 Jun 2013 16:21:38 -0400 Subject: [PATCH 7/7] Add "message" param and print path (that failed) in asserts, the failure (default) messaging is inadequate. It doesnot even print the path that failed. this path should help eloberate the messaging --- .../com/jayway/jsonassert/JsonAsserter.java | 16 ++++++++ .../jsonassert/impl/JsonAsserterImpl.java | 39 ++++++++++++++++++- pom.xml | 2 +- 3 files changed, 55 insertions(+), 2 deletions(-) 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/pom.xml b/pom.xml index e3c9bb00..6fad2e95 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