diff options
Diffstat (limited to 'container-compiler-plugin/src/test')
4 files changed, 159 insertions, 34 deletions
diff --git a/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java index c661f8b..c7891fc 100644 --- a/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java +++ b/container-compiler-plugin/src/test/java/io/trygvis/container/compiler/ProcessorTest.java @@ -12,6 +12,7 @@ import javax.tools.ToolProvider; import java.io.IOException; import java.net.URL; import java.nio.charset.Charset; +import java.util.List; import java.util.Locale; import java.util.Map; @@ -30,11 +31,13 @@ public class ProcessorTest { InMemoryJavaFileManager fileManager = new InMemoryJavaFileManager(standardFileManager); - JavaSourceFromString person = loadCode("io.trygvis.persistence.test.Person"); - JavaSourceFromString packageInfo = loadCode("io.trygvis.persistence.test.package-info"); + List<JavaSourceFromString> sources = asList( + loadJava("io.trygvis.persistence.test.package-info"), + loadJava("io.trygvis.persistence.test.Person"), + loadJava("io.trygvis.persistence.test.ParentEntity"), + loadJava("io.trygvis.persistence.test.ChildEntity")); - JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, collector, null, - null, asList(person, packageInfo)); + JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, collector, null, null, sources); task.setProcessors(asList(new MyProcessor())); @@ -66,7 +69,7 @@ public class ProcessorTest { fileManager.close(); } - private JavaSourceFromString loadCode(String className) throws IOException { + private JavaSourceFromString loadJava(String className) throws IOException { String path = "/" + className.replace('.', '/') + ".java"; URL resource = getClass().getResource(path); if (resource == null) { 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(); + } } diff --git a/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ChildEntity.java b/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ChildEntity.java new file mode 100644 index 0000000..d886fca --- /dev/null +++ b/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ChildEntity.java @@ -0,0 +1,13 @@ +package io.trygvis.persistence.test; + +import javax.persistence.Entity; + +@Entity +public class ChildEntity extends ParentEntity { + public final String name; + + public ChildEntity(Long id, String name) { + super(id); + this.name = name; + } +} diff --git a/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ParentEntity.java b/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ParentEntity.java new file mode 100644 index 0000000..4695739 --- /dev/null +++ b/container-compiler-plugin/src/test/resources/io/trygvis/persistence/test/ParentEntity.java @@ -0,0 +1,22 @@ +package io.trygvis.persistence.test; + +import javax.persistence.Id; + +public class ParentEntity { + @Id + public final Long id; + + public ParentEntity(Long id) { + this.id = id; + } + + private Integer age; + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } +} |