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
d1359220
Commit
d1359220
authored
Apr 01, 2021
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.3.x' into 2.4.x
Closes gh-25861
parents
aa003f23
7d632ea3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
93 additions
and
119 deletions
+93
-119
ValidationAutoConfigurationTests.java
...onfigure/validation/ValidationAutoConfigurationTests.java
+93
-119
No files found.
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/validation/ValidationAutoConfigurationTests.java
View file @
d1359220
/*
/*
* Copyright 2012-202
0
the original author or authors.
* Copyright 2012-202
1
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -24,12 +24,14 @@ import javax.validation.Validator;
...
@@ -24,12 +24,14 @@ import javax.validation.Validator;
import
javax.validation.constraints.Min
;
import
javax.validation.constraints.Min
;
import
javax.validation.constraints.Size
;
import
javax.validation.constraints.Size
;
import
org.junit.jupiter.api.AfterEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.beans.factory.support.BeanDefinitionRegistry
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.validation.ValidationAutoConfigurationTests.CustomValidatorConfiguration.TestBeanPostProcessor
;
import
org.springframework.boot.autoconfigure.validation.ValidationAutoConfigurationTests.CustomValidatorConfiguration.TestBeanPostProcessor
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.boot.test.context.assertj.AssertableApplicationContext
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.boot.validation.beanvalidation.MethodValidationExcludeFilter
;
import
org.springframework.boot.validation.beanvalidation.MethodValidationExcludeFilter
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
...
@@ -55,171 +57,143 @@ import static org.mockito.Mockito.mock;
...
@@ -55,171 +57,143 @@ import static org.mockito.Mockito.mock;
*/
*/
class
ValidationAutoConfigurationTests
{
class
ValidationAutoConfigurationTests
{
private
AnnotationConfigApplicationContext
context
;
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
(
AnnotationConfigApplicationContext:
:
new
)
@AfterEach
.
withConfiguration
(
AutoConfigurations
.
of
(
ValidationAutoConfiguration
.
class
));
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
@Test
void
validationAutoConfigurationShouldConfigureDefaultValidator
()
{
void
validationAutoConfigurationShouldConfigureDefaultValidator
()
{
load
(
Config
.
class
);
this
.
contextRunner
.
run
((
context
)
->
{
String
[]
jsrValidatorNames
=
this
.
context
.
getBeanNamesForType
(
Validator
.
class
);
assertThat
(
context
.
getBeanNamesForType
(
Validator
.
class
)).
containsExactly
(
"defaultValidator"
);
String
[]
springValidatorNames
=
this
.
context
assertThat
(
context
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
))
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
);
.
containsExactly
(
"defaultValidator"
);
assertThat
(
jsrValidatorNames
).
containsExactly
(
"defaultValidator"
);
assertThat
(
context
.
getBean
(
Validator
.
class
)).
isInstanceOf
(
LocalValidatorFactoryBean
.
class
)
assertThat
(
springValidatorNames
).
containsExactly
(
"defaultValidator"
);
.
isEqualTo
(
context
.
getBean
(
org
.
springframework
.
validation
.
Validator
.
class
));
Validator
jsrValidator
=
this
.
context
.
getBean
(
Validator
.
class
);
assertThat
(
isPrimaryBean
(
context
,
"defaultValidator"
)).
isTrue
();
org
.
springframework
.
validation
.
Validator
springValidator
=
this
.
context
});
.
getBean
(
org
.
springframework
.
validation
.
Validator
.
class
);
assertThat
(
jsrValidator
).
isInstanceOf
(
LocalValidatorFactoryBean
.
class
);
assertThat
(
jsrValidator
).
isEqualTo
(
springValidator
);
assertThat
(
isPrimaryBean
(
"defaultValidator"
)).
isTrue
();
}
}
@Test
@Test
void
validationAutoConfigurationWhenUserProvidesValidatorShouldBackOff
()
{
void
validationAutoConfigurationWhenUserProvidesValidatorShouldBackOff
()
{
load
(
UserDefinedValidatorConfig
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
UserDefinedValidatorConfig
.
class
).
run
((
context
)
->
{
String
[]
jsrValidatorNames
=
this
.
context
.
getBeanNamesForType
(
Validator
.
class
);
assertThat
(
context
.
getBeanNamesForType
(
Validator
.
class
)).
containsExactly
(
"customValidator"
);
String
[]
springValidatorNames
=
this
.
context
assertThat
(
context
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
))
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
);
.
containsExactly
(
"customValidator"
);
assertThat
(
jsrValidatorNames
).
containsExactly
(
"customValidator"
);
assertThat
(
context
.
getBean
(
Validator
.
class
)).
isInstanceOf
(
OptionalValidatorFactoryBean
.
class
)
assertThat
(
springValidatorNames
).
containsExactly
(
"customValidator"
);
.
isEqualTo
(
context
.
getBean
(
org
.
springframework
.
validation
.
Validator
.
class
));
org
.
springframework
.
validation
.
Validator
springValidator
=
this
.
context
assertThat
(
isPrimaryBean
(
context
,
"customValidator"
)).
isFalse
();
.
getBean
(
org
.
springframework
.
validation
.
Validator
.
class
);
});
Validator
jsrValidator
=
this
.
context
.
getBean
(
Validator
.
class
);
assertThat
(
jsrValidator
).
isInstanceOf
(
OptionalValidatorFactoryBean
.
class
);
assertThat
(
jsrValidator
).
isEqualTo
(
springValidator
);
assertThat
(
isPrimaryBean
(
"customValidator"
)).
isFalse
();
}
}
@Test
@Test
void
validationAutoConfigurationWhenUserProvidesDefaultValidatorShouldNotEnablePrimary
()
{
void
validationAutoConfigurationWhenUserProvidesDefaultValidatorShouldNotEnablePrimary
()
{
load
(
UserDefinedDefaultValidatorConfig
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
UserDefinedDefaultValidatorConfig
.
class
).
run
((
context
)
->
{
String
[]
jsrValidatorNames
=
this
.
context
.
getBeanNamesForType
(
Validator
.
class
);
assertThat
(
context
.
getBeanNamesForType
(
Validator
.
class
)).
containsExactly
(
"defaultValidator"
);
String
[]
springValidatorNames
=
this
.
context
assertThat
(
context
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
))
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
);
.
containsExactly
(
"defaultValidator"
);
assertThat
(
jsrValidatorNames
).
containsExactly
(
"defaultValidator"
);
assertThat
(
isPrimaryBean
(
context
,
"defaultValidator"
)).
isFalse
();
assertThat
(
springValidatorNames
).
containsExactly
(
"defaultValidator"
);
});
assertThat
(
isPrimaryBean
(
"defaultValidator"
)).
isFalse
();
}
}
@Test
@Test
void
validationAutoConfigurationWhenUserProvidesJsrValidatorShouldBackOff
()
{
void
validationAutoConfigurationWhenUserProvidesJsrValidatorShouldBackOff
()
{
load
(
UserDefinedJsrValidatorConfig
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
UserDefinedJsrValidatorConfig
.
class
).
run
((
context
)
->
{
String
[]
jsrValidatorNames
=
this
.
context
.
getBeanNamesForType
(
Validator
.
class
);
assertThat
(
context
.
getBeanNamesForType
(
Validator
.
class
)).
containsExactly
(
"customValidator"
);
String
[]
springValidatorNames
=
this
.
context
assertThat
(
context
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
)).
isEmpty
();
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
);
assertThat
(
isPrimaryBean
(
context
,
"customValidator"
)).
isFalse
();
assertThat
(
jsrValidatorNames
).
containsExactly
(
"customValidator"
);
});
assertThat
(
springValidatorNames
).
isEmpty
();
assertThat
(
isPrimaryBean
(
"customValidator"
)).
isFalse
();
}
}
@Test
@Test
void
validationAutoConfigurationWhenUserProvidesSpringValidatorShouldCreateJsrValidator
()
{
void
validationAutoConfigurationWhenUserProvidesSpringValidatorShouldCreateJsrValidator
()
{
load
(
UserDefinedSpringValidatorConfig
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
UserDefinedSpringValidatorConfig
.
class
).
run
((
context
)
->
{
String
[]
jsrValidatorNames
=
this
.
context
.
getBeanNamesForType
(
Validator
.
class
);
assertThat
(
context
.
getBeanNamesForType
(
Validator
.
class
)).
containsExactly
(
"defaultValidator"
);
String
[]
springValidatorNames
=
this
.
context
assertThat
(
context
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
))
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
);
.
containsExactly
(
"customValidator"
,
"anotherCustomValidator"
,
"defaultValidator"
);
assertThat
(
jsrValidatorNames
).
containsExactly
(
"defaultValidator"
);
assertThat
(
context
.
getBean
(
Validator
.
class
)).
isInstanceOf
(
LocalValidatorFactoryBean
.
class
)
assertThat
(
springValidatorNames
).
containsExactly
(
"customValidator"
,
"anotherCustomValidator"
,
.
isEqualTo
(
context
.
getBean
(
org
.
springframework
.
validation
.
Validator
.
class
));
"defaultValidator"
);
assertThat
(
isPrimaryBean
(
context
,
"defaultValidator"
)).
isTrue
();
Validator
jsrValidator
=
this
.
context
.
getBean
(
Validator
.
class
);
});
org
.
springframework
.
validation
.
Validator
springValidator
=
this
.
context
.
getBean
(
org
.
springframework
.
validation
.
Validator
.
class
);
assertThat
(
jsrValidator
).
isInstanceOf
(
LocalValidatorFactoryBean
.
class
);
assertThat
(
jsrValidator
).
isEqualTo
(
springValidator
);
assertThat
(
isPrimaryBean
(
"defaultValidator"
)).
isTrue
();
}
}
@Test
@Test
void
validationAutoConfigurationWhenUserProvidesPrimarySpringValidatorShouldRemovePrimaryFlag
()
{
void
validationAutoConfigurationWhenUserProvidesPrimarySpringValidatorShouldRemovePrimaryFlag
()
{
load
(
UserDefinedPrimarySpringValidatorConfig
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
UserDefinedPrimarySpringValidatorConfig
.
class
).
run
((
context
)
->
{
String
[]
jsrValidatorNames
=
this
.
context
.
getBeanNamesForType
(
Validator
.
class
);
assertThat
(
context
.
getBeanNamesForType
(
Validator
.
class
)).
containsExactly
(
"defaultValidator"
);
String
[]
springValidatorNames
=
this
.
context
assertThat
(
context
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
))
.
getBeanNamesForType
(
org
.
springframework
.
validation
.
Validator
.
class
);
.
containsExactly
(
"customValidator"
,
"anotherCustomValidator"
,
"defaultValidator"
);
assertThat
(
jsrValidatorNames
).
containsExactly
(
"defaultValidator"
);
assertThat
(
context
.
getBean
(
Validator
.
class
)).
isInstanceOf
(
LocalValidatorFactoryBean
.
class
);
assertThat
(
springValidatorNames
).
containsExactly
(
"customValidator"
,
"anotherCustomValidator"
,
assertThat
(
context
.
getBean
(
org
.
springframework
.
validation
.
Validator
.
class
))
"defaultValidator"
);
.
isEqualTo
(
context
.
getBean
(
"anotherCustomValidator"
));
Validator
jsrValidator
=
this
.
context
.
getBean
(
Validator
.
class
);
assertThat
(
isPrimaryBean
(
context
,
"defaultValidator"
)).
isFalse
();
org
.
springframework
.
validation
.
Validator
springValidator
=
this
.
context
});
.
getBean
(
org
.
springframework
.
validation
.
Validator
.
class
);
assertThat
(
jsrValidator
).
isInstanceOf
(
LocalValidatorFactoryBean
.
class
);
assertThat
(
springValidator
).
isEqualTo
(
this
.
context
.
getBean
(
"anotherCustomValidator"
));
assertThat
(
isPrimaryBean
(
"defaultValidator"
)).
isFalse
();
}
}
@Test
@Test
void
validationIsEnabled
()
{
void
validationIsEnabled
()
{
load
(
SampleService
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
SampleService
.
class
).
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
assertThat
(
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
SampleService
service
=
this
.
context
.
getBean
(
SampleService
.
class
);
SampleService
service
=
context
.
getBean
(
SampleService
.
class
);
service
.
doSomething
(
"Valid"
);
service
.
doSomething
(
"Valid"
);
assertThatExceptionOfType
(
ConstraintViolationException
.
class
).
isThrownBy
(()
->
service
.
doSomething
(
"KO"
));
assertThatExceptionOfType
(
ConstraintViolationException
.
class
).
isThrownBy
(()
->
service
.
doSomething
(
"KO"
));
});
}
}
@Test
@Test
void
classCanBeExcludedFromValidation
()
{
void
classCanBeExcludedFromValidation
()
{
load
(
ExcludedServiceConfiguration
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
ExcludedServiceConfiguration
.
class
).
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
assertThat
(
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
ExcludedService
service
=
this
.
context
.
getBean
(
ExcludedService
.
class
);
ExcludedService
service
=
context
.
getBean
(
ExcludedService
.
class
);
service
.
doSomething
(
"Valid"
);
service
.
doSomething
(
"Valid"
);
assertThatNoException
().
isThrownBy
(()
->
service
.
doSomething
(
"KO"
));
assertThatNoException
().
isThrownBy
(()
->
service
.
doSomething
(
"KO"
));
});
}
}
@Test
@Test
void
validationUsesCglibProxy
()
{
void
validationUsesCglibProxy
()
{
load
(
DefaultAnotherSampleService
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
DefaultAnotherSampleService
.
class
).
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
assertThat
(
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
DefaultAnotherSampleService
service
=
this
.
context
.
getBean
(
DefaultAnotherSampleService
.
class
);
DefaultAnotherSampleService
service
=
context
.
getBean
(
DefaultAnotherSampleService
.
class
);
service
.
doSomething
(
42
);
service
.
doSomething
(
42
);
assertThatExceptionOfType
(
ConstraintViolationException
.
class
).
isThrownBy
(()
->
service
.
doSomething
(
2
));
assertThatExceptionOfType
(
ConstraintViolationException
.
class
).
isThrownBy
(()
->
service
.
doSomething
(
2
));
});
}
}
@Test
@Test
void
validationCanBeConfiguredToUseJdkProxy
()
{
void
validationCanBeConfiguredToUseJdkProxy
()
{
load
(
AnotherSampleServiceConfiguration
.
class
,
"spring.aop.proxy-target-class=false"
);
this
.
contextRunner
.
withUserConfiguration
(
AnotherSampleServiceConfiguration
.
class
)
assertThat
(
this
.
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
.
withPropertyValues
(
"spring.aop.proxy-target-class=false"
).
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeansOfType
(
DefaultAnotherSampleService
.
class
)).
isEmpty
();
assertThat
(
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
AnotherSampleService
service
=
this
.
context
.
getBean
(
AnotherSampleService
.
class
);
assertThat
(
context
.
getBeansOfType
(
DefaultAnotherSampleService
.
class
)).
isEmpty
();
service
.
doSomething
(
42
);
AnotherSampleService
service
=
context
.
getBean
(
AnotherSampleService
.
class
);
assertThatExceptionOfType
(
ConstraintViolationException
.
class
).
isThrownBy
(()
->
service
.
doSomething
(
2
));
service
.
doSomething
(
42
);
assertThatExceptionOfType
(
ConstraintViolationException
.
class
)
.
isThrownBy
(()
->
service
.
doSomething
(
2
));
});
}
}
@Test
@Test
void
userDefinedMethodValidationPostProcessorTakesPrecedence
()
{
void
userDefinedMethodValidationPostProcessorTakesPrecedence
()
{
load
(
SampleConfiguration
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
SampleConfiguration
.
class
).
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
assertThat
(
context
.
getBeansOfType
(
Validator
.
class
)).
hasSize
(
1
);
Object
userMethodValidationPostProcessor
=
this
.
context
.
getBean
(
"testMethodValidationPostProcessor"
);
Object
userMethodValidationPostProcessor
=
context
.
getBean
(
"testMethodValidationPostProcessor"
);
assertThat
(
this
.
context
.
getBean
(
MethodValidationPostProcessor
.
class
))
assertThat
(
context
.
getBean
(
MethodValidationPostProcessor
.
class
))
.
isSameAs
(
userMethodValidationPostProcessor
);
.
isSameAs
(
userMethodValidationPostProcessor
);
assertThat
(
this
.
context
.
getBeansOfType
(
MethodValidationPostProcessor
.
class
)).
hasSize
(
1
);
assertThat
(
context
.
getBeansOfType
(
MethodValidationPostProcessor
.
class
)).
hasSize
(
1
);
assertThat
(
this
.
context
.
getBean
(
Validator
.
class
))
assertThat
(
context
.
getBean
(
Validator
.
class
))
.
isNotSameAs
(
ReflectionTestUtils
.
getField
(
userMethodValidationPostProcessor
,
"validator"
));
.
isNotSameAs
(
ReflectionTestUtils
.
getField
(
userMethodValidationPostProcessor
,
"validator"
));
});
}
}
@Test
@Test
void
methodValidationPostProcessorValidatorDependencyDoesNotTriggerEarlyInitialization
()
{
void
methodValidationPostProcessorValidatorDependencyDoesNotTriggerEarlyInitialization
()
{
load
(
CustomValidatorConfiguration
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
CustomValidatorConfiguration
.
class
)
assertThat
(
this
.
context
.
getBean
(
TestBeanPostProcessor
.
class
).
postProcessed
).
contains
(
"someService"
);
.
run
((
context
)
->
assertThat
(
context
.
getBean
(
TestBeanPostProcessor
.
class
).
postProcessed
)
.
contains
(
"someService"
));
}
}
private
boolean
isPrimaryBean
(
String
beanName
)
{
private
boolean
isPrimaryBean
(
AssertableApplicationContext
context
,
String
beanName
)
{
return
this
.
context
.
getBeanDefinition
(
beanName
).
isPrimary
();
return
((
BeanDefinitionRegistry
)
context
.
getSourceApplicationContext
()).
getBeanDefinition
(
beanName
).
isPrimary
();
}
private
void
load
(
Class
<?>
config
,
String
...
environment
)
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
environment
).
applyTo
(
ctx
);
if
(
config
!=
null
)
{
ctx
.
register
(
config
);
}
ctx
.
register
(
ValidationAutoConfiguration
.
class
);
ctx
.
refresh
();
this
.
context
=
ctx
;
}
}
@Configuration
(
proxyBeanMethods
=
false
)
@Configuration
(
proxyBeanMethods
=
false
)
...
...
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