Skip to content

Commit 03961e6

Browse files
committed
Polish contribution
Closes gh-7004
1 parent 7fcb197 commit 03961e6

File tree

6 files changed

+78
-32
lines changed

6 files changed

+78
-32
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.charset.Charset;
2020
import java.util.LinkedHashMap;
2121
import java.util.Map;
22+
import java.util.UUID;
2223

2324
import javax.sql.DataSource;
2425

@@ -58,9 +59,9 @@ public class DataSourceProperties
5859
private String name = "testdb";
5960

6061
/**
61-
* If <code>true</code> the database name is randomly generated.
62+
* Generate a random datasource name.
6263
*/
63-
private boolean generateName = false;
64+
private boolean generateUniqueName;
6465

6566
/**
6667
* Fully qualified name of the connection pool implementation to use. By default, it
@@ -153,6 +154,8 @@ public class DataSourceProperties
153154

154155
private Xa xa = new Xa();
155156

157+
private String uniqueName;
158+
156159
@Override
157160
public void setBeanClassLoader(ClassLoader classLoader) {
158161
this.classLoader = classLoader;
@@ -188,12 +191,12 @@ public void setName(String name) {
188191
this.name = name;
189192
}
190193

191-
public boolean isGenerateName() {
192-
return this.generateName;
194+
public boolean isGenerateUniqueName() {
195+
return this.generateUniqueName;
193196
}
194197

195-
public void setGenerateName(boolean generateName) {
196-
this.generateName = generateName;
198+
public void setGenerateUniqueName(boolean generateUniqueName) {
199+
this.generateUniqueName = generateUniqueName;
197200
}
198201

199202
public Class<? extends DataSource> getType() {
@@ -281,14 +284,24 @@ public String determineUrl() {
281284
if (StringUtils.hasText(this.url)) {
282285
return this.url;
283286
}
284-
String url = this.embeddedDatabaseConnection.getUrl(this.name);
287+
String url = this.embeddedDatabaseConnection.getUrl(determineDatabaseName());
285288
if (!StringUtils.hasText(url)) {
286289
throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection,
287290
this.environment, "url");
288291
}
289292
return url;
290293
}
291294

295+
private String determineDatabaseName() {
296+
if (this.generateUniqueName) {
297+
if (this.uniqueName == null) {
298+
this.uniqueName = UUID.randomUUID().toString();
299+
}
300+
return this.uniqueName;
301+
}
302+
return this.name;
303+
}
304+
292305
/**
293306
* Return the configured username or {@code null} if none was configured.
294307
* @return the configured username

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,8 @@ public void setBeanClassLoader(ClassLoader classLoader) {
5555
public EmbeddedDatabase dataSource() {
5656
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
5757
.setType(EmbeddedDatabaseConnection.get(this.classLoader).getType());
58-
if (this.properties.isGenerateName()) {
59-
builder.generateUniqueName(true);
60-
}
61-
else {
62-
builder.setName(this.properties.getName());
63-
}
64-
this.database = builder.build();
58+
this.database = builder.setName(this.properties.getName())
59+
.generateUniqueName(this.properties.isGenerateUniqueName()).build();
6560
return this.database;
6661
}
6762

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ public void determineUrlWithExplicitConfig() throws Exception {
6666
assertThat(properties.determineUrl()).isEqualTo("jdbc:mysql://mydb");
6767
}
6868

69+
@Test
70+
public void determineUrlWithGenerateUniqueName() throws Exception {
71+
DataSourceProperties properties = new DataSourceProperties();
72+
properties.setGenerateUniqueName(true);
73+
properties.afterPropertiesSet();
74+
assertThat(properties.determineUrl()).isEqualTo(properties.determineUrl());
75+
76+
DataSourceProperties properties2 = new DataSourceProperties();
77+
properties2.setGenerateUniqueName(true);
78+
properties2.afterPropertiesSet();
79+
assertThat(properties.determineUrl()).isNotEqualTo(properties2.determineUrl());
80+
}
81+
6982
@Test
7083
public void determineUsername() throws Exception {
7184
DataSourceProperties properties = new DataSourceProperties();

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,47 +19,55 @@
1919
import java.sql.Connection;
2020
import java.sql.ResultSet;
2121
import java.sql.SQLException;
22-
import java.util.Properties;
2322

2423
import javax.sql.DataSource;
2524

25+
import org.junit.After;
2626
import org.junit.Test;
2727

28+
import org.springframework.boot.test.util.EnvironmentTestUtils;
2829
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
29-
import org.springframework.core.env.PropertiesPropertySource;
3030

3131
import static org.assertj.core.api.Assertions.assertThat;
3232

3333
/**
3434
* Tests for {@link EmbeddedDataSourceConfiguration}.
3535
*
3636
* @author Dave Syer
37+
* @author Stephane Nicoll
3738
*/
3839
public class EmbeddedDataSourceConfigurationTests {
3940

4041
private AnnotationConfigApplicationContext context;
4142

43+
@After
44+
public void closeContext() {
45+
if (this.context != null) {
46+
this.context.close();
47+
}
48+
}
49+
4250
@Test
43-
public void testDefaultEmbeddedDatabase() throws Exception {
44-
this.context = new AnnotationConfigApplicationContext();
45-
this.context.register(EmbeddedDataSourceConfiguration.class);
46-
this.context.refresh();
51+
public void defaultEmbeddedDatabase() {
52+
this.context = load();
4753
assertThat(this.context.getBean(DataSource.class)).isNotNull();
48-
this.context.close();
4954
}
5055

5156
@Test
52-
public void generatesUniqueDatabaseName() throws Exception {
53-
Properties myProps = new Properties();
54-
myProps.setProperty("spring.datasource.generate-name", "true");
55-
56-
this.context = new AnnotationConfigApplicationContext();
57-
this.context.register(EmbeddedDataSourceConfiguration.class);
58-
this.context.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("whatever", myProps));
59-
this.context.refresh();
60-
DataSource dataSource = this.context.getBean(DataSource.class);
61-
assertThat(getDatabaseName(dataSource)).isNotEqualToIgnoringCase("testdb");
62-
this.context.close();
57+
public void generateUniqueName() throws Exception {
58+
this.context = load("spring.datasource.generate-unique-name=true");
59+
AnnotationConfigApplicationContext context2 =
60+
load("spring.datasource.generate-unique-name=true");
61+
try {
62+
DataSource dataSource = this.context.getBean(DataSource.class);
63+
DataSource dataSource2 = context2.getBean(DataSource.class);
64+
assertThat(getDatabaseName(dataSource))
65+
.isNotEqualTo(getDatabaseName(dataSource2));
66+
System.out.println(dataSource2);
67+
}
68+
finally {
69+
context2.close();
70+
}
6371
}
6472

6573
private String getDatabaseName(DataSource dataSource) throws SQLException {
@@ -78,4 +86,12 @@ private String getDatabaseName(DataSource dataSource) throws SQLException {
7886
}
7987
}
8088

89+
private AnnotationConfigApplicationContext load(String... environment) {
90+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
91+
EnvironmentTestUtils.addEnvironment(ctx, environment);
92+
ctx.register(EmbeddedDataSourceConfiguration.class);
93+
ctx.refresh();
94+
return ctx;
95+
}
96+
8197
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ content into your application; rather pick only the properties that you need.
620620
spring.datasource.dbcp.*= # Commons DBCP specific settings
621621
spring.datasource.dbcp2.*= # Commons DBCP2 specific settings
622622
spring.datasource.driver-class-name= # Fully qualified name of the JDBC driver. Auto-detected based on the URL by default.
623+
spring.datasource.generate-unique-name=false # Generate a random datasource name.
623624
spring.datasource.hikari.*= # Hikari specific settings
624625
spring.datasource.initialize=true # Populate the database using 'data.sql'.
625626
spring.datasource.jmx-enabled=false # Enable JMX support (if provided by the underlying pool).

spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,6 +2655,14 @@ http://hsqldb.org/[HSQL] and http://db.apache.org/derby/[Derby] databases. You d
26552655
to provide any connection URLs, simply include a build dependency to the embedded database
26562656
that you want to use.
26572657

2658+
[NOTE]
2659+
====
2660+
If you are using this feature in your tests, you may notice that the same database is
2661+
reused by your whole test suite regardless of the number of application contexts that
2662+
you use. If you want to make sure that each context has a separate embedded database,
2663+
you should set `spring.datasource.generate-unique-name` to `true`.
2664+
====
2665+
26582666
For example, typical POM dependencies would be:
26592667

26602668
[source,xml,indent=0]

0 commit comments

Comments
 (0)