summaryrefslogtreecommitdiff
path: root/container-compiler-plugin/src/test
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2013-08-04 10:24:43 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2013-08-04 10:48:16 +0200
commitc0c9c358e8703c1af917d7270adbb04160ad34b3 (patch)
tree3d033cb87f8305f7d75812f869455fcedb8d5c11 /container-compiler-plugin/src/test
parent2b1c32590db960be2b5f62897e65bb10d434cae0 (diff)
downloadcontainer-playground-c0c9c358e8703c1af917d7270adbb04160ad34b3.tar.gz
container-playground-c0c9c358e8703c1af917d7270adbb04160ad34b3.tar.bz2
container-playground-c0c9c358e8703c1af917d7270adbb04160ad34b3.tar.xz
container-playground-c0c9c358e8703c1af917d7270adbb04160ad34b3.zip
wip
Diffstat (limited to 'container-compiler-plugin/src/test')
-rw-r--r--container-compiler-plugin/src/test/java/io/trygvis/persistence/EntityMirrorTest.java66
1 files changed, 64 insertions, 2 deletions
diff --git a/container-compiler-plugin/src/test/java/io/trygvis/persistence/EntityMirrorTest.java b/container-compiler-plugin/src/test/java/io/trygvis/persistence/EntityMirrorTest.java
index 9d90eef..3f0f345 100644
--- a/container-compiler-plugin/src/test/java/io/trygvis/persistence/EntityMirrorTest.java
+++ b/container-compiler-plugin/src/test/java/io/trygvis/persistence/EntityMirrorTest.java
@@ -1,10 +1,15 @@
package io.trygvis.persistence;
import io.trygvis.container.compiler.SqlUnitModel;
+import io.trygvis.container.compiler.model.Imports;
+import io.trygvis.container.compiler.model.MethodRef;
import io.trygvis.container.compiler.model.TypeRef;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
+import java.io.CharArrayWriter;
+import java.io.PrintWriter;
+
import static io.trygvis.persistence.FieldMirror.PrimitiveFieldMirror;
import static java.lang.String.format;
import static org.testng.Assert.assertEquals;
@@ -15,6 +20,7 @@ public class EntityMirrorTest {
static FieldMirror idLong = new PrimitiveFieldMirror(new TypeRef(Long.class), "id", "id", true, false, true);
static FieldMirror idString = new PrimitiveFieldMirror(new TypeRef(String.class), "id", "id", true, false, true);
static FieldMirror name = new PrimitiveFieldMirror(new TypeRef(String.class), "name", "name", false, false, false);
+ static FieldMirror age = new PrimitiveFieldMirror(new TypeRef(Integer.class), "age", "age", false, true, false);
static FieldMirror ref = new FieldMirror.ReferenceFieldMirror(entityType, "parent", "parent", false, false);
@DataProvider(name = "insertIntoSql", parallel = true)
@@ -60,11 +66,67 @@ public class EntityMirrorTest {
myTable.add(field);
}
- SqlUnitModel unit = new SqlUnitModel();
- unit.add(myTable);
+ SqlUnitModel unit = new SqlUnitModel().add(myTable);
assertEquals(myTable.insertIntoSql(), insert);
assertEquals(myTable.deleteFromSql(), delete);
assertEquals(myTable.createTableSql(unit), create);
}
+
+ @Test
+ public void testInsertIntoMethod() {
+ eq(insertInto(idString),
+ "try(java.sql.PreparedStatement stmt = con.prepareStatement(insertIntoSql)) {",
+ " java.lang.String id = o.id;",
+ " if(id == null) {",
+ " stmt.setNull(1, java.sql.Types.VARCHAR);",
+ " } else {",
+ " stmt.setString(1, o.id);",
+ " }",
+ " stmt.executeUpdate();",
+ "}");
+
+ eq(insertInto(age),
+ "try(java.sql.PreparedStatement stmt = con.prepareStatement(insertIntoSql)) {",
+ " stmt.setInt(1, o.age);",
+ " stmt.executeUpdate();",
+ "}");
+ }
+
+ @Test
+ public void testFromResultSet() {
+ eq(fromResultSet(age),
+ "java.lang.Integer age = rs.getInt(1);",
+ "return new Wat(age);");
+ }
+
+ private MethodRef insertInto(FieldMirror... fields) {
+ EntityMirror myTable = new EntityMirror(new GeneratorConfiguration(), entityType, "my_table");
+ myTable.add(fields);
+ SqlUnitModel unit = new SqlUnitModel().add(myTable);
+ return myTable.insertInto(unit, new Imports());
+ }
+
+ private MethodRef fromResultSet(FieldMirror... fields) {
+ EntityMirror myTable = new EntityMirror(new GeneratorConfiguration(), entityType, "my_table");
+ myTable.add(fields);
+ SqlUnitModel unit = new SqlUnitModel().add(myTable);
+ return myTable.fromResultSet(unit, new Imports());
+ }
+
+ private void eq(MethodRef m, String... expected) {
+ CharArrayWriter actual = new CharArrayWriter();
+ PrintWriter w = new PrintWriter(actual);
+ for (String s : m.body) {
+ w.println(s);
+ }
+
+ CharArrayWriter e = new CharArrayWriter();
+ w = new PrintWriter(e);
+ for (String s : expected) {
+ w.println(s);
+ }
+
+ assertEquals(actual.toString(), e.toString());
+ }
}