Commit 17527738 authored by Andy Wilkinson's avatar Andy Wilkinson

Default JSON loading to UTF-8 and provide methods to configure charset

Closes gh-6597
parent 6d78066a
......@@ -18,6 +18,7 @@ package org.springframework.boot.test.json;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.Resource;
......@@ -42,6 +43,7 @@ import org.springframework.util.Assert;
* See {@link AbstractJsonMarshalTester} for more details.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.4.0
*/
public class BasicJsonTester {
......@@ -55,23 +57,47 @@ public class BasicJsonTester {
}
/**
* Create a new {@link BasicJsonTester} instance.
* Create a new {@link BasicJsonTester} instance that will load resources as UTF-8.
* @param resourceLoadClass the source class used to load resources
*/
public BasicJsonTester(Class<?> resourceLoadClass) {
this(resourceLoadClass, null);
}
/**
* Create a new {@link BasicJsonTester} instance.
* @param resourceLoadClass the source class used to load resources
* @param charset the charset used to load resources
* @since 1.4.1
*/
public BasicJsonTester(Class<?> resourceLoadClass, Charset charset) {
Assert.notNull(resourceLoadClass, "ResourceLoadClass must not be null");
this.loader = new JsonLoader(resourceLoadClass);
this.loader = new JsonLoader(resourceLoadClass, charset);
}
/**
* Initialize the marshal tester for use.
* Initialize the marshal tester for use, configuring it to load JSON resources as
* UTF-8.
* @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) {
this.initialize(resourceLoadClass, null, type);
}
/**
* 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 type the type under test
* @since 1.4.1
*/
protected final void initialize(Class<?> resourceLoadClass, Charset charset,
ResolvableType type) {
if (this.loader == null) {
this.loader = new JsonLoader(resourceLoadClass);
this.loader = new JsonLoader(resourceLoadClass, charset);
}
}
......
......@@ -18,6 +18,7 @@ package org.springframework.boot.test.json;
import java.io.File;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
......@@ -44,6 +45,7 @@ import org.springframework.util.StringUtils;
* AssertJ {@link Assert} for {@link JsonContent}.
*
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.4.0
*/
public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSequence> {
......@@ -51,13 +53,26 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
private final JsonLoader loader;
/**
* Create a new {@link JsonContentAssert} instance.
* Create a new {@link JsonContentAssert} instance that will load resources as UTF-8.
* @param resourceLoadClass the source class used to load resources
* @param json the actual JSON content
*/
public JsonContentAssert(Class<?> resourceLoadClass, CharSequence json) {
this(resourceLoadClass, null, json);
}
/**
* 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
* @since 1.4.1
*/
public JsonContentAssert(Class<?> resourceLoadClass, Charset charset,
CharSequence json) {
super(json, JsonContentAssert.class);
this.loader = new JsonLoader(resourceLoadClass);
this.loader = new JsonLoader(resourceLoadClass, charset);
}
/**
......
......@@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
......@@ -31,20 +32,24 @@ import org.springframework.util.FileCopyUtils;
* Internal helper used to load JSON from various sources.
*
* @author Phillip Webb
* @author Andy Wilkinson
*/
class JsonLoader {
private final Class<?> resourceLoadClass;
JsonLoader(Class<?> resourceLoadClass) {
private final Charset charset;
JsonLoader(Class<?> resourceLoadClass, Charset charset) {
this.resourceLoadClass = resourceLoadClass;
this.charset = charset == null ? Charset.forName("UTF-8") : charset;
}
public Class<?> getResourceLoadClass() {
Class<?> getResourceLoadClass() {
return this.resourceLoadClass;
}
public String getJson(CharSequence source) {
String getJson(CharSequence source) {
if (source == null) {
return null;
}
......@@ -55,15 +60,15 @@ class JsonLoader {
return source.toString();
}
public String getJson(String path, Class<?> resourceLoadClass) {
String getJson(String path, Class<?> resourceLoadClass) {
return getJson(new ClassPathResource(path, resourceLoadClass));
}
public String getJson(byte[] source) {
String getJson(byte[] source) {
return getJson(new ByteArrayInputStream(source));
}
public String getJson(File source) {
String getJson(File source) {
try {
return getJson(new FileInputStream(source));
}
......@@ -72,7 +77,7 @@ class JsonLoader {
}
}
public String getJson(Resource source) {
String getJson(Resource source) {
try {
return getJson(source.getInputStream());
}
......@@ -81,9 +86,10 @@ class JsonLoader {
}
}
public String getJson(InputStream source) {
String getJson(InputStream source) {
try {
return FileCopyUtils.copyToString(new InputStreamReader(source));
return FileCopyUtils
.copyToString(new InputStreamReader(source, this.charset));
}
catch (IOException ex) {
throw new IllegalStateException("Unable to load JSON from InputStream", ex);
......
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