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
ab02084e
Commit
ab02084e
authored
Oct 14, 2020
by
Asha Somayajula
Committed by
Stephane Nicoll
Oct 16, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix detection logic for embedded databases
See gh-23693
parent
23073d9e
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
81 additions
and
14 deletions
+81
-14
DataSourceProperties.java
...amework/boot/autoconfigure/jdbc/DataSourceProperties.java
+10
-10
FlywayAutoConfigurationTests.java
...ot/autoconfigure/flyway/FlywayAutoConfigurationTests.java
+0
-1
DataSourcePropertiesTests.java
...rk/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java
+44
-1
LiquibaseAutoConfigurationTests.java
...oconfigure/liquibase/LiquibaseAutoConfigurationTests.java
+0
-1
TestDatabaseAutoConfiguration.java
...est/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java
+1
-1
EmbeddedDatabaseConnection.java
...springframework/boot/jdbc/EmbeddedDatabaseConnection.java
+15
-0
EmbeddedDatabaseConnectionTests.java
...gframework/boot/jdbc/EmbeddedDatabaseConnectionTests.java
+11
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java
View file @
ab02084e
...
@@ -322,13 +322,13 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
...
@@ -322,13 +322,13 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
* @since 1.4.0
* @since 1.4.0
*/
*/
public
String
determineUsername
()
{
public
String
determineUsername
()
{
if
(
StringUtils
.
hasText
(
this
.
username
))
{
if
(
EmbeddedDatabaseConnection
.
isEmbedded
(
determineDriverClassName
(),
determineUrl
())
return
this
.
username
;
&&
!
StringUtils
.
hasText
(
this
.
username
))
{
}
if
(
EmbeddedDatabaseConnection
.
isEmbedded
(
determineDriverClassName
()))
{
return
"sa"
;
return
"sa"
;
}
}
return
null
;
else
{
return
this
.
username
;
}
}
}
/**
/**
...
@@ -350,13 +350,13 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
...
@@ -350,13 +350,13 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
* @since 1.4.0
* @since 1.4.0
*/
*/
public
String
determinePassword
()
{
public
String
determinePassword
()
{
if
(
StringUtils
.
hasText
(
this
.
password
))
{
if
(
EmbeddedDatabaseConnection
.
isEmbedded
(
determineDriverClassName
(),
determineUrl
())
return
this
.
password
;
&&
!
StringUtils
.
hasText
(
this
.
password
))
{
}
if
(
EmbeddedDatabaseConnection
.
isEmbedded
(
determineDriverClassName
()))
{
return
""
;
return
""
;
}
}
return
null
;
else
{
return
this
.
password
;
}
}
}
public
String
getJndiName
()
{
public
String
getJndiName
()
{
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfigurationTests.java
View file @
ab02084e
...
@@ -127,7 +127,6 @@ class FlywayAutoConfigurationTests {
...
@@ -127,7 +127,6 @@ class FlywayAutoConfigurationTests {
assertThat
(
context
).
hasSingleBean
(
Flyway
.
class
);
assertThat
(
context
).
hasSingleBean
(
Flyway
.
class
);
DataSource
dataSource
=
context
.
getBean
(
Flyway
.
class
).
getConfiguration
().
getDataSource
();
DataSource
dataSource
=
context
.
getBean
(
Flyway
.
class
).
getConfiguration
().
getDataSource
();
assertThat
(
dataSource
).
isNotNull
();
assertThat
(
dataSource
).
isNotNull
();
assertThat
(
dataSource
).
hasFieldOrPropertyWithValue
(
"user"
,
"sa"
);
assertThat
(
dataSource
).
hasFieldOrPropertyWithValue
(
"password"
,
""
);
assertThat
(
dataSource
).
hasFieldOrPropertyWithValue
(
"password"
,
""
);
});
});
}
}
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourcePropertiesTests.java
View file @
ab02084e
...
@@ -78,6 +78,32 @@ class DataSourcePropertiesTests {
...
@@ -78,6 +78,32 @@ class DataSourcePropertiesTests {
assertThat
(
properties
.
determineUrl
()).
isEqualTo
(
"jdbc:mysql://mydb"
);
assertThat
(
properties
.
determineUrl
()).
isEqualTo
(
"jdbc:mysql://mydb"
);
}
}
@Test
void
determineIsEmbeddedWithExplicitConfigforH2
()
throws
Exception
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
properties
.
setUrl
(
"jdbc:h2:~/test"
);
properties
.
setUsername
(
""
);
properties
.
setPassword
(
""
);
properties
.
afterPropertiesSet
();
assertThat
(
properties
.
getUrl
()).
isEqualTo
(
"jdbc:h2:~/test"
);
assertThat
(
properties
.
determineUrl
()).
isEqualTo
(
"jdbc:h2:~/test"
);
assertThat
(
properties
.
determineUsername
()).
isEqualTo
(
""
);
assertThat
(
properties
.
determinePassword
()).
isEqualTo
(
""
);
}
@Test
void
determineWithExplicitConfigforH2WithCustomJdbcUrl
()
throws
Exception
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
properties
.
setUrl
(
"jdbc:h2:~/test"
);
properties
.
setUsername
(
"as"
);
properties
.
setPassword
(
"as"
);
properties
.
afterPropertiesSet
();
assertThat
(
properties
.
getUrl
()).
isEqualTo
(
"jdbc:h2:~/test"
);
assertThat
(
properties
.
determineUrl
()).
isEqualTo
(
"jdbc:h2:~/test"
);
assertThat
(
properties
.
determineUsername
()).
isEqualTo
(
"as"
);
assertThat
(
properties
.
determinePassword
()).
isEqualTo
(
"as"
);
}
@Test
@Test
void
determineUrlWithGenerateUniqueName
()
throws
Exception
{
void
determineUrlWithGenerateUniqueName
()
throws
Exception
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
DataSourceProperties
properties
=
new
DataSourceProperties
();
...
@@ -98,6 +124,24 @@ class DataSourcePropertiesTests {
...
@@ -98,6 +124,24 @@ class DataSourcePropertiesTests {
assertThat
(
properties
.
determineUsername
()).
isEqualTo
(
"sa"
);
assertThat
(
properties
.
determineUsername
()).
isEqualTo
(
"sa"
);
}
}
@Test
void
determineUsernameWhenEmpty
()
throws
Exception
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
properties
.
setUsername
(
""
);
properties
.
afterPropertiesSet
();
assertThat
(
properties
.
getUsername
());
assertThat
(
properties
.
determineUsername
()).
isEqualTo
(
"sa"
);
}
@Test
void
determineUsernameWhenNull
()
throws
Exception
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
properties
.
setUsername
(
null
);
properties
.
afterPropertiesSet
();
assertThat
(
properties
.
getUsername
());
assertThat
(
properties
.
determineUsername
()).
isEqualTo
(
"sa"
);
}
@Test
@Test
void
determineUsernameWithExplicitConfig
()
throws
Exception
{
void
determineUsernameWithExplicitConfig
()
throws
Exception
{
DataSourceProperties
properties
=
new
DataSourceProperties
();
DataSourceProperties
properties
=
new
DataSourceProperties
();
...
@@ -112,7 +156,6 @@ class DataSourcePropertiesTests {
...
@@ -112,7 +156,6 @@ class DataSourcePropertiesTests {
DataSourceProperties
properties
=
new
DataSourceProperties
();
DataSourceProperties
properties
=
new
DataSourceProperties
();
properties
.
afterPropertiesSet
();
properties
.
afterPropertiesSet
();
assertThat
(
properties
.
getPassword
()).
isNull
();
assertThat
(
properties
.
getPassword
()).
isNull
();
assertThat
(
properties
.
determinePassword
()).
isEqualTo
(
""
);
}
}
@Test
@Test
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java
View file @
ab02084e
...
@@ -246,7 +246,6 @@ class LiquibaseAutoConfigurationTests {
...
@@ -246,7 +246,6 @@ class LiquibaseAutoConfigurationTests {
.
run
(
assertLiquibase
((
liquibase
)
->
{
.
run
(
assertLiquibase
((
liquibase
)
->
{
DataSource
dataSource
=
liquibase
.
getDataSource
();
DataSource
dataSource
=
liquibase
.
getDataSource
();
assertThat
(((
HikariDataSource
)
dataSource
).
isClosed
()).
isTrue
();
assertThat
(((
HikariDataSource
)
dataSource
).
isClosed
()).
isTrue
();
assertThat
(((
HikariDataSource
)
dataSource
).
getUsername
()).
isEqualTo
(
"sa"
);
assertThat
(((
HikariDataSource
)
dataSource
).
getPassword
()).
isEqualTo
(
""
);
assertThat
(((
HikariDataSource
)
dataSource
).
getPassword
()).
isEqualTo
(
""
);
}));
}));
}
}
...
...
spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/jdbc/TestDatabaseAutoConfiguration.java
View file @
ab02084e
...
@@ -101,7 +101,7 @@ public class TestDatabaseAutoConfiguration {
...
@@ -101,7 +101,7 @@ public class TestDatabaseAutoConfiguration {
private
BeanDefinition
createEmbeddedBeanDefinition
(
boolean
primary
)
{
private
BeanDefinition
createEmbeddedBeanDefinition
(
boolean
primary
)
{
BeanDefinition
beanDefinition
=
new
RootBeanDefinition
(
EmbeddedDataSourceFactoryBean
.
class
);
BeanDefinition
beanDefinition
=
new
RootBeanDefinition
(
EmbeddedDataSourceFactoryBean
.
class
);
beanDefinition
.
setPrimary
(
primary
);
beanDefinition
.
setPrimary
(
true
);
return
beanDefinition
;
return
beanDefinition
;
}
}
...
...
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnection.java
View file @
ab02084e
...
@@ -122,11 +122,26 @@ public enum EmbeddedDatabaseConnection {
...
@@ -122,11 +122,26 @@ public enum EmbeddedDatabaseConnection {
* @param driverClass the driver class
* @param driverClass the driver class
* @return true if the driver class is one of the embedded types
* @return true if the driver class is one of the embedded types
*/
*/
@Deprecated
public
static
boolean
isEmbedded
(
String
driverClass
)
{
public
static
boolean
isEmbedded
(
String
driverClass
)
{
return
driverClass
!=
null
&&
(
matches
(
HSQL
,
driverClass
)
||
matches
(
H2
,
driverClass
)
return
driverClass
!=
null
&&
(
matches
(
HSQL
,
driverClass
)
||
matches
(
H2
,
driverClass
)
||
matches
(
DERBY
,
driverClass
)
||
matches
(
HSQLDB
,
driverClass
));
||
matches
(
DERBY
,
driverClass
)
||
matches
(
HSQLDB
,
driverClass
));
}
}
/**
* Convenience method to determine if a given driver class name and url represents an
* embedded database type.The exception is made for the H2 database for embedded
* types.
* @param driverClass the driver class
* @param url the jdbc url
* @return true if the driver class is one of the embedded types
*/
public
static
boolean
isEmbedded
(
String
driverClass
,
String
url
)
{
return
(
driverClass
!=
null
&&
(
matches
(
HSQL
,
driverClass
)
||
(
matches
(
H2
,
driverClass
)
&&
url
.
contains
(
":h2:mem"
))
||
matches
(
DERBY
,
driverClass
)
||
matches
(
HSQLDB
,
driverClass
)));
}
private
static
boolean
matches
(
EmbeddedDatabaseConnection
candidate
,
String
driverClass
)
{
private
static
boolean
matches
(
EmbeddedDatabaseConnection
candidate
,
String
driverClass
)
{
return
driverClass
.
equals
(
candidate
.
driverClass
)
||
driverClass
.
equals
(
candidate
.
alternativeDriverClass
);
return
driverClass
.
equals
(
candidate
.
driverClass
)
||
driverClass
.
equals
(
candidate
.
alternativeDriverClass
);
}
}
...
...
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/jdbc/EmbeddedDatabaseConnectionTests.java
View file @
ab02084e
...
@@ -78,4 +78,15 @@ class EmbeddedDatabaseConnectionTests {
...
@@ -78,4 +78,15 @@ class EmbeddedDatabaseConnectionTests {
.
withMessageContaining
(
"DatabaseName must not be empty"
);
.
withMessageContaining
(
"DatabaseName must not be empty"
);
}
}
@Test
void
isEmbeddedForh2CustomDatabaseName
()
{
assertThat
(
EmbeddedDatabaseConnection
.
isEmbedded
(
"org.h2.Driver"
,
"jdbc:h2:~/test"
)).
isFalse
();
}
@Test
void
isEmbeddedForh2EmbeddedDatabaseName
()
{
assertThat
(
EmbeddedDatabaseConnection
.
isEmbedded
(
"org.h2.Driver"
,
"jdbc:h2:mem:b3c7d078-1362-4be7-a088-e25dcc3aee32;DB_CLOSE_DELAY=-1"
)).
isTrue
();
}
}
}
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