Refactor QueryMapper tests to run against JdbcMapper too, partially done

This commit is contained in:
Travis Burtrum 2018-04-21 01:27:45 -04:00
parent 3af0d94e82
commit 670e14d57e
5 changed files with 297 additions and 55 deletions

View File

@ -0,0 +1,93 @@
package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.dto.*;
import java.sql.SQLException;
@JdbcMapper.Mapper(
cachePreparedStatements = JdbcMapper.OptionalBool.FALSE
, allowReflection = JdbcMapper.OptionalBool.TRUE
)
public interface QmDao extends JdbcMapper {
public static final String personRegular = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}";
public static final String bossRegularAndUnderscore = "SELECT p.person_no, p.first_name AS firstName, p.last_name, p.birth_date, b.department, p.first_name " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = {personNo}";
public static final String bossRegularAndUnderscoreReverse = "SELECT p.person_no, p.first_name, p.last_name, p.birth_date, b.department, p.first_name AS firstName " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = {personNo}";
public static final String bossRegular = "SELECT p.person_no, p.first_name AS firstName, p.last_name, p.birth_date, b.department " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = {personNo}";
public static final String bossUnderscore = "SELECT p.person_no, p.first_name, p.last_name, p.birth_date, b.department " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = {personNo}";
@JdbcMapper.SQL(personRegular)
FieldPerson getFieldRegularPerson(long personNo) throws SQLException;
@JdbcMapper.SQL(personRegular)
BuilderPerson getBuilderPerson(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegularAndUnderscore)
FieldBoss getFieldRegularAndUnderscore(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegularAndUnderscoreReverse)
FieldBoss getFieldRegularAndUnderscoreReverse(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegular)
FieldBoss getFieldRegular(long personNo) throws SQLException;
@JdbcMapper.SQL(bossUnderscore)
FieldBoss getFieldUnderscore(long personNo) throws SQLException;
@JdbcMapper.SQL(personRegular)
SetPerson getSetRegularPerson(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegularAndUnderscore)
SetBoss getSetRegularAndUnderscore(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegularAndUnderscoreReverse)
SetBoss getSetRegularAndUnderscoreReverse(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegular)
SetBoss getSetRegular(long personNo) throws SQLException;
@JdbcMapper.SQL(bossUnderscore)
SetBoss getSetUnderscore(long personNo) throws SQLException;
@JdbcMapper.SQL(personRegular)
ReverseFieldPerson getReverseFieldRegularPerson(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegularAndUnderscore)
ReverseFieldBoss getReverseFieldRegularAndUnderscore(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegularAndUnderscoreReverse)
ReverseFieldBoss getReverseFieldRegularAndUnderscoreReverse(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegular)
ReverseFieldBoss getReverseFieldRegular(long personNo) throws SQLException;
@JdbcMapper.SQL(bossUnderscore)
ReverseFieldBoss getReverseFieldUnderscore(long personNo) throws SQLException;
@JdbcMapper.SQL(personRegular)
ReverseSetPerson getReverseSetRegularPerson(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegularAndUnderscore)
ReverseSetBoss getReverseSetRegularAndUnderscore(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegularAndUnderscoreReverse)
ReverseSetBoss getReverseSetRegularAndUnderscoreReverse(long personNo) throws SQLException;
@JdbcMapper.SQL(bossRegular)
ReverseSetBoss getReverseSetRegular(long personNo) throws SQLException;
@JdbcMapper.SQL(bossUnderscore)
ReverseSetBoss getReverseSetUnderscore(long personNo) throws SQLException;
}

View File

