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
8194dc4a
Commit
8194dc4a
authored
Jul 10, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.0.x'
parents
87b72cc9
177c54d4
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 @
8194dc4a
/*
/*
* 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");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -16,16 +16,12 @@
...
@@ -16,16 +16,12 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
package
org
.
springframework
.
boot
.
autoconfigure
.
jdbc
;
import
java.util.Random
;
import
javax.sql.DataSource
;
import
javax.sql.DataSource
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
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
;
...
@@ -46,129 +42,121 @@ import static org.mockito.Mockito.mock;
...
@@ -46,129 +42,121 @@ import static org.mockito.Mockito.mock;
*/
*/
public
class
JdbcTemplateAutoConfigurationTests
{
public
class
JdbcTemplateAutoConfigurationTests
{
private
ConfigurableApplicationContext
context
;
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
.
withPropertyValues
(
"spring.datasource.initialization-mode=never"
,
@After
"spring.datasource.generate-unique-name=true"
)
public
void
restore
()
{
.
withConfiguration
(
AutoConfigurations
.
of
(
DataSourceAutoConfiguration
.
class
,
if
(
this
.
context
!=
null
)
{
JdbcTemplateAutoConfiguration
.
class
));
this
.
context
.
close
();
}
}
@Test
@Test
public
void
testJdbcTemplateExists
()
{
public
void
testJdbcTemplateExists
()
{
load
();
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
JdbcTemplate
jdbcTemplate
=
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
.
getDataSource
())
assertThat
(
jdbcTemplate
.
getDataSource
())
.
isEqualTo
(
this
.
context
.
getBean
(
DataSource
.
class
));
.
isEqualTo
(
context
.
getBean
(
DataSource
.
class
));
assertThat
(
jdbcTemplate
.
getFetchSize
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getFetchSize
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getQueryTimeout
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getQueryTimeout
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getMaxRows
()).
isEqualTo
(-
1
);
assertThat
(
jdbcTemplate
.
getMaxRows
()).
isEqualTo
(-
1
);
});
}
}
@Test
@Test
public
void
testJdbcTemplateWithCustomProperties
()
{
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.query-timeout:60"
,
"spring.jdbc.template.max-rows:1000"
);
"spring.jdbc.template.max-rows:1000"
).
run
((
context
)
->
{
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
assertThat
(
jdbcTemplate
).
isNotNull
();
JdbcTemplate
jdbcTemplate
=
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
.
getDataSource
()).
isNotNull
();
assertThat
(
jdbcTemplate
.
getDataSource
()).
isNotNull
();
assertThat
(
jdbcTemplate
.
getFetchSize
()).
isEqualTo
(
100
);
assertThat
(
jdbcTemplate
.
getFetchSize
()).
isEqualTo
(
100
);
assertThat
(
jdbcTemplate
.
getQueryTimeout
()).
isEqualTo
(
60
);
assertThat
(
jdbcTemplate
.
getQueryTimeout
()).
isEqualTo
(
60
);
assertThat
(
jdbcTemplate
.
getMaxRows
()).
isEqualTo
(
1000
);
assertThat
(
jdbcTemplate
.
getMaxRows
()).
isEqualTo
(
1000
);
});
}
}
@Test
@Test
public
void
testJdbcTemplateExistsWithCustomDataSource
()
{
public
void
testJdbcTemplateExistsWithCustomDataSource
()
{
load
(
TestDataSourceConfiguration
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
TestDataSourceConfiguration
.
class
)
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
.
run
((
context
)
->
{
JdbcTemplate
jdbcTemplate
=
this
.
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
assertThat
(
jdbcTemplate
).
isNotNull
();
JdbcTemplate
jdbcTemplate
=
context
.
getBean
(
JdbcTemplate
.
class
);
assertThat
(
jdbcTemplate
.
getDataSource
())
assertThat
(
jdbcTemplate
.
getDataSource
())
.
isEqualTo
(
this
.
context
.
getBean
(
"customDataSource"
));
.
isEqualTo
(
context
.
getBean
(
"customDataSource"
));
});
}
}
@Test
@Test
public
void
testNamedParameterJdbcTemplateExists
()
{
public
void
testNamedParameterJdbcTemplateExists
()
{
load
();
this
.
contextRunner
.
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
assertThat
(
context
).
hasSingleBean
(
NamedParameterJdbcOperations
.
class
);
.
hasSize
(
1
);
NamedParameterJdbcTemplate
namedParameterJdbcTemplate
=
context
NamedParameterJdbcTemplate
namedParameterJdbcTemplate
=
this
.
context
.
getBean
(
NamedParameterJdbcTemplate
.
class
);
.
getBean
(
NamedParameterJdbcTemplate
.
class
);
assertThat
(
namedParameterJdbcTemplate
.
getJdbcOperations
())
assertThat
(
namedParameterJdbcTemplate
.
getJdbcOperations
())
.
isEqualTo
(
context
.
getBean
(
JdbcOperations
.
class
));
.
isEqualTo
(
this
.
context
.
getBean
(
JdbcOperations
.
class
)
);
}
);
}
}
@Test
@Test
public
void
testMultiDataSource
()
{
public
void
testMultiDataSource
()
{
load
(
MultiDataSourceConfiguration
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
MultiDataSourceConfiguration
.
class
)
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
isEmpty
();
.
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
assertThat
(
context
).
doesNotHaveBean
(
JdbcOperations
.
class
);
.
isEmpty
();
assertThat
(
context
)
.
doesNotHaveBean
(
NamedParameterJdbcOperations
.
class
);
});
}
}
@Test
@Test
public
void
testMultiJdbcTemplate
()
{
public
void
testMultiJdbcTemplate
()
{
load
(
MultiJdbcTemplateConfiguration
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
MultiJdbcTemplateConfiguration
.
class
)
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
)
)
.
run
((
context
)
->
assertThat
(
context
)
.
isEmpty
(
);
.
doesNotHaveBean
(
NamedParameterJdbcOperations
.
class
)
);
}
}
@Test
@Test
public
void
testMultiDataSourceUsingPrimary
()
{
public
void
testMultiDataSourceUsingPrimary
()
{
load
(
MultiDataSourceUsingPrimaryConfiguration
.
class
);
this
.
contextRunner
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
.
withUserConfiguration
(
MultiDataSourceUsingPrimaryConfiguration
.
class
)
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
run
((
context
)
->
{
.
hasSize
(
1
);
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
assertThat
(
this
.
context
.
getBean
(
JdbcTemplate
.
class
).
getDataSource
())
assertThat
(
context
).
hasSingleBean
(
NamedParameterJdbcOperations
.
class
);
.
isEqualTo
(
this
.
context
.
getBean
(
"test1DataSource"
));
assertThat
(
context
.
getBean
(
JdbcTemplate
.
class
).
getDataSource
())
.
isEqualTo
(
context
.
getBean
(
"test1DataSource"
));
});
}
}
@Test
@Test
public
void
testMultiJdbcTemplateUsingPrimary
()
{
public
void
testMultiJdbcTemplateUsingPrimary
()
{
load
(
MultiJdbcTemplateUsingPrimaryConfiguration
.
class
);
this
.
contextRunner
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
withUserConfiguration
(
MultiJdbcTemplateUsingPrimaryConfiguration
.
class
)
.
hasSize
(
1
);
.
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBean
(
NamedParameterJdbcTemplate
.
class
)
assertThat
(
context
).
hasSingleBean
(
NamedParameterJdbcOperations
.
class
);
.
getJdbcOperations
()).
isEqualTo
(
this
.
context
.
getBean
(
"test1Template"
));
assertThat
(
context
.
getBean
(
NamedParameterJdbcTemplate
.
class
)
.
getJdbcOperations
())
.
isEqualTo
(
context
.
getBean
(
"test1Template"
));
});
}
}
@Test
@Test
public
void
testExistingCustomJdbcTemplate
()
{
public
void
testExistingCustomJdbcTemplate
()
{
load
(
CustomConfiguration
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
CustomConfiguration
.
class
)
assertThat
(
this
.
context
.
getBeansOfType
(
JdbcOperations
.
class
)).
hasSize
(
1
);
.
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBean
(
JdbcOperations
.
class
))
assertThat
(
context
).
hasSingleBean
(
JdbcOperations
.
class
);
.
isEqualTo
(
this
.
context
.
getBean
(
"customJdbcOperations"
));
assertThat
(
context
.
getBean
(
JdbcOperations
.
class
))
.
isEqualTo
(
context
.
getBean
(
"customJdbcOperations"
));
});
}
}
@Test
@Test
public
void
testExistingCustomNamedParameterJdbcTemplate
()
{
public
void
testExistingCustomNamedParameterJdbcTemplate
()
{
load
(
CustomConfiguration
.
class
);
this
.
contextRunner
.
withUserConfiguration
(
CustomConfiguration
.
class
)
assertThat
(
this
.
context
.
getBeansOfType
(
NamedParameterJdbcOperations
.
class
))
.
run
((
context
)
->
{
.
hasSize
(
1
);
assertThat
(
context
).
hasSingleBean
(
NamedParameterJdbcOperations
.
class
);
assertThat
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
))
assertThat
(
context
.
getBean
(
NamedParameterJdbcOperations
.
class
))
.
isEqualTo
(
this
.
context
.
getBean
(
"customNamedParameterJdbcOperations"
));
.
isEqualTo
(
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
;
}
}
@Configuration
@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