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
e2cba40d
Commit
e2cba40d
authored
Jun 02, 2021
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.4.x' into main
Closes gh-26739
parents
e9ed92a3
b102fdd3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
14 deletions
+107
-14
RandomValuePropertySource.java
...g/springframework/boot/env/RandomValuePropertySource.java
+56
-14
RandomValuePropertySourceTests.java
...ingframework/boot/env/RandomValuePropertySourceTests.java
+51
-0
No files found.
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/RandomValuePropertySource.java
View file @
e2cba40d
/*
* 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");
* you may not use this file except in compliance with the License.
...
...
@@ -16,8 +16,13 @@
package
org
.
springframework
.
boot
.
env
;
import
java.util.OptionalInt
;
import
java.util.OptionalLong
;
import
java.util.Random
;
import
java.util.UUID
;
import
java.util.function.BiPredicate
;
import
java.util.function.Function
;
import
java.util.function.Predicate
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
...
...
@@ -27,6 +32,7 @@ import org.springframework.core.env.MutablePropertySources;
import
org.springframework.core.env.PropertySource
;
import
org.springframework.core.env.StandardEnvironment
;
import
org.springframework.core.log.LogMessage
;
import
org.springframework.util.Assert
;
import
org.springframework.util.DigestUtils
;
import
org.springframework.util.StringUtils
;
...
...
@@ -46,11 +52,13 @@ import org.springframework.util.StringUtils;
* suffix whose syntax is:
* <p>
* {@code OPEN value (,max) CLOSE} where the {@code OPEN,CLOSE} are any character and
* {@code value,max} are integers. If {@code max} is provided then {@code value} is the
* minimum value and {@code max} is the maximum (exclusive).
* {@code value,max} are integers. If {@code max} is not provided, then 0 is used as the
* lower bound and {@code value} is the upper bound. If {@code max} is provided then
* {@code value} is the minimum value and {@code max} is the maximum (exclusive).
*
* @author Dave Syer
* @author Matt Benson
* @author Madhura Bhave
* @since 1.0.0
*/
public
class
RandomValuePropertySource
extends
PropertySource
<
Random
>
{
...
...
@@ -113,22 +121,21 @@ public class RandomValuePropertySource extends PropertySource<Random> {
}
private
int
getNextIntInRange
(
String
range
)
{
String
[]
tokens
=
StringUtils
.
commaDelimitedListToStringArray
(
range
);
int
start
=
Integer
.
parseInt
(
tokens
[
0
]
);
if
(
tokens
.
length
==
1
)
{
return
getSource
().
nextInt
(
start
);
Range
<
Integer
>
intRange
=
Range
.
get
(
range
,
Integer:
:
parseInt
,
(
t
)
->
t
>
0
,
0
,
(
t1
,
t2
)
->
t1
<
t2
);
OptionalInt
first
=
getSource
().
ints
(
1
,
intRange
.
getMin
(),
intRange
.
getMax
()).
findFirst
(
);
if
(
!
first
.
isPresent
()
)
{
throw
new
RuntimeException
(
"Could not get random number for range '"
+
range
+
"'"
);
}
return
start
+
getSource
().
nextInt
(
Integer
.
parseInt
(
tokens
[
1
])
-
start
);
return
first
.
getAsInt
(
);
}
private
long
getNextLongInRange
(
String
range
)
{
String
[]
tokens
=
StringUtils
.
commaDelimitedListToStringArray
(
range
);
if
(
tokens
.
length
==
1
)
{
return
Math
.
abs
(
getSource
().
nextLong
()
%
Long
.
parseLong
(
tokens
[
0
]));
Range
<
Long
>
longRange
=
Range
.
get
(
range
,
Long:
:
parseLong
,
(
t
)
->
t
>
0L
,
0L
,
(
t1
,
t2
)
->
t1
<
t2
);
OptionalLong
first
=
getSource
().
longs
(
1
,
longRange
.
getMin
(),
longRange
.
getMax
()).
findFirst
();
if
(!
first
.
isPresent
())
{
throw
new
RuntimeException
(
"Could not get random number for range '"
+
range
+
"'"
);
}
long
lowerBound
=
Long
.
parseLong
(
tokens
[
0
]);
long
upperBound
=
Long
.
parseLong
(
tokens
[
1
])
-
lowerBound
;
return
lowerBound
+
Math
.
abs
(
getSource
().
nextLong
()
%
upperBound
);
return
first
.
getAsLong
();
}
private
Object
getRandomBytes
()
{
...
...
@@ -158,4 +165,39 @@ public class RandomValuePropertySource extends PropertySource<Random> {
logger
.
trace
(
"RandomValuePropertySource add to Environment"
);
}
static
final
class
Range
<
T
extends
Number
>
{
private
final
T
min
;
private
final
T
max
;
private
Range
(
T
min
,
T
max
)
{
this
.
min
=
min
;
this
.
max
=
max
;
}
static
<
T
extends
Number
>
Range
<
T
>
get
(
String
range
,
Function
<
String
,
T
>
parse
,
Predicate
<
T
>
boundValidator
,
T
defaultMin
,
BiPredicate
<
T
,
T
>
rangeValidator
)
{
String
[]
tokens
=
StringUtils
.
commaDelimitedListToStringArray
(
range
);
T
token1
=
parse
.
apply
(
tokens
[
0
]);
if
(
tokens
.
length
==
1
)
{
Assert
.
isTrue
(
boundValidator
.
test
(
token1
),
"Bound must be positive."
);
return
new
Range
<>(
defaultMin
,
token1
);
}
T
token2
=
parse
.
apply
(
tokens
[
1
]);
Assert
.
isTrue
(
rangeValidator
.
test
(
token1
,
token2
),
"Lower bound must be less than upper bound."
);
return
new
Range
<>(
token1
,
token2
);
}
T
getMin
()
{
return
this
.
min
;
}
T
getMax
()
{
return
this
.
max
;
}
}
}
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/RandomValuePropertySourceTests.java
View file @
e2cba40d
...
...
@@ -28,6 +28,7 @@ import org.springframework.core.env.SystemEnvironmentPropertySource;
import
org.springframework.mock.env.MockEnvironment
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatIllegalArgumentException
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Mockito
.
spy
;
...
...
@@ -72,12 +73,37 @@ class RandomValuePropertySourceTests {
assertThat
(
value
<
10
).
isTrue
();
}
@Test
void
intRangeWhenLowerBoundEqualsUpperBoundShouldFailWithIllegalArgumentException
()
{
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
source
.
getProperty
(
"random.int[4,4]"
))
.
withMessage
(
"Lower bound must be less than upper bound."
);
}
@Test
void
intRangeWhenLowerBoundNegative
()
{
Integer
value
=
(
Integer
)
this
.
source
.
getProperty
(
"random.int[-4,4]"
);
assertThat
(
value
>=
-
4
).
isTrue
();
assertThat
(
value
<
4
).
isTrue
();
}
@Test
void
getPropertyWhenIntMaxReturnsValue
()
{
Integer
value
=
(
Integer
)
this
.
source
.
getProperty
(
"random.int(10)"
);
assertThat
(
value
).
isNotNull
().
isLessThan
(
10
);
}
@Test
void
intMaxZero
()
{
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
source
.
getProperty
(
"random.int(0)"
))
.
withMessage
(
"Bound must be positive."
);
}
@Test
void
intNegativeBound
()
{
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
source
.
getProperty
(
"random.int(-5)"
))
.
withMessage
(
"Bound must be positive."
);
}
@Test
void
getPropertyWhenLongReturnsValue
()
{
Long
value
=
(
Long
)
this
.
source
.
getProperty
(
"random.long"
);
...
...
@@ -90,12 +116,37 @@ class RandomValuePropertySourceTests {
assertThat
(
value
).
isNotNull
().
isBetween
(
4L
,
10L
);
}
@Test
void
longRangeWhenLowerBoundEqualsUpperBoundShouldFailWithIllegalArgumentException
()
{
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
source
.
getProperty
(
"random.long[4,4]"
))
.
withMessage
(
"Lower bound must be less than upper bound."
);
}
@Test
void
longRangeWhenLowerBoundNegativeShouldFailWithIllegalArgumentException
()
{
Long
value
=
(
Long
)
this
.
source
.
getProperty
(
"random.long[-4,4]"
);
assertThat
(
value
>=
-
4
).
isTrue
();
assertThat
(
value
<
4
).
isTrue
();
}
@Test
void
getPropertyWhenLongMaxReturnsValue
()
{
Long
value
=
(
Long
)
this
.
source
.
getProperty
(
"random.long(10)"
);
assertThat
(
value
).
isNotNull
().
isLessThan
(
10L
);
}
@Test
void
longMaxZero
()
{
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
source
.
getProperty
(
"random.long(0)"
))
.
withMessage
(
"Bound must be positive."
);
}
@Test
void
longNegativeBound
()
{
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
source
.
getProperty
(
"random.long(-5)"
))
.
withMessage
(
"Bound must be positive."
);
}
@Test
void
getPropertyWhenLongOverflowReturnsValue
()
{
RandomValuePropertySource
source
=
spy
(
this
.
source
);
...
...
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