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
71654382
Commit
71654382
authored
Jan 03, 2018
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor some tests to use ApplicationContextRunner
parent
3ab32df2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
282 additions
and
349 deletions
+282
-349
FreeMarkerAutoConfigurationReactiveIntegrationTests.java
.../FreeMarkerAutoConfigurationReactiveIntegrationTests.java
+47
-54
FreeMarkerAutoConfigurationTests.java
...onfigure/freemarker/FreeMarkerAutoConfigurationTests.java
+17
-29
JacksonAutoConfigurationTests.java
.../autoconfigure/jackson/JacksonAutoConfigurationTests.java
+218
-266
No files found.
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationReactiveIntegrationTests.java
View file @
71654382
...
@@ -19,12 +19,12 @@ package org.springframework.boot.autoconfigure.freemarker;
...
@@ -19,12 +19,12 @@ package org.springframework.boot.autoconfigure.freemarker;
import
java.io.StringWriter
;
import
java.io.StringWriter
;
import
java.util.Locale
;
import
java.util.Locale
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.junit.Test
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext
;
import
org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.MediaType
;
import
org.springframework.mock.http.server.reactive.MockServerHttpRequest
;
import
org.springframework.mock.http.server.reactive.MockServerHttpRequest
;
import
org.springframework.mock.web.server.MockServerWebExchange
;
import
org.springframework.mock.web.server.MockServerWebExchange
;
...
@@ -42,90 +42,83 @@ import static org.assertj.core.api.Assertions.assertThat;
...
@@ -42,90 +42,83 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
*/
public
class
FreeMarkerAutoConfigurationReactiveIntegrationTests
{
public
class
FreeMarkerAutoConfigurationReactiveIntegrationTests
{
private
AnnotationConfigReactiveWebApplicationContext
context
=
new
AnnotationConfigReactiveWebApplicationContext
();
private
final
ReactiveWebApplicationContextRunner
contextRunner
=
new
ReactiveWebApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
FreeMarkerAutoConfiguration
.
class
));
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
@Test
public
void
defaultConfiguration
()
{
public
void
defaultConfiguration
()
{
registerAndRefreshContext
();
this
.
contextRunner
.
run
(
context
->
{
assertThat
(
this
.
context
.
getBean
(
FreeMarkerViewResolver
.
class
)).
isNotNull
();
assertThat
(
context
.
getBean
(
FreeMarkerViewResolver
.
class
)).
isNotNull
();
assertThat
(
this
.
context
.
getBean
(
FreeMarkerConfigurer
.
class
)).
isNotNull
();
assertThat
(
context
.
getBean
(
FreeMarkerConfigurer
.
class
)).
isNotNull
();
assertThat
(
this
.
context
.
getBean
(
FreeMarkerConfig
.
class
)).
isNotNull
();
assertThat
(
context
.
getBean
(
FreeMarkerConfig
.
class
)).
isNotNull
();
assertThat
(
this
.
context
.
getBean
(
freemarker
.
template
.
Configuration
.
class
))
assertThat
(
context
.
getBean
(
freemarker
.
template
.
Configuration
.
class
))
.
isNotNull
();
.
isNotNull
();
});
}
}
@Test
@Test
public
void
defaultViewResolution
()
{
public
void
defaultViewResolution
()
{
registerAndRefreshContext
();
this
.
contextRunner
.
run
(
context
->
{
MockServerWebExchange
exchange
=
render
(
"home"
);
MockServerWebExchange
exchange
=
render
(
context
,
"home"
);
String
result
=
exchange
.
getResponse
().
getBodyAsString
().
block
();
String
result
=
exchange
.
getResponse
().
getBodyAsString
().
block
();
assertThat
(
result
).
contains
(
"home"
);
assertThat
(
result
).
contains
(
"home"
);
assertThat
(
exchange
.
getResponse
().
getHeaders
().
getContentType
())
assertThat
(
exchange
.
getResponse
().
getHeaders
().
getContentType
())
.
isEqualTo
(
MediaType
.
TEXT_HTML
);
.
isEqualTo
(
MediaType
.
TEXT_HTML
);
});
}
}
@Test
@Test
public
void
customPrefix
()
{
public
void
customPrefix
()
{
registerAndRefreshContext
(
"spring.freemarker.prefix:prefix/"
);
this
.
contextRunner
.
withPropertyValues
(
"spring.freemarker.prefix:prefix/"
).
run
(
context
->
{
MockServerWebExchange
exchange
=
render
(
"prefixed"
);
MockServerWebExchange
exchange
=
render
(
context
,
"prefixed"
);
String
result
=
exchange
.
getResponse
().
getBodyAsString
().
block
();
String
result
=
exchange
.
getResponse
().
getBodyAsString
().
block
();
assertThat
(
result
).
contains
(
"prefixed"
);
assertThat
(
result
).
contains
(
"prefixed"
);
});
}
}
@Test
@Test
public
void
customSuffix
()
{
public
void
customSuffix
()
{
registerAndRefreshContext
(
"spring.freemarker.suffix:.freemarker"
);
this
.
contextRunner
.
withPropertyValues
(
"spring.freemarker.suffix:.freemarker"
).
run
(
context
->
{
MockServerWebExchange
exchange
=
render
(
"suffixed"
);
MockServerWebExchange
exchange
=
render
(
context
,
"suffixed"
);
String
result
=
exchange
.
getResponse
().
getBodyAsString
().
block
();
String
result
=
exchange
.
getResponse
().
getBodyAsString
().
block
();
assertThat
(
result
).
contains
(
"suffixed"
);
assertThat
(
result
).
contains
(
"suffixed"
);
});
}
}
@Test
@Test
public
void
customTemplateLoaderPath
()
{
public
void
customTemplateLoaderPath
()
{
registerAndRefreshContext
(
this
.
contextRunner
.
withPropertyValues
(
"spring.freemarker.templateLoaderPath:classpath:/custom-templates/"
).
run
(
context
->
{
"spring.freemarker.templateLoaderPath:classpath:/custom-templates/
"
);
MockServerWebExchange
exchange
=
render
(
context
,
"custom
"
);
MockServerWebExchange
exchange
=
render
(
"custom"
);
String
result
=
exchange
.
getResponse
().
getBodyAsString
().
block
(
);
String
result
=
exchange
.
getResponse
().
getBodyAsString
().
block
(
);
assertThat
(
result
).
contains
(
"custom"
);
assertThat
(
result
).
contains
(
"custom"
);
}
);
}
}
@SuppressWarnings
(
"deprecation"
)
@SuppressWarnings
(
"deprecation"
)
@Test
@Test
public
void
customFreeMarkerSettings
()
{
public
void
customFreeMarkerSettings
()
{
registerAndRefreshContext
(
"spring.freemarker.settings.boolean_format:yup,nope"
);
this
.
contextRunner
.
withPropertyValues
(
"spring.freemarker.settings.boolean_format:yup,nope"
)
assertThat
(
this
.
context
.
getBean
(
FreeMarkerConfigurer
.
class
).
getConfiguration
()
.
run
(
context
->
assertThat
(
context
.
getBean
(
FreeMarkerConfigurer
.
class
).
getConfiguration
()
.
getSetting
(
"boolean_format"
)).
isEqualTo
(
"yup,nope"
);
.
getSetting
(
"boolean_format"
)).
isEqualTo
(
"yup,nope"
)
);
}
}
@Test
@Test
public
void
renderTemplate
()
throws
Exception
{
public
void
renderTemplate
()
{
registerAndRefreshContext
();
this
.
contextRunner
.
withPropertyValues
().
run
(
context
->
{
FreeMarkerConfigurer
freemarker
=
this
.
context
FreeMarkerConfigurer
freemarker
=
context
.
getBean
(
FreeMarkerConfigurer
.
class
);
.
getBean
(
FreeMarkerConfigurer
.
class
);
StringWriter
writer
=
new
StringWriter
();
StringWriter
writer
=
new
StringWriter
();
freemarker
.
getConfiguration
().
getTemplate
(
"message.ftl"
).
process
(
this
,
writer
);
freemarker
.
getConfiguration
().
getTemplate
(
"message.ftl"
).
process
(
this
,
writer
);
assertThat
(
writer
.
toString
()).
contains
(
"Hello World"
);
assertThat
(
writer
.
toString
()).
contains
(
"Hello World"
);
}
});
private
void
registerAndRefreshContext
(
String
...
env
)
{
TestPropertyValues
.
of
(
env
).
applyTo
(
this
.
context
);
this
.
context
.
register
(
FreeMarkerAutoConfiguration
.
class
);
this
.
context
.
refresh
();
}
}
public
String
getGreeting
()
{
public
String
getGreeting
()
{
return
"Hello World"
;
return
"Hello World"
;
}
}
private
MockServerWebExchange
render
(
String
viewName
)
{
private
MockServerWebExchange
render
(
ApplicationContext
context
,
String
viewName
)
{
FreeMarkerViewResolver
resolver
=
this
.
context
FreeMarkerViewResolver
resolver
=
context
.
getBean
(
FreeMarkerViewResolver
.
class
);
.
getBean
(
FreeMarkerViewResolver
.
class
);
Mono
<
View
>
view
=
resolver
.
resolveViewName
(
viewName
,
Locale
.
UK
);
Mono
<
View
>
view
=
resolver
.
resolveViewName
(
viewName
,
Locale
.
UK
);
MockServerWebExchange
exchange
=
MockServerWebExchange
MockServerWebExchange
exchange
=
MockServerWebExchange
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java
View file @
71654382
...
@@ -19,13 +19,12 @@ package org.springframework.boot.autoconfigure.freemarker;
...
@@ -19,13 +19,12 @@ package org.springframework.boot.autoconfigure.freemarker;
import
java.io.File
;
import
java.io.File
;
import
java.io.StringWriter
;
import
java.io.StringWriter
;
import
org.junit.After
;
import
org.junit.Rule
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.boot.test.rule.OutputCapture
;
import
org.springframework.boot.test.rule.OutputCapture
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
import
static
org
.
hamcrest
.
Matchers
.
containsString
;
...
@@ -41,24 +40,18 @@ public class FreeMarkerAutoConfigurationTests {
...
@@ -41,24 +40,18 @@ public class FreeMarkerAutoConfigurationTests {
@Rule
@Rule
public
OutputCapture
output
=
new
OutputCapture
();
public
OutputCapture
output
=
new
OutputCapture
();
private
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
FreeMarkerAutoConfiguration
.
class
));
private
void
registerAndRefreshContext
(
String
...
env
)
{
TestPropertyValues
.
of
(
env
).
applyTo
(
this
.
context
);
this
.
context
.
register
(
FreeMarkerAutoConfiguration
.
class
);
this
.
context
.
refresh
();
}
@Test
@Test
public
void
renderNonWebAppTemplate
()
throws
Exception
{
public
void
renderNonWebAppTemplate
()
{
try
(
AnnotationConfigApplicationContext
customContext
=
new
AnnotationConfigApplicationContext
(
this
.
contextRunner
.
run
(
context
->
{
FreeMarkerAutoConfiguration
.
class
))
{
freemarker
.
template
.
Configuration
freemarker
=
context
freemarker
.
template
.
Configuration
freemarker
=
customContext
.
getBean
(
freemarker
.
template
.
Configuration
.
class
);
.
getBean
(
freemarker
.
template
.
Configuration
.
class
);
StringWriter
writer
=
new
StringWriter
();
StringWriter
writer
=
new
StringWriter
();
freemarker
.
getTemplate
(
"message.ftl"
).
process
(
this
,
writer
);
freemarker
.
getTemplate
(
"message.ftl"
).
process
(
this
,
writer
);
assertThat
(
writer
.
toString
()).
contains
(
"Hello World"
);
assertThat
(
writer
.
toString
()).
contains
(
"Hello World"
);
}
}
);
}
}
public
String
getGreeting
()
{
public
String
getGreeting
()
{
...
@@ -67,30 +60,25 @@ public class FreeMarkerAutoConfigurationTests {
...
@@ -67,30 +60,25 @@ public class FreeMarkerAutoConfigurationTests {
@Test
@Test
public
void
nonExistentTemplateLocation
()
{
public
void
nonExistentTemplateLocation
()
{
registerAndRefreshContext
(
"spring.freemarker.templateLoaderPath:"
this
.
contextRunner
.
withPropertyValues
(
"spring.freemarker.templateLoaderPath:"
+
"classpath:/does-not-exist/,classpath:/also-does-not-exist"
)
;
+
"classpath:/does-not-exist/,classpath:/also-does-not-exist"
)
this
.
output
.
expect
(
containsString
(
"Cannot find template location"
));
.
run
(
context
->
this
.
output
.
expect
(
containsString
(
"Cannot find template location"
)
));
}
}
@Test
@Test
public
void
emptyTemplateLocation
()
{
public
void
emptyTemplateLocation
()
{
new
File
(
"target/test-classes/templates/empty-directory"
).
mkdir
();
new
File
(
"target/test-classes/templates/empty-directory"
).
mkdir
();
registerAndRefreshContext
(
"spring.freemarker.templateLoaderPath:"
this
.
contextRunner
.
withPropertyValues
(
"spring.freemarker.templateLoaderPath:"
+
"classpath:/templates/empty-directory/"
);
+
"classpath:/templates/empty-directory/"
).
run
(
context
->
{
});
}
}
@Test
@Test
public
void
nonExistentLocationAndEmptyLocation
()
{
public
void
nonExistentLocationAndEmptyLocation
()
{
new
File
(
"target/test-classes/templates/empty-directory"
).
mkdir
();
new
File
(
"target/test-classes/templates/empty-directory"
).
mkdir
();
registerAndRefreshContext
(
"spring.freemarker.templateLoaderPath:"
this
.
contextRunner
.
withPropertyValues
(
"spring.freemarker.templateLoaderPath:"
+
"classpath:/does-not-exist/,classpath:/templates/empty-directory/"
);
+
"classpath:/does-not-exist/,classpath:/templates/empty-directory/"
).
run
(
context
->
{
}
});
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
}
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfigurationTests.java
View file @
71654382
...
@@ -47,15 +47,13 @@ import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
...
@@ -47,15 +47,13 @@ import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import
org.joda.time.DateTime
;
import
org.joda.time.DateTime
;
import
org.joda.time.DateTimeZone
;
import
org.joda.time.DateTimeZone
;
import
org.joda.time.LocalDateTime
;
import
org.joda.time.LocalDateTime
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration
;
import
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration
;
import
org.springframework.boot.jackson.JsonComponent
;
import
org.springframework.boot.jackson.JsonComponent
;
import
org.springframework.boot.jackson.JsonObjectSerializer
;
import
org.springframework.boot.jackson.JsonObjectSerializer
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.context.annotation.Import
;
...
@@ -79,35 +77,24 @@ import static org.mockito.Mockito.mock;
...
@@ -79,35 +77,24 @@ import static org.mockito.Mockito.mock;
*/
*/
public
class
JacksonAutoConfigurationTests
{
public
class
JacksonAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
JacksonAutoConfiguration
.
class
));
@Before
public
void
setUp
()
{
this
.
context
=
new
AnnotationConfigApplicationContext
();
}
@After
public
void
tearDown
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
@Test
public
void
registersJodaModuleAutomatically
()
{
public
void
registersJodaModuleAutomatically
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
context
Runner
.
run
(
context
->
{
this
.
context
.
refresh
(
);
ObjectMapper
objectMapper
=
context
.
getBean
(
ObjectMapper
.
class
);
ObjectMapper
objectMapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
objectMapper
.
canSerialize
(
LocalDateTime
.
class
)).
isTrue
(
);
assertThat
(
objectMapper
.
canSerialize
(
LocalDateTime
.
class
)).
isTrue
(
);
}
);
}
}
@Test
@Test
public
void
doubleModuleRegistration
()
throws
Exception
{
public
void
doubleModuleRegistration
()
{
this
.
context
.
register
(
DoubleModulesConfig
.
class
,
this
.
context
Runner
.
withUserConfiguration
(
DoubleModulesConfig
.
class
).
withConfiguration
(
AutoConfigurations
.
of
(
HttpMessageConvertersAutoConfiguration
.
class
)
;
HttpMessageConvertersAutoConfiguration
.
class
)
).
run
(
context
->
{
this
.
context
.
refresh
(
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
writeValueAsString
(
new
Foo
())).
isEqualTo
(
"{\"foo\":\"bar\"}"
);
assertThat
(
mapper
.
writeValueAsString
(
new
Foo
())).
isEqualTo
(
"{\"foo\":\"bar\"}"
);
}
);
}
}
/*
/*
...
@@ -118,323 +105,288 @@ public class JacksonAutoConfigurationTests {
...
@@ -118,323 +105,288 @@ public class JacksonAutoConfigurationTests {
@Test
@Test
public
void
noCustomDateFormat
()
{
public
void
noCustomDateFormat
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
context
Runner
.
run
(
context
->
{
this
.
context
.
refresh
(
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getDateFormat
()).
isInstanceOf
(
StdDateFormat
.
class
);
assertThat
(
mapper
.
getDateFormat
()).
isInstanceOf
(
StdDateFormat
.
class
);
}
);
}
}
@Test
@Test
public
void
customDateFormat
()
{
public
void
customDateFormat
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.date-format:yyyyMMddHHmmss"
).
run
(
context
->
{
TestPropertyValues
.
of
(
"spring.jackson.date-format:yyyyMMddHHmmss"
)
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
.
applyTo
(
this
.
context
);
DateFormat
dateFormat
=
mapper
.
getDateFormat
();
this
.
context
.
refresh
();
assertThat
(
dateFormat
).
isInstanceOf
(
SimpleDateFormat
.
class
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(((
SimpleDateFormat
)
dateFormat
).
toPattern
())
DateFormat
dateFormat
=
mapper
.
getDateFormat
();
.
isEqualTo
(
"yyyyMMddHHmmss"
);
assertThat
(
dateFormat
).
isInstanceOf
(
SimpleDateFormat
.
class
);
});
assertThat
(((
SimpleDateFormat
)
dateFormat
).
toPattern
())
.
isEqualTo
(
"yyyyMMddHHmmss"
);
}
}
@Test
@Test
public
void
customJodaDateTimeFormat
()
throws
Exception
{
public
void
customJodaDateTimeFormat
()
throws
Exception
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.date-format:yyyyMMddHHmmss"
,
TestPropertyValues
"spring.jackson.joda-date-time-format:yyyy-MM-dd HH:mm:ss"
).
run
(
context
->
{
.
of
(
"spring.jackson.date-format:yyyyMMddHHmmss"
,
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
"spring.jackson.joda-date-time-format:yyyy-MM-dd HH:mm:ss"
)
DateTime
dateTime
=
new
DateTime
(
1988
,
6
,
25
,
20
,
30
,
DateTimeZone
.
UTC
);
.
applyTo
(
this
.
context
);
assertThat
(
mapper
.
writeValueAsString
(
dateTime
))
this
.
context
.
refresh
();
.
isEqualTo
(
"\"1988-06-25 20:30:00\""
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
Date
date
=
dateTime
.
toDate
();
DateTime
dateTime
=
new
DateTime
(
1988
,
6
,
25
,
20
,
30
,
DateTimeZone
.
UTC
);
assertThat
(
mapper
.
writeValueAsString
(
date
)).
isEqualTo
(
"\"19880625203000\""
);
assertThat
(
mapper
.
writeValueAsString
(
dateTime
))
});
.
isEqualTo
(
"\"1988-06-25 20:30:00\""
);
Date
date
=
dateTime
.
toDate
();
assertThat
(
mapper
.
writeValueAsString
(
date
)).
isEqualTo
(
"\"19880625203000\""
);
}
}
@Test
@Test
public
void
customDateFormatClass
()
{
public
void
customDateFormatClass
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.date-format:org.springframework.boot.autoconfigure.jackson.JacksonAutoConfigurationTests.MyDateFormat"
).
run
(
context
->
{
TestPropertyValues
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
.
of
(
"spring.jackson.date-format:org.springframework.boot.autoconfigure.jackson.JacksonAutoConfigurationTests.MyDateFormat"
)
assertThat
(
mapper
.
getDateFormat
()).
isInstanceOf
(
MyDateFormat
.
class
);
.
applyTo
(
this
.
context
);
});
this
.
context
.
refresh
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getDateFormat
()).
isInstanceOf
(
MyDateFormat
.
class
);
}
}
@Test
@Test
public
void
noCustomPropertyNamingStrategy
()
{
public
void
noCustomPropertyNamingStrategy
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
context
Runner
.
run
(
context
->
{
this
.
context
.
refresh
(
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getPropertyNamingStrategy
()).
isNull
(
);
assertThat
(
mapper
.
getPropertyNamingStrategy
()).
isNull
(
);
}
);
}
}
@Test
@Test
public
void
customPropertyNamingStrategyField
()
{
public
void
customPropertyNamingStrategyField
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.property-naming-strategy:SNAKE_CASE"
).
run
(
context
->
{
TestPropertyValues
.
of
(
"spring.jackson.property-naming-strategy:SNAKE_CASE"
)
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
.
applyTo
(
this
.
context
);
assertThat
(
mapper
.
getPropertyNamingStrategy
())
this
.
context
.
refresh
();
.
isInstanceOf
(
SnakeCaseStrategy
.
class
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
});
assertThat
(
mapper
.
getPropertyNamingStrategy
())
.
isInstanceOf
(
SnakeCaseStrategy
.
class
);
}
}
@Test
@Test
public
void
customPropertyNamingStrategyClass
()
{
public
void
customPropertyNamingStrategyClass
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.property-naming-strategy:com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy"
)
TestPropertyValues
.
run
(
context
->
{
.
of
(
"spring.jackson.property-naming-strategy:com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy"
)
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
.
applyTo
(
this
.
context
);
assertThat
(
mapper
.
getPropertyNamingStrategy
())
this
.
context
.
refresh
();
.
isInstanceOf
(
SnakeCaseStrategy
.
class
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
});
assertThat
(
mapper
.
getPropertyNamingStrategy
())
.
isInstanceOf
(
SnakeCaseStrategy
.
class
);
}
}
@Test
@Test
public
void
enableSerializationFeature
()
{
public
void
enableSerializationFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.serialization.indent_output:true"
)
TestPropertyValues
.
of
(
"spring.jackson.serialization.indent_output:true"
)
.
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
this
.
context
.
refresh
();
assertThat
(
SerializationFeature
.
INDENT_OUTPUT
.
enabledByDefault
()).
isFalse
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getSerializationConfig
()
assertThat
(
SerializationFeature
.
INDENT_OUTPUT
.
enabledByDefault
()).
isFalse
();
.
hasSerializationFeatures
(
SerializationFeature
.
INDENT_OUTPUT
.
getMask
()))
assertThat
(
mapper
.
getSerializationConfig
()
.
isTrue
();
.
hasSerializationFeatures
(
SerializationFeature
.
INDENT_OUTPUT
.
getMask
()))
});
.
isTrue
();
}
}
@Test
@Test
public
void
disableSerializationFeature
()
{
public
void
disableSerializationFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.serialization.write_dates_as_timestamps:false"
)
TestPropertyValues
.
run
(
context
->
{
.
of
(
"spring.jackson.serialization.write_dates_as_timestamps:false"
)
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
.
applyTo
(
this
.
context
);
assertThat
(
SerializationFeature
.
WRITE_DATES_AS_TIMESTAMPS
.
enabledByDefault
())
this
.
context
.
refresh
();
.
isTrue
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getSerializationConfig
().
hasSerializationFeatures
(
assertThat
(
SerializationFeature
.
WRITE_DATES_AS_TIMESTAMPS
.
enabledByDefault
())
SerializationFeature
.
WRITE_DATES_AS_TIMESTAMPS
.
getMask
())).
isFalse
();
.
isTrue
();
});
assertThat
(
mapper
.
getSerializationConfig
().
hasSerializationFeatures
(
SerializationFeature
.
WRITE_DATES_AS_TIMESTAMPS
.
getMask
())).
isFalse
();
}
}
@Test
@Test
public
void
enableDeserializationFeature
()
{
public
void
enableDeserializationFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.deserialization.use_big_decimal_for_floats:true"
)
TestPropertyValues
.
run
(
context
->
{
.
of
(
"spring.jackson.deserialization.use_big_decimal_for_floats:true"
)
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
.
applyTo
(
this
.
context
);
assertThat
(
DeserializationFeature
.
USE_BIG_DECIMAL_FOR_FLOATS
.
enabledByDefault
())
this
.
context
.
refresh
();
.
isFalse
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getDeserializationConfig
().
hasDeserializationFeatures
(
assertThat
(
DeserializationFeature
.
USE_BIG_DECIMAL_FOR_FLOATS
.
enabledByDefault
())
DeserializationFeature
.
USE_BIG_DECIMAL_FOR_FLOATS
.
getMask
())).
isTrue
();
.
isFalse
();
});
assertThat
(
mapper
.
getDeserializationConfig
().
hasDeserializationFeatures
(
DeserializationFeature
.
USE_BIG_DECIMAL_FOR_FLOATS
.
getMask
())).
isTrue
();
}
}
@Test
@Test
public
void
disableDeserializationFeature
()
{
public
void
disableDeserializationFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.deserialization.fail-on-unknown-properties:false"
)
TestPropertyValues
.
run
(
context
->
{
.
of
(
"spring.jackson.deserialization.fail-on-unknown-properties:false"
)
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
.
applyTo
(
this
.
context
);
assertThat
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
.
enabledByDefault
())
this
.
context
.
refresh
();
.
isTrue
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getDeserializationConfig
().
hasDeserializationFeatures
(
assertThat
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
.
enabledByDefault
())
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
.
getMask
())).
isFalse
();
.
isTrue
();
});
assertThat
(
mapper
.
getDeserializationConfig
().
hasDeserializationFeatures
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
.
getMask
())).
isFalse
();
}
}
@Test
@Test
public
void
enableMapperFeature
()
{
public
void
enableMapperFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.mapper.require_setters_for_getters:true"
)
TestPropertyValues
.
of
(
"spring.jackson.mapper.require_setters_for_getters:true"
)
.
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
this
.
context
.
refresh
();
assertThat
(
MapperFeature
.
REQUIRE_SETTERS_FOR_GETTERS
.
enabledByDefault
())
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
.
isFalse
();
assertThat
(
MapperFeature
.
REQUIRE_SETTERS_FOR_GETTERS
.
enabledByDefault
())
assertThat
(
mapper
.
getSerializationConfig
()
.
isFalse
();
.
hasMapperFeatures
(
MapperFeature
.
REQUIRE_SETTERS_FOR_GETTERS
.
getMask
()))
assertThat
(
mapper
.
getSerializationConfig
()
.
isTrue
();
.
hasMapperFeatures
(
MapperFeature
.
REQUIRE_SETTERS_FOR_GETTERS
.
getMask
()))
assertThat
(
mapper
.
getDeserializationConfig
()
.
isTrue
();
.
hasMapperFeatures
(
MapperFeature
.
REQUIRE_SETTERS_FOR_GETTERS
.
getMask
()))
assertThat
(
mapper
.
getDeserializationConfig
()
.
isTrue
();
.
hasMapperFeatures
(
MapperFeature
.
REQUIRE_SETTERS_FOR_GETTERS
.
getMask
()))
});
.
isTrue
();
}
}
@Test
@Test
public
void
disableMapperFeature
()
{
public
void
disableMapperFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.mapper.use_annotations:false"
)
TestPropertyValues
.
of
(
"spring.jackson.mapper.use_annotations:false"
)
.
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
this
.
context
.
refresh
();
assertThat
(
MapperFeature
.
USE_ANNOTATIONS
.
enabledByDefault
()).
isTrue
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getDeserializationConfig
()
assertThat
(
MapperFeature
.
USE_ANNOTATIONS
.
enabledByDefault
()).
isTrue
();
.
hasMapperFeatures
(
MapperFeature
.
USE_ANNOTATIONS
.
getMask
())).
isFalse
();
assertThat
(
mapper
.
getDeserializationConfig
()
assertThat
(
mapper
.
getSerializationConfig
()
.
hasMapperFeatures
(
MapperFeature
.
USE_ANNOTATIONS
.
getMask
())).
isFalse
();
.
hasMapperFeatures
(
MapperFeature
.
USE_ANNOTATIONS
.
getMask
())).
isFalse
();
assertThat
(
mapper
.
getSerializationConfig
()
});
.
hasMapperFeatures
(
MapperFeature
.
USE_ANNOTATIONS
.
getMask
())).
isFalse
();
}
}
@Test
@Test
public
void
enableParserFeature
()
{
public
void
enableParserFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.parser.allow_single_quotes:true"
)
TestPropertyValues
.
of
(
"spring.jackson.parser.allow_single_quotes:true"
)
.
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
this
.
context
.
refresh
();
assertThat
(
JsonParser
.
Feature
.
ALLOW_SINGLE_QUOTES
.
enabledByDefault
()).
isFalse
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getFactory
().
isEnabled
(
JsonParser
.
Feature
.
ALLOW_SINGLE_QUOTES
))
assertThat
(
JsonParser
.
Feature
.
ALLOW_SINGLE_QUOTES
.
enabledByDefault
()).
isFalse
();
.
isTrue
();
assertThat
(
mapper
.
getFactory
().
isEnabled
(
JsonParser
.
Feature
.
ALLOW_SINGLE_QUOTES
))
});
.
isTrue
();
}
}
@Test
@Test
public
void
disableParserFeature
()
{
public
void
disableParserFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.parser.auto_close_source:false"
)
TestPropertyValues
.
of
(
"spring.jackson.parser.auto_close_source:false"
)
.
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
this
.
context
.
refresh
();
assertThat
(
JsonParser
.
Feature
.
AUTO_CLOSE_SOURCE
.
enabledByDefault
()).
isTrue
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getFactory
().
isEnabled
(
JsonParser
.
Feature
.
AUTO_CLOSE_SOURCE
))
assertThat
(
JsonParser
.
Feature
.
AUTO_CLOSE_SOURCE
.
enabledByDefault
()).
isTrue
();
.
isFalse
();
assertThat
(
mapper
.
getFactory
().
isEnabled
(
JsonParser
.
Feature
.
AUTO_CLOSE_SOURCE
))
});
.
isFalse
();
}
}
@Test
@Test
public
void
enableGeneratorFeature
()
{
public
void
enableGeneratorFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.generator.write_numbers_as_strings:true"
)
TestPropertyValues
.
of
(
"spring.jackson.generator.write_numbers_as_strings:true"
)
.
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
this
.
context
.
refresh
();
assertThat
(
JsonGenerator
.
Feature
.
WRITE_NUMBERS_AS_STRINGS
.
enabledByDefault
())
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
.
isFalse
();
assertThat
(
JsonGenerator
.
Feature
.
WRITE_NUMBERS_AS_STRINGS
.
enabledByDefault
())
assertThat
(
mapper
.
getFactory
()
.
isFalse
();
.
isEnabled
(
JsonGenerator
.
Feature
.
WRITE_NUMBERS_AS_STRINGS
)).
isTrue
();
assertThat
(
mapper
.
getFactory
()
});
.
isEnabled
(
JsonGenerator
.
Feature
.
WRITE_NUMBERS_AS_STRINGS
)).
isTrue
();
}
}
@Test
@Test
public
void
disableGeneratorFeature
()
{
public
void
disableGeneratorFeature
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.generator.auto_close_target:false"
)
TestPropertyValues
.
of
(
"spring.jackson.generator.auto_close_target:false"
)
.
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
this
.
context
.
refresh
();
assertThat
(
JsonGenerator
.
Feature
.
AUTO_CLOSE_TARGET
.
enabledByDefault
()).
isTrue
();
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
assertThat
(
mapper
.
getFactory
().
isEnabled
(
JsonGenerator
.
Feature
.
AUTO_CLOSE_TARGET
))
assertThat
(
JsonGenerator
.
Feature
.
AUTO_CLOSE_TARGET
.
enabledByDefault
()).
isTrue
();
.
isFalse
();
assertThat
(
mapper
.
getFactory
().
isEnabled
(
JsonGenerator
.
Feature
.
AUTO_CLOSE_TARGET
))
});
.
isFalse
();
}
}
@Test
@Test
public
void
defaultObjectMapperBuilder
()
{
public
void
defaultObjectMapperBuilder
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
context
Runner
.
run
(
context
->
{
this
.
context
.
refresh
();
Jackson2ObjectMapperBuilder
builder
=
context
Jackson2ObjectMapperBuilder
builder
=
this
.
context
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
);
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
);
ObjectMapper
mapper
=
builder
.
build
(
);
ObjectMapper
mapper
=
builder
.
build
();
assertThat
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
.
enabledByDefault
()).
isTrue
();
assertThat
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
.
enabledByDefault
()).
isTrue
();
assertThat
(
mapper
.
getDeserializationConfig
()
assertThat
(
mapper
.
getDeserializationConfig
()
.
isEnabled
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
)).
isFalse
();
.
isEnabled
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
)).
isFals
e
();
assertThat
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
.
enabledByDefault
()).
isTru
e
();
assertThat
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
.
enabledByDefault
()).
isTrue
();
assertThat
(
mapper
.
getDeserializationConfig
()
assertThat
(
mapper
.
getDeserializationConfig
()
.
isEnabled
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
)).
isFalse
();
.
isEnabled
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
)).
isFalse
();
assertThat
(
mapper
.
getSerializationConfig
()
assertThat
(
mapper
.
getSerializationConfig
()
.
isEnabled
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
)).
isFalse
();
.
isEnabled
(
MapperFeature
.
DEFAULT_VIEW_INCLUSION
)).
isFalse
();
assertThat
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
.
enabledByDefault
())
assertThat
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
.
enabledByDefault
())
.
isTrue
();
.
isTrue
();
assertThat
(
mapper
.
getDeserializationConfig
()
assertThat
(
mapper
.
getDeserializationConfig
()
.
isEnabled
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
)).
isFalse
();
.
isEnabled
(
DeserializationFeature
.
FAIL_ON_UNKNOWN_PROPERTIES
)).
isFalse
(
);
}
);
}
}
@Test
@Test
public
void
moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder
()
{
public
void
moduleBeansAndWellKnownModulesAreRegisteredWithTheObjectMapperBuilder
()
{
this
.
context
.
register
(
ModuleConfig
.
class
,
JacksonAutoConfiguration
.
class
);
this
.
context
Runner
.
withUserConfiguration
(
ModuleConfig
.
class
).
run
(
context
->
{
this
.
context
.
refresh
();
ObjectMapper
objectMapper
=
context
ObjectMapper
objectMapper
=
this
.
context
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
assertThat
(
context
.
getBean
(
CustomModule
.
class
).
getOwners
())
assertThat
(
this
.
context
.
getBean
(
CustomModule
.
class
).
getOwners
())
.
contains
((
ObjectCodec
)
objectMapper
);
.
contains
((
ObjectCodec
)
objectMapper
);
assertThat
(
objectMapper
.
canSerialize
(
LocalDateTime
.
class
)).
isTrue
(
);
assertThat
(
objectMapper
.
canSerialize
(
LocalDateTime
.
class
)).
isTrue
();
assertThat
(
objectMapper
.
canSerialize
(
Baz
.
class
)).
isTrue
();
assertThat
(
objectMapper
.
canSerialize
(
Baz
.
class
)).
isTrue
(
);
}
);
}
}
@Test
@Test
public
void
defaultSerializationInclusion
()
{
public
void
defaultSerializationInclusion
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
context
Runner
.
run
(
context
->
{
this
.
context
.
refresh
();
ObjectMapper
objectMapper
=
context
ObjectMapper
objectMapper
=
this
.
context
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
assertThat
(
objectMapper
.
getSerializationConfig
().
getDefaultPropertyInclusion
()
assertThat
(
objectMapper
.
getSerializationConfig
().
getDefaultPropertyInclusion
()
.
getValueInclusion
()).
isEqualTo
(
JsonInclude
.
Include
.
USE_DEFAULTS
);
.
getValueInclusion
()).
isEqualTo
(
JsonInclude
.
Include
.
USE_DEFAULTS
);
}
);
}
}
@Test
@Test
public
void
customSerializationInclusion
()
{
public
void
customSerializationInclusion
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.default-property-inclusion:non_null"
)
TestPropertyValues
.
of
(
"spring.jackson.default-property-inclusion:non_null"
)
.
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
objectMapper
=
context
this
.
context
.
refresh
();
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
ObjectMapper
objectMapper
=
this
.
context
assertThat
(
objectMapper
.
getSerializationConfig
().
getDefaultPropertyInclusion
()
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
.
getValueInclusion
()).
isEqualTo
(
JsonInclude
.
Include
.
NON_NULL
);
assertThat
(
objectMapper
.
getSerializationConfig
().
getDefaultPropertyInclusion
()
});
.
getValueInclusion
()).
isEqualTo
(
JsonInclude
.
Include
.
NON_NULL
);
}
}
@Test
@Test
public
void
customTimeZoneFormattingADateTime
()
throws
JsonProcessingException
{
public
void
customTimeZoneFormattingADateTime
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.time-zone:America/Los_Angeles"
,
TestPropertyValues
.
of
(
"spring.jackson.time-zone:America/Los_Angeles"
)
"spring.jackson.date-format:zzzz"
,
"spring.jackson.locale:en"
).
run
(
context
->
{
.
applyTo
(
this
.
context
);
ObjectMapper
objectMapper
=
context
TestPropertyValues
.
of
(
"spring.jackson.date-format:zzzz"
).
applyTo
(
this
.
context
);
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
TestPropertyValues
.
of
(
"spring.jackson.locale:en"
).
applyTo
(
this
.
context
);
DateTime
dateTime
=
new
DateTime
(
1436966242231L
,
DateTimeZone
.
UTC
);
this
.
context
.
refresh
();
assertThat
(
objectMapper
.
writeValueAsString
(
dateTime
))
ObjectMapper
objectMapper
=
this
.
context
.
isEqualTo
(
"\"Pacific Daylight Time\""
);
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
});
DateTime
dateTime
=
new
DateTime
(
1436966242231L
,
DateTimeZone
.
UTC
);
assertThat
(
objectMapper
.
writeValueAsString
(
dateTime
))
.
isEqualTo
(
"\"Pacific Daylight Time\""
);
}
}
@Test
@Test
public
void
customTimeZoneFormattingADate
()
throws
JsonProcessingException
{
public
void
customTimeZoneFormattingADate
()
throws
JsonProcessingException
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.time-zone:GMT+10"
,
TestPropertyValues
.
of
(
"spring.jackson.time-zone:GMT+10"
).
applyTo
(
this
.
context
);
"spring.jackson.date-format:z"
).
run
(
context
->
{
TestPropertyValues
.
of
(
"spring.jackson.date-format:z"
).
applyTo
(
this
.
context
);
ObjectMapper
objectMapper
=
context
this
.
context
.
refresh
();
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
ObjectMapper
objectMapper
=
this
.
context
Date
date
=
new
Date
(
1436966242231L
);
.
getBean
(
Jackson2ObjectMapperBuilder
.
class
).
build
();
assertThat
(
objectMapper
.
writeValueAsString
(
date
)).
isEqualTo
(
"\"GMT+10:00\""
);
Date
date
=
new
Date
(
1436966242231L
);
});
assertThat
(
objectMapper
.
writeValueAsString
(
date
)).
isEqualTo
(
"\"GMT+10:00\""
);
}
}
@Test
@Test
public
void
customLocaleWithJodaTime
()
throws
JsonProcessingException
{
public
void
customLocaleWithJodaTime
()
throws
JsonProcessingException
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
this
.
contextRunner
.
withPropertyValues
(
"spring.jackson.locale:de_DE"
,
"spring.jackson.date-format:zzzz"
,
context
.
register
(
JacksonAutoConfiguration
.
class
);
"spring.jackson.serialization.write-dates-with-zone-id:true"
).
run
(
context
->
{
TestPropertyValues
ObjectMapper
objectMapper
=
context
.
getBean
(
ObjectMapper
.
class
);
.
of
(
"spring.jackson.locale:de_DE"
,
"spring.jackson.date-format:zzzz"
,
DateTime
jodaTime
=
new
DateTime
(
1478424650000L
,
"spring.jackson.serialization.write-dates-with-zone-id:true"
)
DateTimeZone
.
forID
(
"Europe/Rome"
));
.
applyTo
(
context
);
assertThat
(
objectMapper
.
writeValueAsString
(
jodaTime
))
context
.
refresh
();
.
startsWith
(
"\"Mitteleuropäische "
);
ObjectMapper
objectMapper
=
context
.
getBean
(
ObjectMapper
.
class
);
});
DateTime
jodaTime
=
new
DateTime
(
1478424650000L
,
DateTimeZone
.
forID
(
"Europe/Rome"
));
assertThat
(
objectMapper
.
writeValueAsString
(
jodaTime
))
.
startsWith
(
"\"Mitteleuropäische "
);
}
}
@Test
@Test
public
void
additionalJacksonBuilderCustomization
()
{
public
void
additionalJacksonBuilderCustomization
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
,
this
.
contextRunner
.
withUserConfiguration
(
ObjectMapperBuilderCustomConfig
.
class
).
run
(
context
->
{
ObjectMapperBuilderCustomConfig
.
class
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
this
.
context
.
refresh
();
assertThat
(
mapper
.
getDateFormat
()).
isInstanceOf
(
MyDateFormat
.
class
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
});
assertThat
(
mapper
.
getDateFormat
()).
isInstanceOf
(
MyDateFormat
.
class
);
}
}
@Test
@Test
...
@@ -450,27 +402,27 @@ public class JacksonAutoConfigurationTests {
...
@@ -450,27 +402,27 @@ public class JacksonAutoConfigurationTests {
}
}
@Test
@Test
public
void
writeDatesAsTimestampsDefault
()
throws
Exception
{
public
void
writeDatesAsTimestampsDefault
()
{
this
.
context
.
register
(
JacksonAutoConfiguration
.
class
);
this
.
context
Runner
.
run
(
context
->
{
this
.
context
.
refresh
(
);
ObjectMapper
mapper
=
context
.
getBean
(
ObjectMapper
.
class
);
ObjectMapper
mapper
=
this
.
context
.
getBean
(
ObjectMapper
.
class
);
DateTime
dateTime
=
new
DateTime
(
1988
,
6
,
25
,
20
,
30
,
DateTimeZone
.
UTC
);
DateTime
dateTime
=
new
DateTime
(
1988
,
6
,
25
,
20
,
30
,
DateTimeZone
.
UTC
);
String
expected
=
FormatConfig
.
DEFAULT_DATETIME_PRINTER
.
rawFormatter
()
String
expected
=
FormatConfig
.
DEFAULT_DATETIME_PRINTER
.
rawFormatter
(
)
.
withZone
(
DateTimeZone
.
UTC
)
.
withZone
(
DateTimeZone
.
UTC
)
.
print
(
dateTime
);
.
print
(
dateTime
);
assertThat
(
mapper
.
writeValueAsString
(
dateTime
)).
isEqualTo
(
"\""
+
expected
+
"\""
);
assertThat
(
mapper
.
writeValueAsString
(
dateTime
)).
isEqualTo
(
"\""
+
expected
+
"\""
);
}
);
}
}
private
void
assertParameterNamesModuleCreatorBinding
(
Mode
expectedMode
,
private
void
assertParameterNamesModuleCreatorBinding
(
Mode
expectedMode
,
Class
<?>...
configClasses
)
{
Class
<?>...
configClasses
)
{
this
.
context
.
register
(
configClasses
);
this
.
context
Runner
.
withUserConfiguration
(
configClasses
).
run
(
context
->
{
this
.
context
.
refresh
();
DeserializationConfig
deserializationConfig
=
context
DeserializationConfig
deserializationConfig
=
this
.
context
.
getBean
(
ObjectMapper
.
class
).
getDeserializationConfig
();
.
getBean
(
ObjectMapper
.
class
).
getDeserializationConfig
();
AnnotationIntrospector
annotationIntrospector
=
deserializationConfig
AnnotationIntrospector
annotationIntrospector
=
deserializationConfig
.
getAnnotationIntrospector
().
allIntrospectors
().
iterator
().
next
();
.
getAnnotationIntrospector
().
allIntrospectors
().
iterator
().
next
();
assertThat
(
ReflectionTestUtils
.
getField
(
annotationIntrospector
,
"creatorBinding"
))
assertThat
(
ReflectionTestUtils
.
getField
(
annotationIntrospector
,
"creatorBinding"
))
.
isEqualTo
(
expectedMode
);
.
isEqualTo
(
expectedMode
);
}
);
}
}
public
static
class
MyDateFormat
extends
SimpleDateFormat
{
public
static
class
MyDateFormat
extends
SimpleDateFormat
{
...
...
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