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>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.10.2.0</version>
<version>${derby.version}</version>
<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>
</dependencies>
</dependencyManagement>
@ -188,6 +217,10 @@
<source.classifier>jdk16-sources</source.classifier>
<maven.javadoc.skip>true</maven.javadoc.skip>
<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>
<modules>
<module>common</module>
@ -291,7 +324,12 @@
<activation>
<jdk>[1.8,)</jdk>
</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>
<module>common</module>
<module>runtime-compiler</module>

View File

@ -36,6 +36,31 @@
<scope>test</scope>
<optional>true</optional>
</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>
<profiles>
<profile>

View File

@ -31,8 +31,6 @@ import static org.junit.Assert.assertNull;
@RunWith(Parameterized.class)
public class QueryMapperTest {
private static Connection conn;
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 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 reverseSetBoss3 = new ReverseSetBoss(fieldBoss3);
public static final Collection<String> jdbcUrls;
static {
// load db once
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Connection conn = null;
QueryMapper qm = null;
try {
conn = getConnection();
qm = new QueryMapper(conn);
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);
tryClose(conn);
}
} catch (Throwable e) {
throw new RuntimeException(e);
final Collection<String> jUrls = new ArrayList<String>();
final String jdbcUrl = System.getProperty("jdbcUrl", "all");
if(jdbcUrl.equals("all")) {
jUrls.add("jdbc:hsqldb:mem:testDB");
jUrls.add("jdbc:derby:memory:testDB;create=true");
jUrls.add("jdbc:h2:mem:testDB");
} else if(jdbcUrl.equals("hsql") || jdbcUrl.equals("hsqldb")) {
jUrls.add("jdbc:hsqldb:mem:testDB");
} else if(jdbcUrl.equals("derby")) {
jUrls.add("jdbc:derby:memory:testDB;create=true");
} else if(jdbcUrl.equals("h2")) {
jUrls.add("jdbc:h2:mem:testDB");
} else {
jUrls.add(jdbcUrl);
}
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 {
@ -104,30 +96,54 @@ public class QueryMapperTest {
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:derby:memory:derbyDB;create=true");
return getConnection(jdbcUrls.iterator().next());
}
@BeforeClass
public static void setUp() throws Throwable {
conn = getConnection();
}
@AfterClass
public static void tearDown() throws Throwable {
tryClose(conn);
public static Connection getConnection(final String url) throws SQLException {
// don't need Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); anymore?
final Connection conn = DriverManager.getConnection(url);
QueryMapper qm = null;
try {
qm = new QueryMapper(conn);
try {
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 final int qmDaoType;
protected final String jdbcUrl;
protected final int qmDaoType;
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.rsm = rsm;
}
@Before
public void open() {
public void open() throws SQLException {
this.conn = getConnection(jdbcUrl);
switch (qmDaoType) {
case 0:
this.qm = new QueryMapperQmDao(conn, rsm);
@ -145,24 +161,28 @@ public class QueryMapperTest {
@After
public void close() {
tryClose(qm);
tryClose(conn);
}
@Parameterized.Parameters(name="{0}")
public static Collection<Object[]> getParameters()
{
return Arrays.asList(new Object[][] {
{ 0, new ResultSetMapper() },
{ 0, new CachingResultSetMapper() },
{ 0, new CaseInsensitiveMapResultSetMapper() },
{ 0, new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) },
final Collection<Object[]> params = new ArrayList<Object[]>();
for(final String jdbcUrl : jdbcUrls)
params.addAll(Arrays.asList(new Object[][] {
{ jdbcUrl, 0, new ResultSetMapper() },
{ jdbcUrl, 0, new CachingResultSetMapper() },
{ jdbcUrl, 0, new CaseInsensitiveMapResultSetMapper() },
{ jdbcUrl, 0, new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) },
{ 1, new ResultSetMapper() },
{ 1, new CachingResultSetMapper() },
{ 1, new CaseInsensitiveMapResultSetMapper() },
{ 1, new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) },
{ jdbcUrl, 1, new ResultSetMapper() },
{ jdbcUrl, 1, new CachingResultSetMapper() },
{ jdbcUrl, 1, new CaseInsensitiveMapResultSetMapper() },
{ 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