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
768aa5df
Commit
768aa5df
authored
Apr 01, 2019
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ignore exception if ValidationAdapter can't get a MessageInterpolator
Fixes gh-16177
parent
c592e714
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
55 additions
and
41 deletions
+55
-41
ValidatorAdapter.java
...ework/boot/autoconfigure/validation/ValidatorAdapter.java
+9
-1
ValidatorAdapterTests.java
.../boot/autoconfigure/validation/ValidatorAdapterTests.java
+46
-40
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapter.java
View file @
768aa5df
...
...
@@ -16,6 +16,8 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
validation
;
import
javax.validation.ValidationException
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.DisposableBean
;
import
org.springframework.beans.factory.InitializingBean
;
...
...
@@ -135,7 +137,13 @@ public class ValidatorAdapter implements SmartValidator, ApplicationContextAware
private
static
Validator
create
()
{
OptionalValidatorFactoryBean
validator
=
new
OptionalValidatorFactoryBean
();
validator
.
setMessageInterpolator
(
new
MessageInterpolatorFactory
().
getObject
());
try
{
validator
.
setMessageInterpolator
(
new
MessageInterpolatorFactory
().
getObject
());
}
catch
(
ValidationException
ex
)
{
// Ignore
}
return
wrap
(
validator
,
false
);
}
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapterTests.java
View file @
768aa5df
...
...
@@ -20,13 +20,14 @@ import java.util.HashMap;
import
javax.validation.constraints.Min
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.test.context.FilteredClassLoader
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.validation.MapBindingResult
;
import
org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
;
...
...
@@ -41,60 +42,65 @@ import static org.mockito.Mockito.verify;
* Tests for {@link ValidatorAdapter}.
*
* @author Stephane Nicoll
* @author Madhura Bhave
*/
public
class
ValidatorAdapterTests
{
private
AnnotationConfigApplicationContext
context
;
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
private
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
();
@Test
public
void
wrapLocalValidatorFactoryBean
()
{
ValidatorAdapter
wrapper
=
load
(
LocalValidatorFactoryBeanConfig
.
class
);
assertThat
(
wrapper
.
supports
(
SampleData
.
class
)).
isTrue
();
MapBindingResult
errors
=
new
MapBindingResult
(
new
HashMap
<
String
,
Object
>(),
"test"
);
wrapper
.
validate
(
new
SampleData
(
40
),
errors
);
assertThat
(
errors
.
getErrorCount
()).
isEqualTo
(
1
);
this
.
contextRunner
.
withUserConfiguration
(
LocalValidatorFactoryBeanConfig
.
class
)
.
run
((
context
)
->
{
ValidatorAdapter
wrapper
=
context
.
getBean
(
ValidatorAdapter
.
class
);
assertThat
(
wrapper
.
supports
(
SampleData
.
class
)).
isTrue
();
MapBindingResult
errors
=
new
MapBindingResult
(
new
HashMap
<
String
,
Object
>(),
"test"
);
wrapper
.
validate
(
new
SampleData
(
40
),
errors
);
assertThat
(
errors
.
getErrorCount
()).
isEqualTo
(
1
);
});
}
@Test
public
void
wrapperInvokesCallbackOnNonManagedBean
()
{
load
(
NonManagedBeanConfig
.
class
);
LocalValidatorFactoryBean
validator
=
this
.
context
.
getBean
(
NonManagedBeanConfig
.
class
).
validator
;
verify
(
validator
,
times
(
1
)).
setApplicationContext
(
any
(
ApplicationContext
.
class
));
verify
(
validator
,
times
(
1
)).
afterPropertiesSet
();
verify
(
validator
,
never
()).
destroy
();
this
.
context
.
close
();
this
.
context
=
null
;
verify
(
validator
,
times
(
1
)).
destroy
();
this
.
contextRunner
.
withUserConfiguration
(
NonManagedBeanConfig
.
class
)
.
run
((
context
)
->
{
LocalValidatorFactoryBean
validator
=
context
.
getBean
(
NonManagedBeanConfig
.
class
).
validator
;
verify
(
validator
,
times
(
1
))
.
setApplicationContext
(
any
(
ApplicationContext
.
class
));
verify
(
validator
,
times
(
1
)).
afterPropertiesSet
();
verify
(
validator
,
never
()).
destroy
();
context
.
close
();
verify
(
validator
,
times
(
1
)).
destroy
();
});
}
@Test
public
void
wrapperDoesNotInvokeCallbackOnManagedBean
()
{
load
(
ManagedBeanConfig
.
class
);
LocalValidatorFactoryBean
validator
=
this
.
context
.
getBean
(
ManagedBeanConfig
.
class
).
validator
;
verify
(
validator
,
never
()).
setApplicationContext
(
any
(
ApplicationContext
.
class
));
verify
(
validator
,
never
()).
afterPropertiesSet
();
verify
(
validator
,
never
()).
destroy
();
this
.
context
.
close
();
this
.
context
=
null
;
verify
(
validator
,
never
()).
destroy
();
this
.
contextRunner
.
withUserConfiguration
(
ManagedBeanConfig
.
class
)
.
run
((
context
)
->
{
LocalValidatorFactoryBean
validator
=
context
.
getBean
(
ManagedBeanConfig
.
class
).
validator
;
verify
(
validator
,
never
())
.
setApplicationContext
(
any
(
ApplicationContext
.
class
));
verify
(
validator
,
never
()).
afterPropertiesSet
();
verify
(
validator
,
never
()).
destroy
();
context
.
close
();
verify
(
validator
,
never
()).
destroy
();
});
}
private
ValidatorAdapter
load
(
Class
<?>
config
)
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
ctx
.
register
(
config
);
ctx
.
refresh
();
this
.
context
=
ctx
;
return
this
.
context
.
getBean
(
ValidatorAdapter
.
class
);
@Test
public
void
wrapperWhenValidationProviderNotPresentShouldNotThrowException
()
{
ClassPathResource
hibernateValidator
=
new
ClassPathResource
(
"META-INF/services/javax.validation.spi.ValidationProvider"
);
this
.
contextRunner
.
withClassLoader
(
new
FilteredClassLoader
(
FilteredClassLoader
.
ClassPathResourceFilter
.
of
(
hibernateValidator
),
FilteredClassLoader
.
PackageFilter
.
of
(
"org.hibernate.validator"
)))
.
run
((
context
)
->
ValidatorAdapter
.
get
(
context
,
null
));
}
@Configuration
...
...
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