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
177c54d4
Commit
177c54d4
authored
Jul 10, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate JdbcTemplateAutoConfiguration tests to context runner
parent
3423c5dd
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
90 deletions
+78
-90
JdbcTemplateAutoConfigurationTests.java
...utoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java
+78
-90
No files found.
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java
View file @
177c54d4
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
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.
...
...
@@ -16,16 +16,12 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
import
java.util.Random
;
import
javax.sql.DataSource
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Primary
;
...
...
@@ -46,129 +42,121 @@ import static org.mockito.Mockito.mock;
*/
public
class
JdbcTemplateAutoConfigurationTests
{
private
ConfigurableApplicationContext
context
;
@After
public
void
restore
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withPropertyValues
(
"spring.datasource.initialization-mode=never"
,
"spring.datasource.generate-unique-name=true"
)
.
withConfiguration
(
AutoConfigurations
.
of
(
DataSourceAutoConfiguration
.
class
,
JdbcTemplateAutoConfiguration
.
class
));
@Test
public
void
testJdbcTemplateExists
()
{
load
();
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
.
getDataSource
())
.
isEqualTo
(
this
.
context
.
getBean
(
DataSource
.
class
));
assertThat
(
jdbcTemplate
.
getFetchSize
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getQueryTimeout
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getMaxRows
()).
isEqualTo
(-
1
);
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
JdbcTemplate
jdbcTemplate
=
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
.
getDataSource
())
.
isEqualTo
(
context
.
getBean
(
DataSource
.
class
));
assertThat
(
jdbcTemplate
.
getFetchSize
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getQueryTimeout
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getMaxRows
()).
isEqualTo
(-
1
);
});
}
@Test
public
void
testJdbcTemplateWithCustomProperties
()
{
load
(
"spring.jdbc.template.fetch-size:100"
,
this
.
contextRunner
.
withPropertyValues
(
"spring.jdbc.template.fetch-size:100"
,
"spring.jdbc.template.query-timeout:60"
,
"spring.jdbc.template.max-rows:1000"
);
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
).
isNotNull
();
assertThat
(
jdbcTemplate
.
getDataSource
()).
isNotNull
();
assertThat
(
jdbcTemplate
.
getFetchSize
()).
isEqualTo
(
100
);
assertThat
(
jdbcTemplate
.
getQueryTimeout
()).
isEqualTo
(
60
);
assertThat
(
jdbcTemplate
.
getMaxRows
()).
isEqualTo
(
1000
);
"spring.jdbc.template.max-rows:1000"
).
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
JdbcTemplate
jdbcTemplate
=
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
.
getDataSource
()).
isNotNull
();
assertThat
(
jdbcTemplate
.
getFetchSize
()).
isEqualTo
(
100
);
assertThat
(
jdbcTemplate
.
getQueryTimeout
()).
isEqualTo
(
60
);
assertThat
(
jdbcTemplate
.
getMaxRows
()).
isEqualTo
(
1000
);
});
}
@Test
public
void
testJdbcTemplateExistsWithCustomDataSource
()
{
load
(
TestDataSourceConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
).
isNotNull
();
assertThat
(
jdbcTemplate
.
getDataSource
())
.
isEqualTo
(
this
.
context
.
getBean
(
"customDataSource"
));
this
.
contextRunner
.
withUserConfiguration
(
TestDataSourceConfiguration
.
class
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
JdbcTemplate
jdbcTemplate
=
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
.
getDataSource
())
.
isEqualTo
(
context
.
getBean
(
"customDataSource"
));
});
}
@Test
public
void
testNamedParameterJdbcTemplateExists
()
{
load
();
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
hasSize
(
1
);
NamedParameterJdbcTemplate
namedParameterJdbcTemplate
=
this
.
context
.
getBean
(
NamedParameterJdbcTemplate
.
class
);
assertThat
(
namedParameterJdbcTemplate
.
getJdbcOperations
())
.
isEqualTo
(
this
.
context
.
getBean
(
JdbcOperations
.
class
)
);
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
NamedParameterJdbcOperations
.
class
);
NamedParameterJdbcTemplate
namedParameterJdbcTemplate
=
context
.
getBean
(
NamedParameterJdbcTemplate
.
class
);
assertThat
(
namedParameterJdbcTemplate
.
getJdbcOperations
())
.
isEqualTo
(
context
.
getBean
(
JdbcOperations
.
class
));
}
);
}
@Test
public
void
testMultiDataSource
()
{
load
(
MultiDataSourceConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
isEmpty
();
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
isEmpty
();
this
.
contextRunner
.
withUserConfiguration
(
MultiDataSourceConfiguration
.
class
)
.
run
((
context
)
->
{
assertThat
(
context
).
doesNotHaveBean
(
JdbcOperations
.
class
);
assertThat
(
context
)
.
doesNotHaveBean
(
NamedParameterJdbcOperations
.
class
);
});
}
@Test
public
void
testMultiJdbcTemplate
()
{
load
(
MultiJdbcTemplateConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
)
)
.
isEmpty
(
);
this
.
contextRunner
.
withUserConfiguration
(
MultiJdbcTemplateConfiguration
.
class
)
.
run
((
context
)
->
assertThat
(
context
)
.
doesNotHaveBean
(
NamedParameterJdbcOperations
.
class
)
);
}
@Test
public
void
testMultiDataSourceUsingPrimary
()
{
load
(
MultiDataSourceUsingPrimaryConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
hasSize
(
1
);
assertThat
(
this
.
context
.
getBean
(
JdbcTemplate
.
class
).
getDataSource
())
.
isEqualTo
(
this
.
context
.
getBean
(
"test1DataSource"
));
this
.
contextRunner
.
withUserConfiguration
(
MultiDataSourceUsingPrimaryConfiguration
.
class
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
assertThat
(
context
).
hasSingleBean
(
NamedParameterJdbcOperations
.
class
);
assertThat
(
context
.
getBean
(
JdbcTemplate
.
class
).
getDataSource
())
.
isEqualTo
(
context
.
getBean
(
"test1DataSource"
));
});
}
@Test
public
void
testMultiJdbcTemplateUsingPrimary
()
{
load
(
MultiJdbcTemplateUsingPrimaryConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
hasSize
(
1
);
assertThat
(
this
.
context
.
getBean
(
NamedParameterJdbcTemplate
.
class
)
.
getJdbcOperations
()).
isEqualTo
(
this
.
context
.
getBean
(
"test1Template"
));
this
.
contextRunner
.
withUserConfiguration
(
MultiJdbcTemplateUsingPrimaryConfiguration
.
class
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
NamedParameterJdbcOperations
.
class
);
assertThat
(
context
.
getBean
(
NamedParameterJdbcTemplate
.
class
)
.
getJdbcOperations
())
.
isEqualTo
(
context
.
getBean
(
"test1Template"
));
});
}
@Test
public
void
testExistingCustomJdbcTemplate
()
{
load
(
CustomConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
assertThat
(
this
.
context
.
getBean
(
JdbcOperations
.
class
))
.
isEqualTo
(
this
.
context
.
getBean
(
"customJdbcOperations"
));
this
.
contextRunner
.
withUserConfiguration
(
CustomConfiguration
.
class
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
assertThat
(
context
.
getBean
(
JdbcOperations
.
class
))
.
isEqualTo
(
context
.
getBean
(
"customJdbcOperations"
));
});
}
@Test
public
void
testExistingCustomNamedParameterJdbcTemplate
()
{
load
(
CustomConfiguration
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
hasSize
(
1
);
assertThat
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
))
.
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.initialization-mode:never"
,
"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
;
this
.
contextRunner
.
withUserConfiguration
(
CustomConfiguration
.
class
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
NamedParameterJdbcOperations
.
class
);
assertThat
(
context
.
getBean
(
NamedParameterJdbcOperations
.
class
))
.
isEqualTo
(
context
.
getBean
(
"customNamedParameterJdbcOperations"
));
});
}
@Configuration
...
...
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