Setup tests for all ResultSetMappers, fix some problems

This commit is contained in:
Travis Burtrum 2017-05-17 15:45:45 -04:00
parent c9e8037963
commit b425bb49b0
11 changed files with 113 additions and 44 deletions

View File

@ -93,9 +93,18 @@ public class CompilingRowToObjectMapper<T> extends RowToObjectMapper<T> {
} }
protected String typeFromName(final Class<?> type) { protected String typeFromName(final Class<?> type) {
if (returnMap || componentType == null) if(_columnCount == 1 && type.isPrimitive()) {
// need the object equivalent here, what is the best way? this works, isn't pretty...
if(type.equals(Character.TYPE))
return "Character";
if(type.equals(Integer.TYPE))
return "Integer";
final char[] name = type.getName().toCharArray();
name[0] = Character.toUpperCase(name[0]);
return new String(name);
} else if (returnMap || componentType == null) {
return type.getName(); return type.getName();
else { } else {
// an array, annoying syntax // an array, annoying syntax
final String name = type.getName(); final String name = type.getName();
final char charType = name.charAt(1); final char charType = name.charAt(1);

View File

@ -174,10 +174,11 @@ public class RowToObjectMapper<T> extends RowMapper {
} }
else if(componentType != null) // we want an array else if(componentType != null) // we want an array
try { try {
final Object[] ret = (Object[])Array.newInstance(componentType, _columnCount); final Object ret = Array.newInstance(componentType, _columnCount);
final int typeId = _tmf.getTypeId(componentType); final int typeId = _tmf.getTypeId(componentType);
for(int x = 0; x < _columnCount;) for(int x = 0; x < _columnCount;)
ret[x] = extractColumnValue(++x, typeId); Array.set(ret, x, extractColumnValue(++x, typeId));
//ret[x] = extractColumnValue(++x, typeId);
return _returnTypeClass.cast(ret); return _returnTypeClass.cast(ret);
} catch (Throwable e) { } catch (Throwable e) {
throw new MapperException(e.getClass().getName() + " when trying to create a " throw new MapperException(e.getClass().getName() + " when trying to create a "

View File

@ -1,10 +1,9 @@
package com.moparisthebest.jdbc; package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.dto.*;
import org.junit.AfterClass; import org.junit.*;
import org.junit.Assert; import org.junit.runner.RunWith;
import org.junit.BeforeClass; import org.junit.runners.Parameterized;
import org.junit.Test;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
@ -15,10 +14,10 @@ import static com.moparisthebest.jdbc.TryClose.tryClose;
/** /**
* Created by mopar on 6/10/14. * Created by mopar on 6/10/14.
*/ */
@RunWith(Parameterized.class)
public class QueryMapperTest { public class QueryMapperTest {
private static Connection conn; private static Connection conn;
protected static QueryMapper qm;
protected static final Person fieldPerson1 = new FieldPerson(1, new Date(0), "First", "Person"); protected static final Person fieldPerson1 = new FieldPerson(1, new Date(0), "First", "Person");
protected static final Boss fieldBoss1 = new FieldBoss(2, new Date(0), "Second", "Person", "Finance", "Second"); protected static final Boss fieldBoss1 = new FieldBoss(2, new Date(0), "Second", "Person", "Finance", "Second");
@ -62,6 +61,8 @@ public class QueryMapperTest {
public static void setUp() throws Throwable { public static void setUp() throws Throwable {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance(); Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
conn = DriverManager.getConnection("jdbc:derby:memory:derbyDB;create=true"); conn = DriverManager.getConnection("jdbc:derby:memory:derbyDB;create=true");
QueryMapper qm = null;
try {
qm = new QueryMapper(conn); 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 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 boss (person_no NUMERIC, department VARCHAR(40))");
@ -71,14 +72,43 @@ public class QueryMapperTest {
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 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()); qm.executeUpdate("INSERT INTO boss (person_no, department) VALUES (?, ?)", boss.getPersonNo(), boss.getDepartment());
} }
} finally {
tryClose(qm);
}
} }
@AfterClass @AfterClass
public static void tearDown() throws Throwable { public static void tearDown() throws Throwable {
tryClose(qm);
tryClose(conn); tryClose(conn);
} }
protected QueryMapper qm;
protected final ResultSetMapper rsm;
public QueryMapperTest(final ResultSetMapper rsm) {
this.rsm = rsm;
}
@Before
public void open() {
this.qm = new QueryMapper(conn, rsm);
}
@After
public void close() {
tryClose(qm);
}
@Parameterized.Parameters(name="{0}")
public static Collection<Object[]> getParameters()
{
return Arrays.asList(new Object[][] {
{ new ResultSetMapper() },
{ new CachingResultSetMapper() },
{ new CompilingResultSetMapper() },
});
}
// fields // fields
@Test @Test
@ -212,6 +242,21 @@ public class QueryMapperTest {
Assert.assertEquals(map, qm.toMap("SELECT first_name, last_name FROM person WHERE person_no < 4", String.class, String.class)); Assert.assertEquals(map, qm.toMap("SELECT first_name, last_name FROM person WHERE person_no < 4", String.class, String.class));
} }
@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),
})
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));
}
@Test @Test
public void testSelectMapLong() throws Throwable { public void testSelectMapLong() throws Throwable {
final Map<Long, Long> map = new HashMap<Long, Long>(); final Map<Long, Long> map = new HashMap<Long, Long>();
@ -232,6 +277,12 @@ public class QueryMapperTest {
Assert.assertEquals((Object)expected, qm.toObject("SELECT person_no FROM person WHERE person_no = ?", long.class, expected)); Assert.assertEquals((Object)expected, qm.toObject("SELECT person_no FROM person WHERE person_no = ?", long.class, 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));
}
@Test @Test
public void testSelectLongObjectArray() throws Throwable { public void testSelectLongObjectArray() throws Throwable {
final Long[] expected = {fieldPerson1.getPersonNo()}; final Long[] expected = {fieldPerson1.getPersonNo()};
@ -239,11 +290,17 @@ public class QueryMapperTest {
} }
@Test @Test
public void testSelectPrimitiveArray() throws Throwable { public void testSelectObjectArray() throws Throwable {
final Long[] arr = {1L, 2L, 3L}; final Long[] arr = {1L, 2L, 3L};
Assert.assertArrayEquals(arr, qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long[].class, fieldPerson1.getPersonNo())); Assert.assertArrayEquals(arr, qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long[].class, fieldPerson1.getPersonNo()));
} }
@Test
public void testSelectPrimitiveArray() throws Throwable {
final long[] arr = {1L, 2L, 3L};
Assert.assertArrayEquals(arr, qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", long[].class, fieldPerson1.getPersonNo()));
}
@Test(expected = com.moparisthebest.jdbc.MapperException.class) @Test(expected = com.moparisthebest.jdbc.MapperException.class)
public void testNoDefaultConstructorFails() throws Throwable { public void testNoDefaultConstructorFails() throws Throwable {
qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long.class, fieldPerson1.getPersonNo()); qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long.class, fieldPerson1.getPersonNo());
@ -260,10 +317,12 @@ public class QueryMapperTest {
return arrayMap; return arrayMap;
} }
private static void testPerson(final Person expected, final String query) throws Throwable { private void testPerson(final Person expected, final String query) throws Throwable {
final Person actual = qm.toObject(query, expected.getClass(), expected.getPersonNo()); final Person actual = qm.toObject(query, expected.getClass(), expected.getPersonNo());
//System.out.println("expected: " + expected); /*
//System.out.println("actual: " + actual); System.out.println("expected: " + expected);
System.out.println("actual: " + actual);
*/
Assert.assertEquals(expected, actual); Assert.assertEquals(expected, actual);
} }
} }

