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
7fcb1970
Commit
7fcb1970
authored
Sep 23, 2016
by
Rob Fletcher
Committed by
Stephane Nicoll
Oct 06, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow configuration to specify randomly generated database name
See gh-7004
parent
0f97ccf2
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
56 additions
and
1 deletion
+56
-1
DataSourceProperties.java
...amework/boot/autoconfigure/jdbc/DataSourceProperties.java
+13
-0
EmbeddedDataSourceConfiguration.java
...t/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java
+7
-1
EmbeddedDataSourceConfigurationTests.java
...oconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java
+36
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java
View file @
7fcb1970
...
@@ -57,6 +57,11 @@ public class DataSourceProperties
...
@@ -57,6 +57,11 @@ public class DataSourceProperties
*/
*/
private
String
name
=
"testdb"
;
private
String
name
=
"testdb"
;
/**
* If <code>true</code> the database name is randomly generated.
*/
private
boolean
generateName
=
false
;
/**
/**
* Fully qualified name of the connection pool implementation to use. By default, it
* Fully qualified name of the connection pool implementation to use. By default, it
* is auto-detected from the classpath.
* is auto-detected from the classpath.
...
@@ -183,6 +188,14 @@ public class DataSourceProperties
...
@@ -183,6 +188,14 @@ public class DataSourceProperties
this
.
name
=
name
;
this
.
name
=
name
;
}
}
public
boolean
isGenerateName
()
{
return
this
.
generateName
;
}
public
void
setGenerateName
(
boolean
generateName
)
{
this
.
generateName
=
generateName
;
}
public
Class
<?
extends
DataSource
>
getType
()
{
public
Class
<?
extends
DataSource
>
getType
()
{
return
this
.
type
;
return
this
.
type
;
}
}
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java
View file @
7fcb1970
...
@@ -55,7 +55,13 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
...
@@ -55,7 +55,13 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
public
EmbeddedDatabase
dataSource
()
{
public
EmbeddedDatabase
dataSource
()
{
EmbeddedDatabaseBuilder
builder
=
new
EmbeddedDatabaseBuilder
()
EmbeddedDatabaseBuilder
builder
=
new
EmbeddedDatabaseBuilder
()
.
setType
(
EmbeddedDatabaseConnection
.
get
(
this
.
classLoader
).
getType
());
.
setType
(
EmbeddedDatabaseConnection
.
get
(
this
.
classLoader
).
getType
());
this
.
database
=
builder
.
setName
(
this
.
properties
.
getName
()).
build
();
if
(
this
.
properties
.
isGenerateName
())
{
builder
.
generateUniqueName
(
true
);
}
else
{
builder
.
setName
(
this
.
properties
.
getName
());
}
this
.
database
=
builder
.
build
();
return
this
.
database
;
return
this
.
database
;
}
}
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java
View file @
7fcb1970
...
@@ -16,11 +16,17 @@
...
@@ -16,11 +16,17 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
import
java.sql.Connection
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.Properties
;
import
javax.sql.DataSource
;
import
javax.sql.DataSource
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.core.env.PropertiesPropertySource
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
@@ -42,4 +48,34 @@ public class EmbeddedDataSourceConfigurationTests {
...
@@ -42,4 +48,34 @@ public class EmbeddedDataSourceConfigurationTests {
this
.
context
.
close
();
this
.
context
.
close
();
}
}
@Test
public
void
generatesUniqueDatabaseName
()
throws
Exception
{
Properties
myProps
=
new
Properties
();
myProps
.
setProperty
(
"spring.datasource.generate-name"
,
"true"
);
this
.
context
=
new
AnnotationConfigApplicationContext
();
this
.
context
.
register
(
EmbeddedDataSourceConfiguration
.
class
);
this
.
context
.
getEnvironment
().
getPropertySources
().
addFirst
(
new
PropertiesPropertySource
(
"whatever"
,
myProps
));
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
getDatabaseName
(
dataSource
)).
isNotEqualToIgnoringCase
(
"testdb"
);
this
.
context
.
close
();
}
private
String
getDatabaseName
(
DataSource
dataSource
)
throws
SQLException
{
Connection
connection
=
dataSource
.
getConnection
();
try
{
ResultSet
catalogs
=
connection
.
getMetaData
().
getCatalogs
();
if
(
catalogs
.
next
())
{
return
catalogs
.
getString
(
1
);
}
else
{
throw
new
IllegalStateException
(
"Unable to get database name"
);
}
}
finally
{
connection
.
close
();
}
}
}
}
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