mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
improved tests for JavaWriter
This commit is contained in:
parent
d2059b0a19
commit
379e56d659
16
pom.xml
16
pom.xml
@ -21,12 +21,7 @@
|
||||
<groupId>net.sourceforge.collections</groupId>
|
||||
<artifactId>collections-generic</artifactId>
|
||||
<version>4.01</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mysema.commons</groupId>
|
||||
<artifactId>mysema-commons-lang</artifactId>
|
||||
<version>0.1.7</version>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
@ -45,7 +40,14 @@
|
||||
<artifactId>servlet-api</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>1.3.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@ -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();
|
||||
}
|
||||
|
||||
|
||||
5
src/test/java/com/mysema/codegen/Entity.java
Normal file
5
src/test/java/com/mysema/codegen/Entity.java
Normal file
@ -0,0 +1,5 @@
|
||||
package com.mysema.codegen;
|
||||
|
||||
public @interface Entity {
|
||||
|
||||
}
|
||||
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
13
src/test/resources/testAnnotations
Normal file
13
src/test/resources/testAnnotations
Normal file
@ -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() {
|
||||
}
|
||||
|
||||
}
|
||||
14
src/test/resources/testBasic
Normal file
14
src/test/resources/testBasic
Normal file
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
16
src/test/resources/testFields
Normal file
16
src/test/resources/testFields
Normal file
@ -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";
|
||||
|
||||
}
|
||||
12
src/test/resources/testJavadoc
Normal file
12
src/test/resources/testJavadoc
Normal file
@ -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 {
|
||||
|
||||
}
|
||||
3
src/test/resources/testMethods
Normal file
3
src/test/resources/testMethods
Normal file
@ -0,0 +1,3 @@
|
||||
public class MethodTests {
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user