2014-06-11 13:22:01 -04:00
package com.moparisthebest.jdbc ;
2018-04-21 01:27:45 -04:00
import com.moparisthebest.jdbc.codegen.JdbcMapperFactory ;
import com.moparisthebest.jdbc.codegen.QmDao ;
import com.moparisthebest.jdbc.codegen.QueryMapperQmDao ;
2014-06-11 13:22:01 -04:00
import com.moparisthebest.jdbc.dto.* ;
2018-04-24 00:04:53 -04:00
import com.moparisthebest.jdbc.util.ResultSetIterable ;
2017-05-17 15:45:45 -04:00
import org.junit.* ;
import org.junit.runner.RunWith ;
import org.junit.runners.Parameterized ;
2014-06-11 13:22:01 -04:00
import java.sql.Connection ;
import java.sql.DriverManager ;
2017-06-08 23:02:12 -04:00
import java.sql.SQLException ;
2014-06-11 14:30:22 -04:00
import java.util.* ;
2017-06-12 22:57:43 -04:00
//IFJAVA8_START
2018-05-04 00:26:04 -04:00
import java.util.stream.Collectors ;
import java.util.stream.Stream ;
2017-06-12 22:57:43 -04:00
//IFJAVA8_END
2014-06-11 13:22:01 -04:00
import static com.moparisthebest.jdbc.TryClose.tryClose ;
2017-06-08 23:02:12 -04:00
import static org.junit.Assert.assertArrayEquals ;
2017-05-18 14:26:23 -04:00
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertNull ;
2014-06-11 13:22:01 -04:00
/ * *
* Created by mopar on 6 / 10 / 14 .
* /
2017-05-17 15:45:45 -04:00
@RunWith ( Parameterized . class )
2014-06-11 13:22:01 -04:00
public class QueryMapperTest {
private static Connection conn ;
2017-05-18 14:26:23 -04:00
public static final Person fieldPerson1 = new FieldPerson ( 1 , new Date ( 0 ) , " First " , " Person " ) ;
public static final Boss fieldBoss1 = new FieldBoss ( 2 , new Date ( 0 ) , " Second " , " Person " , " Finance " , " Second " ) ;
public static final Boss fieldBoss2 = new FieldBoss ( 3 , new Date ( 0 ) , " Third " , " Person " , " Finance " , null ) ;
public static final Boss fieldBoss3 = new FieldBoss ( 4 , new Date ( 0 ) , null , " Person " , " Finance " , " Fourth " ) ;
2017-06-08 23:02:12 -04:00
public static final Person fieldPerson2 = new FieldPerson ( 5 , new Date ( 0 ) , " Second " , " Person " ) ;
public static final Person fieldPerson3 = new FieldPerson ( 6 , new Date ( 0 ) , " Third " , " Person " ) ;
2018-01-12 16:46:22 -05:00
public static final Person fieldPerson1NullName = new FieldPerson ( 1 , new Date ( 0 ) , null , null ) ;
2017-06-08 23:02:12 -04:00
public static final Person [ ] people = new Person [ ] { fieldPerson1 , fieldPerson2 , fieldPerson3 } ;
2017-06-13 22:56:00 -04:00
public static final Boss [ ] bosses = new Boss [ ] { fieldBoss1 , fieldBoss2 , fieldBoss3 } ;
public static final Val [ ] vals = new Val [ ] {
new Val ( 1 , 1969 , " 1969 " ) ,
new Val ( 2 , 0 , " America/New_York " ) ,
new Val ( 3 , - 5 , " -5 " ) ,
2018-01-12 16:46:22 -05:00
new Val ( 4 , 4 , null ) ,
2017-06-13 22:56:00 -04:00
} ;
2014-06-11 13:22:01 -04:00
2017-05-18 14:26:23 -04:00
public static final Person setPerson1 = new SetPerson ( fieldPerson1 ) ;
public static final Boss setBoss1 = new SetBoss ( fieldBoss1 ) ;
public static final Boss setBoss2 = new SetBoss ( fieldBoss2 ) ;
public static final Boss setBoss3 = new SetBoss ( fieldBoss3 ) ;
2014-06-11 13:22:01 -04:00
2017-05-18 14:26:23 -04:00
public static final Person reverseFieldPerson1 = new ReverseFieldPerson ( fieldPerson1 ) ;
public static final Boss reverseFieldBoss1 = new ReverseFieldBoss ( fieldBoss1 ) ;
public static final Boss reverseFieldBoss2 = new ReverseFieldBoss ( fieldBoss2 ) ;
public static final Boss reverseFieldBoss3 = new ReverseFieldBoss ( fieldBoss3 ) ;
2014-06-11 13:22:01 -04:00
2017-05-18 14:26:23 -04:00
public static final Person reverseSetPerson1 = new ReverseSetPerson ( fieldPerson1 ) ;
public static final Boss reverseSetBoss1 = new ReverseSetBoss ( fieldBoss1 ) ;
public static final Boss reverseSetBoss2 = new ReverseSetBoss ( fieldBoss2 ) ;
public static final Boss reverseSetBoss3 = new ReverseSetBoss ( fieldBoss3 ) ;
2014-06-11 13:22:01 -04:00
2018-04-21 01:27:45 -04:00
2014-06-11 13:22:01 -04:00
2017-05-18 14:59:16 -04:00
static {
// load db once
2017-05-17 15:45:45 -04:00
try {
2017-05-18 14:59:16 -04:00
Class . forName ( " org.apache.derby.jdbc.EmbeddedDriver " ) . newInstance ( ) ;
Connection conn = null ;
QueryMapper qm = null ;
try {
conn = getConnection ( ) ;
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)) " ) ;
2017-06-13 22:56:00 -04:00
qm . executeUpdate ( " CREATE TABLE val (val_no NUMERIC, num_val NUMERIC, str_val VARCHAR(40)) " ) ;
2017-06-08 23:02:12 -04:00
for ( final Person person : people )
2018-01-12 16:46:22 -05:00
insertPerson ( qm , person ) ;
2017-06-08 23:02:12 -04:00
for ( final Boss boss : bosses ) {
2017-05-18 14:59:16 -04:00
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 ( ) ) ;
}
2017-06-13 22:56:00 -04:00
for ( final Val val : vals )
qm . executeUpdate ( " INSERT INTO val (val_no, num_val, str_val) VALUES (?, ?, ?) " , val . valNo , val . numVal , val . strVal ) ;
2017-05-18 14:59:16 -04:00
} finally {
tryClose ( qm ) ;
tryClose ( conn ) ;
2017-05-17 15:45:45 -04:00
}
2017-05-18 14:59:16 -04:00
} catch ( Throwable e ) {
throw new RuntimeException ( e ) ;
2014-06-11 13:22:01 -04:00
}
2017-05-18 14:59:16 -04:00
}
2018-01-12 16:46:22 -05:00
public static void insertPerson ( final QueryMapper qm , final Person person ) throws SQLException {
qm . executeUpdate ( " INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES (?, ?, ?, ?) " , person . getPersonNo ( ) , person . getBirthDate ( ) , person . getLastName ( ) , person . getFirstName ( ) ) ;
}
2017-07-01 02:07:04 -04:00
public static Connection getConnection ( ) throws SQLException {
2017-05-18 14:59:16 -04:00
return DriverManager . getConnection ( " jdbc:derby:memory:derbyDB;create=true " ) ;
2017-05-18 14:26:23 -04:00
}
@BeforeClass
public static void setUp ( ) throws Throwable {
conn = getConnection ( ) ;
2014-06-11 13:22:01 -04:00
}
@AfterClass
public static void tearDown ( ) throws Throwable {
tryClose ( conn ) ;
}
2018-04-21 01:27:45 -04:00
protected QmDao qm ;
2017-05-17 15:45:45 -04:00
protected final ResultSetMapper rsm ;
public QueryMapperTest ( final ResultSetMapper rsm ) {
this . rsm = rsm ;
}
@Before
public void open ( ) {
2018-04-21 01:27:45 -04:00
qm = this . rsm = = null ? JdbcMapperFactory . create ( QmDao . class , conn ) : new QueryMapperQmDao ( conn , rsm ) ;
2017-05-17 15:45:45 -04:00
}
@After
public void close ( ) {
tryClose ( qm ) ;
}
@Parameterized.Parameters ( name = " {0} " )
public static Collection < Object [ ] > getParameters ( )
{
return Arrays . asList ( new Object [ ] [ ] {
{ new ResultSetMapper ( ) } ,
{ new CachingResultSetMapper ( ) } ,
2017-05-18 14:26:23 -04:00
{ new CaseInsensitiveMapResultSetMapper ( ) } ,
2017-06-18 21:58:55 -04:00
{ new CompilingResultSetMapper ( new CompilingRowToObjectMapper . Cache ( true ) ) } ,
2018-04-21 01:27:45 -04:00
{ null /* means QmDao.class is used */ } ,
2017-05-17 15:45:45 -04:00
} ) ;
}
2014-06-11 13:22:01 -04:00
// fields
@Test
public void testFieldRegularPerson ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = fieldPerson1 ;
Assert . assertEquals ( expected , qm . getFieldRegularPerson ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
2018-01-15 13:24:53 -05:00
@Test
public void testBuilderPerson ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = new BuilderPerson ( fieldPerson1 ) ;
Assert . assertEquals ( expected , qm . getBuilderPerson ( expected . getPersonNo ( ) ) ) ;
2018-01-15 13:24:53 -05:00
}
2014-06-11 13:22:01 -04:00
@Test
public void testFieldRegularAndUnderscore ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = fieldBoss1 ;
Assert . assertEquals ( expected , qm . getFieldRegularAndUnderscore ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testFieldRegularAndUnderscoreReverse ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = fieldBoss1 ; // todo: these call constructor, write another test to call setters
Assert . assertEquals ( expected , qm . getFieldRegularAndUnderscoreReverse ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testFieldRegular ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = fieldBoss2 ;
Assert . assertEquals ( expected , qm . getFieldRegular ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testFieldUnderscore ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = fieldBoss3 ;
Assert . assertEquals ( expected , qm . getFieldUnderscore ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
// sets
@Test
public void testSetRegularPerson ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = setPerson1 ;
Assert . assertEquals ( expected , qm . getSetRegularPerson ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testSetRegularAndUnderscore ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = setBoss1 ;
Assert . assertEquals ( expected , qm . getSetRegularAndUnderscore ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testSetRegularAndUnderscoreReverse ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = setBoss1 ;
Assert . assertEquals ( expected , qm . getSetRegularAndUnderscoreReverse ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testSetRegular ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = setBoss2 ;
Assert . assertEquals ( expected , qm . getSetRegular ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testSetUnderscore ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = setBoss3 ;
Assert . assertEquals ( expected , qm . getSetUnderscore ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
// reverse fields
@Test
public void testReverseFieldRegularPerson ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseFieldPerson1 ;
Assert . assertEquals ( expected , qm . getReverseFieldRegularPerson ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testReverseFieldRegularAndUnderscore ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseFieldBoss1 ;
Assert . assertEquals ( expected , qm . getReverseFieldRegularAndUnderscore ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testReverseFieldRegularAndUnderscoreReverse ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseFieldBoss1 ;
Assert . assertEquals ( expected , qm . getReverseFieldRegularAndUnderscoreReverse ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testReverseFieldRegular ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseFieldBoss3 ;
Assert . assertEquals ( expected , qm . getReverseFieldRegular ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testReverseFieldUnderscore ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseFieldBoss2 ;
Assert . assertEquals ( expected , qm . getReverseFieldUnderscore ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
// reverse sets
@Test
public void testReverseSetRegularPerson ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseSetPerson1 ;
Assert . assertEquals ( expected , qm . getReverseSetRegularPerson ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testReverseSetRegularAndUnderscore ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseSetBoss1 ;
Assert . assertEquals ( expected , qm . getReverseSetRegularAndUnderscore ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testReverseSetRegularAndUnderscoreReverse ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseSetBoss1 ;
Assert . assertEquals ( expected , qm . getReverseSetRegularAndUnderscoreReverse ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testReverseSetRegular ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseSetBoss3 ;
Assert . assertEquals ( expected , qm . getReverseSetRegular ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
@Test
public void testReverseSetUnderscore ( ) throws Throwable {
2018-04-21 01:27:45 -04:00
final Person expected = reverseSetBoss2 ;
Assert . assertEquals ( expected , qm . getReverseSetUnderscore ( expected . getPersonNo ( ) ) ) ;
2014-06-11 13:22:01 -04:00
}
2014-06-11 14:30:22 -04:00
@Test
public void testSelectListMap ( ) throws Throwable {
final List < Map < String , String > > arrayMap = getListMap ( ) ;
2018-04-24 00:04:53 -04:00
Assert . assertEquals ( arrayMap , qm . getAllNames ( ) ) ;
2014-06-11 14:30:22 -04:00
}
2018-04-24 00:04:53 -04:00
/ *
// todo: fix jdbcmapper for this
2014-06-11 14:30:22 -04:00
@Test
public void testSelectArrayMap ( ) throws Throwable {
final List < Map < String , String > > arrayMap = getListMap ( ) ;
2018-04-24 00:04:53 -04:00
assertArrayEquals ( arrayMap . toArray ( new Map [ arrayMap . size ( ) ] ) , qm . getAllNamesArray ( ) ) ;
2014-06-11 14:30:22 -04:00
}
2018-04-24 00:04:53 -04:00
* /
2014-06-11 14:30:22 -04:00
2014-06-19 08:57:23 -04:00
@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 ( ) ) ;
2018-04-24 00:04:53 -04:00
Assert . assertEquals ( map , qm . getAllNameMap ( ) ) ;
2014-06-19 08:57:23 -04:00
}
2017-05-17 15:45:45 -04:00
@Test
public void testSelectMapLongPerson ( ) throws Throwable {
final Map < Long , Person > map = new HashMap < Long , Person > ( ) ;
for ( final Person person : new Person [ ] {
2018-04-24 00:04:53 -04:00
qm . getFieldRegular ( 2 ) ,
qm . getFieldRegular ( 3 ) ,
qm . getFieldRegular ( 4 ) ,
2017-05-17 15:45:45 -04:00
} )
map . put ( person . getPersonNo ( ) , person ) ;
2018-04-24 00:04:53 -04:00
Assert . assertEquals ( map , qm . getMapLongPerson ( ) ) ;
2017-05-17 15:45:45 -04:00
}
2014-06-19 08:57:23 -04:00
@Test
public void testSelectMapLong ( ) throws Throwable {
final Map < Long , Long > map = new HashMap < Long , Long > ( ) ;
for ( final Person person : new Person [ ] { fieldPerson1 , fieldBoss1 , fieldBoss2 } )
map . put ( person . getPersonNo ( ) , person . getPersonNo ( ) ) ;
2018-04-24 00:04:53 -04:00
Assert . assertEquals ( map , qm . getMapLongLong ( ) ) ;
2014-06-19 08:57:23 -04:00
}
2014-06-25 12:10:12 -04:00
@Test
public void testSelectLongObject ( ) throws Throwable {
final Long expected = fieldPerson1 . getPersonNo ( ) ;
2018-04-24 00:04:53 -04:00
Assert . assertEquals ( expected , qm . getPersonNo ( expected ) ) ;
2014-06-25 12:10:12 -04:00
}
@Test
public void testSelectLongPrimitive ( ) throws Throwable {
2014-07-24 12:13:47 -04:00
final long expected = fieldPerson1 . getPersonNo ( ) ;
2018-04-24 00:04:53 -04:00
Assert . assertEquals ( expected , qm . getPersonNoPrimitive ( expected ) ) ;
2014-06-25 12:10:12 -04:00
}
2017-05-17 15:45:45 -04:00
@Test
public void testSelectIntPrimitive ( ) throws Throwable {
final int expected = ( int ) fieldPerson1 . getPersonNo ( ) ;
2018-04-24 00:04:53 -04:00
Assert . assertEquals ( expected , qm . getPersonNoPrimitiveInt ( expected ) ) ;
2017-05-17 15:45:45 -04:00
}
2014-07-24 12:13:47 -04:00
@Test
public void testSelectLongObjectArray ( ) throws Throwable {
final Long [ ] expected = { fieldPerson1 . getPersonNo ( ) } ;
2018-04-24 00:04:53 -04:00
assertArrayEquals ( expected , qm . getPersonNoObjectArray ( expected [ 0 ] ) ) ;
2014-07-24 12:13:47 -04:00
}
2018-04-24 00:04:53 -04:00
/ *
// todo: fix these
2015-03-16 13:18:37 -04:00
@Test
2017-05-17 15:45:45 -04:00
public void testSelectObjectArray ( ) throws Throwable {
2015-03-16 13:18:37 -04:00
final Long [ ] arr = { 1L , 2L , 3L } ;
2018-04-24 00:04:53 -04:00
assertArrayEquals ( arr , qm . getLongObjectArray ( ) ) ;
2015-03-16 13:18:37 -04:00
}
2017-05-17 15:45:45 -04:00
@Test
public void testSelectPrimitiveArray ( ) throws Throwable {
final long [ ] arr = { 1L , 2L , 3L } ;
2017-06-08 23:02:12 -04:00
assertArrayEquals ( arr , qm . toObject ( " SELECT 1, 2, 3 FROM person WHERE person_no = ? " , long [ ] . class , fieldPerson1 . getPersonNo ( ) ) ) ;
2017-05-17 15:45:45 -04:00
}
2018-04-24 00:04:53 -04:00
* /
2015-03-16 14:50:08 -04:00
@Test ( expected = com . moparisthebest . jdbc . MapperException . class )
public void testNoDefaultConstructorFails ( ) throws Throwable {
2018-04-24 00:04:53 -04:00
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... " ) ;
2015-03-16 14:50:08 -04:00
}
2014-06-11 14:30:22 -04:00
private List < Map < String , String > > getListMap ( ) {
final List < Map < String , String > > arrayMap = new ArrayList < Map < String , String > > ( ) ;
for ( final Person person : new Person [ ] { fieldPerson1 , fieldBoss1 , fieldBoss2 } ) {
final Map < String , String > map = new HashMap < String , String > ( ) ;
map . put ( " last_name " , person . getLastName ( ) ) ;
map . put ( " first_name " , person . getFirstName ( ) ) ;
arrayMap . add ( map ) ;
}
return arrayMap ;
}
2017-05-18 14:26:23 -04:00
@Test
public void testCaseInsensitiveMap ( ) throws Throwable {
2018-04-24 00:04:53 -04:00
final Map < String , String > map = qm . getBobTomMap ( ) . get ( 0 ) ;
2017-05-18 14:26:23 -04:00
if ( rsm instanceof CaseInsensitiveMapResultSetMapper ) {
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 " ) ) ;
} else {
assertEquals ( " bob " , map . get ( " bob " ) ) ;
assertNull ( map . get ( " Bob " ) ) ;
assertNull ( map . get ( " BoB " ) ) ;
assertNull ( map . get ( " BOb " ) ) ;
assertNull ( map . get ( " BOB " ) ) ;
assertEquals ( " tom " , map . get ( " tom " ) ) ;
assertNull ( map . get ( " Tom " ) ) ;
assertNull ( map . get ( " ToM " ) ) ;
assertNull ( map . get ( " TOm " ) ) ;
assertNull ( map . get ( " TOM " ) ) ;
}
}
2017-06-08 23:02:12 -04:00
2018-04-24 00:04:53 -04:00
@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 " ) ) ;
}
2017-06-08 23:02:12 -04:00
@Test
public void testList ( ) throws SQLException {
2018-04-24 00:04:53 -04:00
final List < FieldPerson > fromDb = qm . getThreePeople ( people [ 0 ] . getPersonNo ( ) , people [ 1 ] . getPersonNo ( ) , people [ 2 ] . getPersonNo ( ) ) ;
2017-06-08 23:02:12 -04:00
assertArrayEquals ( people , fromDb . toArray ( ) ) ;
}
2017-06-29 00:00:46 -04:00
@Test
public void testListType ( ) throws SQLException {
2018-04-24 00:04:53 -04:00
final List < FieldPerson > fromDb = qm . getThreePeopleType ( people [ 0 ] . getPersonNo ( ) , people [ 1 ] . getPersonNo ( ) , people [ 2 ] . getPersonNo ( ) ) ;
2017-06-29 00:00:46 -04:00
assertArrayEquals ( people , fromDb . toArray ( ) ) ;
}
2018-04-24 00:04:53 -04:00
/ *
// todo: port this one
2017-06-08 23:02:12 -04:00
@Test
public void testListQueryMapperList ( ) throws SQLException {
final ListQueryMapper lqm = new ListQueryMapper ( qm ) ;
final List < FieldPerson > fromDb = lqm . toList ( " SELECT * from person WHERE " + ListQueryMapper . inListReplace + " ORDER BY person_no " ,
FieldPerson . class , lqm . inList ( " person_no " , Arrays . asList ( people [ 0 ] . getPersonNo ( ) , people [ 1 ] . getPersonNo ( ) , people [ 2 ] . getPersonNo ( ) ) ) ) ;
assertArrayEquals ( people , fromDb . toArray ( ) ) ;
lqm . close ( ) ;
}
2018-04-24 00:04:53 -04:00
* /
2017-06-08 23:02:12 -04:00
@Test
public void testResultSetIterable ( ) throws SQLException {
2018-04-24 00:04:53 -04:00
final ResultSetIterable < FieldPerson > rsi = qm . getThreePeopleResultSetIterable ( people [ 0 ] . getPersonNo ( ) , people [ 1 ] . getPersonNo ( ) , people [ 2 ] . getPersonNo ( ) ) ;
2017-06-08 23:02:12 -04:00
final List < FieldPerson > fromDb = new ArrayList < FieldPerson > ( ) ;
for ( final FieldPerson fieldPerson : rsi )
fromDb . add ( fieldPerson ) ;
rsi . close ( ) ;
assertArrayEquals ( people , fromDb . toArray ( ) ) ;
}
2018-05-04 00:26:04 -04:00
2018-04-21 01:45:45 -04:00
//IFJAVA 8_START
2017-06-12 22:57:43 -04:00
@Test
public void testStream ( ) throws SQLException {
final List < FieldPerson > fromDb ;
2018-05-04 00:26:04 -04:00
try ( Stream < FieldPerson > rsi = qm . getThreePeopleStream ( people [ 0 ] . getPersonNo ( ) , people [ 1 ] . getPersonNo ( ) , people [ 2 ] . getPersonNo ( ) ) ) {
2017-06-12 22:57:43 -04:00
fromDb = rsi . collect ( Collectors . toList ( ) ) ;
}
assertArrayEquals ( people , fromDb . toArray ( ) ) ;
}
2018-04-21 01:45:45 -04:00
//IFJAVA 8_END
2017-06-13 21:42:32 -04:00
2018-05-04 00:26:04 -04:00
/ *
2017-06-13 21:42:32 -04:00
@Test
public void testEnumPerson ( ) throws SQLException {
assertEquals ( new EnumPerson ( FirstName . First ) , qm . toObject ( " SELECT first_name, last_name FROM person WHERE person_no = ? " , EnumPerson . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
2018-01-12 16:46:22 -05:00
@Test
public void testEnumPersonConstructor ( ) throws SQLException {
assertEquals ( new EnumPerson ( FirstName . First ) , qm . toObject ( " SELECT first_name FROM person WHERE person_no = ? " , EnumPerson . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
2017-06-13 21:42:32 -04:00
@Test
public void testEnum ( ) throws SQLException {
assertEquals ( FirstName . First , qm . toObject ( " SELECT first_name FROM person WHERE person_no = ? " , FirstName . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
2017-06-13 22:56:00 -04:00
2018-01-12 16:46:22 -05:00
@Test
public void testEnumPersonNull ( ) throws SQLException {
assertEquals ( new EnumPerson ( null ) , qm . toObject ( " SELECT str_val as first_name, str_val as last_name FROM val WHERE val_no = 4 " , EnumPerson . class ) ) ;
}
@Test
public void testEnumNull ( ) throws SQLException {
assertEquals ( null , qm . toObject ( " SELECT str_val FROM val WHERE val_no = 4 " , FirstName . class ) ) ;
}
2018-01-15 12:54:59 -05:00
@Test
public void testCaseInsensitiveMethods ( ) throws SQLException {
final CaseSensitivePerson expected = new CaseSensitivePerson ( ) ;
expected . setmPersonFirstName ( fieldPerson1 . getFirstName ( ) ) ;
assertEquals ( expected , qm . toObject ( " SELECT first_name AS M_PERSON_FIRST_NAME FROM person WHERE person_no = ? " , CaseSensitivePerson . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
2018-04-21 01:45:45 -04:00
//IFJAVA 8_START
2017-06-13 22:56:00 -04:00
@Test
public void testInstant ( ) throws SQLException {
assertEquals ( fieldPerson1 . getBirthDate ( ) . toInstant ( ) ,
qm . toObject ( " SELECT birth_date FROM person WHERE person_no = ? " , Instant . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
@Test
public void testLocalDateTime ( ) throws SQLException {
assertEquals ( fieldPerson1 . getBirthDate ( ) . toInstant ( ) . atZone ( ZoneId . systemDefault ( ) ) . toLocalDateTime ( ) ,
qm . toObject ( " SELECT birth_date FROM person WHERE person_no = ? " , LocalDateTime . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
@Test
public void testLocalDate ( ) throws SQLException {
assertEquals ( fieldPerson1 . getBirthDate ( ) . toInstant ( ) . atZone ( ZoneId . systemDefault ( ) ) . toLocalDate ( ) ,
qm . toObject ( " SELECT birth_date FROM person WHERE person_no = ? " , LocalDate . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
@Test
public void testLocalTime ( ) throws SQLException {
assertEquals ( fieldPerson1 . getBirthDate ( ) . toInstant ( ) . atZone ( ZoneId . systemDefault ( ) ) . toLocalTime ( ) ,
qm . toObject ( " SELECT birth_date FROM person WHERE person_no = ? " , LocalTime . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
@Test
public void testZonedDateTime ( ) throws SQLException {
assertEquals ( fieldPerson1 . getBirthDate ( ) . toInstant ( ) . atZone ( ZoneId . systemDefault ( ) ) ,
qm . toObject ( " SELECT birth_date FROM person WHERE person_no = ? " , ZonedDateTime . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
@Test
public void testOffsetDateTime ( ) throws SQLException {
assertEquals ( fieldPerson1 . getBirthDate ( ) . toInstant ( ) . atZone ( ZoneId . systemDefault ( ) ) . toOffsetDateTime ( ) ,
qm . toObject ( " SELECT birth_date FROM person WHERE person_no = ? " , OffsetDateTime . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
@Test
public void testZonedOffsetTime ( ) throws SQLException {
assertEquals ( fieldPerson1 . getBirthDate ( ) . toInstant ( ) . atZone ( ZoneId . systemDefault ( ) ) . toOffsetDateTime ( ) . toOffsetTime ( ) ,
qm . toObject ( " SELECT birth_date FROM person WHERE person_no = ? " , OffsetTime . class , fieldPerson1 . getPersonNo ( ) ) ) ;
}
@Test
public void testYearInt ( ) throws SQLException {
final Val val = vals [ 0 ] ;
assertEquals ( Year . of ( ( int ) val . numVal ) ,
qm . toObject ( " SELECT num_val FROM val WHERE val_no = ? " , Year . class , val . valNo )
) ;
}
@Test
public void testYearString ( ) throws SQLException {
final Val val = vals [ 0 ] ;
assertEquals ( Year . parse ( val . strVal ) ,
qm . toObject ( " SELECT str_val FROM val WHERE val_no = ? " , Year . class , val . valNo )
) ;
}
@Test
public void testZoneId ( ) throws SQLException {
final Val val = vals [ 1 ] ;
assertEquals ( ZoneId . of ( val . strVal ) ,
qm . toObject ( " SELECT str_val FROM val WHERE val_no = ? " , ZoneId . class , val . valNo )
) ;
}
@Test
public void testZoneOffsetInt ( ) throws SQLException {
final Val val = vals [ 2 ] ;
assertEquals ( ZoneOffset . of ( val . strVal ) ,
qm . toObject ( " SELECT str_val FROM val WHERE val_no = ? " , ZoneOffset . class , val . valNo )
) ;
}
@Test
public void testZoneOffsetStr ( ) throws SQLException {
final Val val = vals [ 2 ] ;
assertEquals ( ZoneOffset . ofHours ( ( int ) val . numVal ) ,
qm . toObject ( " SELECT num_val FROM val WHERE val_no = ? " , ZoneOffset . class , val . valNo )
) ;
}
2018-04-21 01:45:45 -04:00
//IFJAVA 8_END
2018-04-21 01:27:45 -04:00
* /
2014-06-11 13:22:01 -04:00
}