SPR-7364: added separator property to database populator to deal with things like PL/SQL

This commit is contained in:
David Syer
2011-06-02 16:22:26 +00:00
parent ac735d73ac
commit 14edc9fc03
21 changed files with 288 additions and 72 deletions

View File

@@ -1,7 +1,9 @@
package org.springframework.jdbc.config;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import javax.sql.DataSource;
@@ -22,6 +24,7 @@ public class JdbcNamespaceIntegrationTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-config.xml");
assertCorrectSetup(context, "dataSource", "h2DataSource", "derbyDataSource");
context.close();
}
@Test
@@ -30,6 +33,7 @@ public class JdbcNamespaceIntegrationTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-config.xml");
assertCorrectSetup(context, "derbyDataSource");
context.close();
}
@Test
@@ -37,6 +41,15 @@ public class JdbcNamespaceIntegrationTest {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-config-pattern.xml");
assertCorrectSetup(context, "dataSource");
context.close();
}
@Test
public void testCreateWithEndings() throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/jdbc/config/jdbc-initialize-endings-config.xml");
assertCorrectSetup(context, 2, "dataSource");
context.close();
}
@Test
@@ -58,15 +71,21 @@ 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(1, t.queryForInt("select count(*) from T_TEST"));
assertEquals(count, t.queryForInt("select count(*) from T_TEST"));
}
} finally {
context.close();
}
}
}

View File

@@ -23,8 +23,6 @@ import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.sql.Types;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.apache.commons.logging.LogFactory;

View File

@@ -42,7 +42,6 @@ import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.SQLWarningException;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator;

View File

@@ -102,6 +102,84 @@ public class DatabasePopulatorTests {
assertTestDatabaseCreated("\\$Keith\\$");
}
@Test
public void testBuildWithMultipleStatements() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-multiple.sql"));
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void testBuildWithMultipleStatementsLongSeparator() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-endings.sql"));
databasePopulator.setSeparator("@@");
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void testBuildWithMultipleStatementsWhitespaceSeparator() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-whitespace.sql"));
databasePopulator.setSeparator("/\n");
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void testBuildWithMultipleStatementsNewlineSeparator() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-newline.sql"));
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void testBuildWithMultipleStatementsMultipleNewlineSeparator() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("db-schema.sql"));
databasePopulator.addScript(resourceLoader.getResource("db-test-data-multi-newline.sql"));
databasePopulator.setSeparator("\n\n");
Connection connection = db.getConnection();
try {
databasePopulator.populate(connection);
} finally {
connection.close();
}
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'"));
assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'"));
}
@Test
public void scriptWithEolBetweenTokens() throws Exception {
databasePopulator.addScript(resourceLoader.getResource("users-schema.sql"));

View File

@@ -27,7 +27,6 @@ import java.util.Map;
import org.easymock.MockControl;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.JdkVersion;
import org.springframework.jdbc.AbstractJdbcTests;
import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;
import org.springframework.jdbc.core.SqlParameter;

View File

@@ -0,0 +1,2 @@
insert into T_TEST (NAME) values ('Keith')@@
insert into T_TEST (NAME) values ('Dave')@@

View File

@@ -0,0 +1,15 @@
<?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"/>
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data-endings.sql" separator="@@"/>
</jdbc:initialize-database>
</beans>

View File

@@ -0,0 +1,2 @@
insert into T_TEST (NAME) values ('Keith')@@
insert into T_TEST (NAME) values ('Dave')@@

View File

@@ -0,0 +1,5 @@
insert into T_TEST (NAME)
values ('Keith')
insert into T_TEST (NAME)
values ('Dave')

View File

@@ -0,0 +1,2 @@
insert into T_TEST (NAME) values ('Keith');
insert into T_TEST (NAME) values ('Dave');

View File

@@ -0,0 +1,2 @@
insert into T_TEST (NAME) values ('Keith')
insert into T_TEST (NAME) values ('Dave')

View File

@@ -0,0 +1,5 @@
insert into T_TEST (NAME) values ('Keith')
/
insert into T_TEST (NAME) values ('Dave')
/