[#3088] ResultQuery.fetchInto() doesn't work with Scala case classes - Added integration test, works

This commit is contained in:
Lukas Eder 2014-03-10 18:52:49 +01:00
parent a95a137339
commit c591ebada9
4 changed files with 70 additions and 15 deletions

View File

@ -13,8 +13,8 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-test-sources/jooq-h2"/>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="src" path="target/generated-test-sources/jooq-h2"/>
<classpathentry kind="con" path="org.scala-ide.sdt.launching.SCALA_CONTAINER"/>
<classpathentry exported="true" kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">

View File

@ -51,28 +51,34 @@ import java.sql.DriverManager
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import java.util.ArrayList
import org.jooq.scala.test.pojo.BookCaseWithConstructorProperties
import org.jooq.scala.test.pojo.BookCase
@RunWith(classOf[JUnitRunner])
class MapperTest extends FunSuite {
test("RecordMapper") {
def dsl() = {
val c = DriverManager.getConnection("jdbc:h2:~/test", "sa", "");
val f = DSL.using(c, SQLDialect.H2);
f;
}
test("RecordMapper") {
val r =
f.select(
field("A", classOf[java.lang.Integer]),
field("B", classOf[java.lang.Integer])
)
.from(
values(
row(1, 2),
row(1, 3),
row(2, 3)
)
as("T", "A", "B")
)
.fetch();
dsl().select(
field("A", classOf[java.lang.Integer]),
field("B", classOf[java.lang.Integer])
)
.from(
values(
row(1, 2),
row(1, 3),
row(2, 3)
)
as("T", "A", "B")
)
.fetch();
val mapped1 = r.map((x1 : java.lang.Integer, x2 : java.lang.Integer) => (x1 + ", " + x2))
val mapped2 = r.map((x : (java.lang.Integer, java.lang.Integer)) => (x._1 + ", " + x._2))
@ -84,4 +90,29 @@ class MapperTest extends FunSuite {
assert(mapped1 == mapped2, "Tuple vs ArgumentList")
}
test("MapCaseClass") {
val books1 =
dsl().select(T_BOOK.ID, T_BOOK.AUTHOR_ID, T_BOOK.TITLE)
.from(T_BOOK)
.orderBy(T_BOOK.ID asc)
.fetchInto(classOf[BookCase])
assert(4 == books1.size());
assert(BookCase(1, 1, "1984") == books1.get(0));
assert(BookCase(2, 1, "Animal Farm") == books1.get(1));
assert(BookCase(3, 2, "O Alquimista") == books1.get(2));
assert(BookCase(4, 2, "Brida") == books1.get(3));
val books2 =
dsl().select()
.from(T_BOOK)
.orderBy(T_BOOK.ID asc)
.fetchInto(classOf[BookCaseWithConstructorProperties])
assert(4 == books2.size());
assert(BookCaseWithConstructorProperties(1, 1, "1984") == books2.get(0));
assert(BookCaseWithConstructorProperties(2, 1, "Animal Farm") == books2.get(1));
assert(BookCaseWithConstructorProperties(3, 2, "O Alquimista") == books2.get(2));
assert(BookCaseWithConstructorProperties(4, 2, "Brida") == books2.get(3));
}
}

View File

@ -0,0 +1,12 @@
package org.jooq.scala.test.pojo
import java.beans.ConstructorProperties
/**
* Case classes are in fact classes with a public constructor
* @author Lukas Eder
*/
case class BookCase(
id: Int,
authorId: Int,
title: String)

View File

@ -0,0 +1,12 @@
package org.jooq.scala.test.pojo
import java.beans.ConstructorProperties
/**
* Case classes are in fact classes with a public constructor, which can be annotated using {@link ConstructorProperties}
* @author Lukas Eder
*/
case class BookCaseWithConstructorProperties @ConstructorProperties(Array("id", "authorId", "title")) (
id: Int,
authorId: Int,
title: String)