From 670afe178bc5786c8ca590ad197e2b0fe536361f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timo=20Westk=C3=A4mper?= Date: Mon, 26 Apr 2010 11:20:05 +0000 Subject: [PATCH] improved tests --- .../java/com/mysema/codegen/JavaWriter.java | 12 +++--- .../com/mysema/codegen/JavaWriterTest.java | 43 +++++++++++++++++++ src/test/resources/testAnnotations2 | 13 ++++++ src/test/resources/testInterface | 9 ++++ 4 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/test/resources/testAnnotations2 create mode 100644 src/test/resources/testInterface diff --git a/src/main/java/com/mysema/codegen/JavaWriter.java b/src/main/java/com/mysema/codegen/JavaWriter.java index 2b4b44553..e11aea5a7 100644 --- a/src/main/java/com/mysema/codegen/JavaWriter.java +++ b/src/main/java/com/mysema/codegen/JavaWriter.java @@ -81,13 +81,15 @@ public final class JavaWriter implements Appendable, CodeWriter{ public JavaWriter annotation(Annotation annotation) throws IOException { append(indent).append("@").appendType(annotation.annotationType()).append("("); boolean first = true; - for (Method method : annotation.annotationType().getDeclaredMethods()){ - if (!first){ - append(COMMA); - } - append(method.getName()+"="); + for (Method method : annotation.annotationType().getDeclaredMethods()){ try { Object value = method.invoke(annotation); + if (value == null){ + continue; + }else if (!first){ + append(COMMA); + } + append(method.getName()+"="); annotationConstant(value); } catch (IllegalArgumentException e) { throw new CodegenException(e); diff --git a/src/test/java/com/mysema/codegen/JavaWriterTest.java b/src/test/java/com/mysema/codegen/JavaWriterTest.java index eeeb1a443..16fb4a5e6 100644 --- a/src/test/java/com/mysema/codegen/JavaWriterTest.java +++ b/src/test/java/com/mysema/codegen/JavaWriterTest.java @@ -9,6 +9,7 @@ import static org.junit.Assert.assertEquals; import java.io.IOException; import java.io.StringWriter; +import java.lang.annotation.Annotation; import org.apache.commons.io.IOUtils; import org.junit.Test; @@ -37,6 +38,18 @@ public class JavaWriterTest { match("/testBasic", w.toString()); } + @Test + public void testInterface() 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.beginInterface("JavaWriterTest"); + writer.end(); + + match("/testInterface", w.toString()); + } + @Test public void testJavadoc() throws IOException{ StringWriter w = new StringWriter(); @@ -66,6 +79,36 @@ public class JavaWriterTest { match("/testAnnotations", w.toString()); } + @Test + public void testAnnotations2() throws IOException{ + StringWriter w = new StringWriter(); + CodeWriter writer = new JavaWriter(w); + writer.packageDecl("com.mysema.codegen"); + writer.imports(IOException.class.getPackage(), StringWriter.class.getPackage()); + writer.annotation(Entity.class); + writer.beginClass("JavaWriterTest"); + writer.annotation(new Test(){ + @Override + public Class expected() { + // TODO Auto-generated method stub + return null; + } + @Override + public long timeout() { + + return 0; + } + @Override + public Class annotationType() { + return Test.class; + }}); + writer.beginPublicMethod("void", "test"); + writer.end(); + writer.end(); + + match("/testAnnotations2", w.toString()); + } + @Test public void testFields() throws IOException{ StringWriter w = new StringWriter(); diff --git a/src/test/resources/testAnnotations2 b/src/test/resources/testAnnotations2 new file mode 100644 index 000000000..1e5ecc785 --- /dev/null +++ b/src/test/resources/testAnnotations2 @@ -0,0 +1,13 @@ +package com.mysema.codegen; + +import java.io.*; +import java.io.*; + +@Entity +public class JavaWriterTest { + + @org.junit.Test(timeout=0) + public void test() { + } + +} \ No newline at end of file diff --git a/src/test/resources/testInterface b/src/test/resources/testInterface new file mode 100644 index 000000000..3785a05ed --- /dev/null +++ b/src/test/resources/testInterface @@ -0,0 +1,9 @@ +package com.mysema.codegen; + +import java.io.IOException; +import java.io.StringWriter; +import org.junit.Test; + +public interface JavaWriterTest { + +} \ No newline at end of file