Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.StringReader;
import java.lang.reflect.Field;

import com.jayway.jsonpath.Configuration;
import org.assertj.core.api.Assertions;

import org.springframework.beans.factory.ObjectFactory;
Expand Down Expand Up @@ -72,6 +73,8 @@ public abstract class AbstractJsonMarshalTester<T> {

private ResolvableType type;

private Configuration configuration;

/**
* Create a new uninitialized {@link AbstractJsonMarshalTester} instance.
*/
Expand All @@ -85,9 +88,22 @@ protected AbstractJsonMarshalTester() {
* @param type the type under test
*/
public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type) {
this(resourceLoadClass, type, Configuration.defaultConfiguration());
}

/**
* Create a new {@link AbstractJsonMarshalTester} instance.
* @param resourceLoadClass the source class used when loading relative classpath
* resources
* @param type the type under test
* @param configuration the json-path configuration
*/
public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type,
Configuration configuration) {
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
Assert.notNull(type, "Type must not be null");
initialize(resourceLoadClass, type);
Assert.notNull(configuration, "Configuration must not be null");
initialize(resourceLoadClass, type, configuration);
}

/**
Expand All @@ -97,9 +113,23 @@ public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type
* @param type the type under test
*/
protected final void initialize(Class<?> resourceLoadClass, ResolvableType type) {
if (this.resourceLoadClass == null && this.type == null) {
initialize(resourceLoadClass, type, Configuration.defaultConfiguration());
}

/**
* Initialize the marshal tester for use.
* @param resourceLoadClass the source class used when loading relative classpath
* resources
* @param type the type under test
* @param configuration the json-path configuration
*/
protected final void initialize(Class<?> resourceLoadClass, ResolvableType type,
Configuration configuration) {
if (this.resourceLoadClass == null && this.type == null
&& this.configuration == null) {
this.resourceLoadClass = resourceLoadClass;
this.type = type;
this.configuration = configuration;
}
}

Expand Down Expand Up @@ -129,7 +159,8 @@ public JsonContent<T> write(T value) throws IOException {
verify();
Assert.notNull(value, "Value must not be null");
String json = writeObject(value, this.type);
return new JsonContent<>(this.resourceLoadClass, this.type, json);
return new JsonContent<>(this.resourceLoadClass, this.type, json,
this.configuration);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.io.InputStream;
import java.nio.charset.Charset;

import com.jayway.jsonpath.Configuration;

import org.springframework.core.io.Resource;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -49,6 +51,8 @@ public class BasicJsonTester {

private JsonLoader loader;

private Configuration configuration;

/**
* Create a new uninitialized {@link BasicJsonTester} instance.
*/
Expand All @@ -70,8 +74,21 @@ public BasicJsonTester(Class<?> resourceLoadClass) {
* @since 1.4.1
*/
public BasicJsonTester(Class<?> resourceLoadClass, Charset charset) {
this(resourceLoadClass, charset, Configuration.defaultConfiguration());
}

/**
* Create a new {@link BasicJsonTester} instance.
* @param resourceLoadClass the source class used to load resources
* @param charset the charset used to load resources
* @param configuration the json-path configuration
*/
public BasicJsonTester(Class<?> resourceLoadClass, Charset charset,
Configuration configuration) {
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
Assert.notNull(configuration, "Configuration must not be null");
this.loader = new JsonLoader(resourceLoadClass, charset);
this.configuration = configuration;
}

/**
Expand All @@ -92,8 +109,22 @@ protected final void initialize(Class<?> resourceLoadClass) {
* @since 1.4.1
*/
protected final void initialize(Class<?> resourceLoadClass, Charset charset) {
if (this.loader == null) {
initialize(resourceLoadClass, charset, Configuration.defaultConfiguration());
}

/**
* Initialize the marshal tester for use.
* @param resourceLoadClass the source class used when loading relative classpath
* resources
* @param charset the charset used when loading relative classpath resources
* @param configuration the json-path configuration
* @since
*/
protected final void initialize(Class<?> resourceLoadClass, Charset charset,
Configuration configuration) {
if (this.loader == null && this.configuration == null) {
this.loader = new JsonLoader(resourceLoadClass, charset);
this.configuration = configuration;
}
}

Expand Down Expand Up @@ -165,7 +196,8 @@ private void verify() {
}

private JsonContent<Object> getJsonContent(String json) {
return new JsonContent<>(this.loader.getResourceLoadClass(), null, json);
return new JsonContent<>(this.loader.getResourceLoadClass(), null, json,
this.configuration);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.Reader;

import com.google.gson.Gson;
import com.jayway.jsonpath.Configuration;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.core.ResolvableType;
Expand Down Expand Up @@ -74,7 +75,20 @@ protected GsonTester(Gson gson) {
* @see #initFields(Object, Gson)
*/
public GsonTester(Class<?> resourceLoadClass, ResolvableType type, Gson gson) {
super(resourceLoadClass, type);
this(resourceLoadClass, type, gson, Configuration.defaultConfiguration());
}

/**
* Create a new {@link GsonTester} instance.
* @param resourceLoadClass the source class used to load resources
* @param type the type under test
* @param gson the Gson instance
* @param configuration the json-path configuration
* @see #initFields(Object, Gson)
*/
public GsonTester(Class<?> resourceLoadClass, ResolvableType type, Gson gson,
Configuration configuration) {
super(resourceLoadClass, type, configuration);
Assert.notNull(gson, "Gson must not be null");
this.gson = gson;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.core.ResolvableType;
Expand Down Expand Up @@ -86,7 +89,9 @@ public JacksonTester(Class<?> resourceLoadClass, ResolvableType type,

public JacksonTester(Class<?> resourceLoadClass, ResolvableType type,
ObjectMapper objectMapper, Class<?> view) {
super(resourceLoadClass, type);
super(resourceLoadClass, type, Configuration.builder()
.jsonProvider(new JacksonJsonProvider(objectMapper))
.mappingProvider(new JacksonMappingProvider(objectMapper)).build());
Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.objectMapper = objectMapper;
this.view = view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.test.json;

import com.jayway.jsonpath.Configuration;
import org.assertj.core.api.AssertProvider;

import org.springframework.core.ResolvableType;
Expand All @@ -38,18 +39,34 @@ public final class JsonContent<T> implements AssertProvider<JsonContentAssert> {

private final String json;

private final Configuration configuration;

/**
* Create a new {@link JsonContent} instance.
* @param resourceLoadClass the source class used to load resources
* @param type the type under test (or {@code null} if not known)
* @param json the actual JSON content
*/
public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json) {
this(resourceLoadClass, type, json, Configuration.defaultConfiguration());
}

/**
* Create a new {@link JsonContent} instance.
* @param resourceLoadClass the source class used to load resources
* @param type the type under test (or {@code null} if not known)
* @param json the actual JSON content
* @param configuration the json-path configuration
*/
public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json,
Configuration configuration) {
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
Assert.notNull(json, "JSON must not be null");
Assert.notNull(configuration, "Configuration must not be null");
this.resourceLoadClass = resourceLoadClass;
this.type = type;
this.json = json;
this.configuration = configuration;
}

/**
Expand All @@ -61,7 +78,8 @@ public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json)
@Override
@Deprecated
public JsonContentAssert assertThat() {
return new JsonContentAssert(this.resourceLoadClass, this.json);
return new JsonContentAssert(this.resourceLoadClass, null, this.json,
this.configuration);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;

import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractBooleanAssert;
Expand Down Expand Up @@ -52,6 +53,8 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq

private final JsonLoader loader;

private final Configuration configuration;

/**
* Create a new {@link JsonContentAssert} instance that will load resources as UTF-8.
* @param resourceLoadClass the source class used to load resources
Expand All @@ -71,7 +74,21 @@ public JsonContentAssert(Class<?> resourceLoadClass, CharSequence json) {
*/
public JsonContentAssert(Class<?> resourceLoadClass, Charset charset,
CharSequence json) {
this(resourceLoadClass, charset, json, Configuration.defaultConfiguration());
}

/**
* Create a new {@link JsonContentAssert} instance that will load resources in the
* given {@code charset}.
* @param resourceLoadClass the source class used to load resources
* @param charset the charset of the JSON resources
* @param json the actual JSON content
* @param configuration the json-path configuration
*/
public JsonContentAssert(Class<?> resourceLoadClass, Charset charset,
CharSequence json, Configuration configuration) {
super(json, JsonContentAssert.class);
this.configuration = configuration;
this.loader = new JsonLoader(resourceLoadClass, charset);
}

Expand Down Expand Up @@ -1110,7 +1127,8 @@ private boolean isEmpty() {
public Object getValue(boolean required) {
try {
CharSequence json = JsonContentAssert.this.actual;
return this.jsonPath.read((json != null) ? json.toString() : null);
return this.jsonPath.read((json != null) ? json.toString() : null,
JsonContentAssert.this.configuration);
}
catch (Exception ex) {
if (required) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import javax.json.bind.Jsonb;

import com.jayway.jsonpath.Configuration;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.core.ResolvableType;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -74,7 +76,20 @@ protected JsonbTester(Jsonb jsonb) {
* @see #initFields(Object, Jsonb)
*/
public JsonbTester(Class<?> resourceLoadClass, ResolvableType type, Jsonb jsonb) {
super(resourceLoadClass, type);
this(resourceLoadClass, type, jsonb, Configuration.defaultConfiguration());
}

/**
* Create a new {@link JsonbTester} instance.
* @param resourceLoadClass the source class used to load resources
* @param type the type under test
* @param jsonb the Jsonb instance
* @param configuration the json-path configuration
* @see #initFields(Object, Jsonb)
*/
public JsonbTester(Class<?> resourceLoadClass, ResolvableType type, Jsonb jsonb,
Configuration configuration) {
super(resourceLoadClass, type, configuration);
Assert.notNull(jsonb, "Jsonb must not be null");
this.jsonb = jsonb;
}
Expand Down
Loading