View File

@ -6,10 +6,10 @@ import java.util.Date;
* Created by mopar on 6/10/14. * Created by mopar on 6/10/14.
*/ */
public class FieldBoss extends FieldPerson implements Boss { public class FieldBoss extends FieldPerson implements Boss {
protected String department; public String department;
protected String first_name; public String first_name;
protected FieldBoss() { public FieldBoss() {
super(); super();
} }

View File

@ -7,12 +7,12 @@ import java.util.Date;
*/ */
public class FieldPerson implements Person { public class FieldPerson implements Person {
protected long personNo; public long personNo;
protected Date birthDate; public Date birthDate;
protected String firstName; public String firstName;
protected String lastName; public String lastName;
protected FieldPerson(){ public FieldPerson(){
} }
public FieldPerson(long personNo, Date birthDate, String firstName, String lastName) { public FieldPerson(long personNo, Date birthDate, String firstName, String lastName) {

View File

@ -6,10 +6,10 @@ import java.util.Date;
* Created by mopar on 6/10/14. * Created by mopar on 6/10/14.
*/ */
public class ReverseFieldBoss extends ReverseFieldPerson implements Boss { public class ReverseFieldBoss extends ReverseFieldPerson implements Boss {
protected String department; public String department;
protected String firstName; public String firstName;
protected ReverseFieldBoss() { public ReverseFieldBoss() {
super(); super();
} }

View File

@ -7,12 +7,12 @@ import java.util.Date;
*/ */
public class ReverseFieldPerson implements Person { public class ReverseFieldPerson implements Person {
protected long personNo; public long personNo;
protected Date birthDate; public Date birthDate;
protected String first_name; public String first_name;
protected String lastName; public String lastName;
protected ReverseFieldPerson(){ public ReverseFieldPerson(){
} }
public ReverseFieldPerson(long personNo, Date birthDate, String firstName, String lastName) { public ReverseFieldPerson(long personNo, Date birthDate, String firstName, String lastName) {

View File

@ -6,10 +6,10 @@ import java.util.Date;
* Created by mopar on 6/10/14. * Created by mopar on 6/10/14.
*/ */
public class ReverseSetBoss extends ReverseSetPerson implements Boss { public class ReverseSetBoss extends ReverseSetPerson implements Boss {
protected String department; public String department;
protected String firstName; public String firstName;
protected ReverseSetBoss() { public ReverseSetBoss() {
super(); super();
} }

View File

@ -6,7 +6,7 @@ import java.util.Date;
* Created by mopar on 6/10/14. * Created by mopar on 6/10/14.
*/ */
public class ReverseSetPerson extends ReverseFieldPerson { public class ReverseSetPerson extends ReverseFieldPerson {
protected ReverseSetPerson() { public ReverseSetPerson() {
} }
public ReverseSetPerson(long personNo, Date birthDate, String firstName, String lastName) { public ReverseSetPerson(long personNo, Date birthDate, String firstName, String lastName) {

View File

@ -9,7 +9,7 @@ public class SetBoss extends SetPerson implements Boss {
protected String department; protected String department;
protected String first_name; protected String first_name;
protected SetBoss() { public SetBoss() {
super(); super();
} }

View File

@ -6,7 +6,7 @@ import java.util.Date;
* Created by mopar on 6/10/14. * Created by mopar on 6/10/14.
*/ */
public class SetPerson extends FieldPerson { public class SetPerson extends FieldPerson {
protected SetPerson() { public SetPerson() {
} }
public SetPerson(long personNo, Date birthDate, String firstName, String lastName) { public SetPerson(long personNo, Date birthDate, String firstName, String lastName) {