From 670e14d57ee9d63eeff1912d8c6a60ff7082d258 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Sat, 21 Apr 2018 01:27:45 -0400 Subject: [PATCH] Refactor QueryMapper tests to run against JdbcMapper too, partially done --- .../moparisthebest/jdbc/codegen/QmDao.java | 93 +++++++++++ .../jdbc/codegen/QueryMapperQmDao.java | 152 ++++++++++++++++++ .../jdbc/CleaningQueryMapperTest.java | 1 - .../moparisthebest/jdbc/QueryMapperTest.java | 105 ++++++------ .../moparisthebest/jdbc/QueryRunnerTest.java | 1 + 5 files changed, 297 insertions(+), 55 deletions(-) create mode 100644 test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java create mode 100644 test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java diff --git a/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java b/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java new file mode 100644 index 0000000..ae788f9 --- /dev/null +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QmDao.java @@ -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; +} diff --git a/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java new file mode 100644 index 0000000..1f7e8c6 --- /dev/null +++ b/test/src/main/java/com/moparisthebest/jdbc/codegen/QueryMapperQmDao.java @@ -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); + } +} diff --git a/test/src/test/java/com/moparisthebest/jdbc/CleaningQueryMapperTest.java b/test/src/test/java/com/moparisthebest/jdbc/CleaningQueryMapperTest.java index 60db7d4..ead1757 100644 --- a/test/src/test/java/com/moparisthebest/jdbc/CleaningQueryMapperTest.java +++ b/test/src/test/java/com/moparisthebest/jdbc/CleaningQueryMapperTest.java @@ -14,7 +14,6 @@ import java.util.*; import static com.moparisthebest.jdbc.QueryMapperTest.fieldPerson1; import static com.moparisthebest.jdbc.QueryMapperTest.getConnection; -import static com.moparisthebest.jdbc.QueryMapperTest.personRegular; import static com.moparisthebest.jdbc.TryClose.tryClose; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; diff --git a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java index 313a55f..9e9a20b 100644 --- a/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java +++ b/test/src/test/java/com/moparisthebest/jdbc/QueryMapperTest.java @@ -1,7 +1,9 @@ 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.util.ResultSetIterable; import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -11,9 +13,6 @@ import java.sql.DriverManager; import java.sql.SQLException; import java.util.*; //IFJAVA8_START -import java.util.stream.Collectors; -import java.util.stream.Stream; -import java.time.*; //IFJAVA8_END 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 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 { // load db once @@ -129,7 +112,7 @@ public class QueryMapperTest { tryClose(conn); } - protected QueryMapper qm; + protected QmDao qm; protected final ResultSetMapper rsm; public QueryMapperTest(final ResultSetMapper rsm) { @@ -138,7 +121,7 @@ public class QueryMapperTest { @Before public void open() { - this.qm = new QueryMapper(conn, rsm); + qm = this.rsm == null ? JdbcMapperFactory.create(QmDao.class, conn) : new QueryMapperQmDao(conn, rsm); } @After @@ -154,6 +137,7 @@ public class QueryMapperTest { { new CachingResultSetMapper() }, { new CaseInsensitiveMapResultSetMapper() }, { new CompilingResultSetMapper(new CompilingRowToObjectMapper.Cache(true)) }, + { null /* means QmDao.class is used */ }, }); } @@ -161,115 +145,136 @@ public class QueryMapperTest { @Test public void testFieldRegularPerson() throws Throwable { - testPerson(fieldPerson1, personRegular); + final Person expected = fieldPerson1; + Assert.assertEquals(expected, qm.getFieldRegularPerson(expected.getPersonNo())); } @Test public void testBuilderPerson() throws Throwable { - testPerson(new BuilderPerson(fieldPerson1), personRegular); + final Person expected = new BuilderPerson(fieldPerson1); + Assert.assertEquals(expected, qm.getBuilderPerson(expected.getPersonNo())); } @Test public void testFieldRegularAndUnderscore() throws Throwable { - testPerson(fieldBoss1, bossRegularAndUnderscore); + final Person expected = fieldBoss1; + Assert.assertEquals(expected, qm.getFieldRegularAndUnderscore(expected.getPersonNo())); } @Test 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 public void testFieldRegular() throws Throwable { - testPerson(fieldBoss2, bossRegular); + final Person expected = fieldBoss2; + Assert.assertEquals(expected, qm.getFieldRegular(expected.getPersonNo())); } @Test public void testFieldUnderscore() throws Throwable { - testPerson(fieldBoss3, bossUnderscore); + final Person expected = fieldBoss3; + Assert.assertEquals(expected, qm.getFieldUnderscore(expected.getPersonNo())); } // sets @Test public void testSetRegularPerson() throws Throwable { - testPerson(setPerson1, personRegular); + final Person expected = setPerson1; + Assert.assertEquals(expected, qm.getSetRegularPerson(expected.getPersonNo())); } @Test public void testSetRegularAndUnderscore() throws Throwable { - testPerson(setBoss1, bossRegularAndUnderscore); + final Person expected = setBoss1; + Assert.assertEquals(expected, qm.getSetRegularAndUnderscore(expected.getPersonNo())); } @Test public void testSetRegularAndUnderscoreReverse() throws Throwable { - testPerson(setBoss1, bossRegularAndUnderscoreReverse); + final Person expected = setBoss1; + Assert.assertEquals(expected, qm.getSetRegularAndUnderscoreReverse(expected.getPersonNo())); } @Test public void testSetRegular() throws Throwable { - testPerson(setBoss2, bossRegular); + final Person expected = setBoss2; + Assert.assertEquals(expected, qm.getSetRegular(expected.getPersonNo())); } @Test public void testSetUnderscore() throws Throwable { - testPerson(setBoss3, bossUnderscore); + final Person expected = setBoss3; + Assert.assertEquals(expected, qm.getSetUnderscore(expected.getPersonNo())); } // reverse fields @Test public void testReverseFieldRegularPerson() throws Throwable { - testPerson(reverseFieldPerson1, personRegular); + final Person expected = reverseFieldPerson1; + Assert.assertEquals(expected, qm.getReverseFieldRegularPerson(expected.getPersonNo())); } @Test public void testReverseFieldRegularAndUnderscore() throws Throwable { - testPerson(reverseFieldBoss1, bossRegularAndUnderscore); + final Person expected = reverseFieldBoss1; + Assert.assertEquals(expected, qm.getReverseFieldRegularAndUnderscore(expected.getPersonNo())); } @Test public void testReverseFieldRegularAndUnderscoreReverse() throws Throwable { - testPerson(reverseFieldBoss1, bossRegularAndUnderscoreReverse); + final Person expected = reverseFieldBoss1; + Assert.assertEquals(expected, qm.getReverseFieldRegularAndUnderscoreReverse(expected.getPersonNo())); } @Test public void testReverseFieldRegular() throws Throwable { - testPerson(reverseFieldBoss3, bossRegular); + final Person expected = reverseFieldBoss3; + Assert.assertEquals(expected, qm.getReverseFieldRegular(expected.getPersonNo())); } @Test public void testReverseFieldUnderscore() throws Throwable { - testPerson(reverseFieldBoss2, bossUnderscore); + final Person expected = reverseFieldBoss2; + Assert.assertEquals(expected, qm.getReverseFieldUnderscore(expected.getPersonNo())); } // reverse sets @Test public void testReverseSetRegularPerson() throws Throwable { - testPerson(reverseSetPerson1, personRegular); + final Person expected = reverseSetPerson1; + Assert.assertEquals(expected, qm.getReverseSetRegularPerson(expected.getPersonNo())); } @Test public void testReverseSetRegularAndUnderscore() throws Throwable { - testPerson(reverseSetBoss1, bossRegularAndUnderscore); + final Person expected = reverseSetBoss1; + Assert.assertEquals(expected, qm.getReverseSetRegularAndUnderscore(expected.getPersonNo())); } @Test public void testReverseSetRegularAndUnderscoreReverse() throws Throwable { - testPerson(reverseSetBoss1, bossRegularAndUnderscoreReverse); + final Person expected = reverseSetBoss1; + Assert.assertEquals(expected, qm.getReverseSetRegularAndUnderscoreReverse(expected.getPersonNo())); } @Test public void testReverseSetRegular() throws Throwable { - testPerson(reverseSetBoss3, bossRegular); + final Person expected = reverseSetBoss3; + Assert.assertEquals(expected, qm.getReverseSetRegular(expected.getPersonNo())); } @Test public void testReverseSetUnderscore() throws Throwable { - testPerson(reverseSetBoss2, bossUnderscore); + final Person expected = reverseSetBoss2; + Assert.assertEquals(expected, qm.getReverseSetUnderscore(expected.getPersonNo())); } - +/* @Test public void testSelectLong() throws Throwable { 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; } - 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 public void testCaseInsensitiveMap() throws Throwable { final Map 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 + */ } diff --git a/test/src/test/java/com/moparisthebest/jdbc/QueryRunnerTest.java b/test/src/test/java/com/moparisthebest/jdbc/QueryRunnerTest.java index 9293017..b3b1dfd 100644 --- a/test/src/test/java/com/moparisthebest/jdbc/QueryRunnerTest.java +++ b/test/src/test/java/com/moparisthebest/jdbc/QueryRunnerTest.java @@ -9,6 +9,7 @@ import java.sql.Connection; import java.sql.SQLException; import static com.moparisthebest.jdbc.QueryMapperTest.*; +import static com.moparisthebest.jdbc.codegen.QueryMapperQmDao.*; /** * Created by mopar on 7/1/17.