Improved POJO-related test cases. Don't catch ReflectException to

discover whether POJOs are available.
This commit is contained in:
Lukas Eder 2012-09-08 10:12:41 +02:00
parent cd3d563e8d
commit 0b9a667ae8
4 changed files with 119 additions and 85 deletions

View File

@ -378,6 +378,18 @@ public abstract class BaseTest<
return delegate.TAuthorDao();
}
protected Class<AP> TAuthorPojo() {
return delegate.TAuthorPojo();
}
protected Class<?> TBookPojo() {
return delegate.TBookPojo();
}
protected Class<?> TBooleansPojo() {
return delegate.TBooleansPojo();
}
protected UpdatableTable<A> TAuthor() {
return delegate.TAuthor();
}

View File

@ -55,8 +55,6 @@ import org.jooq.test._.converters.Boolean_YES_NO_LC;
import org.jooq.test._.converters.Boolean_YES_NO_UC;
import org.jooq.test._.converters.Boolean_YN_LC;
import org.jooq.test._.converters.Boolean_YN_UC;
import org.jooq.tools.reflect.Reflect;
import org.jooq.tools.reflect.ReflectException;
import org.junit.Test;
@ -209,13 +207,14 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
// Conversion to custom POJOs
// --------------------------------------------------------------------
try {
Reflect booleans = on(TBooleans().getClass().getPackage().getName() + ".pojos." + TBooleans().getClass().getSimpleName());
if (TBooleansPojo() == null) {
log.info("SKIPPING", "Generated POJO tests");
}
else {
List<Object> b =
create().selectFrom(TBooleans())
.orderBy(TBooleans_ID().asc())
.fetchInto((Class<?>) booleans.get());
.fetchInto(TBooleansPojo());
assertEquals(2, b.size());
assertEquals(1, on(b.get(0)).call("getId").get());
@ -242,10 +241,6 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
assertEquals(Boolean_YN_UC.N, on(b.get(0)).call("getYNUc").get());
assertEquals(Boolean_YN_UC.Y, on(b.get(1)).call("getYNUc").get());
}
catch (ReflectException e) {
log.info("SKIPPING", "Generated POJO tests");
}
}
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })

View File

