Polishing
This commit is contained in:
@@ -26,6 +26,9 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Abstract base class for integration tests involving database initialization.
|
||||
*
|
||||
@@ -36,8 +39,9 @@ public abstract class AbstractDatabaseInitializationTests {
|
||||
|
||||
private final ClassRelativeResourceLoader resourceLoader = new ClassRelativeResourceLoader(getClass());
|
||||
|
||||
protected EmbeddedDatabase db;
|
||||
protected JdbcTemplate jdbcTemplate;
|
||||
EmbeddedDatabase db;
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
@Before
|
||||
@@ -55,10 +59,26 @@ public abstract class AbstractDatabaseInitializationTests {
|
||||
db.shutdown();
|
||||
}
|
||||
|
||||
protected abstract EmbeddedDatabaseType getEmbeddedDatabaseType();
|
||||
abstract EmbeddedDatabaseType getEmbeddedDatabaseType();
|
||||
|
||||
protected Resource resource(String path) {
|
||||
Resource resource(String path) {
|
||||
return resourceLoader.getResource(path);
|
||||
}
|
||||
|
||||
Resource defaultSchema() {
|
||||
return resource("db-schema.sql");
|
||||
}
|
||||
|
||||
Resource usersSchema() {
|
||||
return resource("users-schema.sql");
|
||||
}
|
||||
|
||||
void assertUsersDatabaseCreated(String... lastNames) {
|
||||
for (String lastName : lastNames) {
|
||||
assertThat("Did not find user with last name [" + lastName + "].",
|
||||
jdbcTemplate.queryForObject("select count(0) from users where last_name = ?", Integer.class, lastName),
|
||||
equalTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.jdbc.datasource.DataSourceUtils;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
@@ -29,7 +28,8 @@ import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Abstract base class for integration tests for {@link ResourceDatabasePopulator}.
|
||||
* Abstract base class for integration tests for {@link ResourceDatabasePopulator}
|
||||
* and {@link DatabasePopulatorUtils}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Sam Brannen
|
||||
@@ -141,7 +141,7 @@ public abstract class AbstractDatabasePopulatorTests extends AbstractDatabaseIni
|
||||
|
||||
@Test
|
||||
public void constructorWithMultipleScriptResources() throws Exception {
|
||||
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(false, false, null, usersSchema(),
|
||||
final ResourceDatabasePopulator populator = new ResourceDatabasePopulator(usersSchema(),
|
||||
resource("users-data-with-comments.sql"));
|
||||
DatabasePopulatorUtils.execute(populator, db);
|
||||
assertUsersDatabaseCreated("Brannen", "Hoeller");
|
||||
@@ -188,20 +188,4 @@ public abstract class AbstractDatabasePopulatorTests extends AbstractDatabaseIni
|
||||
assertEquals(name, jdbcTemplate.queryForObject("select NAME from T_TEST", String.class));
|
||||
}
|
||||
|
||||
private void assertUsersDatabaseCreated(String... lastNames) {
|
||||
for (String lastName : lastNames) {
|
||||
assertThat("Did not find user with last name [" + lastName + "].",
|
||||
jdbcTemplate.queryForObject("select count(0) from users where last_name = ?", Integer.class, lastName),
|
||||
equalTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
private Resource defaultSchema() {
|
||||
return resource("db-schema.sql");
|
||||
}
|
||||
|
||||
private Resource usersSchema() {
|
||||
return resource("users-schema.sql");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,11 +19,9 @@ package org.springframework.jdbc.datasource.init;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.jdbc.datasource.init.ScriptUtils.*;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link ScriptUtils}.
|
||||
@@ -39,20 +37,11 @@ public class ScriptUtilsIntegrationTests extends AbstractDatabaseInitializationT
|
||||
}
|
||||
|
||||
@Test
|
||||
public void executeSqlScript() throws SQLException {
|
||||
ScriptUtils.executeSqlScript(db.getConnection(), resource("users-schema.sql"));
|
||||
ScriptUtils.executeSqlScript(db.getConnection(), resource("test-data-with-multi-line-comments.sql"));
|
||||
public void executeSqlScriptContainingMuliLineComments() throws SQLException {
|
||||
executeSqlScript(db.getConnection(), usersSchema());
|
||||
executeSqlScript(db.getConnection(), resource("test-data-with-multi-line-comments.sql"));
|
||||
|
||||
assertUsersDatabaseCreated("Hoeller", "Brannen");
|
||||
}
|
||||
|
||||
private void assertUsersDatabaseCreated(String... lastNames) {
|
||||
final JdbcTemplate jdbcTemplate = new JdbcTemplate(db);
|
||||
for (String lastName : lastNames) {
|
||||
assertThat("Did not find user with last name [" + lastName + "].",
|
||||
jdbcTemplate.queryForObject("select count(0) from users where last_name = ?", Integer.class, lastName),
|
||||
equalTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user