mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
added tests
This commit is contained in:
parent
df2020e74b
commit
7f2ae57f45
@ -26,7 +26,7 @@ import com.mysema.codegen.model.Type;
|
||||
*
|
||||
*/
|
||||
public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
|
||||
|
||||
private static final String DEF = "def ";
|
||||
|
||||
private static final String EXTENDS = " extends ";
|
||||
@ -41,17 +41,17 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
|
||||
private static final String PRIVATE = "private ";
|
||||
|
||||
private static final String PRIVATE_FINAL = "private final ";
|
||||
private static final String PRIVATE_VAL = "private val ";
|
||||
|
||||
private static final String PROTECTED = "protected ";
|
||||
|
||||
private static final String PROTECTED_FINAL = "protected final ";
|
||||
private static final String PROTECTED_VAL = "protected val ";
|
||||
|
||||
private static final String PUBLIC = "public ";
|
||||
|
||||
private static final String PUBLIC_CLASS = "class ";
|
||||
|
||||
private static final String PUBLIC_FINAL = "public final ";
|
||||
private static final String VAL = "val ";
|
||||
|
||||
private static final String TRAIT = "trait ";
|
||||
|
||||
@ -153,7 +153,7 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
packages.add(type.getPackageName());
|
||||
beginLine(PUBLIC_CLASS + type.getSimpleName());
|
||||
if (superClass != null){
|
||||
append(EXTENDS + superClass.getGenericName(false, packages, classes));
|
||||
append(EXTENDS + getGenericName(false, superClass));
|
||||
}
|
||||
if (interfaces.length > 0){
|
||||
append(IMPLEMENTS);
|
||||
@ -161,7 +161,7 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
if (i > 0){
|
||||
append(COMMA);
|
||||
}
|
||||
append(interfaces[i].getGenericName(false, packages, classes));
|
||||
append(getGenericName(false, interfaces[i]));
|
||||
}
|
||||
}
|
||||
append(" {").nl().nl();
|
||||
@ -186,14 +186,14 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
public ScalaWriter beginInterface(Type type, Type... interfaces)
|
||||
throws IOException {
|
||||
packages.add(type.getPackageName());
|
||||
beginLine(TRAIT + type.getGenericName(false, packages, classes));
|
||||
beginLine(TRAIT + getGenericName(false, type));
|
||||
if (interfaces.length > 0){
|
||||
append(EXTENDS);
|
||||
for (int i = 0; i < interfaces.length; i++){
|
||||
if (i > 0){
|
||||
append(COMMA);
|
||||
}
|
||||
append(interfaces[i].getGenericName(false, packages, classes));
|
||||
append(getGenericName(false, interfaces[i]));
|
||||
}
|
||||
}
|
||||
append(" {").nl().nl();
|
||||
@ -235,25 +235,56 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
}
|
||||
|
||||
public ScalaWriter field(Type type, String name) throws IOException {
|
||||
return line(type.getGenericName(true, packages, classes) + SPACE + name + SEMICOLON).nl();
|
||||
return line(name + ": " + getGenericName(true, type) + SPACE + SEMICOLON).nl();
|
||||
}
|
||||
|
||||
private ScalaWriter field(String modifier, Type type, String name) throws IOException{
|
||||
return line(modifier + type.getGenericName(true, packages, classes) + SPACE + name + SEMICOLON).nl();
|
||||
return line(modifier + name + ": " + getGenericName(true, type) + SEMICOLON).nl();
|
||||
}
|
||||
|
||||
private ScalaWriter field(String modifier, Type type, String name, String value) throws IOException{
|
||||
return line(modifier + type.getGenericName(true, packages, classes) + SPACE + name + ASSIGN + value + SEMICOLON).nl();
|
||||
return line(modifier + name + ": " + getGenericName(true, type) + ASSIGN + value + SEMICOLON).nl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGenericName(boolean asArgType, Type type) {
|
||||
return type.getGenericName(asArgType, packages, classes);
|
||||
if (type.getParameters().isEmpty()){
|
||||
return getRawName(type);
|
||||
}else{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append(getRawName(type));
|
||||
builder.append("[");
|
||||
boolean first = true;
|
||||
String fullName = type.getFullName();
|
||||
for (Type parameter : type.getParameters()){
|
||||
if (!first){
|
||||
builder.append(", ");
|
||||
}
|
||||
if (parameter == null || parameter.getFullName().equals(fullName)){
|
||||
builder.append("?");
|
||||
}else{
|
||||
builder.append(getGenericName(false, parameter));
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRawName(Type type) {
|
||||
return type.getRawName(packages, classes);
|
||||
String fullName = type.getFullName();
|
||||
String packageName = type.getPackageName();
|
||||
if (packages.contains(packageName) || "java.lang".equals(packageName) || classes.contains(fullName)){
|
||||
if (packageName.length() > 0){
|
||||
return fullName.substring(packageName.length()+1);
|
||||
}else{
|
||||
return fullName;
|
||||
}
|
||||
}else{
|
||||
return fullName;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -341,7 +372,7 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
private ScalaWriter param(Parameter parameter) throws IOException{
|
||||
append(parameter.getName());
|
||||
append(": ");
|
||||
append(parameter.getType().getGenericName(true, packages, classes));
|
||||
append(getGenericName(true, parameter.getType()));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -352,17 +383,17 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
|
||||
@Override
|
||||
public ScalaWriter privateFinal(Type type, String name) throws IOException {
|
||||
return field(PRIVATE_FINAL, type, name);
|
||||
return field(PRIVATE_VAL, type, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalaWriter privateFinal(Type type, String name, String value) throws IOException {
|
||||
return field(PRIVATE_FINAL, type, name, value);
|
||||
return field(PRIVATE_VAL, type, name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalaWriter privateStaticFinal(Type type, String name, String value) throws IOException {
|
||||
return field(PRIVATE_FINAL, type, name, value);
|
||||
return field(PRIVATE_VAL, type, name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -372,12 +403,12 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
|
||||
@Override
|
||||
public ScalaWriter protectedFinal(Type type, String name) throws IOException {
|
||||
return field(PROTECTED_FINAL, type, name);
|
||||
return field(PROTECTED_VAL, type, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalaWriter protectedFinal(Type type, String name, String value) throws IOException {
|
||||
return field(PROTECTED_FINAL, type, name, value);
|
||||
return field(PROTECTED_VAL, type, name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -387,17 +418,17 @@ public class ScalaWriter extends AbstractCodeWriter<ScalaWriter>{
|
||||
|
||||
@Override
|
||||
public ScalaWriter publicFinal(Type type, String name) throws IOException {
|
||||
return field(PUBLIC_FINAL, type, name);
|
||||
return field(VAL, type, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalaWriter publicFinal(Type type, String name, String value) throws IOException {
|
||||
return field(PUBLIC_FINAL, type, name, value);
|
||||
return field(VAL, type, name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScalaWriter publicStaticFinal(Type type, String name, String value) throws IOException {
|
||||
return field(PUBLIC_FINAL, type, name, value);
|
||||
return field(VAL, type, name, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -1,21 +1,49 @@
|
||||
package com.mysema.codegen;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.collections15.Transformer;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.codegen.model.ClassType;
|
||||
import com.mysema.codegen.model.Parameter;
|
||||
import com.mysema.codegen.model.SimpleType;
|
||||
import com.mysema.codegen.model.Type;
|
||||
import com.mysema.codegen.model.Types;
|
||||
|
||||
public class ScalaWriterTest {
|
||||
|
||||
private static final Transformer<Parameter,Parameter> transformer = new Transformer<Parameter,Parameter>(){
|
||||
@Override
|
||||
public Parameter transform(Parameter input) {
|
||||
return input;
|
||||
}
|
||||
};
|
||||
|
||||
private Writer w = new StringWriter();
|
||||
|
||||
private ScalaWriter writer = new ScalaWriter(w);
|
||||
|
||||
private Type testType, testType2, testSuperType, testInterface1, testInterface2;
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
testType = new ClassType(JavaWriterTest.class);
|
||||
testType2 = new SimpleType("com.mysema.codegen.Test","com.mysema.codegen","Test");
|
||||
testSuperType = new SimpleType("com.mysema.codegen.Superclass","com.mysema.codegen","Superclass");
|
||||
testInterface1 = new SimpleType("com.mysema.codegen.TestInterface1","com.mysema.codegen","TestInterface1");
|
||||
testInterface2 = new SimpleType("com.mysema.codegen.TestInterface2","com.mysema.codegen","TestInterface2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beanAccessors() throws IOException{
|
||||
writer.beginClass(new SimpleType("Person"));
|
||||
@ -31,7 +59,9 @@ public class ScalaWriterTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mainMethod() throws IOException{
|
||||
@Ignore
|
||||
public void arrays() throws IOException{
|
||||
// FIXME
|
||||
// def main(args: Array[String]) {
|
||||
writer.beginClass(new SimpleType("Main"));
|
||||
writer.beginPublicMethod(Types.VOID, "main", new Parameter("args",Types.STRING.asArrayType()));
|
||||
@ -40,15 +70,192 @@ public class ScalaWriterTest {
|
||||
writer.end();
|
||||
|
||||
System.out.println(w);
|
||||
assertTrue(w.toString().contains("def main(args: Array[String])"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void trait() throws IOException{
|
||||
// trait MyTrait
|
||||
writer.beginInterface(new SimpleType("MyTrait"));
|
||||
writer.line("//");
|
||||
writer.end();
|
||||
|
||||
System.out.println(w);
|
||||
assertTrue(w.toString().contains("trait MyTrait"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fields() throws IOException{
|
||||
// private val people: List[Person]
|
||||
writer.imports(List.class);
|
||||
writer.beginClass(new SimpleType("Main"));
|
||||
writer.privateFinal(new SimpleType(Types.LIST, new SimpleType("Person")), "people");
|
||||
writer.end();
|
||||
|
||||
System.out.println(w);
|
||||
assertTrue(w.toString().contains("private val people: List[Person]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasic() throws IOException {
|
||||
writer.packageDecl("com.mysema.codegen");
|
||||
writer.imports(IOException.class, StringWriter.class, Test.class);
|
||||
writer.beginClass(testType);
|
||||
writer.annotation(Test.class);
|
||||
writer.beginPublicMethod(Types.VOID, "test");
|
||||
writer.line("// TODO");
|
||||
writer.end();
|
||||
writer.end();
|
||||
|
||||
// match("/testBasic", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExtends() throws IOException{
|
||||
writer.beginClass(testType2, testSuperType);
|
||||
writer.end();
|
||||
|
||||
// match("/testExtends", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testImplements() throws IOException{
|
||||
writer.beginClass(testType2, null, testInterface1,testInterface2);
|
||||
writer.end();
|
||||
|
||||
// match("/testImplements", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterface() throws IOException{
|
||||
writer.packageDecl("com.mysema.codegen");
|
||||
writer.imports(IOException.class, StringWriter.class, Test.class);
|
||||
writer.beginInterface(testType);
|
||||
writer.end();
|
||||
|
||||
// match("/testInterface", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterface2() throws IOException{
|
||||
writer.beginInterface(testType2, testInterface1);
|
||||
writer.end();
|
||||
|
||||
// match("/testInterface2", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJavadoc() throws IOException{
|
||||
writer.packageDecl("com.mysema.codegen");
|
||||
writer.imports(IOException.class, StringWriter.class, Test.class);
|
||||
writer.javadoc("JavaWriterTest is a test class");
|
||||
writer.beginClass(testType);
|
||||
writer.end();
|
||||
|
||||
// match("/testJavadoc", w.toString());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testAnnotations() throws IOException{
|
||||
writer.packageDecl("com.mysema.codegen");
|
||||
writer.imports(IOException.class, StringWriter.class);
|
||||
writer.annotation(Entity.class);
|
||||
writer.beginClass(testType);
|
||||
writer.annotation(Test.class);
|
||||
writer.beginPublicMethod(Types.VOID, "test");
|
||||
writer.end();
|
||||
writer.end();
|
||||
|
||||
// match("/testAnnotations", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnnotations2() throws IOException{
|
||||
writer.packageDecl("com.mysema.codegen");
|
||||
writer.imports(IOException.class.getPackage(), StringWriter.class.getPackage());
|
||||
writer.annotation(Entity.class);
|
||||
writer.beginClass(testType);
|
||||
writer.annotation(new Test(){
|
||||
@Override
|
||||
public Class<? extends Throwable> expected() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public long timeout() {
|
||||
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public Class<? extends Annotation> annotationType() {
|
||||
return Test.class;
|
||||
}});
|
||||
writer.beginPublicMethod(Types.VOID, "test");
|
||||
writer.end();
|
||||
writer.end();
|
||||
|
||||
// match("/testAnnotations2", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFields() throws IOException{
|
||||
writer.beginClass(testType);
|
||||
// private
|
||||
writer.privateField(Types.STRING, "privateField");
|
||||
writer.privateStaticFinal(Types.STRING, "privateStaticFinal", "\"val\"");
|
||||
// protected
|
||||
writer.protectedField(Types.STRING,"protectedField");
|
||||
// field
|
||||
writer.field(Types.STRING,"field");
|
||||
// public
|
||||
writer.publicField(Types.STRING,"publicField");
|
||||
writer.publicStaticFinal(Types.STRING, "publicStaticFinal", "\"val\"");
|
||||
writer.publicFinal(Types.STRING, "publicFinalField");
|
||||
writer.publicFinal(Types.STRING, "publicFinalField2", "\"val\"");
|
||||
|
||||
writer.end();
|
||||
|
||||
// match("/testFields", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethods() throws IOException{
|
||||
writer.beginClass(testType);
|
||||
// private
|
||||
|
||||
// protected
|
||||
|
||||
// method
|
||||
|
||||
// public
|
||||
writer.beginPublicMethod(Types.STRING, "publicMethod", Arrays.asList(new Parameter("a", Types.STRING)), transformer);
|
||||
writer.line("return null;");
|
||||
writer.end();
|
||||
|
||||
writer.beginStaticMethod(Types.STRING, "staticMethod", Arrays.asList(new Parameter("a", Types.STRING)), transformer);
|
||||
writer.line("return null;");
|
||||
writer.end();
|
||||
|
||||
writer.end();
|
||||
|
||||
// match("/testMethods", w.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConstructors() throws IOException{
|
||||
writer.beginClass(testType);
|
||||
|
||||
writer.beginConstructor(Arrays.asList(new Parameter("a", Types.STRING), new Parameter("b", Types.STRING)), transformer);
|
||||
writer.end();
|
||||
|
||||
writer.beginConstructor(new Parameter("a", Types.STRING));
|
||||
writer.end();
|
||||
|
||||
writer.end();
|
||||
|
||||
// match("/testConstructors", w.toString());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user