@ -93,8 +93,6 @@ import org.jooq.test._.IBookWithoutAnnotations;
import org.jooq.test._.ImmutableAuthor;
import org.jooq.test._.StaticWithAnnotations;
import org.jooq.test._.StaticWithoutAnnotations;
import org.jooq.tools.reflect.Reflect;
import org.jooq.tools.reflect.ReflectException;
import org.junit.Test;
@ -1300,23 +1298,21 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
@Test
public void testFetchIntoGeneratedPojos() throws Exception {
try {
Reflect book = on(TBook().getClass().getPackage().getName() + ".pojos." + TBook().getClass().getSimpleName());
List<Object> books =
create().selectFrom(TBook())
.orderBy(TBook_ID())
.fetchInto((Class<?>) book.get());
assertEquals(4, books.size());
for (int i = 0; i < 4; i++) {
assertEquals(BOOK_IDS.get(i), on(books.get(i)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(i), on(books.get(i)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(i), on(books.get(i)).call("getTitle").get());
}
}
catch (ReflectException e) {
if (TAuthorPojo() == null) {
log.info("SKIPPING", "Generated POJO tests");
return;
}
List<Object> books =
create().selectFrom(TBook())
.orderBy(TBook_ID())
.fetchInto(TBookPojo());
assertEquals(4, books.size());
for (int i = 0; i < 4; i++) {
assertEquals(BOOK_IDS.get(i), on(books.get(i)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(i), on(books.get(i)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(i), on(books.get(i)).call("getTitle").get());
}
}
@ -1503,65 +1499,61 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
@Test
public void testFetchIntoGroups() throws Exception {
try {
// Use generated POJOs as entities
Reflect book = on(TBook().getClass().getPackage().getName() + ".pojos." + TBook().getClass().getSimpleName());
// Group by BOOK.ID
Map<Integer, List<Object>> map1 =
create().selectFrom(TBook())
.orderBy(TBook_ID())
.fetchIntoGroups(TBook_ID(), (Class<?>) book.get());
assertEquals(4, map1.size());
assertEquals(BOOK_IDS, new ArrayList<Integer>(map1.keySet()));
List<Entry<Integer, List<Object>>> entries =
new ArrayList<Map.Entry<Integer,List<Object>>>(map1.entrySet());
for (int i = 0; i < map1.size(); i++) {
Entry<Integer, List<Object>> entry = entries.get(i);
assertEquals(1, entry.getValue().size());
assertEquals(BOOK_IDS.get(i), on(entry.getValue().get(0)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(i), on(entry.getValue().get(0)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(i), on(entry.getValue().get(0)).call("getTitle").get());
}
// Group by BOOK.AUTHOR_ID
Map<Integer, List<Object>> map2 =
create().selectFrom(TBook())
.orderBy(TBook_ID())
.fetchIntoGroups(TBook_AUTHOR_ID(), (Class<?>) book.get());
assertEquals(2, map2.size());
assertEquals(AUTHOR_IDS, new ArrayList<Integer>(map2.keySet()));
Iterator<Entry<Integer, List<Object>>> it = map2.entrySet().iterator();
Entry<Integer, List<Object>> entry21 = it.next();
assertEquals(2, entry21.getValue().size());
assertEquals(BOOK_IDS.get(0), on(entry21.getValue().get(0)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(0), on(entry21.getValue().get(0)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(0), on(entry21.getValue().get(0)).call("getTitle").get());
assertEquals(BOOK_IDS.get(1), on(entry21.getValue().get(1)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(1), on(entry21.getValue().get(1)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(1), on(entry21.getValue().get(1)).call("getTitle").get());
Entry<Integer, List<Object>> entry22 = it.next();
assertEquals(2, entry22.getValue().size());
assertEquals(BOOK_IDS.get(2), on(entry22.getValue().get(0)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(2), on(entry22.getValue().get(0)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(2), on(entry22.getValue().get(0)).call("getTitle").get());
assertEquals(BOOK_IDS.get(3), on(entry22.getValue().get(1)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(3), on(entry22.getValue().get(1)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(3), on(entry22.getValue().get(1)).call("getTitle").get());
assertFalse(it.hasNext());
}
catch (ReflectException e) {
if (TBookPojo() == null) {
log.info("SKIPPING", "Generated POJO tests");
return;
}
// Group by BOOK.ID
Map<Integer, List<Object>> map1 =
create().selectFrom(TBook())
.orderBy(TBook_ID())
.fetchIntoGroups(TBook_ID(), TBookPojo());
assertEquals(4, map1.size());
assertEquals(BOOK_IDS, new ArrayList<Integer>(map1.keySet()));
List<Entry<Integer, List<Object>>> entries =
new ArrayList<Map.Entry<Integer,List<Object>>>(map1.entrySet());
for (int i = 0; i < map1.size(); i++) {
Entry<Integer, List<Object>> entry = entries.get(i);
assertEquals(1, entry.getValue().size());
assertEquals(BOOK_IDS.get(i), on(entry.getValue().get(0)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(i), on(entry.getValue().get(0)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(i), on(entry.getValue().get(0)).call("getTitle").get());
}
// Group by BOOK.AUTHOR_ID
Map<Integer, List<Object>> map2 =
create().selectFrom(TBook())
.orderBy(TBook_ID())
.fetchIntoGroups(TBook_AUTHOR_ID(), TBookPojo());
assertEquals(2, map2.size());
assertEquals(AUTHOR_IDS, new ArrayList<Integer>(map2.keySet()));
Iterator<Entry<Integer, List<Object>>> it = map2.entrySet().iterator();
Entry<Integer, List<Object>> entry21 = it.next();
assertEquals(2, entry21.getValue().size());
assertEquals(BOOK_IDS.get(0), on(entry21.getValue().get(0)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(0), on(entry21.getValue().get(0)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(0), on(entry21.getValue().get(0)).call("getTitle").get());
assertEquals(BOOK_IDS.get(1), on(entry21.getValue().get(1)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(1), on(entry21.getValue().get(1)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(1), on(entry21.getValue().get(1)).call("getTitle").get());
Entry<Integer, List<Object>> entry22 = it.next();
assertEquals(2, entry22.getValue().size());
assertEquals(BOOK_IDS.get(2), on(entry22.getValue().get(0)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(2), on(entry22.getValue().get(0)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(2), on(entry22.getValue().get(0)).call("getTitle").get());
assertEquals(BOOK_IDS.get(3), on(entry22.getValue().get(1)).call("getId").get());
assertEquals(BOOK_AUTHOR_IDS.get(3), on(entry22.getValue().get(1)).call("getAuthorId").get());
assertEquals(BOOK_TITLES.get(3), on(entry22.getValue().get(1)).call("getTitle").get());
assertFalse(it.hasNext());
}
}

View File

@ -38,6 +38,7 @@ package org.jooq.test;
import static junit.framework.Assert.assertEquals;
import static org.jooq.SQLDialect.CUBRID;
import static org.jooq.SQLDialect.FIREBIRD;
import static org.jooq.tools.reflect.Reflect.on;
import java.io.File;
import java.io.InputStream;
@ -119,6 +120,7 @@ import org.jooq.test._.testcases.ThreadSafetyTests;
import org.jooq.tools.JooqLogger;
import org.jooq.tools.StopWatch;
import org.jooq.tools.StringUtils;
import org.jooq.tools.reflect.ReflectException;
import org.jooq.tools.unsigned.UByte;
import org.jooq.tools.unsigned.UInteger;
import org.jooq.tools.unsigned.ULong;
@ -589,6 +591,39 @@ public abstract class jOOQAbstractTest<
return null;
}
protected final Class<AP> TAuthorPojo() {
// Not all test configurations have generated POJOs. Discover them dynamically
try {
return on(TBook().getClass().getPackage().getName() + ".pojos." + TAuthor().getClass().getSimpleName()).get();
}
catch (ReflectException ignore) {
return null;
}
}
protected final Class<?> TBookPojo() {
// Not all test configurations have generated POJOs. Discover them dynamically
try {
return on(TBook().getClass().getPackage().getName() + ".pojos." + TBook().getClass().getSimpleName()).get();
}
catch (ReflectException ignore) {
return null;
}
}
protected final Class<?> TBooleansPojo() {
// Not all test configurations have generated POJOs. Discover them dynamically
try {
return on(TBook().getClass().getPackage().getName() + ".pojos." + TBooleans().getClass().getSimpleName()).get();
}
catch (ReflectException ignore) {
return null;
}
}
protected abstract UpdatableTable<A> TAuthor();
protected abstract TableField<A, String> TAuthor_LAST_NAME();
protected abstract TableField<A, String> TAuthor_FIRST_NAME();