From ba824b240b24714d9d7783fab9ddd6f25f44a9d2 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Sun, 22 May 2016 10:36:01 +0100 Subject: [PATCH] Add uuid generator to RandomValuePropertySource A well-formed string represenation of a UUID can now be generated by ${random.uuid}. --- .../src/main/asciidoc/spring-boot-features.adoc | 3 ++- .../boot/context/config/RandomValuePropertySource.java | 4 ++++ .../context/config/RandomValuePropertySourceTests.java | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 6b7b5840917..a54e51f0e8b 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -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]} ---- diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java b/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java index 82b067b1a7d..26a755de971 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java @@ -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 { if (range != null) { return getNextLongInRange(range); } + if (type.equals("uuid")) { + return UUID.randomUUID().toString(); + } return getRandomBytes(); } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java b/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java index 45ca29b6811..2941aa11b50 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/config/RandomValuePropertySourceTests.java @@ -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]");