diff --git a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java index 9ee6681f..c46eb07c 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java +++ b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java @@ -13,14 +13,10 @@ import java.io.IOException; public class IsJson extends TypeSafeMatcher { private final Matcher jsonMatcher; - protected IsJson(Matcher jsonMatcher) { + public IsJson(Matcher jsonMatcher) { this.jsonMatcher = jsonMatcher; } - public static Matcher isJson(final Matcher matcher) { - return new IsJson(matcher); - } - @Override protected boolean matchesSafely(T json) { try { diff --git a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonFile.java b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonFile.java deleted file mode 100644 index 28d87977..00000000 --- a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonFile.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.jayway.jsonpath.matchers; - -import com.jayway.jsonpath.ReadContext; -import org.hamcrest.Matcher; - -import java.io.File; - -public class IsJsonFile extends IsJson { - - IsJsonFile(Matcher jsonMatcher) { - super(jsonMatcher); - } - - public static Matcher isJsonFile(final Matcher matcher) { - return new IsJsonFile(matcher); - } -} diff --git a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonString.java b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonString.java deleted file mode 100644 index 94e5225b..00000000 --- a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonString.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.jayway.jsonpath.matchers; - -import com.jayway.jsonpath.ReadContext; -import org.hamcrest.Matcher; - -public class IsJsonString extends IsJson { - - IsJsonString(Matcher jsonMatcher) { - super(jsonMatcher); - } - - public static Matcher isJsonString(final Matcher matcher) { - return new IsJsonString(matcher); - } -} diff --git a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java index 0c2e85fd..5103e699 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java +++ b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java @@ -1,9 +1,12 @@ package com.jayway.jsonpath.matchers; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.Predicate; import com.jayway.jsonpath.ReadContext; import org.hamcrest.Matcher; -import static com.jayway.jsonpath.matchers.WithJsonPath.withJsonPath; +import java.io.File; + import static org.hamcrest.Matchers.*; public class JsonPathMatchers { @@ -17,14 +20,38 @@ public class JsonPathMatchers { } public static Matcher hasJsonPath(final String jsonPath, final Matcher resultMatcher) { - return IsJson.isJson(withJsonPath(jsonPath, resultMatcher)); + return isJson(withJsonPath(jsonPath, resultMatcher)); } public static Matcher isJson() { - return IsJson.isJson(withJsonPath("$..*")); + return isJson(withJsonPath("$..*")); } public static Matcher isJson(final Matcher matcher) { - return IsJson.isJson(matcher); + return new IsJson(matcher); + } + + public static Matcher isJsonString(final Matcher matcher) { + return new IsJson(matcher); + } + + public static Matcher isJsonFile(final Matcher matcher) { + return new IsJson(matcher); + } + + public static Matcher withJsonPath(String jsonPath, Predicate... filters) { + return withJsonPath(JsonPath.compile(jsonPath, filters)); + } + + public static Matcher withJsonPath(JsonPath jsonPath) { + return withJsonPath(jsonPath, not(anyOf(nullValue(), empty()))); + } + + public static Matcher withJsonPath(String jsonPath, Matcher resultMatcher) { + return withJsonPath(JsonPath.compile(jsonPath), resultMatcher); + } + + public static Matcher withJsonPath(final JsonPath jsonPath, final Matcher resultMatcher) { + return new WithJsonPath(jsonPath, resultMatcher); } } diff --git a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/WithJsonPath.java b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/WithJsonPath.java index 5c448898..73acaaad 100644 --- a/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/WithJsonPath.java +++ b/json-path-assert/src/main/java/com/jayway/jsonpath/matchers/WithJsonPath.java @@ -1,37 +1,22 @@ package com.jayway.jsonpath.matchers; -import com.jayway.jsonpath.*; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.JsonPathException; +import com.jayway.jsonpath.PathNotFoundException; +import com.jayway.jsonpath.ReadContext; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; -import static org.hamcrest.Matchers.*; - public class WithJsonPath extends TypeSafeMatcher { private final JsonPath jsonPath; private final Matcher resultMatcher; - private WithJsonPath(JsonPath jsonPath, Matcher resultMatcher) { + public WithJsonPath(JsonPath jsonPath, Matcher resultMatcher) { this.jsonPath = jsonPath; this.resultMatcher = resultMatcher; } - public static Matcher withJsonPath(String jsonPath, Predicate... filters) { - return withJsonPath(JsonPath.compile(jsonPath, filters)); - } - - public static Matcher withJsonPath(JsonPath jsonPath) { - return withJsonPath(jsonPath, not(anyOf(nullValue(), empty()))); - } - - public static Matcher withJsonPath(String jsonPath, Matcher resultMatcher) { - return withJsonPath(JsonPath.compile(jsonPath), resultMatcher); - } - - public static Matcher withJsonPath(final JsonPath jsonPath, final Matcher resultMatcher) { - return new WithJsonPath(jsonPath, resultMatcher); - } - @Override protected boolean matchesSafely(ReadContext context) { try { diff --git a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/DemoTest.java b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/DemoTest.java index f6d0a15c..8dfe8c07 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/DemoTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/DemoTest.java @@ -5,10 +5,7 @@ import org.junit.Test; import java.io.File; -import static com.jayway.jsonpath.matchers.IsJsonFile.isJsonFile; -import static com.jayway.jsonpath.matchers.IsJsonString.isJsonString; -import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson; -import static com.jayway.jsonpath.matchers.WithJsonPath.withJsonPath; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.*; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile; import static org.hamcrest.Matchers.equalTo; diff --git a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonFileTest.java b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonFileTest.java index 3acb484f..01afa415 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonFileTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonFileTest.java @@ -11,7 +11,7 @@ import org.junit.Test; import java.io.File; -import static com.jayway.jsonpath.matchers.IsJsonFile.isJsonFile; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJsonFile; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile; import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*; import static org.hamcrest.Matchers.*; diff --git a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonStringTest.java b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonStringTest.java index 4935c16f..272eb10b 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonStringTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonStringTest.java @@ -9,7 +9,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import static com.jayway.jsonpath.matchers.IsJsonString.isJsonString; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJsonString; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource; import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*; import static org.hamcrest.Matchers.*; diff --git a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonTest.java b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonTest.java index 4d29a43a..0d08aa02 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonTest.java @@ -12,7 +12,7 @@ import org.junit.Test; import java.io.File; -import static com.jayway.jsonpath.matchers.IsJson.isJson; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile; import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.withPathEvaluatedTo; diff --git a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java index 4967bce6..90ceac01 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java @@ -10,9 +10,7 @@ import java.io.File; import java.nio.file.Paths; import java.util.Collection; -import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; -import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson; -import static com.jayway.jsonpath.matchers.WithJsonPath.withJsonPath; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.*; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile; import static org.hamcrest.Matchers.*; @@ -127,4 +125,10 @@ public class JsonPathMatchersTest { File nonExistingFile = Paths.get("missing-file").toFile(); assertThat(nonExistingFile, not(hasJsonPath("$..*", anything()))); } + + @Test + public void shouldMatchJsonPathOnParsedJsonObject() { + Object json = Configuration.defaultConfiguration().jsonProvider().parse(BOOKS_JSON); + assertThat(json, hasJsonPath("$.store.name", equalTo("Little Shop"))); + } } diff --git a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/WithJsonPathTest.java b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/WithJsonPathTest.java index de4a45fa..7815d154 100644 --- a/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/WithJsonPathTest.java +++ b/json-path-assert/src/test/java/com/jayway/jsonpath/matchers/WithJsonPathTest.java @@ -11,7 +11,7 @@ import org.junit.Test; import java.util.Collection; import static com.jayway.jsonpath.JsonPath.compile; -import static com.jayway.jsonpath.matchers.WithJsonPath.withJsonPath; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.withJsonPath; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat;