Fix test database use with multiple test classes

This commit is contained in:
Travis Burtrum 2017-05-18 14:59:16 -04:00
parent efd1d44808
commit 5d859c49d7
1 changed files with 25 additions and 15 deletions

View File

@ -59,24 +59,34 @@ public class QueryMapperTest {
"JOIN boss b ON p.person_no = b.person_no " + "JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?"; "WHERE p.person_no = ?";
public static Connection getConnection() throws Throwable { static {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); // load db once
final Connection conn = DriverManager.getConnection("jdbc:derby:memory:derbyDB;create=true");
QueryMapper qm = null;
try { try {
qm = new QueryMapper(conn); Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
qm.executeUpdate("CREATE TABLE person (person_no NUMERIC, first_name VARCHAR(40), last_name VARCHAR(40), birth_date TIMESTAMP)"); Connection conn = null;
qm.executeUpdate("CREATE TABLE boss (person_no NUMERIC, department VARCHAR(40))"); QueryMapper qm = null;
for (final Person person : new Person[]{fieldPerson1}) try {
qm.executeUpdate("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?)", person.getPersonNo(), person.getBirthDate(), person.getLastName(), person.getFirstName()); conn = getConnection();
for (final Boss boss : new Boss[]{fieldBoss1, fieldBoss2, fieldBoss3}) { qm = new QueryMapper(conn);
qm.executeUpdate("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?)", boss.getPersonNo(), boss.getBirthDate(), boss.getLastName(), boss.getFirstName() == null ? boss.getFirst_name() : boss.getFirstName()); qm.executeUpdate("CREATE TABLE person (person_no NUMERIC, first_name VARCHAR(40), last_name VARCHAR(40), birth_date TIMESTAMP)");
qm.executeUpdate("INSERT INTO boss (person_no, department) VALUES (?, ?)", boss.getPersonNo(), boss.getDepartment()); qm.executeUpdate("CREATE TABLE boss (person_no NUMERIC, department VARCHAR(40))");
for (final Person person : new Person[]{fieldPerson1})
qm.executeUpdate("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?)", person.getPersonNo(), person.getBirthDate(), person.getLastName(), person.getFirstName());
for (final Boss boss : new Boss[]{fieldBoss1, fieldBoss2, fieldBoss3}) {
qm.executeUpdate("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?)", boss.getPersonNo(), boss.getBirthDate(), boss.getLastName(), boss.getFirstName() == null ? boss.getFirst_name() : boss.getFirstName());
qm.executeUpdate("INSERT INTO boss (person_no, department) VALUES (?, ?)", boss.getPersonNo(), boss.getDepartment());
}
} finally {
tryClose(qm);
tryClose(conn);
} }
} finally { } catch (Throwable e) {
tryClose(qm); throw new RuntimeException(e);
} }
return conn; }
public static Connection getConnection() throws Throwable {
return DriverManager.getConnection("jdbc:derby:memory:derbyDB;create=true");
} }
@BeforeClass @BeforeClass