Add uuid generator to RandomValuePropertySource

A well-formed string represenation of a UUID can now be generated
by ${random.uuid}.
This commit is contained in:
Dave Syer 2016-05-22 10:36:01 +01:00
parent 3cff46f4da
commit ba824b240b
3 changed files with 14 additions and 1 deletions

View File

@ -410,13 +410,14 @@ or as a JNDI variable `java:comp/env/spring.application.json`.
[[boot-features-external-config-random-values]]
=== Configuring random values
The `RandomValuePropertySource` is useful for injecting random values (e.g. into secrets
or test cases). It can produce integers, longs or strings, e.g.
or test cases). It can produce integers, longs, uuids or strings, e.g.
[source,properties,indent=0]
----
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
----

View File

@ -17,6 +17,7 @@
package org.springframework.boot.context.config;
import java.util.Random;
import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -94,6 +95,9 @@ public class RandomValuePropertySource extends PropertySource<Random> {
if (range != null) {
return getNextLongInRange(range);
}
if (type.equals("uuid")) {
return UUID.randomUUID().toString();
}
return getRandomBytes();
}

View File

@ -17,6 +17,7 @@
package org.springframework.boot.context.config;
import java.util.Random;
import java.util.UUID;
import org.junit.Test;
import org.mockito.Mockito;
@ -49,6 +50,13 @@ public class RandomValuePropertySourceTests {
assertThat(value).isNotNull();
}
@Test
public void uuidValue() {
String value = (String) this.source.getProperty("random.uuid");
assertThat(value).isNotNull();
assertThat(UUID.fromString(value)).isNotNull();
}
@Test
public void intRange() {
Integer value = (Integer) this.source.getProperty("random.int[4,10]");