summaryrefslogtreecommitdiff
path: root/container-compiler-plugin/src/test/java/io/trygvis/persistence/EntityMirrorTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-compiler-plugin/src/test/java/io/trygvis/persistence/EntityMirrorTest.java')
-rw-r--r--container-compiler-plugin/src/test/java/io/trygvis/persistence/EntityMirrorTest.java145
1 files changed, 116 insertions, 29 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 4eead41..52b33ca 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
@@ -10,18 +10,23 @@ import org.testng.annotations.Test;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
-import static io.trygvis.persistence.FieldMirror.PrimitiveFieldMirror;
+import static io.trygvis.persistence.FieldMirror.AccessorType.FIELD;
+import static io.trygvis.persistence.FieldMirror.AccessorType.METHOD;
+import static io.trygvis.persistence.FieldMirror.FieldType.PRIMITIVE;
+import static io.trygvis.persistence.FieldMirror.FieldType.REFERENCE;
import static java.lang.String.format;
import static org.testng.Assert.assertEquals;
public class EntityMirrorTest {
static TypeRef entityType = new TypeRef("Wat");
- 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);
+ static FieldMirror idLong = new FieldMirror(PRIMITIVE, FIELD, new TypeRef(Long.class), "id", "id", true, false, true);
+ static FieldMirror idString = new FieldMirror(PRIMITIVE, FIELD, new TypeRef(String.class), "id", "id", true, false, true);
+ static FieldMirror idMethod = new FieldMirror(PRIMITIVE, METHOD, new TypeRef(Long.class), "id", "id", true, false, true);
+ static FieldMirror name = new FieldMirror(PRIMITIVE, FIELD, new TypeRef(String.class), "name", "name", false, false, false);
+ static FieldMirror age = new FieldMirror(PRIMITIVE, FIELD, new TypeRef(Integer.class), "age", "age", false, true, false);
+ static FieldMirror year = new FieldMirror(PRIMITIVE, METHOD, new TypeRef(Integer.class), "year", "year", false, true, false);
+ static FieldMirror ref = new FieldMirror(REFERENCE, FIELD, entityType, "parent", "parent", false, false, false);
@DataProvider(name = "insertIntoSql", parallel = true)
public static Object[][] insertIntoProvider() {
@@ -73,31 +78,32 @@ public class EntityMirrorTest {
assertEquals(myTable.createTableSql(unit), create);
}
- @Test
- public void testInsertIntoMethod() {
- eq(insertInto(name),
- "try(java.sql.PreparedStatement stmt = con.prepareStatement(insertIntoSql)) {",
- " java.lang.String name = o.name;",
- " if(name == null) {",
- " stmt.setNull(1, java.sql.Types.VARCHAR);",
- " } else {",
- " stmt.setString(1, o.name);",
- " }",
- " stmt.executeUpdate();",
- "}");
-
- eq(insertInto(age),
- "try(java.sql.PreparedStatement stmt = con.prepareStatement(insertIntoSql)) {",
- " stmt.setInt(1, o.age);",
- " stmt.executeUpdate();",
- "}");
+ @DataProvider(name = "insertIntoMethod", parallel = true)
+ public static Object[][] insertIntoMethodProvider() {
+ return new Object[][]{
+ new Object[]{new FieldMirror[]{name}, join(
+ "try(java.sql.PreparedStatement stmt = con.prepareStatement(insertIntoSql)) {",
+ " java.lang.String name = o.name;",
+ " if(name == null) {",
+ " stmt.setNull(1, java.sql.Types.VARCHAR);",
+ " } else {",
+ " stmt.setString(1, o.name);",
+ " }",
+ " stmt.executeUpdate();",
+ "}")
+ },
+ new Object[]{new FieldMirror[]{age}, join(
+ "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);");
+ @Test(dataProvider = "insertIntoMethod")
+ public void testInsertIntoMethod(FieldMirror[] fields, String expected) {
+ eq(insertInto(fields), expected.trim());
}
private MethodRef insertInto(FieldMirror... fields) {
@@ -107,6 +113,79 @@ public class EntityMirrorTest {
return myTable.insertInto(unit, new Imports());
}
+ @DataProvider(name = "deleteMethod", parallel = true)
+ public static Object[][] deleteMethodProvider() {
+ return new Object[][]{
+ new Object[]{new FieldMirror[]{idLong}, join(
+ "deleteById(con, o.id);")
+ },
+ new Object[]{new FieldMirror[]{idMethod}, join(
+ "deleteById(con, o.getId());")
+ },
+ };
+ }
+
+ @Test(dataProvider = "deleteMethod")
+ public void testDeleteMethod(FieldMirror[] fields, String expected) {
+ eq(delete(fields), expected.trim());
+ }
+
+ private MethodRef delete(FieldMirror... fields) {
+ EntityMirror myTable = new EntityMirror(new GeneratorConfiguration(), entityType, "my_table");
+ myTable.add(fields);
+ return myTable.delete(new Imports());
+ }
+
+ @DataProvider(name = "deleteByIdMethod", parallel = true)
+ public static Object[][] deleteByIdMethodProvider() {
+ return new Object[][]{
+ new Object[]{new FieldMirror[]{idLong}, join(
+ "try(java.sql.PreparedStatement stmt = con.prepareStatement(deleteFromSql)) {",
+ " stmt.setLong(1, id);",
+ " stmt.executeUpdate();",
+ "}")
+ },
+ new Object[]{new FieldMirror[]{idMethod}, join(
+ "try(java.sql.PreparedStatement stmt = con.prepareStatement(deleteFromSql)) {",
+ " stmt.setLong(1, id);",
+ " stmt.executeUpdate();",
+ "}")
+ },
+ };
+ }
+
+ @Test(dataProvider = "deleteByIdMethod")
+ public void testDeleteByIdMethod(FieldMirror[] fields, String expected) {
+ eq(deleteById(fields), expected.trim());
+ }
+
+ private MethodRef deleteById(FieldMirror... fields) {
+ EntityMirror myTable = new EntityMirror(new GeneratorConfiguration(), entityType, "my_table");
+ myTable.add(fields);
+ return myTable.deleteById(new Imports());
+ }
+
+ @Test
+ public void testFromResultSet() {
+ eq(fromResultSet(age),
+ "java.lang.Integer age = rs.getInt(1);",
+ "Wat returnValue = new Wat(age);",
+ "return returnValue;");
+
+ eq(fromResultSet(age, year),
+ "java.lang.Integer age = rs.getInt(1);",
+ "Wat returnValue = new Wat(age);",
+ "returnValue.setYear(rs.getInt(2));",
+ "return returnValue;");
+
+ eq(fromResultSet(age, year, name),
+ "java.lang.Integer age = rs.getInt(1);",
+ "java.lang.String name = rs.getString(3);",
+ "Wat returnValue = new Wat(age, name);",
+ "returnValue.setYear(rs.getInt(2));",
+ "return returnValue;");
+ }
+
private MethodRef fromResultSet(FieldMirror... fields) {
EntityMirror myTable = new EntityMirror(new GeneratorConfiguration(), entityType, "my_table");
myTable.add(fields);
@@ -128,4 +207,12 @@ public class EntityMirrorTest {
assertEquals(actual.toString(), e.toString());
}
+
+ private static String join(String... strings) {
+ StringBuilder buffer = new StringBuilder();
+ for (String string : strings) {
+ buffer.append(string).append("\n");
+ }
+ return buffer.toString();
+ }
}