Setup tests for all ResultSetMappers, fix some problems
This commit is contained in:
parent
c9e8037963
commit
b425bb49b0
@ -93,9 +93,18 @@ public class CompilingRowToObjectMapper<T> extends RowToObjectMapper<T> {
|
||||
}
|
||||
|
||||
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();
|
||||
else {
|
||||
} else {
|
||||
// an array, annoying syntax
|
||||
final String name = type.getName();
|
||||
final char charType = name.charAt(1);
|
||||
|
@ -174,10 +174,11 @@ public class RowToObjectMapper<T> extends RowMapper {
|
||||
}
|
||||
else if(componentType != null) // we want an array
|
||||
try {
|
||||
final Object[] ret = (Object[])Array.newInstance(componentType, _columnCount);
|
||||
final Object ret = Array.newInstance(componentType, _columnCount);
|
||||
final int typeId = _tmf.getTypeId(componentType);
|
||||
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);
|
||||
} catch (Throwable e) {
|
||||
throw new MapperException(e.getClass().getName() + " when trying to create a "
|
||||
|
@ -1,10 +1,9 @@
|
||||
package com.moparisthebest.jdbc;
|
||||
|
||||
import com.moparisthebest.jdbc.dto.*;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
@ -15,10 +14,10 @@ import static com.moparisthebest.jdbc.TryClose.tryClose;
|
||||
/**
|
||||
* Created by mopar on 6/10/14.
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class QueryMapperTest {
|
||||
|
||||
private static Connection conn;
|
||||
protected static QueryMapper qm;
|
||||
|
||||
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");
|
||||
@ -62,23 +61,54 @@ public class QueryMapperTest {
|
||||
public static void setUp() throws Throwable {
|
||||
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
|
||||
conn = DriverManager.getConnection("jdbc:derby:memory:derbyDB;create=true");
|
||||
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 boss (person_no NUMERIC, department VARCHAR(40))");
|
||||
for (final Person person : new Person[]{fieldPerson1})
|
||||
qm.executeUpdate("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?)", person.getPersonNo(), person.getBirthDate(), person.getLastName(), person.getFirstName());
|
||||
for (final Boss boss : new Boss[]{fieldBoss1, fieldBoss2, fieldBoss3}) {
|
||||
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());
|
||||
QueryMapper qm = null;
|
||||
try {
|
||||
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 boss (person_no NUMERIC, department VARCHAR(40))");
|
||||
for (final Person person : new Person[]{fieldPerson1})
|
||||
qm.executeUpdate("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?)", person.getPersonNo(), person.getBirthDate(), person.getLastName(), person.getFirstName());
|
||||
for (final Boss boss : new Boss[]{fieldBoss1, fieldBoss2, fieldBoss3}) {
|
||||
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());
|
||||
}
|
||||
} finally {
|
||||
tryClose(qm);
|
||||
}
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDown() throws Throwable {
|
||||
tryClose(qm);
|
||||
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
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
@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
|
||||
public void testSelectMapLong() throws Throwable {
|
||||
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));
|
||||
}
|
||||
|
||||
@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
|
||||
public void testSelectLongObjectArray() throws Throwable {
|
||||
final Long[] expected = {fieldPerson1.getPersonNo()};
|
||||
@ -239,11 +290,17 @@ public class QueryMapperTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectPrimitiveArray() throws Throwable {
|
||||
public void testSelectObjectArray() 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
|
||||
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)
|
||||
public void testNoDefaultConstructorFails() throws Throwable {
|
||||
qm.toObject("SELECT 1, 2, 3 FROM person WHERE person_no = ?", Long.class, fieldPerson1.getPersonNo());
|
||||
@ -260,10 +317,12 @@ public class QueryMapperTest {
|
||||
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());
|
||||
//System.out.println("expected: " + expected);
|
||||
//System.out.println("actual: " + actual);
|
||||
/*
|
||||
System.out.println("expected: " + expected);
|
||||
System.out.println("actual: " + actual);
|
||||
*/
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,10 @@ import java.util.Date;
|
||||
* Created by mopar on 6/10/14.
|
||||
*/
|
||||
public class FieldBoss extends FieldPerson implements Boss {
|
||||
protected String department;
|
||||
protected String first_name;
|
||||
public String department;
|
||||
public String first_name;
|
||||
|
||||
protected FieldBoss() {
|
||||
public FieldBoss() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
@ -7,12 +7,12 @@ import java.util.Date;
|
||||
*/
|
||||
public class FieldPerson implements Person {
|
||||
|
||||
protected long personNo;
|
||||
protected Date birthDate;
|
||||
protected String firstName;
|
||||
protected String lastName;
|
||||
public long personNo;
|
||||
public Date birthDate;
|
||||
public String firstName;
|
||||
public String lastName;
|
||||
|
||||
protected FieldPerson(){
|
||||
public FieldPerson(){
|
||||
}
|
||||
|
||||
public FieldPerson(long personNo, Date birthDate, String firstName, String lastName) {
|
||||
|
@ -6,10 +6,10 @@ import java.util.Date;
|
||||
* Created by mopar on 6/10/14.
|
||||
*/
|
||||
public class ReverseFieldBoss extends ReverseFieldPerson implements Boss {
|
||||
protected String department;
|
||||
protected String firstName;
|
||||
public String department;
|
||||
public String firstName;
|
||||
|
||||
protected ReverseFieldBoss() {
|
||||
public ReverseFieldBoss() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
@ -7,12 +7,12 @@ import java.util.Date;
|
||||
*/
|
||||
public class ReverseFieldPerson implements Person {
|
||||
|
||||
protected long personNo;
|
||||
protected Date birthDate;
|
||||
protected String first_name;
|
||||
protected String lastName;
|
||||
public long personNo;
|
||||
public Date birthDate;
|
||||
public String first_name;
|
||||
public String lastName;
|
||||
|
||||
protected ReverseFieldPerson(){
|
||||
public ReverseFieldPerson(){
|
||||
}
|
||||
|
||||
public ReverseFieldPerson(long personNo, Date birthDate, String firstName, String lastName) {
|
||||
|
@ -6,10 +6,10 @@ import java.util.Date;
|
||||
* Created by mopar on 6/10/14.
|
||||
*/
|
||||
public class ReverseSetBoss extends ReverseSetPerson implements Boss {
|
||||
protected String department;
|
||||
protected String firstName;
|
||||
public String department;
|
||||
public String firstName;
|
||||
|
||||
protected ReverseSetBoss() {
|
||||
public ReverseSetBoss() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ import java.util.Date;
|
||||
* Created by mopar on 6/10/14.
|
||||
*/
|
||||
public class ReverseSetPerson extends ReverseFieldPerson {
|
||||
protected ReverseSetPerson() {
|
||||
public ReverseSetPerson() {
|
||||
}
|
||||
|
||||
public ReverseSetPerson(long personNo, Date birthDate, String firstName, String lastName) {
|
||||
|
@ -9,7 +9,7 @@ public class SetBoss extends SetPerson implements Boss {
|
||||
protected String department;
|
||||
protected String first_name;
|
||||
|
||||
protected SetBoss() {
|
||||
public SetBoss() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
@ -6,7 +6,7 @@ import java.util.Date;
|
||||
* Created by mopar on 6/10/14.
|
||||
*/
|
||||
public class SetPerson extends FieldPerson {
|
||||
protected SetPerson() {
|
||||
public SetPerson() {
|
||||
}
|
||||
|
||||
public SetPerson(long personNo, Date birthDate, String firstName, String lastName) {
|
||||
|
Loading…
Reference in New Issue
Block a user