Allow JSON Testers to be @Autowired

Switch `@AutoConfigureJsonTesters` to use regular `@Autowired` injection
for JSON testers. Prior to this commit JSON Tester fields were
initialized directly which caused IDE issues and was also a little
confusing.

Fixes gh-6451
This commit is contained in:
Phillip Webb
2016-07-26 19:44:47 -07:00
parent a44cc196de
commit 296dc7132b
13 changed files with 289 additions and 377 deletions

View File

@@ -70,9 +70,15 @@ import org.springframework.util.ReflectionUtils.FieldCallback;
*/
public abstract class AbstractJsonMarshalTester<T> {
private final Class<?> resourceLoadClass;
private Class<?> resourceLoadClass;
private final ResolvableType type;
private ResolvableType type;
/**
* Create a new uninitialized {@link AbstractJsonMarshalTester} instance.
*/
protected AbstractJsonMarshalTester() {
}
/**
* Create a new {@link AbstractJsonMarshalTester} instance.
@@ -83,8 +89,20 @@ public abstract class AbstractJsonMarshalTester<T> {
public AbstractJsonMarshalTester(Class<?> resourceLoadClass, ResolvableType type) {
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
Assert.notNull(type, "Type must not be null");
this.resourceLoadClass = resourceLoadClass;
this.type = type;
initialize(resourceLoadClass, type);
}
/**
* Initialize the marshal tester for use.
* @param resourceLoadClass the source class used when loading relative classpath
* resources
* @param type the type under test
*/
protected final void initialize(Class<?> resourceLoadClass, ResolvableType type) {
if (this.resourceLoadClass == null && this.type == null) {
this.resourceLoadClass = resourceLoadClass;
this.type = type;
}
}
/**
@@ -102,6 +120,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on write error
*/
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<T>(this.resourceLoadClass, this.type, json);
@@ -114,6 +133,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on parse error
*/
public T parseObject(byte[] jsonBytes) throws IOException {
verify();
return parse(jsonBytes).getObject();
}
@@ -124,6 +144,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on parse error
*/
public ObjectContent<T> parse(byte[] jsonBytes) throws IOException {
verify();
Assert.notNull(jsonBytes, "JsonBytes must not be null");
return read(new ByteArrayResource(jsonBytes));
}
@@ -135,6 +156,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on parse error
*/
public T parseObject(String jsonString) throws IOException {
verify();
return parse(jsonString).getObject();
}
@@ -145,6 +167,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on parse error
*/
public ObjectContent<T> parse(String jsonString) throws IOException {
verify();
Assert.notNull(jsonString, "JsonString must not be null");
return read(new StringReader(jsonString));
}
@@ -157,6 +180,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public T readObject(String resourcePath) throws IOException {
verify();
return read(resourcePath).getObject();
}
@@ -168,6 +192,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public ObjectContent<T> read(String resourcePath) throws IOException {
verify();
Assert.notNull(resourcePath, "ResourcePath must not be null");
return read(new ClassPathResource(resourcePath, this.resourceLoadClass));
}
@@ -179,6 +204,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public T readObject(File file) throws IOException {
verify();
return read(file).getObject();
}
@@ -189,6 +215,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public ObjectContent<T> read(File file) throws IOException {
verify();
Assert.notNull(file, "File must not be null");
return read(new FileSystemResource(file));
}
@@ -200,6 +227,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public T readObject(InputStream inputStream) throws IOException {
verify();
return read(inputStream).getObject();
}
@@ -210,6 +238,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public ObjectContent<T> read(InputStream inputStream) throws IOException {
verify();
Assert.notNull(inputStream, "InputStream must not be null");
return read(new InputStreamResource(inputStream));
}
@@ -221,6 +250,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public T readObject(Resource resource) throws IOException {
verify();
return read(resource).getObject();
}
@@ -231,6 +261,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public ObjectContent<T> read(Resource resource) throws IOException {
verify();
Assert.notNull(resource, "Resource must not be null");
InputStream inputStream = resource.getInputStream();
T object = readObject(inputStream, this.type);
@@ -245,6 +276,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public T readObject(Reader reader) throws IOException {
verify();
return read(reader).getObject();
}
@@ -255,6 +287,7 @@ public abstract class AbstractJsonMarshalTester<T> {
* @throws IOException on read error
*/
public ObjectContent<T> read(Reader reader) throws IOException {
verify();
Assert.notNull(reader, "Reader must not be null");
T object = readObject(reader, this.type);
closeQuietly(reader);
@@ -269,6 +302,12 @@ public abstract class AbstractJsonMarshalTester<T> {
}
}
private void verify() {
Assert.state(this.resourceLoadClass != null,
"Unitialized JsonMarshalTester (ResourceLoadClass is null)");
Assert.state(this.type != null, "Unitialized JsonMarshalTester (Type is null)");
}
/**
* Write the specified object to a JSON string.
* @param value the source value (never {@code null})

View File

@@ -19,6 +19,7 @@ package org.springframework.boot.test.json;
import java.io.File;
import java.io.InputStream;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
@@ -45,7 +46,13 @@ import org.springframework.util.Assert;
*/
public class BasicJsonTester {
private final JsonLoader loader;
private JsonLoader loader;
/**
* Create a new uninialized {@link BasicJsonTester} instance.
*/
protected BasicJsonTester() {
}
/**
* Create a new {@link BasicJsonTester} instance.
@@ -56,6 +63,18 @@ public class BasicJsonTester {
this.loader = new JsonLoader(resourceLoadClass);
}
/**
* Initialize the marshal tester for use.
* @param resourceLoadClass the source class used when loading relative classpath
* resources
* @param type the type under test
*/
protected final void initialize(Class<?> resourceLoadClass, ResolvableType type) {
if (this.loader == null) {
this.loader = new JsonLoader(resourceLoadClass);
}
}
/**
* Create JSON content from the specified String source. The source can contain the
* JSON itself or, if it ends with {@code .json}, the name of a resource to be loaded
@@ -64,6 +83,7 @@ public class BasicJsonTester {
* @return the JSON content
*/
public JsonContent<Object> from(CharSequence source) {
verify();
return getJsonContent(this.loader.getJson(source));
}
@@ -74,6 +94,7 @@ public class BasicJsonTester {
* @return the JSON content
*/
public JsonContent<Object> from(String path, Class<?> resourceLoadClass) {
verify();
return getJsonContent(this.loader.getJson(path, resourceLoadClass));
}
@@ -83,6 +104,7 @@ public class BasicJsonTester {
* @return the JSON content
*/
public JsonContent<Object> from(byte[] source) {
verify();
return getJsonContent(this.loader.getJson(source));
}
@@ -92,6 +114,7 @@ public class BasicJsonTester {
* @return the JSON content
*/
public JsonContent<Object> from(File source) {
verify();
return getJsonContent(this.loader.getJson(source));
}
@@ -101,6 +124,7 @@ public class BasicJsonTester {
* @return the JSON content
*/
public JsonContent<Object> from(InputStream source) {
verify();
return getJsonContent(this.loader.getJson(source));
}
@@ -110,9 +134,14 @@ public class BasicJsonTester {
* @return the JSON content
*/
public JsonContent<Object> from(Resource source) {
verify();
return getJsonContent(this.loader.getJson(source));
}
private void verify() {
Assert.state(this.loader != null, "Unitialized BasicJsonTester");
}
private JsonContent<Object> getJsonContent(String json) {
return new JsonContent<Object>(this.loader.getResourceLoadClass(), null, json);
}

View File

@@ -57,6 +57,15 @@ public class GsonTester<T> extends AbstractJsonMarshalTester<T> {
private final Gson gson;
/**
* Create a new uninitialized {@link GsonTester} instance.
* @param gson the Gson instance
*/
protected GsonTester(Gson gson) {
Assert.notNull(gson, "Gson must not be null");
this.gson = gson;
}
/**
* Create a new {@link GsonTester} instance.
* @param resourceLoadClass the source class used to load resources

View File

@@ -59,6 +59,15 @@ public class JacksonTester<T> extends AbstractJsonMarshalTester<T> {
private final ObjectMapper objectMapper;
/**
* Create a new {@link JacksonTester} instance.
* @param objectMapper the Jackson object mapper
*/
protected JacksonTester(ObjectMapper objectMapper) {
Assert.notNull(objectMapper, "ObjectMapper must not be null");
this.objectMapper = objectMapper;
}
/**
* Create a new {@link JacksonTester} instance.
* @param resourceLoadClass the source class used to load resources