@ -0,0 +1,152 @@
package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.QueryMapper;
import com.moparisthebest.jdbc.ResultSetMapper;
import com.moparisthebest.jdbc.dto.*;
import java.sql.Connection;
import java.sql.SQLException;
import static com.moparisthebest.jdbc.TryClose.tryClose;
public class QueryMapperQmDao implements QmDao {
public static final String personRegular = "SELECT * FROM person WHERE person_no = ?";
public static final String bossRegularAndUnderscore = "SELECT p.person_no, p.first_name AS firstName, p.last_name, p.birth_date, b.department, p.first_name " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
public static final String bossRegularAndUnderscoreReverse = "SELECT p.person_no, p.first_name, p.last_name, p.birth_date, b.department, p.first_name AS firstName " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
public static final String bossRegular = "SELECT p.person_no, p.first_name AS firstName, p.last_name, p.birth_date, b.department " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
public static final String bossUnderscore = "SELECT p.person_no, p.first_name, p.last_name, p.birth_date, b.department " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
private final QueryMapper qm;
public QueryMapperQmDao(final Connection conn, final ResultSetMapper rsm) {
this.qm = new QueryMapper(conn, rsm);
}
@Override
public Connection getConnection() {
return qm.getConnection();
}
@Override
public void close() {
tryClose(qm);
}
@Override
public FieldPerson getFieldRegularPerson(final long personNo) throws SQLException {
return qm.toObject(personRegular, FieldPerson.class, personNo);
}
@Override
public BuilderPerson getBuilderPerson(final long personNo) throws SQLException {
return qm.toObject(personRegular, BuilderPerson.class, personNo);
}
@Override
public FieldBoss getFieldRegularAndUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossRegularAndUnderscore, FieldBoss.class, personNo);
}
@Override
public FieldBoss getFieldRegularAndUnderscoreReverse(final long personNo) throws SQLException {
return qm.toObject(bossRegularAndUnderscoreReverse, FieldBoss.class, personNo);
}
@Override
public FieldBoss getFieldRegular(final long personNo) throws SQLException {
return qm.toObject(bossRegular, FieldBoss.class, personNo);
}
@Override
public FieldBoss getFieldUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossUnderscore, FieldBoss.class, personNo);
}
@Override
public SetPerson getSetRegularPerson(final long personNo) throws SQLException {
return qm.toObject(personRegular, SetPerson.class, personNo);
}
@Override
public SetBoss getSetRegularAndUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossRegularAndUnderscore, SetBoss.class, personNo);
}
@Override
public SetBoss getSetRegularAndUnderscoreReverse(final long personNo) throws SQLException {
return qm.toObject(bossRegularAndUnderscoreReverse, SetBoss.class, personNo);
}
@Override
public SetBoss getSetRegular(final long personNo) throws SQLException {
return qm.toObject(bossRegular, SetBoss.class, personNo);
}
@Override
public SetBoss getSetUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossUnderscore, SetBoss.class, personNo);
}
@Override
public ReverseFieldPerson getReverseFieldRegularPerson(final long personNo) throws SQLException {
return qm.toObject(personRegular, ReverseFieldPerson.class, personNo);
}
@Override
public ReverseFieldBoss getReverseFieldRegularAndUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossRegularAndUnderscore, ReverseFieldBoss.class, personNo);
}
@Override
public ReverseFieldBoss getReverseFieldRegularAndUnderscoreReverse(final long personNo) throws SQLException {
return qm.toObject(bossRegularAndUnderscoreReverse, ReverseFieldBoss.class, personNo);
}
@Override
public ReverseFieldBoss getReverseFieldRegular(final long personNo) throws SQLException {
return qm.toObject(bossRegular, ReverseFieldBoss.class, personNo);
}
@Override
public ReverseFieldBoss getReverseFieldUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossUnderscore, ReverseFieldBoss.class, personNo);
}
@Override
public ReverseSetPerson getReverseSetRegularPerson(final long personNo) throws SQLException {
return qm.toObject(personRegular, ReverseSetPerson.class, personNo);
}
@Override
public ReverseSetBoss getReverseSetRegularAndUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossRegularAndUnderscore, ReverseSetBoss.class, personNo);
}
@Override
public ReverseSetBoss getReverseSetRegularAndUnderscoreReverse(final long personNo) throws SQLException {
return qm.toObject(bossRegularAndUnderscoreReverse, ReverseSetBoss.class, personNo);
}
@Override
public ReverseSetBoss getReverseSetRegular(final long personNo) throws SQLException {
return qm.toObject(bossRegular, ReverseSetBoss.class, personNo);
}
@Override
public ReverseSetBoss getReverseSetUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossUnderscore, ReverseSetBoss.class, personNo);
}
}

