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,17 +65,52 @@ 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;
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 {
qm.executeUpdate("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?)", person.getPersonNo(), person.getBirthDate(), person.getLastName(), person.getFirstName());
}
public static Connection getConnection() throws SQLException {
return getConnection(jdbcUrls.iterator().next());
}
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 {
conn = getConnection();
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))");
@ -92,42 +125,25 @@ public class QueryMapperTest {
} finally {
tryClose(qm);
tryClose(conn);
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
return conn;
}
public static void insertPerson(final QueryMapper qm, final Person person) throws SQLException {
qm.executeUpdate("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?)", person.getPersonNo(), person.getBirthDate(), person.getLastName(), person.getFirstName());
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:derby:memory:derbyDB;create=true");
}
@BeforeClass
public static void setUp() throws Throwable {
conn = getConnection();
}
@AfterClass
public static void tearDown() throws Throwable {
tryClose(conn);
}
protected Connection conn;
protected QmDao qm;
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