Port more tests to JdbcMapper

This commit is contained in:
Travis Burtrum 2018-04-24 00:04:53 -04:00
parent b9f963ca5f
commit f9af32dba2
3 changed files with 213 additions and 34 deletions

View File

@ -1,8 +1,12 @@
package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.util.CaseInsensitiveHashMap;
import com.moparisthebest.jdbc.util.ResultSetIterable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@JdbcMapper.Mapper(
cachePreparedStatements = JdbcMapper.OptionalBool.FALSE
@ -27,6 +31,16 @@ public interface QmDao extends JdbcMapper {
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = {personNo}";
public static final String allNames = "SELECT first_name, last_name FROM person WHERE person_no < 4";
String selectPersonNo = "SELECT person_no FROM person WHERE person_no = {personNo}";
String selectMapLongPerson = "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 in (2,3,4)";
String selectLongLong = "SELECT person_no AS first_no, person_no AS last_no FROM person WHERE person_no < 4";
String selectLongArray = "SELECT 1, 2, 3 FROM person WHERE person_no = 1";
String bobTomMap = "SELECT 'bob' as bob, 'tom' as tom FROM person WHERE person_no = 1";
String selectThreePeople = "SELECT person_no, first_name, last_name, birth_date from person WHERE person_no IN ({personNo1}, {personNo2}, {personNo3}) ORDER BY person_no";
@JdbcMapper.SQL(personRegular)
FieldPerson getFieldRegularPerson(long personNo) throws SQLException;
@ -90,4 +104,56 @@ public interface QmDao extends JdbcMapper {
@JdbcMapper.SQL(bossUnderscore)
ReverseSetBoss getReverseSetUnderscore(long personNo) throws SQLException;
@SQL(selectPersonNo)
Long getPersonNo(long personNo) throws SQLException;
@SQL(selectPersonNo)
long getPersonNoPrimitive(long personNo) throws SQLException;
@SQL(selectPersonNo)
int getPersonNoPrimitiveInt(int personNo) throws SQLException;
@SQL(selectPersonNo)
Long[] getPersonNoObjectArray(Long personNo) throws SQLException;
@SQL(allNames)
List<Map<String, String>> getAllNames() throws SQLException;
/*
@SQL(allNames)
Map[] getAllNamesArray() throws SQLException; // todo: try Map<String, String>[] fix 'generic array creation' error
*/
@SQL(allNames)
Map<String, String> getAllNameMap() throws SQLException;
@SQL(selectMapLongPerson)
Map<Long, FieldBoss> getMapLongPerson() throws SQLException;
@SQL(selectLongLong)
Map<Long, Long> getMapLongLong() throws SQLException;
/*
@SQL(selectLongArray)
Long[] getLongObjectArray() throws SQLException;
@SQL(selectLongArray)
long[] getLongPrimitiveArray() throws SQLException;
*/
@SQL(bobTomMap)
List<Map<String, String>> getBobTomMap() throws SQLException;
@SQL(bobTomMap)
List<CaseInsensitiveHashMap<String, String>> getBobTomMapCaseInsensitive() throws SQLException;
@SQL(selectThreePeople)
List<FieldPerson> getThreePeople(long personNo1, long personNo2, long personNo3) throws SQLException;
@SQL(selectThreePeople)
List<FieldPerson> getThreePeopleType(long personNo1, long personNo2, long personNo3) throws SQLException;
@SQL(selectThreePeople)
ResultSetIterable<FieldPerson> getThreePeopleResultSetIterable(long personNo1, long personNo2, long personNo3) throws SQLException;
}

View File

@ -2,10 +2,15 @@ package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.QueryMapper;
import com.moparisthebest.jdbc.ResultSetMapper;
import com.moparisthebest.jdbc.TypeReference;
import com.moparisthebest.jdbc.dto.*;
import com.moparisthebest.jdbc.util.CaseInsensitiveHashMap;
import com.moparisthebest.jdbc.util.ResultSetIterable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import static com.moparisthebest.jdbc.TryClose.tryClose;
@ -28,6 +33,8 @@ public class QueryMapperQmDao implements QmDao {
"FROM person p " +
"JOIN boss b ON p.person_no = b.person_no " +
"WHERE p.person_no = ?";
public static final String selectPersonNo = "SELECT person_no FROM person WHERE person_no = ?";
public static final String selectThreePeople = "SELECT * from person WHERE person_no IN (?,?,?) ORDER BY person_no";
private final QueryMapper qm;
@ -40,6 +47,10 @@ public class QueryMapperQmDao implements QmDao {
return qm.getConnection();
}
public QueryMapper getQm() {
return qm;
}
@Override
public void close() {
tryClose(qm);
@ -149,4 +160,91 @@ public class QueryMapperQmDao implements QmDao {
public ReverseSetBoss getReverseSetUnderscore(final long personNo) throws SQLException {
return qm.toObject(bossUnderscore, ReverseSetBoss.class, personNo);
}
@Override
public Long getPersonNo(final long personNo) throws SQLException {
return qm.toObject(selectPersonNo, Long.class, personNo);
}
@Override
public long getPersonNoPrimitive(final long personNo) throws SQLException {
return qm.toObject(selectPersonNo, long.class, personNo);
}
@Override
public int getPersonNoPrimitiveInt(final int personNo) throws SQLException {
return qm.toObject(selectPersonNo, int.class, personNo);
}
@Override
public Long[] getPersonNoObjectArray(final Long personNo) throws SQLException {
return qm.toArray(selectPersonNo, Long.class, personNo);
}
@Override
public List<Map<String, String>> getAllNames() throws SQLException {
return qm.toListMap(allNames, Map.class, String.class);
}
/*
@Override
public Map<String, String>[] getAllNamesArray() throws SQLException {
return qm.toArrayMap(allNames, Map.class, String.class);
}
*/
@Override
public Map<String, String> getAllNameMap() throws SQLException {
return qm.toMap(allNames, String.class, String.class);
}
@Override
public Map<Long, FieldBoss> getMapLongPerson() throws SQLException {
return qm.toMap(selectMapLongPerson, Long.class, FieldBoss.class);
}
@Override
public Map<Long, Long> getMapLongLong() throws SQLException {
return qm.toMap(selectLongLong, Long.class, Long.class);
}
/*
@Override
public Long[] getLongObjectArray() throws SQLException {
return qm.toObject(selectLongArray, Long[].class);
}
@Override
public long[] getLongPrimitiveArray() throws SQLException {
return qm.toObject(selectLongArray, long[].class);
}
*/
@Override
public List<Map<String, String>> getBobTomMap() throws SQLException {
return qm.toListMap(bobTomMap, String.class);
}
@Override
public List<CaseInsensitiveHashMap<String, String>> getBobTomMapCaseInsensitive() throws SQLException {
return qm.toType(bobTomMap, new TypeReference<List<CaseInsensitiveHashMap<String, String>>>() {});
}
@Override
public List<FieldPerson> getThreePeople(final long personNo1, final long personNo2, final long personNo3) throws SQLException {
return qm.toList(selectThreePeople,
FieldPerson.class, personNo1, personNo2, personNo3);
}
@Override
public List<FieldPerson> getThreePeopleType(final long personNo1, final long personNo2, final long personNo3) throws SQLException {
return qm.toType(selectThreePeople,
new TypeReference<List<FieldPerson>>() {}, personNo1, personNo2, personNo3);
}
@Override
public ResultSetIterable<FieldPerson> getThreePeopleResultSetIterable(final long personNo1, final long personNo2, final long personNo3) throws SQLException {
return qm.toResultSetIterable(selectThreePeople,
FieldPerson.class, personNo1, personNo2, personNo3);
}
}

View File

@ -4,6 +4,7 @@ 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;
@ -274,45 +275,40 @@ public class QueryMapperTest {
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));
}
@Test
public void testSelectListMap() throws Throwable {
final List<Map<String, String>> arrayMap = getListMap();
Assert.assertEquals(arrayMap, qm.toListMap("SELECT first_name, last_name FROM person WHERE person_no < 4", arrayMap.get(0).getClass(), String.class));
Assert.assertEquals(arrayMap, qm.getAllNames());
}
/*
// todo: fix jdbcmapper for this
@Test
public void testSelectArrayMap() throws Throwable {
final List<Map<String, String>> arrayMap = getListMap();
assertArrayEquals(arrayMap.toArray(new Map[arrayMap.size()]), qm.toArrayMap("SELECT first_name, last_name FROM person WHERE person_no < 4", arrayMap.get(0).getClass(), String.class));
assertArrayEquals(arrayMap.toArray(new Map[arrayMap.size()]), qm.getAllNamesArray());
}
*/
@Test
public void testSelectMapString() throws Throwable {
final Map<String, String> map = new HashMap<String, String>();
for (final Person person : new Person[]{fieldPerson1, fieldBoss1, fieldBoss2})
map.put(person.getFirstName(), person.getLastName());
Assert.assertEquals(map, qm.toMap("SELECT first_name, last_name FROM person WHERE person_no < 4", String.class, String.class));
Assert.assertEquals(map, qm.getAllNameMap());
}
@Test
public void testSelectMapLongPerson() throws Throwable {
final Map<Long, Person> map = new HashMap<Long, Person>();
for (final Person person : new Person[]{
qm.toObject(bossRegular, FieldBoss.class, 2),
qm.toObject(bossRegular, FieldBoss.class, 3),
qm.toObject(bossRegular, FieldBoss.class, 4),
qm.getFieldRegular(2),
qm.getFieldRegular(3),
qm.getFieldRegular(4),
})
map.put(person.getPersonNo(), person);
Assert.assertEquals(map, qm.toMap("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 in (2,3,4)", Long.class, FieldBoss.class));
Assert.assertEquals(map, qm.getMapLongPerson());
}
@Test
@ -320,37 +316,38 @@ public class QueryMapperTest {
final Map<Long, Long> map = new HashMap<Long, Long>();
for (final Person person : new Person[]{fieldPerson1, fieldBoss1, fieldBoss2})
map.put(person.getPersonNo(), person.getPersonNo());
Assert.assertEquals(map, qm.toMap("SELECT person_no AS first_no, person_no AS last_no FROM person WHERE person_no < 4", Long.class, Long.class));
Assert.assertEquals(map, qm.getMapLongLong());
}
@Test
public void testSelectLongObject() throws Throwable {
final Long expected = fieldPerson1.getPersonNo();
Assert.assertEquals(expected, qm.toObject("SELECT person_no FROM person WHERE person_no = ?", Long.class, expected));
Assert.assertEquals(expected, qm.getPersonNo(expected));
}
@Test
public void testSelectLongPrimitive() throws Throwable {
final long expected = fieldPerson1.getPersonNo();
Assert.assertEquals((Object)expected, qm.toObject("SELECT person_no FROM person WHERE person_no = ?", long.class, expected));
Assert.assertEquals(expected, qm.getPersonNoPrimitive(expected));
}
@Test
public void testSelectIntPrimitive() throws Throwable {
final int expected = (int)fieldPerson1.getPersonNo();
Assert.assertEquals((Object)expected, qm.toObject("SELECT person_no FROM person WHERE person_no = ?", int.class, expected));
Assert.assertEquals(expected, qm.getPersonNoPrimitiveInt(expected));
}
@Test
public void testSelectLongObjectArray() throws Throwable {
final Long[] expected = {fieldPerson1.getPersonNo()};
assertArrayEquals(expected, qm.toArray("SELECT person_no FROM person WHERE person_no = ?", Long.class, expected[0]));
assertArrayEquals(expected, qm.getPersonNoObjectArray(expected[0]));
}
/*
// todo: fix these
@Test
public void testSelectObjectArray() throws Throwable {
final Long[] arr = {1L, 2L, 3L};
assertArrayEquals(arr, qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long[].class, fieldPerson1.getPersonNo()));
assertArrayEquals(arr, qm.getLongObjectArray());
}
@Test
@ -358,10 +355,13 @@ public class QueryMapperTest {
final long[] arr = {1L, 2L, 3L};
assertArrayEquals(arr, qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", long[].class, fieldPerson1.getPersonNo()));
}
*/
@Test(expected = com.moparisthebest.jdbc.MapperException.class)
public void testNoDefaultConstructorFails() throws Throwable {
qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long.class, fieldPerson1.getPersonNo());
if(qm instanceof QueryMapperQmDao)
((QueryMapperQmDao)qm).getQm().toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long.class, fieldPerson1.getPersonNo());
else
throw new MapperException("JdbcMapper wouldn't compile so skipping this...");
}
private List<Map<String, String>> getListMap() {
@ -377,7 +377,7 @@ public class QueryMapperTest {
@Test
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.getBobTomMap().get(0);
if (rsm instanceof CaseInsensitiveMapResultSetMapper) {
assertEquals("bob", map.get("bob"));
assertEquals("bob", map.get("Bob"));
@ -403,20 +403,36 @@ public class QueryMapperTest {
}
}
@Test
public void testCaseInsensitiveMapJdbcMapper() throws Throwable {
if(qm instanceof QueryMapperQmDao)
return; // skip todo: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class
final Map<String, String> map = qm.getBobTomMapCaseInsensitive().get(0);
assertEquals("bob", map.get("bob"));
assertEquals("bob", map.get("Bob"));
assertEquals("bob", map.get("BoB"));
assertEquals("bob", map.get("BOb"));
assertEquals("bob", map.get("BOB"));
assertEquals("tom", map.get("tom"));
assertEquals("tom", map.get("Tom"));
assertEquals("tom", map.get("ToM"));
assertEquals("tom", map.get("TOm"));
assertEquals("tom", map.get("TOM"));
}
@Test
public void testList() throws SQLException {
final List<FieldPerson> fromDb = qm.toList("SELECT * from person WHERE person_no IN (?,?,?) ORDER BY person_no",
FieldPerson.class, people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());
final List<FieldPerson> fromDb = qm.getThreePeople(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());
assertArrayEquals(people, fromDb.toArray());
}
@Test
public void testListType() throws SQLException {
final List<FieldPerson> fromDb = qm.toType("SELECT * from person WHERE person_no IN (?,?,?) ORDER BY person_no",
new TypeReference<List<FieldPerson>>() {}, people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());
final List<FieldPerson> fromDb = qm.getThreePeopleType(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());
assertArrayEquals(people, fromDb.toArray());
}
/*
// todo: port this one
@Test
public void testListQueryMapperList() throws SQLException {
final ListQueryMapper lqm = new ListQueryMapper(qm);
@ -425,18 +441,17 @@ public class QueryMapperTest {
assertArrayEquals(people, fromDb.toArray());
lqm.close();
}
*/
@Test
public void testResultSetIterable() throws SQLException {
final ResultSetIterable<FieldPerson> rsi = qm.toResultSetIterable("SELECT * from person WHERE person_no IN (?,?,?) ORDER BY person_no",
FieldPerson.class, people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());
final ResultSetIterable<FieldPerson> rsi = qm.getThreePeopleResultSetIterable(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());
final List<FieldPerson> fromDb = new ArrayList<FieldPerson>();
for(final FieldPerson fieldPerson : rsi)
fromDb.add(fieldPerson);
rsi.close();
assertArrayEquals(people, fromDb.toArray());
}
/*
//IFJAVA 8_START
@Test