Commit d4151e0b authored by Phillip Webb's avatar Phillip Webb

Polish "Use Jackson configuration with JsonPath"

Polish contribution to use a factory method in `AbstractJsonMarshalTester`
rather than additional constructor arguments.

Also change the `JsonContent` tests so that the `Configuration` constructor
is package private. This keeps JsonPath classes out of our public API, at
the expense of limiting custom JsonPath configurations to just our code.

See gh-16629
parent 756a7f12
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,7 +26,6 @@ import java.io.Reader; ...@@ -26,7 +26,6 @@ import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import com.jayway.jsonpath.Configuration;
import org.assertj.core.api.Assertions; import org.assertj.core.api.Assertions;
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectFactory;
...@@ -73,8 +72,6 @@ public abstract class AbstractJsonMarshalTester<T> { ...@@ -73,8 +72,6 @@ public abstract class AbstractJsonMarshalTester<T> {
private ResolvableType type; private ResolvableType type;
private Configuration configuration;
/** /**
* Create a new uninitialized {@link AbstractJsonMarshalTester} instance. * Create a new uninitialized {@link AbstractJsonMarshalTester} instance.
*/ */
...@@ -88,22 +85,9 @@ public abstract class AbstractJsonMarshalTester<T> { ...@@ -88,22 +85,9 @@ public abstract class AbstractJsonMarshalTester<T> {
* @param type the type under test * @param type the type under test
*/ */
public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type) { 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(resourceLoadClass, "ResourceLoadClass must not be null");
Assert.notNull(type, "Type must not be null"); Assert.notNull(type, "Type must not be null");
Assert.notNull(configuration, "Configuration must not be null"); initialize(resourceLoadClass, type);
initialize(resourceLoadClass, type, configuration);
} }
/** /**
...@@ -113,23 +97,9 @@ public abstract class AbstractJsonMarshalTester<T> { ...@@ -113,23 +97,9 @@ public abstract class AbstractJsonMarshalTester<T> {
* @param type the type under test * @param type the type under test
*/ */
protected final void initialize(Class<?> resourceLoadClass, ResolvableType type) { protected final void initialize(Class<?> resourceLoadClass, ResolvableType type) {
initialize(resourceLoadClass, type, Configuration.defaultConfiguration()); if (this.resourceLoadClass == null && this.type == null) {
}
/**
* 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.resourceLoadClass = resourceLoadClass;
this.type = type; this.type = type;
this.configuration = configuration;
} }
} }
...@@ -159,8 +129,18 @@ public abstract class AbstractJsonMarshalTester<T> { ...@@ -159,8 +129,18 @@ public abstract class AbstractJsonMarshalTester<T> {
verify(); verify();
Assert.notNull(value, "Value must not be null"); Assert.notNull(value, "Value must not be null");
String json = writeObject(value, this.type); String json = writeObject(value, this.type);
return new JsonContent<>(this.resourceLoadClass, this.type, json, return getJsonContent(json);
this.configuration); }
/**
* Factory method used to get a {@link JsonContent} instance from a source JSON
* string.
* @param json the source JSON
* @return a new {@link JsonContent} instance
* @since 2.1.5
*/
protected JsonContent<T> getJsonContent(String json) {
return new JsonContent<>(getResourceLoadClass(), getType(), json);
} }
/** /**
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -20,8 +20,6 @@ import java.io.File; ...@@ -20,8 +20,6 @@ import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import com.jayway.jsonpath.Configuration;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -51,8 +49,6 @@ public class BasicJsonTester { ...@@ -51,8 +49,6 @@ public class BasicJsonTester {
private JsonLoader loader; private JsonLoader loader;
private Configuration configuration;
/** /**
* Create a new uninitialized {@link BasicJsonTester} instance. * Create a new uninitialized {@link BasicJsonTester} instance.
*/ */
...@@ -74,21 +70,8 @@ public class BasicJsonTester { ...@@ -74,21 +70,8 @@ public class BasicJsonTester {
* @since 1.4.1 * @since 1.4.1
*/ */
public BasicJsonTester(Class<?> resourceLoadClass, Charset charset) { 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(resourceLoadClass, "ResourceLoadClass must not be null");
Assert.notNull(configuration, "Configuration must not be null");
this.loader = new JsonLoader(resourceLoadClass, charset); this.loader = new JsonLoader(resourceLoadClass, charset);
this.configuration = configuration;
} }
/** /**
...@@ -109,22 +92,8 @@ public class BasicJsonTester { ...@@ -109,22 +92,8 @@ public class BasicJsonTester {
* @since 1.4.1 * @since 1.4.1
*/ */
protected final void initialize(Class<?> resourceLoadClass, Charset charset) { protected final void initialize(Class<?> resourceLoadClass, Charset charset) {
initialize(resourceLoadClass, charset, Configuration.defaultConfiguration()); if (this.loader == null) {
}
/**
* 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.loader = new JsonLoader(resourceLoadClass, charset);
this.configuration = configuration;
} }
} }
...@@ -196,8 +165,7 @@ public class BasicJsonTester { ...@@ -196,8 +165,7 @@ public class BasicJsonTester {
} }
private JsonContent<Object> getJsonContent(String json) { private JsonContent<Object> getJsonContent(String json) {
return new JsonContent<>(this.loader.getResourceLoadClass(), null, json, return new JsonContent<>(this.loader.getResourceLoadClass(), null, json);
this.configuration);
} }
} }
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -20,7 +20,6 @@ import java.io.IOException; ...@@ -20,7 +20,6 @@ import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.jayway.jsonpath.Configuration;
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectFactory;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
...@@ -75,20 +74,7 @@ public class GsonTester<T> extends AbstractJsonMarshalTester<T> { ...@@ -75,20 +74,7 @@ public class GsonTester<T> extends AbstractJsonMarshalTester<T> {
* @see #initFields(Object, Gson) * @see #initFields(Object, Gson)
*/ */
public GsonTester(Class<?> resourceLoadClass, ResolvableType type, Gson gson) { public GsonTester(Class<?> resourceLoadClass, ResolvableType type, Gson gson) {
this(resourceLoadClass, type, gson, Configuration.defaultConfiguration()); super(resourceLoadClass, type);
}
/**
* 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"); Assert.notNull(gson, "Gson must not be null");
this.gson = gson; this.gson = gson;
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -59,6 +59,7 @@ import org.springframework.util.Assert; ...@@ -59,6 +59,7 @@ import org.springframework.util.Assert;
* @param <T> the type under test * @param <T> the type under test
* @author Phillip Webb * @author Phillip Webb
* @author Madhura Bhave * @author Madhura Bhave
* @author Diego Berrueta
* @since 1.4.0 * @since 1.4.0
*/ */
public class JacksonTester<T> extends AbstractJsonMarshalTester<T> { public class JacksonTester<T> extends AbstractJsonMarshalTester<T> {
...@@ -89,14 +90,20 @@ public class JacksonTester<T> extends AbstractJsonMarshalTester<T> { ...@@ -89,14 +90,20 @@ public class JacksonTester<T> extends AbstractJsonMarshalTester<T> {
public JacksonTester(Class<?> resourceLoadClass, ResolvableType type, public JacksonTester(Class<?> resourceLoadClass, ResolvableType type,
ObjectMapper objectMapper, Class<?> view) { ObjectMapper objectMapper, Class<?> view) {
super(resourceLoadClass, type, Configuration.builder() super(resourceLoadClass, type);
.jsonProvider(new JacksonJsonProvider(objectMapper))
.mappingProvider(new JacksonMappingProvider(objectMapper)).build());
Assert.notNull(objectMapper, "ObjectMapper must not be null"); Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.objectMapper = objectMapper; this.objectMapper = objectMapper;
this.view = view; this.view = view;
} }
@Override
protected JsonContent<T> getJsonContent(String json) {
Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonProvider(this.objectMapper))
.mappingProvider(new JacksonMappingProvider(this.objectMapper)).build();
return new JsonContent<>(getResourceLoadClass(), getType(), json, configuration);
}
@Override @Override
protected T readObject(InputStream inputStream, ResolvableType type) protected T readObject(InputStream inputStream, ResolvableType type)
throws IOException { throws IOException {
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -29,6 +29,7 @@ import org.springframework.util.Assert; ...@@ -29,6 +29,7 @@ import org.springframework.util.Assert;
* *
* @param <T> the source type that created the content * @param <T> the source type that created the content
* @author Phillip Webb * @author Phillip Webb
* @author Diego Berrueta
* @since 1.4.0 * @since 1.4.0
*/ */
public final class JsonContent<T> implements AssertProvider<JsonContentAssert> { public final class JsonContent<T> implements AssertProvider<JsonContentAssert> {
...@@ -56,9 +57,9 @@ public final class JsonContent<T> implements AssertProvider<JsonContentAssert> { ...@@ -56,9 +57,9 @@ public final class JsonContent<T> implements AssertProvider<JsonContentAssert> {
* @param resourceLoadClass the source class used to load resources * @param resourceLoadClass the source class used to load resources
* @param type the type under test (or {@code null} if not known) * @param type the type under test (or {@code null} if not known)
* @param json the actual JSON content * @param json the actual JSON content
* @param configuration the json-path configuration * @param configuration the JsonPath configuration
*/ */
public JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json, JsonContent(Class<?> resourceLoadClass, ResolvableType type, String json,
Configuration configuration) { Configuration configuration) {
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null"); Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
Assert.notNull(json, "JSON must not be null"); Assert.notNull(json, "JSON must not be null");
......
...@@ -46,6 +46,7 @@ import org.springframework.util.StringUtils; ...@@ -46,6 +46,7 @@ import org.springframework.util.StringUtils;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Diego Berrueta
* @since 1.4.0 * @since 1.4.0
*/ */
public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> { public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
...@@ -84,8 +85,8 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq ...@@ -84,8 +85,8 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
* @param json the actual JSON content * @param json the actual JSON content
* @param configuration the json-path configuration * @param configuration the json-path configuration
*/ */
public JsonContentAssert(Class<?> resourceLoadClass, Charset charset, JsonContentAssert(Class<?> resourceLoadClass, Charset charset, CharSequence json,
CharSequence json, Configuration configuration) { Configuration configuration) {
super(json, JsonContentAssert.class); super(json, JsonContentAssert.class);
this.configuration = configuration; this.configuration = configuration;
this.loader = new JsonLoader(resourceLoadClass, charset); this.loader = new JsonLoader(resourceLoadClass, charset);
......
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -21,8 +21,6 @@ import java.io.Reader; ...@@ -21,8 +21,6 @@ import java.io.Reader;
import javax.json.bind.Jsonb; import javax.json.bind.Jsonb;
import com.jayway.jsonpath.Configuration;
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectFactory;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -76,20 +74,7 @@ public class JsonbTester<T> extends AbstractJsonMarshalTester<T> { ...@@ -76,20 +74,7 @@ public class JsonbTester<T> extends AbstractJsonMarshalTester<T> {
* @see #initFields(Object, Jsonb) * @see #initFields(Object, Jsonb)
*/ */
public JsonbTester(Class<?> resourceLoadClass, ResolvableType type, Jsonb jsonb) { public JsonbTester(Class<?> resourceLoadClass, ResolvableType type, Jsonb jsonb) {
this(resourceLoadClass, type, jsonb, Configuration.defaultConfiguration()); super(resourceLoadClass, type);
}
/**
* 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"); Assert.notNull(jsonb, "Jsonb must not be null");
this.jsonb = jsonb; this.jsonb = jsonb;
} }
......
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -36,6 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Phillip Webb * @author Phillip Webb
* @author Madhura Bhave * @author Madhura Bhave
* @author Diego Berrueta
*/ */
public class JacksonTesterIntegrationTests { public class JacksonTesterIntegrationTests {
...@@ -91,14 +92,14 @@ public class JacksonTesterIntegrationTests { ...@@ -91,14 +92,14 @@ public class JacksonTesterIntegrationTests {
.isEqualTo(stringWithSpecialCharacters); .isEqualTo(stringWithSpecialCharacters);
} }
// This test confirms that the handling of special characters is symmetrical between
// the serialisation (via the JacksonTester) and the parsing (via json-path). By
// default json-path uses SimpleJson as its parser, which has a slightly different
// behaviour to Jackson and breaks the symmetry. However JacksonTester
// configures json-path to use Jackson for evaluating the path expressions and
// restores the symmetry.
@Test @Test
public void parseSpecialCharactersTest() throws Exception { public void parseSpecialCharactersTest() throws Exception {
// Confirms that the handling of special characters is symmetrical between
// the serialization (via the JacksonTester) and the parsing (via json-path). By
// default json-path uses SimpleJson as its parser, which has a slightly different
// behavior to Jackson and breaks the symmetry. JacksonTester
// configures json-path to use Jackson for evaluating the path expressions and
// restores the symmetry. See gh-15727
String stringWithSpecialCharacters = "\u0006\u007F"; String stringWithSpecialCharacters = "\u0006\u007F";
assertThat(this.stringJson.write(stringWithSpecialCharacters)) assertThat(this.stringJson.write(stringWithSpecialCharacters))
.extractingJsonPathStringValue("@") .extractingJsonPathStringValue("@")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment