From 379e56d6591e57ff05ab952320ae9b4220f6c4dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Sun, 4 Apr 2010 16:39:41 +0000 Subject: [PATCH] improved tests for JavaWriter --- pom.xml | 16 ++++--- .../java/com/mysema/codegen/JavaWriter.java | 16 ++++--- src/test/java/com/mysema/codegen/Entity.java | 5 ++ .../com/mysema/codegen/JavaWriterTest.java | 47 +++++++++++++++++-- src/test/resources/testAnnotations | 13 +++++ src/test/resources/testBasic | 14 ++++++ src/test/resources/testFields | 16 +++++++ src/test/resources/testJavadoc | 12 +++++ src/test/resources/testMethods | 3 ++ 9 files changed, 126 insertions(+), 16 deletions(-) create mode 100644 src/test/java/com/mysema/codegen/Entity.java create mode 100644 src/test/resources/testAnnotations create mode 100644 src/test/resources/testBasic create mode 100644 src/test/resources/testFields create mode 100644 src/test/resources/testJavadoc create mode 100644 src/test/resources/testMethods diff --git a/pom.xml b/pom.xml index 3626cbff2..07c33c4b3 100644 --- a/pom.xml +++ b/pom.xml @@ -21,12 +21,7 @@ net.sourceforge.collections collections-generic 4.01 - - - com.mysema.commons - mysema-commons-lang - 0.1.7 - + commons-lang commons-lang @@ -45,7 +40,14 @@ servlet-api - + + + org.apache.commons + commons-io + 1.3.2 + test + + diff --git a/src/main/java/com/mysema/codegen/JavaWriter.java b/src/main/java/com/mysema/codegen/JavaWriter.java index c38516b8e..11b3d0405 100644 --- a/src/main/java/com/mysema/codegen/JavaWriter.java +++ b/src/main/java/com/mysema/codegen/JavaWriter.java @@ -25,8 +25,6 @@ import java.util.Set; import org.apache.commons.collections15.Transformer; import org.apache.commons.lang.StringEscapeUtils; -import com.mysema.commons.lang.Assert; - /** * @author tiwe * @@ -72,8 +70,11 @@ public final class JavaWriter implements Appendable, CodeWriter{ private String type; public JavaWriter(Appendable appendable){ - this.appendable = Assert.notNull(appendable,"appendable"); - importedPackages.add(Object.class.getPackage()); + if (appendable == null){ + throw new IllegalArgumentException("appendable is null"); + } + this.appendable = appendable; + this.importedPackages.add(Object.class.getPackage()); } @Override @@ -156,7 +157,7 @@ public final class JavaWriter implements Appendable, CodeWriter{ @Override public JavaWriter beginClass(String simpleName, String superClass, String... interfaces) throws IOException{ - nl().append(indent + PUBLIC_CLASS + simpleName); + append(indent + PUBLIC_CLASS + simpleName); if (superClass != null){ append(EXTENDS + superClass); } @@ -187,7 +188,7 @@ public final class JavaWriter implements Appendable, CodeWriter{ @Override public JavaWriter beginInterface(String simpleName, String... interfaces) throws IOException { - nl().append(indent + PUBLIC_INTERFACE + simpleName); + append(indent + PUBLIC_INTERFACE + simpleName); if (interfaces.length > 0){ append(EXTENDS).join(COMMA, interfaces); } @@ -265,6 +266,7 @@ public final class JavaWriter implements Appendable, CodeWriter{ importedClasses.add(cl); line(IMPORT + cl.getName() + SEMICOLON); } + nl(); return this; } @@ -274,6 +276,7 @@ public final class JavaWriter implements Appendable, CodeWriter{ importedPackages.add(p); line(IMPORT + p.getName() + ".*;"); } + nl(); return this; } @@ -332,6 +335,7 @@ public final class JavaWriter implements Appendable, CodeWriter{ @Override public JavaWriter packageDecl(String packageName) throws IOException{ + importedPackages.add(Package.getPackage(packageName)); return line(PACKAGE + packageName + SEMICOLON).nl(); } diff --git a/src/test/java/com/mysema/codegen/Entity.java b/src/test/java/com/mysema/codegen/Entity.java new file mode 100644 index 000000000..9dccc635d --- /dev/null +++ b/src/test/java/com/mysema/codegen/Entity.java @@ -0,0 +1,5 @@ +package com.mysema.codegen; + +public @interface Entity { + +} diff --git a/src/test/java/com/mysema/codegen/JavaWriterTest.java b/src/test/java/com/mysema/codegen/JavaWriterTest.java index a1b91ddd0..65814554a 100644 --- a/src/test/java/com/mysema/codegen/JavaWriterTest.java +++ b/src/test/java/com/mysema/codegen/JavaWriterTest.java @@ -1,12 +1,21 @@ package com.mysema.codegen; +import static org.junit.Assert.assertEquals; + import java.io.IOException; import java.io.StringWriter; +import org.apache.commons.io.IOUtils; import org.junit.Test; public class JavaWriterTest { + private static void match(String resource, String text) throws IOException{ + String expected = IOUtils.toString(JavaWriterTest.class.getResourceAsStream(resource),"UTF-8").replace("\r\n", "\n").trim(); + String actual = text.trim(); + assertEquals(expected, actual); + } + @Test public void testBasic() throws IOException { StringWriter w = new StringWriter(); @@ -19,7 +28,37 @@ public class JavaWriterTest { writer.line("// TODO"); writer.end(); writer.end(); - System.out.println(w); + + match("/testBasic", w.toString()); + } + + @Test + public void testJavadoc() throws IOException{ + StringWriter w = new StringWriter(); + CodeWriter writer = new JavaWriter(w); + writer.packageDecl("com.mysema.codegen"); + writer.imports(IOException.class, StringWriter.class, Test.class); + writer.javadoc("JavaWriterTest is a test class"); + writer.beginClass("JavaWriterTest"); + writer.end(); + + match("/testJavadoc", w.toString()); + } + + @Test + public void testAnnotations() throws IOException{ + StringWriter w = new StringWriter(); + CodeWriter writer = new JavaWriter(w); + writer.packageDecl("com.mysema.codegen"); + writer.imports(IOException.class, StringWriter.class); + writer.annotation(Entity.class); + writer.beginClass("JavaWriterTest"); + writer.annotation(Test.class); + writer.beginPublicMethod("void", "test"); + writer.end(); + writer.end(); + + match("/testAnnotations", w.toString()); } @Test @@ -38,7 +77,8 @@ public class JavaWriterTest { writer.publicField("String","publicField"); writer.publicStaticFinal("String", "publicStaticFinal", "\"val\""); writer.end(); - System.out.println(w); + + match("/testFields", w.toString()); } @Test @@ -55,7 +95,8 @@ public class JavaWriterTest { // public writer.end(); - System.out.println(w); + + match("/testMethods", w.toString()); } } diff --git a/src/test/resources/testAnnotations b/src/test/resources/testAnnotations new file mode 100644 index 000000000..cba34cce4 --- /dev/null +++ b/src/test/resources/testAnnotations @@ -0,0 +1,13 @@ +package com.mysema.codegen; + +import java.io.IOException; +import java.io.StringWriter; + +@Entity +public class JavaWriterTest { + + @org.junit.Test + public void test() { + } + +} \ No newline at end of file diff --git a/src/test/resources/testBasic b/src/test/resources/testBasic new file mode 100644 index 000000000..1439d7862 --- /dev/null +++ b/src/test/resources/testBasic @@ -0,0 +1,14 @@ +package com.mysema.codegen; + +import java.io.IOException; +import java.io.StringWriter; +import org.junit.Test; + +public class JavaWriterTest { + + @Test + public void test() { + // TODO + } + +} \ No newline at end of file diff --git a/src/test/resources/testFields b/src/test/resources/testFields new file mode 100644 index 000000000..de6a0bea4 --- /dev/null +++ b/src/test/resources/testFields @@ -0,0 +1,16 @@ + +public class FieldTests { + + private String privateField; + + private static final String privateStaticFinal = "val"; + + protected String protectedField; + + String field; + + public String publicField; + + public static final String publicStaticFinal = "val"; + +} \ No newline at end of file diff --git a/src/test/resources/testJavadoc b/src/test/resources/testJavadoc new file mode 100644 index 000000000..cf45bb2ae --- /dev/null +++ b/src/test/resources/testJavadoc @@ -0,0 +1,12 @@ +package com.mysema.codegen; + +import java.io.IOException; +import java.io.StringWriter; +import org.junit.Test; + +/** + * JavaWriterTest is a test class + */ +public class JavaWriterTest { + +} \ No newline at end of file diff --git a/src/test/resources/testMethods b/src/test/resources/testMethods new file mode 100644 index 000000000..cd947a8e6 --- /dev/null +++ b/src/test/resources/testMethods @@ -0,0 +1,3 @@ +public class MethodTests { + +}