Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
17527738
Commit
17527738
authored
Aug 16, 2016
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Default JSON loading to UTF-8 and provide methods to configure charset
Closes gh-6597
parent
6d78066a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
15 deletions
+62
-15
BasicJsonTester.java
...a/org/springframework/boot/test/json/BasicJsonTester.java
+30
-4
JsonContentAssert.java
...org/springframework/boot/test/json/JsonContentAssert.java
+17
-2
JsonLoader.java
...n/java/org/springframework/boot/test/json/JsonLoader.java
+15
-9
No files found.
spring-boot-test/src/main/java/org/springframework/boot/test/json/BasicJsonTester.java
View file @
17527738
...
@@ -18,6 +18,7 @@ package org.springframework.boot.test.json;
...
@@ -18,6 +18,7 @@ package org.springframework.boot.test.json;
import
java.io.File
;
import
java.io.File
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.nio.charset.Charset
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.ResolvableType
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.Resource
;
...
@@ -42,6 +43,7 @@ import org.springframework.util.Assert;
...
@@ -42,6 +43,7 @@ import org.springframework.util.Assert;
* See {@link AbstractJsonMarshalTester} for more details.
* See {@link AbstractJsonMarshalTester} for more details.
*
*
* @author Phillip Webb
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.4.0
* @since 1.4.0
*/
*/
public
class
BasicJsonTester
{
public
class
BasicJsonTester
{
...
@@ -55,23 +57,47 @@ 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
* @param resourceLoadClass the source class used to load resources
*/
*/
public
BasicJsonTester
(
Class
<?>
resourceLoadClass
)
{
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"
);
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
* @param resourceLoadClass the source class used when loading relative classpath
* resources
* resources
* @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
)
{
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
)
{
if
(
this
.
loader
==
null
)
{
this
.
loader
=
new
JsonLoader
(
resourceLoadClass
);
this
.
loader
=
new
JsonLoader
(
resourceLoadClass
,
charset
);
}
}
}
}
...
...
spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonContentAssert.java
View file @
17527738
...
@@ -18,6 +18,7 @@ package org.springframework.boot.test.json;
...
@@ -18,6 +18,7 @@ package org.springframework.boot.test.json;
import
java.io.File
;
import
java.io.File
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.nio.charset.Charset
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
...
@@ -44,6 +45,7 @@ import org.springframework.util.StringUtils;
...
@@ -44,6 +45,7 @@ import org.springframework.util.StringUtils;
* AssertJ {@link Assert} for {@link JsonContent}.
* AssertJ {@link Assert} for {@link JsonContent}.
*
*
* @author Phillip Webb
* @author Phillip Webb
* @author Andy Wilkinson
* @since 1.4.0
* @since 1.4.0
*/
*/
public
class
JsonContentAssert
extends
AbstractAssert
<
JsonContentAssert
,
CharSequence
>
{
public
class
JsonContentAssert
extends
AbstractAssert
<
JsonContentAssert
,
CharSequence
>
{
...
@@ -51,13 +53,26 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
...
@@ -51,13 +53,26 @@ public class JsonContentAssert extends AbstractAssert<JsonContentAssert, CharSeq
private
final
JsonLoader
loader
;
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 resourceLoadClass the source class used to load resources
* @param json the actual JSON content
* @param json the actual JSON content
*/
*/
public
JsonContentAssert
(
Class
<?>
resourceLoadClass
,
CharSequence
json
)
{
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
);
super
(
json
,
JsonContentAssert
.
class
);
this
.
loader
=
new
JsonLoader
(
resourceLoadClass
);
this
.
loader
=
new
JsonLoader
(
resourceLoadClass
,
charset
);
}
}
/**
/**
...
...
spring-boot-test/src/main/java/org/springframework/boot/test/json/JsonLoader.java
View file @
17527738
...
@@ -22,6 +22,7 @@ import java.io.FileInputStream;
...
@@ -22,6 +22,7 @@ import java.io.FileInputStream;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
import
java.nio.charset.Charset
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.Resource
;
...
@@ -31,20 +32,24 @@ import org.springframework.util.FileCopyUtils;
...
@@ -31,20 +32,24 @@ import org.springframework.util.FileCopyUtils;
* Internal helper used to load JSON from various sources.
* Internal helper used to load JSON from various sources.
*
*
* @author Phillip Webb
* @author Phillip Webb
* @author Andy Wilkinson
*/
*/
class
JsonLoader
{
class
JsonLoader
{
private
final
Class
<?>
resourceLoadClass
;
private
final
Class
<?>
resourceLoadClass
;
JsonLoader
(
Class
<?>
resourceLoadClass
)
{
private
final
Charset
charset
;
JsonLoader
(
Class
<?>
resourceLoadClass
,
Charset
charset
)
{
this
.
resourceLoadClass
=
resourceLoadClass
;
this
.
resourceLoadClass
=
resourceLoadClass
;
this
.
charset
=
charset
==
null
?
Charset
.
forName
(
"UTF-8"
)
:
charset
;
}
}
public
Class
<?>
getResourceLoadClass
()
{
Class
<?>
getResourceLoadClass
()
{
return
this
.
resourceLoadClass
;
return
this
.
resourceLoadClass
;
}
}
public
String
getJson
(
CharSequence
source
)
{
String
getJson
(
CharSequence
source
)
{
if
(
source
==
null
)
{
if
(
source
==
null
)
{
return
null
;
return
null
;
}
}
...
@@ -55,15 +60,15 @@ class JsonLoader {
...
@@ -55,15 +60,15 @@ class JsonLoader {
return
source
.
toString
();
return
source
.
toString
();
}
}
public
String
getJson
(
String
path
,
Class
<?>
resourceLoadClass
)
{
String
getJson
(
String
path
,
Class
<?>
resourceLoadClass
)
{
return
getJson
(
new
ClassPathResource
(
path
,
resourceLoadClass
));
return
getJson
(
new
ClassPathResource
(
path
,
resourceLoadClass
));
}
}
public
String
getJson
(
byte
[]
source
)
{
String
getJson
(
byte
[]
source
)
{
return
getJson
(
new
ByteArrayInputStream
(
source
));
return
getJson
(
new
ByteArrayInputStream
(
source
));
}
}
public
String
getJson
(
File
source
)
{
String
getJson
(
File
source
)
{
try
{
try
{
return
getJson
(
new
FileInputStream
(
source
));
return
getJson
(
new
FileInputStream
(
source
));
}
}
...
@@ -72,7 +77,7 @@ class JsonLoader {
...
@@ -72,7 +77,7 @@ class JsonLoader {
}
}
}
}
public
String
getJson
(
Resource
source
)
{
String
getJson
(
Resource
source
)
{
try
{
try
{
return
getJson
(
source
.
getInputStream
());
return
getJson
(
source
.
getInputStream
());
}
}
...
@@ -81,9 +86,10 @@ class JsonLoader {
...
@@ -81,9 +86,10 @@ class JsonLoader {
}
}
}
}
public
String
getJson
(
InputStream
source
)
{
String
getJson
(
InputStream
source
)
{
try
{
try
{
return
FileCopyUtils
.
copyToString
(
new
InputStreamReader
(
source
));
return
FileCopyUtils
.
copyToString
(
new
InputStreamReader
(
source
,
this
.
charset
));
}
}
catch
(
IOException
ex
)
{
catch
(
IOException
ex
)
{
throw
new
IllegalStateException
(
"Unable to load JSON from InputStream"
,
ex
);
throw
new
IllegalStateException
(
"Unable to load JSON from InputStream"
,
ex
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment