File tree Expand file tree Collapse file tree 3 files changed +37
-5
lines changed
spring-boot/configuration/src
main/java/io/reflectoring/validation
test/java/io/reflectoring/validation Expand file tree Collapse file tree 3 files changed +37
-5
lines changed Original file line number Diff line number Diff line change 1
1
package io .reflectoring .validation ;
2
2
3
3
import org .springframework .boot .context .properties .ConfigurationProperties ;
4
+ import org .springframework .validation .Errors ;
5
+ import org .springframework .validation .Validator ;
4
6
import org .springframework .validation .annotation .Validated ;
5
7
6
8
import javax .validation .Valid ;
7
9
import javax .validation .constraints .NotBlank ;
8
10
9
11
@ Validated
10
12
@ ConfigurationProperties (prefix = "app.properties" )
11
- class AppProperties {
13
+ class AppProperties implements Validator {
12
14
13
15
@ NotBlank
14
16
private String name ;
15
17
16
18
@ Valid
17
19
private ReportProperties report ;
18
20
21
+ private static final String APP_BASE_NAME = "Application" ;
22
+
23
+ public boolean supports (Class clazz ) {
24
+ return AppProperties .class .isAssignableFrom (clazz );
25
+ }
26
+
27
+ public void validate (Object target , Errors errors ) {
28
+
29
+ AppProperties appProperties = (AppProperties ) target ;
30
+ if (!appProperties .getName ().endsWith (APP_BASE_NAME )) {
31
+ errors .rejectValue ("name" , "field.name.malformed" ,
32
+ new Object []{APP_BASE_NAME },
33
+ "The application name must contain [" + APP_BASE_NAME + "] base name" );
34
+ }
35
+
36
+ }
37
+
19
38
public String getName () {
20
39
return name ;
21
40
}
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ void setup() {
37
37
}
38
38
39
39
@ Test
40
- void whenGivenNameEmpty_thenNotEmptyValidationFails () {
40
+ void whenGivenNameEmpty_thenNotBlankValidationFails () {
41
41
42
42
properties .put ("app.properties.name" , "" );
43
43
@@ -49,6 +49,19 @@ void whenGivenNameEmpty_thenNotEmptyValidationFails() {
49
49
50
50
}
51
51
52
+ @ Test
53
+ void whenGivenNameDoesNotContainBaseName_thenCustomAppPropertiesValidatorFails () {
54
+
55
+ properties .put ("app.properties.name" , "My App" );
56
+
57
+ assertThatThrownBy (application ::run )
58
+ .isInstanceOf (ConfigurationPropertiesBindException .class )
59
+ .hasRootCauseInstanceOf (BindValidationException .class )
60
+ .hasStackTraceContaining ("Field error in object 'app.properties' on field 'name'" )
61
+ .hasStackTraceContaining ("[The application name must contain [Application] base name]" );
62
+
63
+ }
64
+
52
65
@ Test
53
66
void whenGivenReportIntervalInDaysMoreThan30_thenMaxValidationFails () {
54
67
@@ -102,7 +115,7 @@ void whenGivenReportEmailAddressDoesNotContainAnalysisappDomain_thenCustomEmailV
102
115
}
103
116
104
117
@ Test
105
- void whenGivenThirdPartyComponentNameIsEmpty_thenNotEmptyValidationFails () {
118
+ void whenGivenThirdPartyComponentNameIsEmpty_thenNotBlankValidationFails () {
106
119
107
120
properties .put ("app.third-party.properties.name" , "" );
108
121
Original file line number Diff line number Diff line change 8
8
import static org .assertj .core .api .Assertions .assertThat ;
9
9
10
10
@ SpringBootTest (properties = {
11
- "app.properties.name=My Test App " ,
11
+ "app.properties.name=My Test Application " ,
12
12
"app.properties.report.send-emails=true" ,
13
13
"app.properties.report.type=PLAIN_TEXT" ,
14
14
"app.properties.report.interval-in-days=14" ,
@@ -26,7 +26,7 @@ class PropertiesValidInputTest {
26
26
@ Test
27
27
void appPropertiesAreLoaded () {
28
28
assertThat (appProperties ).isNotNull ();
29
- assertThat (appProperties .getName ()).isEqualTo ("My Test App " );
29
+ assertThat (appProperties .getName ()).isEqualTo ("My Test Application " );
30
30
assertThat (appProperties .getReport ()).isNotNull ();
31
31
assertThat (appProperties .getReport ().getSendEmails ()).isTrue ();
32
32
assertThat (appProperties .getReport ().getType ()).isEqualTo (ReportType .PLAIN_TEXT );
You can’t perform that action at this time.
0 commit comments