Add support for testing against multiple database backends

This commit is contained in:
Travis Burtrum 2018-05-15 07:03:55 -04:00
parent 319e4f7765
commit ca4c827788
3 changed files with 137 additions and 54 deletions

42
pom.xml
View File

@ -79,8 +79,37 @@
<dependency> <dependency>
<groupId>org.apache.derby</groupId> <groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId> <artifactId>derby</artifactId>
<version>10.10.2.0</version> <version>${derby.version}</version>
<scope>test</scope> <scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.0</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
@ -188,6 +217,10 @@
<source.classifier>jdk16-sources</source.classifier> <source.classifier>jdk16-sources</source.classifier>
<maven.javadoc.skip>true</maven.javadoc.skip> <maven.javadoc.skip>true</maven.javadoc.skip>
<jar.classifier>jdk16</jar.classifier> <jar.classifier>jdk16</jar.classifier>
<derby.version>10.12.1.1</derby.version>
<h2.version>1.4.191</h2.version>
<postgresql.version>42.2.2.jre6</postgresql.version>
<mariadb.version>1.7.3</mariadb.version>
</properties> </properties>
<modules> <modules>
<module>common</module> <module>common</module>
@ -291,7 +324,12 @@
<activation> <activation>
<jdk>[1.8,)</jdk> <jdk>[1.8,)</jdk>
</activation> </activation>
<properties>
<derby.version>10.14.2.0</derby.version>
<h2.version>1.4.197</h2.version>
<postgresql.version>42.2.2</postgresql.version>
<mariadb.version>2.2.4</mariadb.version>
</properties>
<modules> <modules>
<module>common</module> <module>common</module>
<module>runtime-compiler</module> <module>runtime-compiler</module>

View File

@ -36,6 +36,31 @@
<scope>test</scope> <scope>test</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies> </dependencies>
<profiles> <profiles>
<profile> <profile>

View File

