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
ce02e86b
Commit
ce02e86b
authored
Aug 06, 2014
by
Andy Wilkinson
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '1.1.x'
parents
7f9ef1cf
7d213950
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
18 deletions
+60
-18
DataSourceInitializer.java
...mework/boot/autoconfigure/jdbc/DataSourceInitializer.java
+4
-0
DataSourceInitializerTests.java
...k/boot/autoconfigure/jdbc/DataSourceInitializerTests.java
+54
-18
data.sql
spring-boot-autoconfigure/src/test/resources/data.sql
+1
-0
data.sql
...rces/org/springframework/boot/autoconfigure/jdbc/data.sql
+1
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java
View file @
ce02e86b
...
@@ -88,6 +88,10 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
...
@@ -88,6 +88,10 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
@Override
@Override
public
void
onApplicationEvent
(
DataSourceInitializedEvent
event
)
{
public
void
onApplicationEvent
(
DataSourceInitializedEvent
event
)
{
if
(!
this
.
properties
.
isInitialize
())
{
logger
.
debug
(
"Initialization disabled (not running data scripts)"
);
return
;
}
// NOTE the event can happen more than once and
// NOTE the event can happen more than once and
// the event datasource is not used here
// the event datasource is not used here
if
(!
this
.
initialized
)
{
if
(!
this
.
initialized
)
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java
View file @
ce02e86b
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
import
java.sql.SQLException
;
import
java.util.Random
;
import
java.util.Random
;
import
javax.sql.DataSource
;
import
javax.sql.DataSource
;
...
@@ -31,6 +32,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
...
@@ -31,6 +32,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.jdbc.BadSqlGrammarException
;
import
org.springframework.jdbc.core.JdbcOperations
;
import
org.springframework.jdbc.core.JdbcOperations
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.util.ClassUtils
;
import
org.springframework.util.ClassUtils
;
...
@@ -38,6 +40,7 @@ import org.springframework.util.ClassUtils;
...
@@ -38,6 +40,7 @@ import org.springframework.util.ClassUtils;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
fail
;
/**
/**
* Tests for {@link DataSourceInitializer}.
* Tests for {@link DataSourceInitializer}.
...
@@ -96,7 +99,7 @@ public class DataSourceInitializerTests {
...
@@ -96,7 +99,7 @@ public class DataSourceInitializerTests {
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
assertEquals
(
new
Integer
(
1
),
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
));
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
));
}
}
...
@@ -104,24 +107,29 @@ public class DataSourceInitializerTests {
...
@@ -104,24 +107,29 @@ public class DataSourceInitializerTests {
public
void
testDataSourceInitializedWithExplicitScript
()
throws
Exception
{
public
void
testDataSourceInitializedWithExplicitScript
()
throws
Exception
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
this
.
context
,
"spring.datasource.initialize:true"
,
"spring.datasource.initialize:true"
,
"spring.datasource.schema:"
"spring.datasource.schema:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"schema.sql"
));
"schema.sql"
),
"spring.datasource.data:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"data.sql"
));
this
.
context
.
refresh
();
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
assertEquals
(
new
Integer
(
1
),
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
));
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
));
}
}
@Test
@Test
public
void
testDataSourceInitializedWithMultipleScripts
()
throws
Exception
{
public
void
testDataSourceInitializedWithMultipleScripts
()
throws
Exception
{
EnvironmentTestUtils
.
addEnvironment
(
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
this
.
context
,
"spring.datasource.initialize:true"
,
"spring.datasource.initialize:true"
,
"spring.datasource.schema:"
"spring.datasource.schema:"
...
@@ -129,7 +137,10 @@ public class DataSourceInitializerTests {
...
@@ -129,7 +137,10 @@ public class DataSourceInitializerTests {
"schema.sql"
)
"schema.sql"
)
+
","
+
","
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"another.sql"
));
"another.sql"
),
"spring.datasource.data:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"data.sql"
));
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
this
.
context
.
refresh
();
...
@@ -137,7 +148,7 @@ public class DataSourceInitializerTests {
...
@@ -137,7 +148,7 @@ public class DataSourceInitializerTests {
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
assertEquals
(
new
Integer
(
1
),
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
));
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
));
assertEquals
(
new
Integer
(
0
),
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from SPAM"
,
Integer
.
class
));
template
.
queryForObject
(
"SELECT COUNT(*) from SPAM"
,
Integer
.
class
));
...
@@ -170,6 +181,31 @@ public class DataSourceInitializerTests {
...
@@ -170,6 +181,31 @@ public class DataSourceInitializerTests {
template
.
queryForObject
(
"SELECT name from BAR WHERE id=2"
,
String
.
class
));
template
.
queryForObject
(
"SELECT name from BAR WHERE id=2"
,
String
.
class
));
}
}
@Test
public
void
testInitializationDisabled
()
throws
Exception
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
this
.
context
.
publishEvent
(
new
DataSourceInitializedEvent
(
dataSource
));
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
try
{
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
);
fail
(
"Query should have failed as BAR table does not exist"
);
}
catch
(
BadSqlGrammarException
ex
)
{
SQLException
sqlException
=
ex
.
getSQLException
();
int
expectedCode
=
-
5501
;
// user lacks privilege or object not found
assertEquals
(
expectedCode
,
sqlException
.
getErrorCode
());
}
}
@Configuration
@Configuration
@EnableConfigurationProperties
@EnableConfigurationProperties
protected
static
class
TwoDataSources
{
protected
static
class
TwoDataSources
{
...
...
spring-boot-autoconfigure/src/test/resources/data.sql
0 → 100644
View file @
ce02e86b
INSERT
INTO
BAR
VALUES
(
1
,
'Andy'
);
\ No newline at end of file
spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/jdbc/data.sql
0 → 100644
View file @
ce02e86b
INSERT
INTO
FOO
VALUES
(
1
,
'Andy'
);
\ No newline at end of file
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