Add JdbcMapper cleaner test

This commit is contained in:
Travis Burtrum 2018-04-18 00:16:16 -04:00
parent 337abc8c6d
commit 64e500ca2a
4 changed files with 38 additions and 9 deletions

View File

@ -349,7 +349,7 @@ public class CompileTimeResultSetMapper {
if(cleaner == null) if(cleaner == null)
w.write("ret"); w.write("ret");
else { else {
w.append(cleaner).append(".clean(ret)"); w.append(cleaner).append(" == null ? ret : ").append(cleaner).append(".clean(ret)");
} }
return w; return w;
} }

View File

@ -0,0 +1,15 @@
package com.moparisthebest.jdbc.codegen;
import com.moparisthebest.jdbc.Cleaner;
import com.moparisthebest.jdbc.dto.FieldPerson;
import java.sql.SQLException;
@JdbcMapper.Mapper(
cachePreparedStatements = JdbcMapper.OptionalBool.FALSE
, allowReflection = JdbcMapper.OptionalBool.TRUE
)
public interface CleaningPersonDao {
@JdbcMapper.SQL(value = "SELECT person_no, first_name, last_name, birth_date FROM person WHERE person_no = {personNo}")
FieldPerson getPerson(long personNo, Cleaner<FieldPerson> personCleaner) throws SQLException;
}

View File

@ -1,5 +1,8 @@
package com.moparisthebest.jdbc; package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.codegen.CleaningPersonDao;
import com.moparisthebest.jdbc.codegen.JdbcMapper;
import com.moparisthebest.jdbc.codegen.JdbcMapperFactory;
import com.moparisthebest.jdbc.dto.*; import com.moparisthebest.jdbc.dto.*;
import org.junit.*; import org.junit.*;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -35,15 +38,21 @@ public class CleaningQueryMapperTest {
} }
protected QueryMapper qm; protected QueryMapper qm;
protected CleaningPersonDao cleaningPersonDao;
protected final Cleaner<FieldPerson> personCleaner;
protected final ResultSetMapper rsm; protected final ResultSetMapper rsm;
public CleaningQueryMapperTest(final ResultSetMapper rsm) { public CleaningQueryMapperTest(final Cleaner<FieldPerson> personCleaner, final ResultSetMapper rsm) {
this.personCleaner = personCleaner;
this.rsm = rsm; this.rsm = rsm;
} }
@Before @Before
public void open() { public void open() {
this.qm = new QueryMapper(conn, rsm); if(rsm == null)
this.cleaningPersonDao = JdbcMapperFactory.create(CleaningPersonDao.class, conn);
else
this.qm = new QueryMapper(conn, rsm);
} }
@After @After
@ -61,9 +70,10 @@ public class CleaningQueryMapperTest {
} }
}; };
return Arrays.asList(new Object[][] { return Arrays.asList(new Object[][] {
{ new CleaningResultSetMapper<FieldPerson>(personCleaner) }, { null, new CleaningResultSetMapper<FieldPerson>(personCleaner) },
{ new CleaningCachingResultSetMapper<FieldPerson>(personCleaner) }, { null, new CleaningCachingResultSetMapper<FieldPerson>(personCleaner) },
{ new CleaningCompilingResultSetMapper<FieldPerson>(personCleaner, new CompilingRowToObjectMapper.Cache(true)) }, { null, new CleaningCompilingResultSetMapper<FieldPerson>(personCleaner, new CompilingRowToObjectMapper.Cache(true)) },
{ personCleaner, null },
}); });
} }
@ -72,7 +82,10 @@ public class CleaningQueryMapperTest {
@Test @Test
public void testFieldRegularPerson() throws Throwable { public void testFieldRegularPerson() throws Throwable {
final Person expected = fieldPerson1; final Person expected = fieldPerson1;
final Person actual = qm.toObject(personRegular, expected.getClass(), expected.getPersonNo()); final Person actual = this.cleaningPersonDao == null ?
qm.toObject("SELECT * FROM person WHERE person_no = ?", expected.getClass(), expected.getPersonNo())
:
this.cleaningPersonDao.getPerson(expected.getPersonNo(), personCleaner);
assertEquals(expected.getFirstName() + " " + expected.getLastName(), actual.getFirstName()); assertEquals(expected.getFirstName() + " " + expected.getLastName(), actual.getFirstName());
assertNull(actual.getLastName()); assertNull(actual.getLastName());
} }

View File

@ -23,6 +23,7 @@ public class QueryRunnerTest {
private void testPerson(final Person expected, final String query) throws Throwable { private void testPerson(final Person expected, final String query) throws Throwable {
//final QueryRunner<ListQueryMapper> lqr = qr.withFactory(() -> new ListQueryMapper(QueryMapperTest::getConnection)); //final QueryRunner<ListQueryMapper> lqr = qr.withFactory(() -> new ListQueryMapper(QueryMapperTest::getConnection));
final int[] failCount = new int[]{0};
final Person actual = final Person actual =
//qr.run( //qr.run(
//qr.runRetry( //qr.runRetry(
@ -31,8 +32,8 @@ public class QueryRunnerTest {
new QueryRunner.Runner<QueryMapper, Person>() { new QueryRunner.Runner<QueryMapper, Person>() {
@Override @Override
public Person run(final QueryMapper qm) throws SQLException { public Person run(final QueryMapper qm) throws SQLException {
if(Math.random() < 0.5) { if(++failCount[0] < 5) {
System.out.println("fake fail"); //System.out.println("fake fail");
throw new SQLException("fake 50% failure rate"); throw new SQLException("fake 50% failure rate");
} }
return qm.toObject(query, expected.getClass(), expected.getPersonNo()); return qm.toObject(query, expected.getClass(), expected.getPersonNo());