embedded database support initial commit; moved from spring-test; ResourceDatabasePopulator duplicate code with test.jdbc package that needs review

This commit is contained in:
Keith Donald
2009-04-22 17:07:20 +00:00
parent 6fbd198420
commit 2e45a19be3
17 changed files with 857 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
package org.springframework.jdbc.datasource.embedded;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
public class EmbeddedDatabaseBuilderTests {
@Test
public void testBuildDefaults() {
EmbeddedDatabase db = EmbeddedDatabaseBuilder.buildDefault();
JdbcTemplate template = new JdbcTemplate(db);
assertEquals("Keith", template.queryForObject("select NAME from T_TEST", String.class));
db.shutdown();
}
@Test
public void testBuild() {
EmbeddedDatabaseBuilder builder = EmbeddedDatabaseBuilder.relativeTo(getClass());
EmbeddedDatabase db = builder.schema("db-schema.sql").testData("db-test-data.sql").build();
JdbcTemplate template = new JdbcTemplate(db);
assertEquals("Keith", template.queryForObject("select NAME from T_TEST", String.class));
db.shutdown();
}
@Test
public void testBuildNoSuchSchema() {
try {
new EmbeddedDatabaseBuilder().schema("bogus.sql").build();
fail("Should have failed");
} catch (DataAccessException e) {
}
}
@Test
public void testBuildNoSuchTestdata() {
try {
new EmbeddedDatabaseBuilder().testData("bogus.sql").build();
fail("Should have failed");
} catch (DataAccessException e) {
}
}
}

View File

@@ -0,0 +1,34 @@
package org.springframework.jdbc.datasource.embedded;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.embedded.DatabasePopulator;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory;
public class EmbeddedDatabaseFactoryTests {
private EmbeddedDatabaseFactory factory = new EmbeddedDatabaseFactory();
@Test
public void testGetDataSource() {
StubDatabasePopulator populator = new StubDatabasePopulator();
factory.setDatabasePopulator(populator);
EmbeddedDatabase db = factory.getDatabase();
assertTrue(populator.populateCalled);
db.shutdown();
}
private static class StubDatabasePopulator implements DatabasePopulator {
private boolean populateCalled;
public void populate(JdbcTemplate template) throws DataAccessException {
this.populateCalled = true;
}
}
}

View File

@@ -0,0 +1,3 @@
drop table T_TEST if exists;
create table T_TEST (NAME varchar(50) not null);

View File

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

View File

@@ -0,0 +1,3 @@
drop table T_TEST if exists;
create table T_TEST (NAME varchar(50) not null);

View File

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