SPR-6717: Added support for database destroy scripts

This commit is contained in:
David Syer
2011-06-06 07:28:25 +00:00
parent 97e047ed66
commit 6933a1af28
5 changed files with 111 additions and 22 deletions

View File

@@ -7,7 +7,9 @@ import static org.junit.Assert.assertThat;
import javax.sql.DataSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -15,10 +17,15 @@ import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
public class JdbcNamespaceIntegrationTest {
@Rule
public ExpectedException expected = ExpectedException.none();
@Test
public void testCreateEmbeddedDatabase() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
@@ -52,6 +59,22 @@ public class JdbcNamespaceIntegrationTest {
context.close();
}
@Test
public void testCreateAndDestroy() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-destroy-config.xml");
try {
DataSource dataSource = context.getBean(DataSource.class);
JdbcTemplate template = new JdbcTemplate(dataSource);
assertEquals(1, template.queryForInt("select count(*) from T_TEST"));
context.getBean(DataSourceInitializer.class).destroy();
expected.expect(BadSqlGrammarException.class); // Table has been dropped
assertEquals(1, template.queryForInt("select count(*) from T_TEST"));
} finally {
context.close();
}
}
@Test
public void testMultipleDataSourcesHaveDifferentDatabaseNames() throws Exception {
@@ -73,14 +96,14 @@ public class JdbcNamespaceIntegrationTest {
private void assertCorrectSetup(ConfigurableApplicationContext context, String... dataSources) {
assertCorrectSetup(context, 1, dataSources);
}
private void assertCorrectSetup(ConfigurableApplicationContext context, int count, String... dataSources) {
try {
for (String dataSourceName : dataSources) {
DataSource dataSource = context.getBean(dataSourceName, DataSource.class);
JdbcTemplate t = new JdbcTemplate(dataSource);
assertEquals(count, t.queryForInt("select count(*) from T_TEST"));
JdbcTemplate template = new JdbcTemplate(dataSource);
assertEquals(count, template.queryForInt("select count(*) from T_TEST"));
}
} finally {
context.close();

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd">
<jdbc:embedded-database id="dataSource" type="HSQL"/>
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" execution="INIT"/>
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data.sql" execution="INIT"/>
<jdbc:script location="classpath:org/springframework/jdbc/config/db-drops.sql" execution="DESTROY"/>
</jdbc:initialize-database>
</beans>