View File

@ -14,7 +14,6 @@ import java.util.*;
import static com.moparisthebest.jdbc.QueryMapperTest.fieldPerson1; import static com.moparisthebest.jdbc.QueryMapperTest.fieldPerson1;
import static com.moparisthebest.jdbc.QueryMapperTest.getConnection; import static com.moparisthebest.jdbc.QueryMapperTest.getConnection;
import static com.moparisthebest.jdbc.QueryMapperTest.personRegular;
import static com.moparisthebest.jdbc.TryClose.tryClose; import static com.moparisthebest.jdbc.TryClose.tryClose;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;

View File

@ -1,7 +1,9 @@
package com.moparisthebest.jdbc; package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.codegen.JdbcMapperFactory;
import com.moparisthebest.jdbc.codegen.QmDao;
import com.moparisthebest.jdbc.codegen.QueryMapperQmDao;
import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.util.ResultSetIterable;
import org.junit.*; import org.junit.*;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -11,9 +13,6 @@ import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
//IFJAVA8_START //IFJAVA8_START
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.time.*;
//IFJAVA8_END //IFJAVA8_END
import static com.moparisthebest.jdbc.TryClose.tryClose; import static com.moparisthebest.jdbc.TryClose.tryClose;
@ -63,23 +62,7 @@ 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 String personRegular = "SELECT * FROM person WHERE person_no = ?";
public static final String bossRegularAndUnderscore = "SELECT p.person_no, p.first_name AS firstName, p.last_name, p.birth_date, b.department, p.first_name " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
public static final String bossRegularAndUnderscoreReverse = "SELECT p.person_no, p.first_name, p.last_name, p.birth_date, b.department, p.first_name AS firstName " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
public static final String bossRegular = "SELECT p.person_no, p.first_name AS firstName, p.last_name, p.birth_date, b.department " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
public static final String bossUnderscore = "SELECT p.person_no, p.first_name, p.last_name, p.birth_date, b.department " +
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
static { static {
// load db once // load db once
@ -129,7 +112,7 @@ public class QueryMapperTest {
tryClose(conn); tryClose(conn);
} }
protected QueryMapper qm; protected QmDao qm;
protected final ResultSetMapper rsm; protected final ResultSetMapper rsm;
public QueryMapperTest(final ResultSetMapper rsm) { public QueryMapperTest(final ResultSetMapper rsm) {
@ -138,7 +121,7 @@ public class QueryMapperTest {
@Before @Before
public void open() { public void open() {
this.qm = new QueryMapper(conn, rsm); qm = this.rsm == null ? JdbcMapperFactory.create(QmDao.class, conn) : new QueryMapperQmDao(conn, rsm);
} }
@After @After
@ -154,6 +137,7 @@ public class QueryMapperTest {
{ new CachingResultSetMapper() }, { new CachingResultSetMapper() },
{ new CaseInsensitiveMapResultSetMapper() }, { new CaseInsensitiveMapResultSetMapper() },
{ new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) }, { new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) },
{ null /* means QmDao.class is used */ },
}); });
} }
@ -161,115 +145,136 @@ public class QueryMapperTest {
@Test @Test
public void testFieldRegularPerson() throws Throwable { public void testFieldRegularPerson() throws Throwable {
testPerson(fieldPerson1, personRegular); final Person expected = fieldPerson1;
Assert.assertEquals(expected, qm.getFieldRegularPerson(expected.getPersonNo()));
} }
@Test @Test
public void testBuilderPerson() throws Throwable { public void testBuilderPerson() throws Throwable {
testPerson(new BuilderPerson(fieldPerson1), personRegular); final Person expected = new BuilderPerson(fieldPerson1);
Assert.assertEquals(expected, qm.getBuilderPerson(expected.getPersonNo()));
} }
@Test @Test
public void testFieldRegularAndUnderscore() throws Throwable { public void testFieldRegularAndUnderscore() throws Throwable {
testPerson(fieldBoss1, bossRegularAndUnderscore); final Person expected = fieldBoss1;
Assert.assertEquals(expected, qm.getFieldRegularAndUnderscore(expected.getPersonNo()));
} }
@Test @Test
public void testFieldRegularAndUnderscoreReverse() throws Throwable { public void testFieldRegularAndUnderscoreReverse() throws Throwable {
testPerson(fieldBoss1, bossRegularAndUnderscoreReverse); final Person expected = fieldBoss1; // todo: these call constructor, write another test to call setters
Assert.assertEquals(expected, qm.getFieldRegularAndUnderscoreReverse(expected.getPersonNo()));
} }
@Test @Test
public void testFieldRegular() throws Throwable { public void testFieldRegular() throws Throwable {
testPerson(fieldBoss2, bossRegular); final Person expected = fieldBoss2;
Assert.assertEquals(expected, qm.getFieldRegular(expected.getPersonNo()));
} }
@Test @Test
public void testFieldUnderscore() throws Throwable { public void testFieldUnderscore() throws Throwable {
testPerson(fieldBoss3, bossUnderscore); final Person expected = fieldBoss3;
Assert.assertEquals(expected, qm.getFieldUnderscore(expected.getPersonNo()));
} }
// sets // sets
@Test @Test
public void testSetRegularPerson() throws Throwable { public void testSetRegularPerson() throws Throwable {
testPerson(setPerson1, personRegular); final Person expected = setPerson1;
Assert.assertEquals(expected, qm.getSetRegularPerson(expected.getPersonNo()));
} }
@Test @Test
public void testSetRegularAndUnderscore() throws Throwable { public void testSetRegularAndUnderscore() throws Throwable {
testPerson(setBoss1, bossRegularAndUnderscore); final Person expected = setBoss1;
Assert.assertEquals(expected, qm.getSetRegularAndUnderscore(expected.getPersonNo()));
} }
@Test @Test
public void testSetRegularAndUnderscoreReverse() throws Throwable { public void testSetRegularAndUnderscoreReverse() throws Throwable {
testPerson(setBoss1, bossRegularAndUnderscoreReverse); final Person expected = setBoss1;
Assert.assertEquals(expected, qm.getSetRegularAndUnderscoreReverse(expected.getPersonNo()));
} }
@Test @Test
public void testSetRegular() throws Throwable { public void testSetRegular() throws Throwable {
testPerson(setBoss2, bossRegular); final Person expected = setBoss2;
Assert.assertEquals(expected, qm.getSetRegular(expected.getPersonNo()));
} }
@Test @Test
public void testSetUnderscore() throws Throwable { public void testSetUnderscore() throws Throwable {
testPerson(setBoss3, bossUnderscore); final Person expected = setBoss3;
Assert.assertEquals(expected, qm.getSetUnderscore(expected.getPersonNo()));
} }
// reverse fields // reverse fields
@Test @Test
public void testReverseFieldRegularPerson() throws Throwable { public void testReverseFieldRegularPerson() throws Throwable {
testPerson(reverseFieldPerson1, personRegular); final Person expected = reverseFieldPerson1;
Assert.assertEquals(expected, qm.getReverseFieldRegularPerson(expected.getPersonNo()));
} }
@Test @Test
public void testReverseFieldRegularAndUnderscore() throws Throwable { public void testReverseFieldRegularAndUnderscore() throws Throwable {
testPerson(reverseFieldBoss1, bossRegularAndUnderscore); final Person expected = reverseFieldBoss1;
Assert.assertEquals(expected, qm.getReverseFieldRegularAndUnderscore(expected.getPersonNo()));
} }
@Test @Test
public void testReverseFieldRegularAndUnderscoreReverse() throws Throwable { public void testReverseFieldRegularAndUnderscoreReverse() throws Throwable {
testPerson(reverseFieldBoss1, bossRegularAndUnderscoreReverse); final Person expected = reverseFieldBoss1;
Assert.assertEquals(expected, qm.getReverseFieldRegularAndUnderscoreReverse(expected.getPersonNo()));
} }
@Test @Test
public void testReverseFieldRegular() throws Throwable { public void testReverseFieldRegular() throws Throwable {
testPerson(reverseFieldBoss3, bossRegular); final Person expected = reverseFieldBoss3;
Assert.assertEquals(expected, qm.getReverseFieldRegular(expected.getPersonNo()));
} }
@Test @Test
public void testReverseFieldUnderscore() throws Throwable { public void testReverseFieldUnderscore() throws Throwable {
testPerson(reverseFieldBoss2, bossUnderscore); final Person expected = reverseFieldBoss2;
Assert.assertEquals(expected, qm.getReverseFieldUnderscore(expected.getPersonNo()));
} }
// reverse sets // reverse sets
@Test @Test
public void testReverseSetRegularPerson() throws Throwable { public void testReverseSetRegularPerson() throws Throwable {
testPerson(reverseSetPerson1, personRegular); final Person expected = reverseSetPerson1;
Assert.assertEquals(expected, qm.getReverseSetRegularPerson(expected.getPersonNo()));
} }
@Test @Test
public void testReverseSetRegularAndUnderscore() throws Throwable { public void testReverseSetRegularAndUnderscore() throws Throwable {
testPerson(reverseSetBoss1, bossRegularAndUnderscore); final Person expected = reverseSetBoss1;
Assert.assertEquals(expected, qm.getReverseSetRegularAndUnderscore(expected.getPersonNo()));
} }
@Test @Test
public void testReverseSetRegularAndUnderscoreReverse() throws Throwable { public void testReverseSetRegularAndUnderscoreReverse() throws Throwable {
testPerson(reverseSetBoss1, bossRegularAndUnderscoreReverse); final Person expected = reverseSetBoss1;
Assert.assertEquals(expected, qm.getReverseSetRegularAndUnderscoreReverse(expected.getPersonNo()));
} }
@Test @Test
public void testReverseSetRegular() throws Throwable { public void testReverseSetRegular() throws Throwable {
testPerson(reverseSetBoss3, bossRegular); final Person expected = reverseSetBoss3;
Assert.assertEquals(expected, qm.getReverseSetRegular(expected.getPersonNo()));
} }
@Test @Test
public void testReverseSetUnderscore() throws Throwable { public void testReverseSetUnderscore() throws Throwable {
testPerson(reverseSetBoss2, bossUnderscore); final Person expected = reverseSetBoss2;
Assert.assertEquals(expected, qm.getReverseSetUnderscore(expected.getPersonNo()));
} }
/*
@Test @Test
public void testSelectLong() throws Throwable { public void testSelectLong() throws Throwable {
Assert.assertEquals(new Long(1L), qm.toObject("SELECT person_no FROM person WHERE person_no = ?", Long.class, 1L)); Assert.assertEquals(new Long(1L), qm.toObject("SELECT person_no FROM person WHERE person_no = ?", Long.class, 1L));
@ -370,15 +375,6 @@ public class QueryMapperTest {
return arrayMap; return arrayMap;
} }
private void testPerson(final Person expected, final String query) throws Throwable {
final Person actual = qm.toObject(query, expected.getClass(), expected.getPersonNo());
/*
System.out.println("expected: " + expected);
System.out.println("actual: " + actual);
*/
Assert.assertEquals(expected, actual);
}
@Test @Test
public void testCaseInsensitiveMap() throws Throwable { public void testCaseInsensitiveMap() throws Throwable {
final Map<String, String> map = qm.toListMap("SELECT 'bob' as bob, 'tom' as tom FROM person WHERE person_no = ?", String.class, 1).get(0); final Map<String, String> map = qm.toListMap("SELECT 'bob' as bob, 'tom' as tom FROM person WHERE person_no = ?", String.class, 1).get(0);
@ -574,4 +570,5 @@ public class QueryMapperTest {
} }
//IFJAVA8_END //IFJAVA8_END
*/
} }

View File

@ -9,6 +9,7 @@ import java.sql.Connection;
import java.sql.SQLException; import java.sql.SQLException;
import static com.moparisthebest.jdbc.QueryMapperTest.*; import static com.moparisthebest.jdbc.QueryMapperTest.*;
import static com.moparisthebest.jdbc.codegen.QueryMapperQmDao.*;
/** /**
* Created by mopar on 7/1/17. * Created by mopar on 7/1/17.