@ -31,8 +31,6 @@ import static org.junit.Assert.assertNull;
@RunWith(Parameterized.class) @RunWith(Parameterized.class)
public class QueryMapperTest { public class QueryMapperTest {
private static Connection conn;
public static final Person fieldPerson1 = new FieldPerson(1, new Date(0), "First", "Person"); public static final Person fieldPerson1 = new FieldPerson(1, new Date(0), "First", "Person");
public static final Boss fieldBoss1 = new FieldBoss(2, new Date(0), "Second", "Person", "Finance", "Second"); public static final Boss fieldBoss1 = new FieldBoss(2, new Date(0), "Second", "Person", "Finance", "Second");
public static final Boss fieldBoss2 = new FieldBoss(3, new Date(0), "Third", "Person", "Finance", null); public static final Boss fieldBoss2 = new FieldBoss(3, new Date(0), "Third", "Person", "Finance", null);
@ -67,36 +65,30 @@ public class QueryMapperTest {
public static final Boss reverseSetBoss2 = new ReverseSetBoss(fieldBoss2); public static final Boss reverseSetBoss2 = new ReverseSetBoss(fieldBoss2);
public static final Boss reverseSetBoss3 = new ReverseSetBoss(fieldBoss3); public static final Boss reverseSetBoss3 = new ReverseSetBoss(fieldBoss3);
public static final Collection<String> jdbcUrls;
static { static {
// load db once final Collection<String> jUrls = new ArrayList<String>();
try { final String jdbcUrl = System.getProperty("jdbcUrl", "all");
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); if(jdbcUrl.equals("all")) {
Connection conn = null; jUrls.add("jdbc:hsqldb:mem:testDB");
QueryMapper qm = null; jUrls.add("jdbc:derby:memory:testDB;create=true");
try { jUrls.add("jdbc:h2:mem:testDB");
conn = getConnection(); } else if(jdbcUrl.equals("hsql") || jdbcUrl.equals("hsqldb")) {
qm = new QueryMapper(conn); jUrls.add("jdbc:hsqldb:mem:testDB");
qm.executeUpdate("CREATE TABLE person (person_no NUMERIC, first_name VARCHAR(40), last_name VARCHAR(40), birth_date TIMESTAMP)"); } else if(jdbcUrl.equals("derby")) {
qm.executeUpdate("CREATE TABLE boss (person_no NUMERIC, department VARCHAR(40))"); jUrls.add("jdbc:derby:memory:testDB;create=true");
qm.executeUpdate("CREATE TABLE val (val_no NUMERIC, num_val NUMERIC, str_val VARCHAR(40))"); } else if(jdbcUrl.equals("h2")) {
for (final Person person : people) jUrls.add("jdbc:h2:mem:testDB");
insertPerson(qm, person); } else {
for (final Boss boss : bosses) { jUrls.add(jdbcUrl);
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());
}
for (final Val val : vals)
qm.executeUpdate("INSERT INTO val (val_no, num_val, str_val) VALUES (?, ?, ?)", val.valNo, val.numVal, val.strVal);
} finally {
tryClose(qm);
tryClose(conn);
}
} catch (Throwable e) {
throw new RuntimeException(e);
} }
for(final String extraJdbcUrlProp : new String[]{"postgresqlJdbcUrl", "mariadbJdbcUrl", "mysqlJdbcUrl"}) {
final String extrajdbcUrl = System.getProperty(extraJdbcUrlProp);
if(extrajdbcUrl != null)
jUrls.add(extrajdbcUrl);
}
jdbcUrls = Collections.unmodifiableCollection(jUrls);
} }
public static void insertPerson(final QueryMapper qm, final Person person) throws SQLException { public static void insertPerson(final QueryMapper qm, final Person person) throws SQLException {
@ -104,30 +96,54 @@ public class QueryMapperTest {
} }
public static Connection getConnection() throws SQLException { public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:derby:memory:derbyDB;create=true"); return getConnection(jdbcUrls.iterator().next());
} }
@BeforeClass public static Connection getConnection(final String url) throws SQLException {
public static void setUp() throws Throwable { // don't need Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); anymore?
conn = getConnection(); final Connection conn = DriverManager.getConnection(url);
} QueryMapper qm = null;
try {
@AfterClass qm = new QueryMapper(conn);
public static void tearDown() throws Throwable { try {
tryClose(conn); if (fieldPerson1.getPersonNo() == qm.toObject("SELECT person_no FROM person WHERE person_no = ?", Long.class, fieldPerson1.getPersonNo()))
return conn;
} catch(Exception e) {
// ignore, means the database hasn't been set up yet
}
qm.executeUpdate("CREATE TABLE person (person_no NUMERIC, first_name VARCHAR(40), last_name VARCHAR(40), birth_date TIMESTAMP)");
qm.executeUpdate("CREATE TABLE boss (person_no NUMERIC, department VARCHAR(40))");
qm.executeUpdate("CREATE TABLE val (val_no NUMERIC, num_val NUMERIC, str_val VARCHAR(40))");
for (final Person person : people)
insertPerson(qm, person);
for (final Boss boss : bosses) {
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());
}
for (final Val val : vals)
qm.executeUpdate("INSERT INTO val (val_no, num_val, str_val) VALUES (?, ?, ?)", val.valNo, val.numVal, val.strVal);
} finally {
tryClose(qm);
}
return conn;
} }
protected Connection conn;
protected QmDao qm; protected QmDao qm;
protected final int qmDaoType; protected final String jdbcUrl;
protected final int qmDaoType;
protected final ResultSetMapper rsm; protected final ResultSetMapper rsm;
public QueryMapperTest(final int qmDaoType, final ResultSetMapper rsm) { public QueryMapperTest(final String jdbcUrl, final int qmDaoType, final ResultSetMapper rsm) {
this.jdbcUrl = jdbcUrl;
this.qmDaoType = qmDaoType; this.qmDaoType = qmDaoType;
this.rsm = rsm; this.rsm = rsm;
} }
@Before @Before
public void open() { public void open() throws SQLException {
this.conn = getConnection(jdbcUrl);
switch (qmDaoType) { switch (qmDaoType) {
case 0: case 0:
this.qm = new QueryMapperQmDao(conn, rsm); this.qm = new QueryMapperQmDao(conn, rsm);
@ -145,24 +161,28 @@ public class QueryMapperTest {
@After @After
public void close() { public void close() {
tryClose(qm); tryClose(qm);
tryClose(conn);
} }
@Parameterized.Parameters(name="{0}") @Parameterized.Parameters(name="{0}")
public static Collection<Object[]> getParameters() public static Collection<Object[]> getParameters()
{ {
return Arrays.asList(new Object[][] { final Collection<Object[]> params = new ArrayList<Object[]>();
{ 0, new ResultSetMapper() }, for(final String jdbcUrl : jdbcUrls)
{ 0, new CachingResultSetMapper() }, params.addAll(Arrays.asList(new Object[][] {
{ 0, new CaseInsensitiveMapResultSetMapper() }, { jdbcUrl, 0, new ResultSetMapper() },
{ 0, new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) }, { jdbcUrl, 0, new CachingResultSetMapper() },
{ jdbcUrl, 0, new CaseInsensitiveMapResultSetMapper() },
{ jdbcUrl, 0, new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) },
{ 1, new ResultSetMapper() }, { jdbcUrl, 1, new ResultSetMapper() },
{ 1, new CachingResultSetMapper() }, { jdbcUrl, 1, new CachingResultSetMapper() },
{ 1, new CaseInsensitiveMapResultSetMapper() }, { jdbcUrl, 1, new CaseInsensitiveMapResultSetMapper() },
{ 1, new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) }, { jdbcUrl, 1, new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) },
{ 2, null /* means QmDao.class is used */ }, { jdbcUrl, 2, null /* means QmDao.class is used */ },
}); }));
return params;
} }
// fields // fields