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
c7eb0fb2
Commit
c7eb0fb2
authored
Sep 25, 2017
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
d8cfae73
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
178 additions
and
206 deletions
+178
-206
DataSourceInitializerTests.java
...k/boot/autoconfigure/jdbc/DataSourceInitializerTests.java
+178
-206
No files found.
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java
View file @
c7eb0fb2
...
@@ -25,21 +25,18 @@ import java.util.Random;
...
@@ -25,21 +25,18 @@ import java.util.Random;
import
javax.sql.DataSource
;
import
javax.sql.DataSource
;
import
com.zaxxer.hikari.HikariDataSource
;
import
com.zaxxer.hikari.HikariDataSource
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.boot.autoconfigure.
context.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.autoconfigure.
AutoConfigurations
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.jdbc.DataSourceBuilder
;
import
org.springframework.boot.jdbc.DataSourceBuilder
;
import
org.springframework.boot.test.
util.TestPropertyValues
;
import
org.springframework.boot.test.
context.runner.ApplicationContextRunner
;
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.context.annotation.Import
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.context.annotation.Primary
;
import
org.springframework.core.io.DefaultResourceLoader
;
import
org.springframework.core.io.DefaultResourceLoader
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.Resource
;
...
@@ -62,247 +59,222 @@ import static org.junit.Assert.fail;
...
@@ -62,247 +59,222 @@ import static org.junit.Assert.fail;
*/
*/
public
class
DataSourceInitializerTests
{
public
class
DataSourceInitializerTests
{
@Rule
private
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
()
public
ExpectedException
thrown
=
ExpectedException
.
none
();
.
withUserConfiguration
(
BasicConfiguration
.
class
)
.
withPropertyValues
(
"spring.datasource.initialize=false"
,
private
final
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
"spring.datasource.url:jdbc:hsqldb:mem:testdb-"
+
new
Random
().
nextInt
());
@Before
public
void
init
()
{
TestPropertyValues
.
of
(
"spring.datasource.initialize:false"
,
"spring.datasource.url:jdbc:hsqldb:mem:testdb-"
+
new
Random
().
nextInt
())
.
applyTo
(
this
.
context
);
}
@After
public
void
restore
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
@Test
public
void
testDefaultDataSourceDoesNotExists
()
throws
Exception
{
public
void
defaultDataSourceDoesNotExists
()
{
this
.
context
.
register
(
DataSourceInitializer
.
class
,
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
).
doesNotHaveBean
(
PropertyPlaceholderAutoConfiguration
.
class
,
DataSourceProperties
.
class
);
DataSource
.
class
));
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeanNamesForType
(
DataSource
.
class
).
length
)
.
isEqualTo
(
0
);
}
}
@Test
@Test
public
void
testTwoDataSources
()
throws
Exception
{
public
void
twoDataSources
()
{
TestPropertyValues
.
of
(
"datasource.one.url=jdbc:hsqldb:mem:/one"
,
this
.
contextRunner
.
withUserConfiguration
(
TwoDataSources
.
class
)
"datasource.two.url=jdbc:hsqldb:mem:/two"
).
applyTo
(
this
.
context
);
.
withPropertyValues
(
"datasource.one.url=jdbc:hsqldb:mem:/one"
,
this
.
context
.
register
(
TwoDataSources
.
class
,
DataSourceInitializer
.
class
,
"datasource.two.url=jdbc:hsqldb:mem:/two"
).
run
((
context
)
->
PropertyPlaceholderAutoConfiguration
.
class
,
DataSourceProperties
.
class
);
assertThat
(
context
.
getBeanNamesForType
(
DataSource
.
class
)).
hasSize
(
2
));
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeanNamesForType
(
DataSource
.
class
).
length
)
.
isEqualTo
(
2
);
}
}
@Test
@Test
public
void
testDataSourceInitialized
()
throws
Exception
{
public
void
dataSourceInitialized
()
{
TestPropertyValues
.
of
(
"spring.datasource.initialize:true"
).
applyTo
(
this
.
context
);
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
DataSourceAutoConfiguration
.
class
)
PropertyPlaceholderAutoConfiguration
.
class
);
).
withPropertyValues
(
"spring.datasource.initialize:true"
).
run
((
context
)
->
{
this
.
context
.
refresh
(
);
DataSource
dataSource
=
context
.
getBean
(
DataSource
.
class
);
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
dataSource
).
isInstanceOf
(
Hikari
DataSource
.
class
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isNotNull
(
);
assertThat
(
dataSource
).
isNotNull
(
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
))
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
))
.
isEqualTo
(
1
);
.
isEqualTo
(
1
);
}
);
}
}
@Test
@Test
public
void
testDataSourceInitializedWithExplicitScript
()
throws
Exception
{
public
void
dataSourceInitializedWithExplicitScript
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
PropertyPlaceholderAutoConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
)
TestPropertyValues
).
withPropertyValues
(
.
of
(
"spring.datasource.initialize:true"
,
"spring.datasource.initialize:true"
,
"spring.datasource.schema:"
+
ClassUtils
"spring.datasource.schema:"
+
getRelativeLocationFor
(
"schema.sql"
),
.
addResourcePathToPackagePath
(
getClass
(),
"schema.sql"
),
"spring.datasource.data:"
+
getRelativeLocationFor
(
"data.sql"
)
"spring.datasource.data:"
).
run
((
context
)
->
{
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"data.sql"
))
DataSource
dataSource
=
context
.
getBean
(
DataSource
.
class
);
.
applyTo
(
this
.
context
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
this
.
context
.
refresh
();
assertThat
(
dataSource
).
isNotNull
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
assertThat
(
dataSource
).
isNotNull
();
.
isEqualTo
(
1
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
});
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
.
isEqualTo
(
1
);
}
}
@Test
@Test
public
void
testDataSourceInitializedWithMultipleScripts
()
throws
Exception
{
public
void
dataSourceInitializedWithMultipleScripts
()
{
TestPropertyValues
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
.
of
(
"spring.datasource.initialize:true"
,
DataSourceAutoConfiguration
.
class
)
"spring.datasource.schema:"
).
withPropertyValues
(
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"spring.datasource.initialize:true"
,
"schema.sql"
)
"spring.datasource.schema:"
+
getRelativeLocationFor
(
"schema.sql"
)
+
","
+
","
+
getRelativeLocationFor
(
"another.sql"
),
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"spring.datasource.data:"
+
getRelativeLocationFor
(
"data.sql"
)
"another.sql"
),
).
run
((
context
)
->
{
"spring.datasource.data:"
DataSource
dataSource
=
context
.
getBean
(
DataSource
.
class
);
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"data.sql"
))
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
.
applyTo
(
this
.
context
);
assertThat
(
dataSource
).
isNotNull
();
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
PropertyPlaceholderAutoConfiguration
.
class
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
this
.
context
.
refresh
();
.
isEqualTo
(
1
);
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from SPAM"
,
Integer
.
class
))
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
.
isEqualTo
(
0
);
assertThat
(
dataSource
).
isNotNull
();
});
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
.
isEqualTo
(
1
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from SPAM"
,
Integer
.
class
))
.
isEqualTo
(
0
);
}
}
@Test
@Test
public
void
testDataSourceInitializedWithExplicitSqlScriptEncoding
()
public
void
dataSourceInitializedWithExplicitSqlScriptEncoding
()
{
throws
Exception
{
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
DataSourceAutoConfiguration
.
class
)
PropertyPlaceholderAutoConfiguration
.
class
);
).
withPropertyValues
(
"spring.datasource.initialize:true"
,
TestPropertyValues
.
of
(
"spring.datasource.initialize:true"
,
"spring.datasource.sqlScriptEncoding:UTF-8"
,
"spring.datasource.sqlScriptEncoding:UTF-8"
,
"spring.datasource.schema:"
+
ClassUtils
"spring.datasource.schema:"
+
getRelativeLocationFor
(
"encoding-schema.sql"
),
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-schema.sql"
),
"spring.datasource.data:"
+
getRelativeLocationFor
(
"encoding-data.sql"
)
"spring.datasource.data:"
+
ClassUtils
).
run
((
context
)
->
{
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-data.sql"
))
DataSource
dataSource
=
context
.
getBean
(
DataSource
.
class
);
.
applyTo
(
this
.
context
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
this
.
context
.
refresh
();
assertThat
(
dataSource
).
isNotNull
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
))
assertThat
(
dataSource
).
isNotNull
();
.
isEqualTo
(
2
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
))
"SELECT name from BAR WHERE id=1"
,
String
.
class
)).
isEqualTo
(
"bar"
);
.
isEqualTo
(
2
);
assertThat
(
template
.
queryForObject
(
assertThat
(
"SELECT name from BAR WHERE id=2"
,
String
.
class
)).
isEqualTo
(
"ばー"
);
template
.
queryForObject
(
"SELECT name from BAR WHERE id=1"
,
String
.
class
))
});
.
isEqualTo
(
"bar"
);
assertThat
(
template
.
queryForObject
(
"SELECT name from BAR WHERE id=2"
,
String
.
class
))
.
isEqualTo
(
"ばー"
);
}
}
@Test
@Test
public
void
testInitializationDisabled
()
throws
Exception
{
public
void
initializationDisabled
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
this
.
context
Runner
.
withConfiguration
(
AutoConfigurations
.
of
(
PropertyPlaceholderAutoConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
)).
run
((
context
)
->
{
this
.
context
.
refresh
(
);
DataSource
dataSource
=
context
.
getBean
(
DataSource
.
class
);
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
context
.
publishEvent
(
new
DataSourceInitializedEvent
(
dataSource
)
);
this
.
context
.
publishEvent
(
new
DataSourceInitializedEvent
(
dataSource
)
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isNotNull
(
);
assertThat
(
dataSource
).
isNotNull
(
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
try
{
try
{
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
);
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
);
fail
(
"Query should have failed as BAR table does not exist"
);
fail
(
"Query should have failed as BAR table does not exist"
);
}
}
catch
(
BadSqlGrammarException
ex
)
{
catch
(
BadSqlGrammarException
ex
)
{
SQLException
sqlException
=
ex
.
getSQLException
();
SQLException
sqlException
=
ex
.
getSQLException
();
int
expectedCode
=
-
5501
;
// user lacks privilege or object not found
int
expectedCode
=
-
5501
;
// user lacks privilege or object not found
assertThat
(
sqlException
.
getErrorCode
()).
isEqualTo
(
expectedCode
);
assertThat
(
sqlException
.
getErrorCode
()).
isEqualTo
(
expectedCode
);
}
}
}
);
}
}
@Test
@Test
public
void
testD
ataSourceInitializedWithSchemaCredentials
()
{
public
void
d
ataSourceInitializedWithSchemaCredentials
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
this
.
context
Runner
.
withConfiguration
(
AutoConfigurations
.
of
(
PropertyPlaceholderAutoConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
)
TestPropertyValues
.
of
(
"spring.datasource.initialize:true"
,
).
withPropertyValues
(
"spring.datasource.initialize:true"
,
"spring.datasource.sqlScriptEncoding:UTF-8"
,
"spring.datasource.sqlScriptEncoding:UTF-8"
,
"spring.datasource.schema:"
+
ClassUtils
"spring.datasource.schema:"
+
getRelativeLocationFor
(
"encoding-schema.sql"
),
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-schema.sql"
),
"spring.datasource.data:"
+
getRelativeLocationFor
(
"encoding-data.sql"
),
"spring.datasource.data:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-data.sql"
),
"spring.datasource.schema-username:admin"
,
"spring.datasource.schema-username:admin"
,
"spring.datasource.schema-password:admin"
).
applyTo
(
this
.
context
);
"spring.datasource.schema-password:admin"
).
run
((
context
)
->
{
try
{
assertThat
(
context
).
hasFailed
();
this
.
context
.
refresh
();
assertThat
(
context
.
getStartupFailure
()).
isInstanceOf
(
BeanCreationException
.
class
);
fail
(
"User does not exist"
);
});
}
catch
(
Exception
ex
)
{
assertThat
(
ex
).
isInstanceOf
(
BeanCreationException
.
class
);
}
}
}
@Test
@Test
public
void
testD
ataSourceInitializedWithDataCredentials
()
{
public
void
d
ataSourceInitializedWithDataCredentials
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
this
.
context
Runner
.
withConfiguration
(
AutoConfigurations
.
of
(
PropertyPlaceholderAutoConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
)
TestPropertyValues
.
of
(
"spring.datasource.initialize:true"
,
).
withPropertyValues
(
"spring.datasource.initialize:true"
,
"spring.datasource.sqlScriptEncoding:UTF-8"
,
"spring.datasource.sqlScriptEncoding:UTF-8"
,
"spring.datasource.schema:"
+
ClassUtils
"spring.datasource.schema:"
+
getRelativeLocationFor
(
"encoding-schema.sql"
),
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-schema.sql"
),
"spring.datasource.data:"
+
getRelativeLocationFor
(
"encoding-data.sql"
),
"spring.datasource.data:"
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-data.sql"
),
"spring.datasource.data-username:admin"
,
"spring.datasource.data-username:admin"
,
"spring.datasource.data-password:admin"
).
applyTo
(
this
.
context
);
"spring.datasource.data-password:admin"
).
run
((
context
)
->
{
try
{
assertThat
(
context
).
hasFailed
();
this
.
context
.
refresh
();
assertThat
(
context
.
getStartupFailure
()).
isInstanceOf
(
BeanCreationException
.
class
);
fail
(
"User does not exist"
);
});
}
catch
(
Exception
ex
)
{
assertThat
(
ex
).
isInstanceOf
(
BeanCreationException
.
class
);
}
}
}
@Test
@Test
public
void
multipleScriptsAppliedInLexicalOrder
()
throws
Exception
{
public
void
multipleScriptsAppliedInLexicalOrder
()
{
TestPropertyValues
.
of
(
"spring.datasource.initialize:true"
,
new
ApplicationContextRunner
(()
->
{
"spring.datasource.schema:"
+
ClassUtils
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
();
.
addResourcePathToPackagePath
(
getClass
(),
"lexical-schema-*.sql"
),
context
.
setResourceLoader
(
new
ReverseOrderResourceLoader
(
new
DefaultResourceLoader
()));
"spring.datasource.data:"
return
context
;
+
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
"data.sql"
))
}
.
applyTo
(
this
.
context
);
).
withUserConfiguration
(
BasicConfiguration
.
class
).
withConfiguration
(
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
AutoConfigurations
.
of
(
DataSourceAutoConfiguration
.
class
)
PropertyPlaceholderAutoConfiguration
.
class
);
).
withPropertyValues
(
"spring.datasource.initialize=false"
,
ReverseOrderResourceLoader
resourceLoader
=
new
ReverseOrderResourceLoader
(
"spring.datasource.url:jdbc:hsqldb:mem:testdb-"
new
DefaultResourceLoader
());
+
new
Random
().
nextInt
(),
this
.
context
.
setResourceLoader
(
resourceLoader
);
"spring.datasource.initialize:true"
,
this
.
context
.
refresh
();
"spring.datasource.schema:"
+
getRelativeLocationFor
(
"lexical-schema-*.sql"
),
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
"spring.datasource.data:"
+
getRelativeLocationFor
(
"data.sql"
)
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
).
run
((
context
)
->
{
assertThat
(
dataSource
).
isNotNull
();
DataSource
dataSource
=
context
.
getBean
(
DataSource
.
class
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
assertThat
(
dataSource
).
isNotNull
();
.
isEqualTo
(
1
);
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
.
isEqualTo
(
1
);
});
}
}
@Test
@Test
public
void
testDataSourceInitializedWithInvalidSchemaResource
()
{
public
void
testDataSourceInitializedWithInvalidSchemaResource
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
PropertyPlaceholderAutoConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
)).
withPropertyValues
(
TestPropertyValues
"spring.datasource.initialize:true"
,
.
of
(
"spring.datasource.initialize:true"
,
"spring.datasource.schema:classpath:does/not/exist.sql"
"spring.datasource.schema:classpath:does/not/exist.sql"
)
).
run
((
context
)
->
{
.
applyTo
(
this
.
context
);
assertThat
(
context
).
hasFailed
();
assertThat
(
context
.
getStartupFailure
())
this
.
thrown
.
expect
(
BeanCreationException
.
class
);
.
isInstanceOf
(
BeanCreationException
.
class
);
this
.
thrown
.
expectMessage
(
"does/not/exist.sql"
);
assertThat
(
context
.
getStartupFailure
())
this
.
thrown
.
expectMessage
(
"spring.datasource.schema"
);
.
hasMessageContaining
(
"does/not/exist.sql"
);
this
.
context
.
refresh
();
assertThat
(
context
.
getStartupFailure
())
.
hasMessageContaining
(
"spring.datasource.schema"
);
});
}
}
@Test
@Test
public
void
testDataSourceInitializedWithInvalidDataResource
()
{
public
void
dataSourceInitializedWithInvalidDataResource
()
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
PropertyPlaceholderAutoConfiguration
.
class
);
DataSourceAutoConfiguration
.
class
)
TestPropertyValues
).
withPropertyValues
(
"spring.datasource.initialize:true"
,
.
of
(
"spring.datasource.initialize:true"
,
"spring.datasource.schema:"
+
getRelativeLocationFor
(
"schema.sql"
),
"spring.datasource.schema:"
+
ClassUtils
"spring.datasource.data:classpath:does/not/exist.sql"
).
run
((
context
)
->
{
.
addResourcePathToPackagePath
(
getClass
(),
"schema.sql"
),
assertThat
(
context
).
hasFailed
();
"spring.datasource.data:classpath:does/not/exist.sql"
)
assertThat
(
context
.
getStartupFailure
())
.
applyTo
(
this
.
context
);
.
isInstanceOf
(
BeanCreationException
.
class
);
assertThat
(
context
.
getStartupFailure
())
this
.
thrown
.
expect
(
BeanCreationException
.
class
);
.
hasMessageContaining
(
"does/not/exist.sql"
);
this
.
thrown
.
expectMessage
(
"does/not/exist.sql"
);
assertThat
(
context
.
getStartupFailure
())
this
.
thrown
.
expectMessage
(
"spring.datasource.data"
);
.
hasMessageContaining
(
"spring.datasource.data"
);
this
.
context
.
refresh
();
});
}
private
String
getRelativeLocationFor
(
String
resource
)
{
return
ClassUtils
.
addResourcePathToPackagePath
(
getClass
(),
resource
);
}
@Configuration
@EnableConfigurationProperties
(
DataSourceProperties
.
class
)
@Import
(
DataSourceInitializer
.
class
)
static
class
BasicConfiguration
{
}
}
@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