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
2892039c
Commit
2892039c
authored
Jun 28, 2017
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Better tests for datasource initialization
See gh-9528
parent
4e19c47e
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
97 additions
and
1 deletion
+97
-1
DataSourceAutoConfigurationTests.java
.../autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
+26
-0
AbstractJpaAutoConfigurationTests.java
...oconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java
+8
-0
HibernateJpaAutoConfigurationTests.java
...configure/orm/jpa/HibernateJpaAutoConfigurationTests.java
+62
-0
city.sql
spring-boot-autoconfigure/src/test/resources/city.sql
+1
-1
No files found.
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
View file @
2892039c
...
...
@@ -35,12 +35,14 @@ import org.junit.Test;
import
org.junit.rules.ExpectedException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.jdbc.DatabaseDriver
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.datasource.SimpleDriverDataSource
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
...
...
@@ -200,6 +202,14 @@ public class DataSourceAutoConfigurationTests {
assertThat
(
dataSource
).
isInstanceOf
(
BasicDataSource
.
class
);
}
@Test
public
void
testDataSourceIsInitializedEarly
()
{
load
(
TestInitializedDataSourceConfiguration
.
class
,
"spring.datasource.initialize=true"
);
assertThat
(
this
.
context
.
getBean
(
TestInitializedDataSourceConfiguration
.
class
).
called
).
isTrue
();
}
@SuppressWarnings
(
"unchecked"
)
private
<
T
extends
DataSource
>
T
autoConfigureDataSource
(
Class
<
T
>
expectedType
,
final
String
...
hiddenPackages
)
{
...
...
@@ -251,6 +261,22 @@ public class DataSourceAutoConfigurationTests {
}
@Configuration
static
class
TestInitializedDataSourceConfiguration
{
private
boolean
called
;
@Autowired
public
void
validateDataSourceIsInitialized
(
DataSource
dataSource
)
{
// Inject the datasource to validate it is initialized at the injection point
JdbcTemplate
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
))
.
isEqualTo
(
1
);
this
.
called
=
true
;
}
}
// see testExplicitDriverClassClearsUsername
public
static
class
DatabaseTestDriver
implements
Driver
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/AbstractJpaAutoConfigurationTests.java
View file @
2892039c
...
...
@@ -201,7 +201,15 @@ public abstract class AbstractJpaAutoConfigurationTests {
}
protected
void
load
(
Class
<?>[]
configs
,
Class
<?>[]
autoConfigs
,
String
...
environment
)
{
load
(
configs
,
autoConfigs
,
null
,
environment
);
}
protected
void
load
(
Class
<?>[]
configs
,
Class
<?>[]
autoConfigs
,
ClassLoader
classLoader
,
String
...
environment
)
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
if
(
classLoader
!=
null
)
{
ctx
.
setClassLoader
(
classLoader
);
}
TestPropertyValues
.
of
(
environment
)
.
and
(
"spring.datasource.generate-unique-name"
,
"true"
)
.
applyTo
(
ctx
);
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java
View file @
2892039c
...
...
@@ -16,8 +16,17 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
orm
.
jpa
;
import
java.io.IOException
;
import
java.net.URL
;
import
java.net.URLClassLoader
;
import
java.util.Arrays
;
import
java.util.Enumeration
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Vector
;
import
javax.persistence.EntityManager
;
import
javax.persistence.EntityManagerFactory
;
import
javax.transaction.Synchronization
;
import
javax.transaction.SystemException
;
import
javax.transaction.Transaction
;
...
...
@@ -30,10 +39,14 @@ import org.junit.Test;
import
org.junit.rules.ExpectedException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.TestAutoConfigurationPackage
;
import
org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration
;
import
org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration
;
import
org.springframework.boot.autoconfigure.orm.jpa.test.City
;
import
org.springframework.boot.autoconfigure.transaction.jta.JtaAutoConfiguration
;
import
org.springframework.boot.orm.jpa.hibernate.SpringJtaPlatform
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.orm.jpa.JpaTransactionManager
;
import
org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
;
...
...
@@ -77,6 +90,17 @@ public class HibernateJpaAutoConfigurationTests
load
(
"spring.datasource.data:classpath:/city.sql"
);
}
@Test
public
void
testDataScriptRunsEarly
()
{
load
(
new
Class
<?>[]
{
TestInitializedJpaConfiguration
.
class
},
null
,
new
HideDataScriptClassLoader
(),
"spring.jpa.show-sql=true"
,
"spring.jpa.hibernate.ddl-auto:create-drop"
,
"spring.datasource.data:classpath:/city.sql"
);
assertThat
(
this
.
context
.
getBean
(
TestInitializedJpaConfiguration
.
class
).
called
).
isTrue
();
}
@Test
public
void
testFlywayPlusValidation
()
throws
Exception
{
load
(
new
Class
<?>[
0
],
new
Class
<?>[]
{
FlywayAutoConfiguration
.
class
},
...
...
@@ -125,6 +149,25 @@ public class HibernateJpaAutoConfigurationTests
assertThat
(
transactionManager
.
isRollbackOnCommitFailure
()).
isTrue
();
}
@Configuration
@TestAutoConfigurationPackage
(
City
.
class
)
static
class
TestInitializedJpaConfiguration
{
private
boolean
called
;
@Autowired
public
void
validateDataSourceIsInitialized
(
EntityManagerFactory
entityManagerFactory
)
{
// Inject the entity manager to validate it is initialized at the injection point
EntityManager
entityManager
=
entityManagerFactory
.
createEntityManager
();
City
city
=
entityManager
.
find
(
City
.
class
,
2000L
);
assertThat
(
city
).
isNotNull
();
assertThat
(
city
.
getName
()).
isEqualTo
(
"Washington"
);
this
.
called
=
true
;
}
}
public
static
class
TestJtaPlatform
implements
JtaPlatform
{
@Override
...
...
@@ -159,4 +202,23 @@ public class HibernateJpaAutoConfigurationTests
}
private
static
class
HideDataScriptClassLoader
extends
URLClassLoader
{
private
static
final
List
<
String
>
HIDDEN_RESOURCES
=
Arrays
.
asList
(
"schema-all.sql"
,
"schema.sql"
);
HideDataScriptClassLoader
()
{
super
(
new
URL
[
0
],
HideDataScriptClassLoader
.
class
.
getClassLoader
());
}
@Override
public
Enumeration
<
URL
>
getResources
(
String
name
)
throws
IOException
{
if
(
HIDDEN_RESOURCES
.
contains
(
name
))
{
return
new
Vector
().
elements
();
}
return
super
.
getResources
(
name
);
}
}
}
spring-boot-autoconfigure/src/test/resources/city.sql
View file @
2892039c
INSERT
INTO
CITY
(
NAME
,
STATE
,
COUNTRY
,
MAP
)
values
(
'Washington'
,
'DC'
,
'US'
,
'Google'
);
\ No newline at end of file
INSERT
INTO
CITY
(
ID
,
NAME
,
STATE
,
COUNTRY
,
MAP
)
values
(
2000
,
'Washington'
,
'DC'
,
'US'
,
'Google'
);
\ 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