embedded database support initial commit; moved from spring-test; ResourceDatabasePopulator duplicate code with test.jdbc package that needs review
This commit is contained in:
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
drop table T_TEST if exists;
|
||||
|
||||
create table T_TEST (NAME varchar(50) not null);
|
||||
@@ -0,0 +1 @@
|
||||
insert into T_TEST (NAME) values ('Keith');
|
||||
3
org.springframework.jdbc/src/test/java/schema.sql
Normal file
3
org.springframework.jdbc/src/test/java/schema.sql
Normal file
@@ -0,0 +1,3 @@
|
||||
drop table T_TEST if exists;
|
||||
|
||||
create table T_TEST (NAME varchar(50) not null);
|
||||
1
org.springframework.jdbc/src/test/java/test-data.sql
Normal file
1
org.springframework.jdbc/src/test/java/test-data.sql
Normal file
@@ -0,0 +1 @@
|
||||
insert into T_TEST (NAME) values ('Keith');
|
||||
Reference in New Issue
Block a user