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
914452b2
Commit
914452b2
authored
Jul 29, 2020
by
dreis2211
Committed by
Andy Wilkinson
Jul 30, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow DurationFormat and PeriodFormat to be used on parameters
See gh-22646
parent
c7bfc01b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
3 deletions
+84
-3
DurationFormat.java
...java/org/springframework/boot/convert/DurationFormat.java
+2
-2
PeriodFormat.java
...n/java/org/springframework/boot/convert/PeriodFormat.java
+1
-1
ConfigurationPropertiesTests.java
...boot/context/properties/ConfigurationPropertiesTests.java
+81
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationFormat.java
View file @
914452b2
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -30,7 +30,7 @@ import java.time.Duration;
* @author Phillip Webb
* @since 2.0.0
*/
@Target
(
ElementType
.
FIELD
)
@Target
(
{
ElementType
.
FIELD
,
ElementType
.
PARAMETER
}
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
public
@interface
DurationFormat
{
...
...
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/PeriodFormat.java
View file @
914452b2
...
...
@@ -31,7 +31,7 @@ import java.time.Period;
* @author Edson Chávez
* @since 2.3.0
*/
@Target
(
ElementType
.
FIELD
)
@Target
(
{
ElementType
.
FIELD
,
ElementType
.
PARAMETER
}
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
public
@interface
PeriodFormat
{
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java
View file @
914452b2
...
...
@@ -56,7 +56,11 @@ import org.springframework.boot.context.properties.bind.DefaultValue;
import
org.springframework.boot.context.properties.bind.validation.BindValidationException
;
import
org.springframework.boot.context.properties.source.ConfigurationPropertyName
;
import
org.springframework.boot.convert.DataSizeUnit
;
import
org.springframework.boot.convert.DurationFormat
;
import
org.springframework.boot.convert.DurationStyle
;
import
org.springframework.boot.convert.DurationUnit
;
import
org.springframework.boot.convert.PeriodFormat
;
import
org.springframework.boot.convert.PeriodStyle
;
import
org.springframework.boot.convert.PeriodUnit
;
import
org.springframework.boot.testsupport.system.CapturedOutput
;
import
org.springframework.boot.testsupport.system.OutputCaptureExtension
;
...
...
@@ -808,6 +812,53 @@ class ConfigurationPropertiesTests {
assertThat
(
bean
.
getPeriod
()).
isEqualTo
(
Period
.
ofYears
(
4
));
}
@Test
void
loadWhenBindingToConstructorParametersWithCustomDataFormatShouldBind
()
{
MutablePropertySources
sources
=
this
.
context
.
getEnvironment
().
getPropertySources
();
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
source
.
put
(
"test.duration"
,
"12d"
);
source
.
put
(
"test.period"
,
"13y"
);
sources
.
addLast
(
new
MapPropertySource
(
"test"
,
source
));
load
(
ConstructorParameterWithFormatConfiguration
.
class
);
ConstructorParameterWithFormatProperties
bean
=
this
.
context
.
getBean
(
ConstructorParameterWithFormatProperties
.
class
);
assertThat
(
bean
.
getDuration
()).
isEqualTo
(
Duration
.
ofDays
(
12
));
assertThat
(
bean
.
getPeriod
()).
isEqualTo
(
Period
.
ofYears
(
13
));
}
@Test
void
loadWhenBindingToConstructorParametersWithNotMatchingCustomDurationFormatShouldFail
()
{
MutablePropertySources
sources
=
this
.
context
.
getEnvironment
().
getPropertySources
();
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
source
.
put
(
"test.duration"
,
"P12D"
);
sources
.
addLast
(
new
MapPropertySource
(
"test"
,
source
));
assertThatExceptionOfType
(
Exception
.
class
)
.
isThrownBy
(()
->
load
(
ConstructorParameterWithFormatConfiguration
.
class
)).
satisfies
((
ex
)
->
{
assertThat
(
ex
).
hasCauseInstanceOf
(
BindException
.
class
);
});
}
@Test
void
loadWhenBindingToConstructorParametersWithNotMatchingCustomPeriodFormatShouldFail
()
{
MutablePropertySources
sources
=
this
.
context
.
getEnvironment
().
getPropertySources
();
Map
<
String
,
Object
>
source
=
new
HashMap
<>();
source
.
put
(
"test.period"
,
"P12D"
);
sources
.
addLast
(
new
MapPropertySource
(
"test"
,
source
));
assertThatExceptionOfType
(
Exception
.
class
)
.
isThrownBy
(()
->
load
(
ConstructorParameterWithFormatConfiguration
.
class
)).
satisfies
((
ex
)
->
{
assertThat
(
ex
).
hasCauseInstanceOf
(
BindException
.
class
);
});
}
@Test
void
loadWhenBindingToConstructorParametersWithDefaultDataFormatShouldBind
()
{
load
(
ConstructorParameterWithFormatConfiguration
.
class
);
ConstructorParameterWithFormatProperties
bean
=
this
.
context
.
getBean
(
ConstructorParameterWithFormatProperties
.
class
);
assertThat
(
bean
.
getDuration
()).
isEqualTo
(
Duration
.
ofDays
(
2
));
assertThat
(
bean
.
getPeriod
()).
isEqualTo
(
Period
.
ofYears
(
3
));
}
@Test
void
loadWhenBindingToConstructorParametersShouldValidate
()
{
assertThatExceptionOfType
(
Exception
.
class
)
...
...
@@ -2007,6 +2058,31 @@ class ConfigurationPropertiesTests {
}
@ConstructorBinding
@ConfigurationProperties
(
prefix
=
"test"
)
static
class
ConstructorParameterWithFormatProperties
{
private
final
Duration
duration
;
private
final
Period
period
;
ConstructorParameterWithFormatProperties
(
@DefaultValue
(
"2d"
)
@DurationFormat
(
DurationStyle
.
SIMPLE
)
Duration
duration
,
@DefaultValue
(
"3y"
)
@PeriodFormat
(
PeriodStyle
.
SIMPLE
)
Period
period
)
{
this
.
duration
=
duration
;
this
.
period
=
period
;
}
Duration
getDuration
()
{
return
this
.
duration
;
}
Period
getPeriod
()
{
return
this
.
period
;
}
}
@ConstructorBinding
@ConfigurationProperties
(
prefix
=
"test"
)
@Validated
...
...
@@ -2035,6 +2111,11 @@ class ConfigurationPropertiesTests {
}
@EnableConfigurationProperties
(
ConstructorParameterWithFormatProperties
.
class
)
static
class
ConstructorParameterWithFormatConfiguration
{
}
@EnableConfigurationProperties
(
ConstructorParameterValidatedProperties
.
class
)
static
class
ConstructorParameterValidationConfiguration
{
...
...
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