Browse Source

[#119] Some minor refactoring to reduce amount of code, and collect the factory methods in JsonPathMatchers

pull/165/head
Patrik Helsing 9 years ago
parent
commit
3b273503ff
  1. 6
      json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java
  2. 17
      json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonFile.java
  3. 15
      json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonString.java
  4. 35
      json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java
  5. 25
      json-path-assert/src/main/java/com/jayway/jsonpath/matchers/WithJsonPath.java
  6. 5
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/DemoTest.java
  7. 2
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonFileTest.java
  8. 2
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonStringTest.java
  9. 2
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/IsJsonTest.java
  10. 10
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/JsonPathMatchersTest.java
  11. 2
      json-path-assert/src/test/java/com/jayway/jsonpath/matchers/WithJsonPathTest.java

6
json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJson.java

@ -13,14 +13,10 @@ import java.io.IOException;
public class IsJson<T> extends TypeSafeMatcher<T> { public class IsJson<T> extends TypeSafeMatcher<T> {
private final Matcher<? super ReadContext> jsonMatcher; private final Matcher<? super ReadContext> jsonMatcher;
protected IsJson(Matcher<? super ReadContext> jsonMatcher) { public IsJson(Matcher<? super ReadContext> jsonMatcher) {
this.jsonMatcher = jsonMatcher; this.jsonMatcher = jsonMatcher;
} }
public static Matcher<Object> isJson(final Matcher<? super ReadContext> matcher) {
return new IsJson(matcher);
}
@Override @Override
protected boolean matchesSafely(T json) { protected boolean matchesSafely(T json) {
try { try {

17
json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonFile.java

@ -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<File> {
IsJsonFile(Matcher<? super ReadContext> jsonMatcher) {
super(jsonMatcher);
}
public static Matcher<File> isJsonFile(final Matcher<? super ReadContext> matcher) {
return new IsJsonFile(matcher);
}
}

15
json-path-assert/src/main/java/com/jayway/jsonpath/matchers/IsJsonString.java

@ -1,15 +0,0 @@
package com.jayway.jsonpath.matchers;
import com.jayway.jsonpath.ReadContext;
import org.hamcrest.Matcher;
public class IsJsonString extends IsJson<String> {
IsJsonString(Matcher<? super ReadContext> jsonMatcher) {
super(jsonMatcher);
}
public static Matcher<String> isJsonString(final Matcher<? super ReadContext> matcher) {
return new IsJsonString(matcher);
}
}

35
json-path-assert/src/main/java/com/jayway/jsonpath/matchers/JsonPathMatchers.java

@ -1,9 +1,12 @@
package com.jayway.jsonpath.matchers; package com.jayway.jsonpath.matchers;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.ReadContext; import com.jayway.jsonpath.ReadContext;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import static com.jayway.jsonpath.matchers.WithJsonPath.withJsonPath; import java.io.File;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
public class JsonPathMatchers { public class JsonPathMatchers {
@ -17,14 +20,38 @@ public class JsonPathMatchers {
} }
public static <T> Matcher<? super Object> hasJsonPath(final String jsonPath, final Matcher<T> resultMatcher) { public static <T> Matcher<? super Object> hasJsonPath(final String jsonPath, final Matcher<T> resultMatcher) {
return IsJson.isJson(withJsonPath(jsonPath, resultMatcher)); return isJson(withJsonPath(jsonPath, resultMatcher));
} }
public static Matcher<Object> isJson() { public static Matcher<Object> isJson() {
return IsJson.isJson(withJsonPath("$..*")); return isJson(withJsonPath("$..*"));
} }
public static Matcher<Object> isJson(final Matcher<? super ReadContext> matcher) { public static Matcher<Object> isJson(final Matcher<? super ReadContext> matcher) {
return IsJson.isJson(matcher); return new IsJson<Object>(matcher);
}
public static Matcher<String> isJsonString(final Matcher<? super ReadContext> matcher) {
return new IsJson<String>(matcher);
}
public static Matcher<File> isJsonFile(final Matcher<? super ReadContext> matcher) {
return new IsJson<File>(matcher);
}
public static Matcher<? super ReadContext> withJsonPath(String jsonPath, Predicate... filters) {
return withJsonPath(JsonPath.compile(jsonPath, filters));
}
public static Matcher<? super ReadContext> withJsonPath(JsonPath jsonPath) {
return withJsonPath(jsonPath, not(anyOf(nullValue(), empty())));
}
public static <T> Matcher<? super ReadContext> withJsonPath(String jsonPath, Matcher<T> resultMatcher) {
return withJsonPath(JsonPath.compile(jsonPath), resultMatcher);
}
public static <T> Matcher<? super ReadContext> withJsonPath(final JsonPath jsonPath, final Matcher<T> resultMatcher) {
return new WithJsonPath<T>(jsonPath, resultMatcher);
} }
} }

25
json-path-assert/src/main/java/com/jayway/jsonpath/matchers/WithJsonPath.java

@ -1,37 +1,22 @@
package com.jayway.jsonpath.matchers; 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.Description;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher; import org.hamcrest.TypeSafeMatcher;
import static org.hamcrest.Matchers.*;
public class WithJsonPath<T> extends TypeSafeMatcher<ReadContext> { public class WithJsonPath<T> extends TypeSafeMatcher<ReadContext> {
private final JsonPath jsonPath; private final JsonPath jsonPath;
private final Matcher<T> resultMatcher; private final Matcher<T> resultMatcher;
private WithJsonPath(JsonPath jsonPath, Matcher<T> resultMatcher) { public WithJsonPath(JsonPath jsonPath, Matcher<T> resultMatcher) {
this.jsonPath = jsonPath; this.jsonPath = jsonPath;
this.resultMatcher = resultMatcher; this.resultMatcher = resultMatcher;
} }
public static Matcher<? super ReadContext> withJsonPath(String jsonPath, Predicate... filters) {
return withJsonPath(JsonPath.compile(jsonPath, filters));
}
public static Matcher<? super ReadContext> withJsonPath(JsonPath jsonPath) {
return withJsonPath(jsonPath, not(anyOf(nullValue(), empty())));
}
public static <T> Matcher<? super ReadContext> withJsonPath(String jsonPath, Matcher<T> resultMatcher) {
return withJsonPath(JsonPath.compile(jsonPath), resultMatcher);
}
public static <T> Matcher<? super ReadContext> withJsonPath(final JsonPath jsonPath, final Matcher<T> resultMatcher) {
return new WithJsonPath<T>(jsonPath, resultMatcher);
}
@Override @Override
protected boolean matchesSafely(ReadContext context) { protected boolean matchesSafely(ReadContext context) {
try { try {

5
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 java.io.File;
import static com.jayway.jsonpath.matchers.IsJsonFile.isJsonFile; import static com.jayway.jsonpath.matchers.JsonPathMatchers.*;
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.helpers.ResourceHelpers.resource; 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.ResourceHelpers.resourceAsFile;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;

2
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 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.ResourceHelpers.resourceAsFile;
import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*; import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;

2
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.BeforeClass;
import org.junit.Test; 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.ResourceHelpers.resource;
import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*; import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.*;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;

2
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 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.resource;
import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile; import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resourceAsFile;
import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.withPathEvaluatedTo; import static com.jayway.jsonpath.matchers.helpers.TestingMatchers.withPathEvaluatedTo;

10
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.nio.file.Paths;
import java.util.Collection; import java.util.Collection;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath; import static com.jayway.jsonpath.matchers.JsonPathMatchers.*;
import static com.jayway.jsonpath.matchers.JsonPathMatchers.isJson;
import static com.jayway.jsonpath.matchers.WithJsonPath.withJsonPath;
import static com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource; 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.ResourceHelpers.resourceAsFile;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
@ -127,4 +125,10 @@ public class JsonPathMatchersTest {
File nonExistingFile = Paths.get("missing-file").toFile(); File nonExistingFile = Paths.get("missing-file").toFile();
assertThat(nonExistingFile, not(hasJsonPath("$..*", anything()))); 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")));
}
} }

2
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 java.util.Collection;
import static com.jayway.jsonpath.JsonPath.compile; 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 com.jayway.jsonpath.matchers.helpers.ResourceHelpers.resource;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;

Loading…
Cancel
Save