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
6f25131a
Commit
6f25131a
authored
May 31, 2017
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
7524e625
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
57 deletions
+55
-57
JdbcTemplateAutoConfigurationTests.java
...utoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java
+55
-57
No files found.
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java
View file @
6f25131a
...
@@ -20,13 +20,11 @@ import java.util.Random;
...
@@ -20,13 +20,11 @@ import java.util.Random;
import
javax.sql.DataSource
;
import
javax.sql.DataSource
;
import
org.apache.commons.dbcp2.BasicDataSource
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.context.ConfigurableApplicationContext
;
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
;
...
@@ -43,98 +41,98 @@ import static org.assertj.core.api.Assertions.assertThat;
...
@@ -43,98 +41,98 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Dave Syer
* @author Dave Syer
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Kazuki Shimizu
* @author Kazuki Shimizu
* @since 1.4.0
*/
*/
public
class
JdbcTemplateAutoConfigurationTests
{
public
class
JdbcTemplateAutoConfigurationTests
{
private
final
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
private
ConfigurableApplicationContext
context
;
@Before
public
void
init
()
{
EmbeddedDatabaseConnection
.
override
=
null
;
TestPropertyValues
.
of
(
"spring.datasource.initialize:false"
,
"spring.datasource.url:jdbc:hsqldb:mem:testdb-"
+
new
Random
().
nextInt
())
.
applyTo
(
this
.
context
);
}
@After
@After
public
void
restore
()
{
public
void
restore
()
{
EmbeddedDatabaseConnection
.
override
=
null
;
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
this
.
context
.
close
();
}
}
}
@Test
@Test
public
void
testJdbcTemplateExists
()
throws
Exception
{
public
void
testJdbcTemplateExists
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
load
();
JdbcTemplateAutoConfiguration
.
class
,
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcTemplate
.
class
)).
hasSize
(
1
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
).
isNotNull
();
assertThat
(
jdbcTemplate
.
getDataSource
()).
isEqualTo
(
assertThat
(
jdbcTemplate
.
getDataSource
()).
isNotNull
(
);
this
.
context
.
getBean
(
DataSource
.
class
)
);
}
}
@Test
@Test
public
void
testJdbcTemplateExistsWithCustomDataSource
()
throws
Exception
{
public
void
testJdbcTemplateExistsWithCustomDataSource
()
{
this
.
context
.
register
(
TestDataSourceConfiguration
.
class
,
load
(
TestDataSourceConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
,
JdbcTemplateAutoConfiguration
.
class
,
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcTemplate
.
class
)).
hasSize
(
1
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
).
isNotNull
();
assertThat
(
jdbcTemplate
).
isNotNull
();
assertThat
(
jdbcTemplate
.
getDataSource
()
instanceof
BasicDataSource
).
isTrue
();
assertThat
(
jdbcTemplate
.
getDataSource
()).
isEqualTo
(
this
.
context
.
getBean
(
"customDataSource"
));
}
}
@Test
@Test
public
void
testNamedParameterJdbcTemplateExists
()
throws
Exception
{
public
void
testNamedParameterJdbcTemplateExists
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
load
();
JdbcTemplateAutoConfiguration
.
class
,
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
PropertyPlaceholderAutoConfiguration
.
class
);
.
hasSize
(
1
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
)).
isNotNull
();
}
}
@Test
@Test
public
void
testMultiDataSource
()
throws
Exception
{
public
void
testMultiDataSource
()
{
this
.
context
.
register
(
MultiDataSourceConfiguration
.
class
,
load
(
MultiDataSourceConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
,
JdbcTemplateAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
isEmpty
();
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
isEmpty
();
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
isEmpty
();
.
isEmpty
();
}
}
@Test
@Test
public
void
testMultiDataSourceUsingPrimary
()
throws
Exception
{
public
void
testMultiDataSourceUsingPrimary
()
{
this
.
context
.
register
(
MultiDataSourceUsingPrimaryConfiguration
.
class
,
load
(
MultiDataSourceUsingPrimaryConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
,
JdbcTemplateAutoConfiguration
.
class
,
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
PropertyPlaceholderAutoConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
this
.
context
.
refresh
(
);
.
hasSize
(
1
);
assertThat
(
this
.
context
.
getBean
(
Jdbc
Operations
.
class
)).
isNotNull
();
assertThat
(
this
.
context
.
getBean
(
Jdbc
Template
.
class
).
getDataSource
())
assertThat
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
)).
isNotNull
(
);
.
isEqualTo
(
this
.
context
.
getBean
(
"test1DataSource"
)
);
}
}
@Test
@Test
public
void
testExistingCustomJdbcTemplate
()
throws
Exception
{
public
void
testExistingCustomJdbcTemplate
()
{
this
.
context
.
register
(
CustomConfiguration
.
class
,
load
(
CustomConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
,
JdbcTemplateAutoConfiguration
.
class
,
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
JdbcOperations
.
class
))
assertThat
(
this
.
context
.
getBean
(
JdbcOperations
.
class
))
.
isEqualTo
(
this
.
context
.
getBean
(
"customJdbcOperations"
));
.
isEqualTo
(
this
.
context
.
getBean
(
"customJdbcOperations"
));
}
}
@Test
@Test
public
void
testExistingCustomNamedParameterJdbcTemplate
()
throws
Exception
{
public
void
testExistingCustomNamedParameterJdbcTemplate
()
{
this
.
context
.
register
(
CustomConfiguration
.
class
,
load
(
CustomConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
,
JdbcTemplateAutoConfiguration
.
class
,
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
PropertyPlaceholderAutoConfiguration
.
class
);
.
hasSize
(
1
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
))
assertThat
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
))
.
isEqualTo
(
this
.
context
.
getBean
(
"customNamedParameterJdbcOperations"
));
.
isEqualTo
(
this
.
context
.
getBean
(
"customNamedParameterJdbcOperations"
));
}
}
public
void
load
(
String
...
environment
)
{
load
(
null
,
environment
);
}
public
void
load
(
Class
<?>
config
,
String
...
environment
)
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
"spring.datasource.initialize:false"
,
"spring.datasource.url:jdbc:hsqldb:mem:testdb-"
+
new
Random
().
nextInt
())
.
applyTo
(
ctx
);
TestPropertyValues
.
of
(
environment
).
applyTo
(
ctx
);
if
(
config
!=
null
)
{
ctx
.
register
(
config
);
}
ctx
.
register
(
DataSourceAutoConfiguration
.
class
,
JdbcTemplateAutoConfiguration
.
class
);
ctx
.
refresh
();
this
.
context
=
ctx
;
}
@Configuration
@Configuration
static
class
CustomConfiguration
{
static
class
CustomConfiguration
{
...
@@ -155,7 +153,7 @@ public class JdbcTemplateAutoConfigurationTests {
...
@@ -155,7 +153,7 @@ public class JdbcTemplateAutoConfigurationTests {
static
class
TestDataSourceConfiguration
{
static
class
TestDataSourceConfiguration
{
@Bean
@Bean
public
DataSource
d
ataSource
()
{
public
DataSource
customD
ataSource
()
{
return
new
TestDataSource
(
"overridedb"
);
return
new
TestDataSource
(
"overridedb"
);
}
}
...
...
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