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
37121e69
Commit
37121e69
authored
Jun 06, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make DataSourceInitialization a no-op if there is no DataSource
Fixes gh-1041
parent
fbc1d3ee
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
136 additions
and
69 deletions
+136
-69
DataSourceInitialization.java
...ork/boot/autoconfigure/jdbc/DataSourceInitialization.java
+4
-4
DataSourceAutoConfigurationTests.java
.../autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
+8
-65
DataSourceInitializationTests.java
...oot/autoconfigure/jdbc/DataSourceInitializationTests.java
+124
-0
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitialization.java
View file @
37121e69
...
@@ -40,14 +40,15 @@ import org.springframework.util.StringUtils;
...
@@ -40,14 +40,15 @@ import org.springframework.util.StringUtils;
/**
/**
* @author Dave Syer
* @author Dave Syer
* @since 1.1
*/
*/
@Configuration
@Configuration
@EnableConfigurationProperties
(
DataSourceProperties
.
class
)
@EnableConfigurationProperties
(
DataSourceProperties
.
class
)
public
class
DataSourceInitialization
{
public
class
DataSourceInitialization
{
private
static
Log
logger
=
LogFactory
.
getLog
(
DataSource
AutoConfigur
ation
.
class
);
private
static
Log
logger
=
LogFactory
.
getLog
(
DataSource
Initializ
ation
.
class
);
@Autowired
@Autowired
(
required
=
false
)
private
DataSource
dataSource
;
private
DataSource
dataSource
;
@Autowired
@Autowired
...
@@ -59,8 +60,7 @@ public class DataSourceInitialization {
...
@@ -59,8 +60,7 @@ public class DataSourceInitialization {
private
boolean
initialized
=
false
;
private
boolean
initialized
=
false
;
@Bean
@Bean
public
ApplicationListener
<
DataSourceInitializedEvent
>
dataSourceInitializedListener
(
public
ApplicationListener
<
DataSourceInitializedEvent
>
dataSourceInitializedListener
()
{
DataSource
dataSource
)
{
return
new
DataSourceInitializedListener
();
return
new
DataSourceInitializedListener
();
}
}
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
View file @
37121e69
...
@@ -16,6 +16,11 @@
...
@@ -16,6 +16,11 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
java.net.URL
;
import
java.net.URL
;
import
java.net.URLClassLoader
;
import
java.net.URLClassLoader
;
import
java.sql.Connection
;
import
java.sql.Connection
;
...
@@ -40,18 +45,11 @@ import org.springframework.boot.test.EnvironmentTestUtils;
...
@@ -40,18 +45,11 @@ import org.springframework.boot.test.EnvironmentTestUtils;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
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.jdbc.core.JdbcOperations
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations
;
import
org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations
;
import
org.springframework.util.ClassUtils
;
import
com.zaxxer.hikari.HikariDataSource
;
import
com.zaxxer.hikari.HikariDataSource
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
/**
* Tests for {@link DataSourceAutoConfiguration}.
* Tests for {@link DataSourceAutoConfiguration}.
*
*
...
@@ -72,6 +70,9 @@ public class DataSourceAutoConfigurationTests {
...
@@ -72,6 +70,9 @@ public class DataSourceAutoConfigurationTests {
@After
@After
public
void
restore
()
{
public
void
restore
()
{
EmbeddedDatabaseConnection
.
override
=
null
;
EmbeddedDatabaseConnection
.
override
=
null
;
if
(
context
!=
null
)
{
context
.
close
();
}
}
}
@Test
@Test
...
@@ -214,64 +215,6 @@ public class DataSourceAutoConfigurationTests {
...
@@ -214,64 +215,6 @@ public class DataSourceAutoConfigurationTests {
assertNotNull
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
));
assertNotNull
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
));
}
}
@Test
public
void
testDataSourceInitialized
()
throws
Exception
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:true"
);
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
));
}
@Test
public
void
testDataSourceInitializedWithExplicitScript
()
throws
Exception
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:true"
,
"spring.datasource.schema:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"schema.sql"
));
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
));
}
@Test
public
void
testDataSourceInitializedWithMultipleScripts
()
throws
Exception
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:true"
,
"spring.datasource.schema:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"schema.sql"
)
+
","
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"another.sql"
));
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
));
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from SPAM"
,
Integer
.
class
));
}
@Configuration
@Configuration
static
class
TestDataSourceConfiguration
{
static
class
TestDataSourceConfiguration
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializationTests.java
0 → 100644
View file @
37121e69
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
import
static
org
.
junit
.
Assert
.*;
import
java.util.Random
;
import
javax.sql.DataSource
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.jdbc.core.JdbcOperations
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.util.ClassUtils
;
/**
* @author Dave Syer
*
*/
public
class
DataSourceInitializationTests
{
private
final
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
@Before
public
void
init
()
{
EmbeddedDatabaseConnection
.
override
=
null
;
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:false"
,
"spring.datasource.url:jdbc:hsqldb:mem:testdb-"
+
new
Random
().
nextInt
());
}
@After
public
void
restore
()
{
EmbeddedDatabaseConnection
.
override
=
null
;
if
(
context
!=
null
)
{
context
.
close
();
}
}
@Test
public
void
testDefaultDataSourceDoesNotExists
()
throws
Exception
{
this
.
context
.
register
(
DataSourceInitialization
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertEquals
(
0
,
this
.
context
.
getBeanNamesForType
(
DataSource
.
class
).
length
);
}
@Test
public
void
testDataSourceInitialized
()
throws
Exception
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:true"
);
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
));
}
@Test
public
void
testDataSourceInitializedWithExplicitScript
()
throws
Exception
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:true"
,
"spring.datasource.schema:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"schema.sql"
));
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
));
}
@Test
public
void
testDataSourceInitializedWithMultipleScripts
()
throws
Exception
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.initialize:true"
,
"spring.datasource.schema:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"schema.sql"
)
+
","
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"another.sql"
));
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertTrue
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
);
assertNotNull
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
));
assertEquals
(
new
Integer
(
0
),
template
.
queryForObject
(
"SELECT COUNT(*) from SPAM"
,
Integer
.
class
));
}
}
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