2017-05-27 23:54:22 -04:00
|
|
|
package com.moparisthebest.jdbc.codegen;
|
|
|
|
|
2018-01-15 12:54:59 -05:00
|
|
|
import com.moparisthebest.jdbc.dto.*;
|
2017-06-09 00:36:41 -04:00
|
|
|
import com.moparisthebest.jdbc.util.ResultSetIterable;
|
2017-05-27 23:54:22 -04:00
|
|
|
import org.junit.AfterClass;
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2017-06-09 00:36:41 -04:00
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
2017-10-23 00:34:37 -04:00
|
|
|
import java.util.HashMap;
|
2017-06-09 00:36:41 -04:00
|
|
|
import java.util.List;
|
2017-10-23 00:34:37 -04:00
|
|
|
import java.util.Map;
|
2017-10-23 00:38:39 -04:00
|
|
|
//IFJAVA8_START
|
2017-06-12 23:48:54 -04:00
|
|
|
import java.util.stream.Collectors;
|
|
|
|
import java.util.stream.Stream;
|
2017-06-13 22:56:00 -04:00
|
|
|
import java.time.*;
|
2017-06-12 23:48:54 -04:00
|
|
|
//IFJAVA8_END
|
2017-06-09 00:36:41 -04:00
|
|
|
|
2017-06-13 22:56:00 -04:00
|
|
|
import static com.moparisthebest.jdbc.QueryMapperTest.*;
|
2017-05-27 23:54:22 -04:00
|
|
|
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
2017-06-09 00:36:41 -04:00
|
|
|
import static org.junit.Assert.assertArrayEquals;
|
2017-05-27 23:54:22 -04:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mopar on 5/24/17.
|
|
|
|
*/
|
|
|
|
public class JdbcMapperTest {
|
|
|
|
|
2017-06-12 20:59:04 -04:00
|
|
|
public static PersonDAO dao;
|
2017-05-27 23:54:22 -04:00
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
public static void setUp() throws Throwable {
|
|
|
|
dao = JdbcMapperFactory.create(PersonDAO.class, getConnection());
|
|
|
|
//dao = new com.moparisthebest.jdbc.codegen.PersonDAOBean(getConnection());
|
|
|
|
}
|
|
|
|
|
|
|
|
@AfterClass
|
|
|
|
public static void tearDown() throws Throwable {
|
2017-06-09 00:36:41 -04:00
|
|
|
tryClose(dao);
|
2017-05-27 23:54:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testName() throws Throwable {
|
2017-06-12 20:59:04 -04:00
|
|
|
assertEquals(fieldPerson1.getFirstName(), dao.getFirstName(fieldPerson1.getPersonNo()));
|
2017-05-27 23:54:22 -04:00
|
|
|
}
|
2017-06-09 00:36:41 -04:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testList() throws SQLException {
|
|
|
|
final List<FieldPerson> fromDb = dao.getPeopleList(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());
|
|
|
|
assertArrayEquals(people, fromDb.toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testResultSetIterable() throws SQLException {
|
|
|
|
final ResultSetIterable<FieldPerson> rsi = dao.getPeopleResultSetIterable(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());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testResultSetIterableCachedPreparedStatement() throws SQLException {
|
|
|
|
final ResultSetIterable<FieldPerson> rsi = dao.getPeopleResultSetIterableCachedPreparedStatement(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());
|
|
|
|
}
|
2017-06-12 23:48:54 -04:00
|
|
|
|
|
|
|
//IFJAVA8_START
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testStream() throws SQLException {
|
|
|
|
final List<FieldPerson> fromDb;
|
|
|
|
try(Stream<FieldPerson> rsi = dao.getPeopleStream(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());) {
|
|
|
|
fromDb = rsi.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
assertArrayEquals(people, fromDb.toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testStreamCachedPreparedStatement() throws SQLException {
|
|
|
|
final List<FieldPerson> fromDb;
|
|
|
|
try(Stream<FieldPerson> rsi = dao.getPeopleStreamCachedPreparedStatement(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());) {
|
|
|
|
fromDb = rsi.collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
assertArrayEquals(people, fromDb.toArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
//IFJAVA8_END
|
2017-06-13 21:42:32 -04:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEnumPerson() throws SQLException {
|
|
|
|
assertEquals(new EnumPerson(FirstName.First), dao.getEnumPerson(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
2018-01-12 16:46:22 -05:00
|
|
|
@Test
|
|
|
|
public void testEnumPersonConstructor() throws SQLException {
|
|
|
|
assertEquals(new EnumPerson(FirstName.First), dao.getEnumPersonConstructor(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
2017-06-13 21:42:32 -04:00
|
|
|
@Test
|
|
|
|
public void testEnum() throws SQLException {
|
|
|
|
assertEquals(FirstName.First, dao.getFirstNameEnum(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), dao.getEnumPersonNull());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEnumNull() throws SQLException {
|
|
|
|
assertEquals(null, dao.getEnumNull());
|
|
|
|
}
|
|
|
|
|
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, dao.getCaseSensitivePerson(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
2017-06-13 22:56:00 -04:00
|
|
|
//IFJAVA8_START
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testInstant() throws SQLException {
|
|
|
|
assertEquals(fieldPerson1.getBirthDate().toInstant(),
|
|
|
|
dao.getBirthDateInstant(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testLocalDateTime() throws SQLException {
|
|
|
|
assertEquals(fieldPerson1.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
|
|
|
|
dao.getBirthDateLocalDateTime(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testLocalDate() throws SQLException {
|
|
|
|
assertEquals(fieldPerson1.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
|
|
|
|
dao.getBirthDateLocalDate(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testLocalTime() throws SQLException {
|
|
|
|
assertEquals(fieldPerson1.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toLocalTime(),
|
|
|
|
dao.getBirthDateLocalTime(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testZonedDateTime() throws SQLException {
|
|
|
|
assertEquals(fieldPerson1.getBirthDate().toInstant().atZone(ZoneId.systemDefault()),
|
|
|
|
dao.getBirthDateZonedDateTime(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOffsetDateTime() throws SQLException {
|
|
|
|
assertEquals(fieldPerson1.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime(),
|
|
|
|
dao.getBirthDateOffsetDateTime(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testZonedOffsetTime() throws SQLException {
|
|
|
|
assertEquals(fieldPerson1.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime().toOffsetTime(),
|
|
|
|
dao.getBirthDateZonedOffsetTime(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testYearInt() throws SQLException {
|
|
|
|
final Val val = vals[0];
|
|
|
|
assertEquals(Year.of((int)val.numVal),
|
|
|
|
dao.getYearInt(val.valNo)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testYearString() throws SQLException {
|
|
|
|
final Val val = vals[0];
|
|
|
|
assertEquals(Year.parse(val.strVal),
|
|
|
|
dao.getYearString(val.valNo)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testZoneId() throws SQLException {
|
|
|
|
final Val val = vals[1];
|
|
|
|
assertEquals(ZoneId.of(val.strVal),
|
|
|
|
dao.getZoneId(val.valNo)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testZoneOffsetInt() throws SQLException {
|
|
|
|
final Val val = vals[2];
|
|
|
|
assertEquals(ZoneOffset.of(val.strVal),
|
|
|
|
dao.getZoneOffsetInt(val.valNo)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testZoneOffsetStr() throws SQLException {
|
|
|
|
final Val val = vals[2];
|
|
|
|
assertEquals(ZoneOffset.ofHours((int)val.numVal),
|
|
|
|
dao.getZoneOffsetStr(val.valNo)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//IFJAVA8_END
|
2017-06-19 00:34:23 -04:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testPerson() throws SQLException {
|
|
|
|
assertEquals(fieldPerson1, dao.getPerson(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
2017-10-23 00:34:37 -04:00
|
|
|
|
2018-01-15 13:24:53 -05:00
|
|
|
@Test
|
|
|
|
public void testBuilderPerson() throws SQLException {
|
|
|
|
assertEquals(new BuilderPerson(fieldPerson1), dao.getBuilderPerson(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
2017-10-23 00:34:37 -04:00
|
|
|
@Test
|
|
|
|
public void testSinglePersonNameArray() throws SQLException {
|
|
|
|
assertArrayEquals(new String[]{fieldPerson1.getFirstName(), fieldPerson1.getLastName()}, dao.getSinglePersonNameArray(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testSinglePersonNameMap() throws SQLException {
|
|
|
|
final Map<String, String> expected = new HashMap<String, String>();
|
|
|
|
expected.put("first_name", fieldPerson1.getFirstName());
|
|
|
|
expected.put("last_name", fieldPerson1.getLastName());
|
|
|
|
assertEquals(expected, dao.getSinglePersonNameMap(fieldPerson1.getPersonNo()));
|
|
|
|
}
|
2017-05-27 23:54:22 -04:00
|
|
|
}
|