Search in sources :

Example 21 with JavaWriter

use of com.querydsl.codegen.utils.JavaWriter in project querydsl by querydsl.

the class EntitySerializerTest method no_package.

@Test
public void no_package() throws IOException {
    SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity", false, false);
    EntityType entityType = new EntityType(type);
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("public class QEntity extends EntityPathBase<Entity> {"));
    CompileUtils.assertCompiles("QEntity", writer.toString());
}
Also used : JavaWriter(com.querydsl.codegen.utils.JavaWriter) Test(org.junit.Test)

Example 22 with JavaWriter

use of com.querydsl.codegen.utils.JavaWriter in project querydsl by querydsl.

the class EntitySerializerTest method primitive_array.

@Test
public void primitive_array() throws IOException {
    SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity", false, false);
    EntityType entityType = new EntityType(type);
    entityType.addProperty(new Property(entityType, "bytes", new ClassType(byte[].class)));
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("public final SimplePath<byte[]> bytes"));
    CompileUtils.assertCompiles("QEntity", writer.toString());
}
Also used : JavaWriter(com.querydsl.codegen.utils.JavaWriter) Test(org.junit.Test)

Example 23 with JavaWriter

use of com.querydsl.codegen.utils.JavaWriter in project querydsl by querydsl.

the class EntitySerializerTest method delegates.

@Test
public void delegates() throws IOException {
    SimpleType type = new SimpleType(TypeCategory.ENTITY, "Entity", "", "Entity", false, false);
    EntityType entityType = new EntityType(type);
    Delegate delegate = new Delegate(type, type, "test", Collections.<Parameter>emptyList(), Types.STRING);
    entityType.addDelegate(delegate);
    typeMappings.register(entityType, queryTypeFactory.create(entityType));
    serializer.serialize(entityType, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    assertTrue(writer.toString().contains("return Entity.test(this);"));
    CompileUtils.assertCompiles("QEntity", writer.toString());
}
Also used : JavaWriter(com.querydsl.codegen.utils.JavaWriter) Test(org.junit.Test)

Example 24 with JavaWriter

use of com.querydsl.codegen.utils.JavaWriter in project querydsl by querydsl.

the class MetaDataExporter method write.

private void write(Serializer serializer, File targetFile, EntityType type) throws IOException {
    if (!classes.add(targetFile.getPath())) {
        throw new IllegalStateException("Attempted to write multiple times to " + targetFile.getPath() + ", please check your configuration");
    }
    StringWriter w = new StringWriter();
    CodeWriter writer = createScalaSources ? new ScalaWriter(w) : new JavaWriter(w);
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, writer);
    // conditional creation
    boolean generate = true;
    byte[] bytes = w.toString().getBytes(sourceEncoding);
    if (targetFile.exists() && targetFile.length() == bytes.length) {
        String str = new String(Files.readAllBytes(targetFile.toPath()), Charset.forName(sourceEncoding));
        if (str.equals(w.toString())) {
            generate = false;
        }
    } else {
        targetFile.getParentFile().mkdirs();
    }
    if (generate) {
        Files.write(targetFile.toPath(), bytes);
    }
}
Also used : StringWriter(java.io.StringWriter) JavaWriter(com.querydsl.codegen.utils.JavaWriter) ScalaWriter(com.querydsl.codegen.utils.ScalaWriter) CodeWriter(com.querydsl.codegen.utils.CodeWriter)

Example 25 with JavaWriter

use of com.querydsl.codegen.utils.JavaWriter in project querydsl by querydsl.

the class GroovyBeanSerializerTest method properties.

@Test
public void properties() throws IOException {
    // property
    type.addProperty(new Property(type, "entityField", type));
    type.addProperty(new Property(type, "collection", new SimpleType(Types.COLLECTION, typeModel)));
    type.addProperty(new Property(type, "listField", new SimpleType(Types.LIST, typeModel)));
    type.addProperty(new Property(type, "setField", new SimpleType(Types.SET, typeModel)));
    type.addProperty(new Property(type, "arrayField", new ClassType(TypeCategory.ARRAY, String[].class)));
    type.addProperty(new Property(type, "mapField", new SimpleType(Types.MAP, typeModel, typeModel)));
    for (Class<?> cl : Arrays.<Class<?>>asList(Boolean.class, Comparable.class, Integer.class, Date.class, java.sql.Date.class, java.sql.Time.class)) {
        Type classType = new ClassType(TypeCategory.get(cl.getName()), cl);
        type.addProperty(new Property(type, StringUtils.uncapitalize(cl.getSimpleName()), classType));
    }
    GroovyBeanSerializer serializer = new GroovyBeanSerializer();
    serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
    String str = writer.toString();
    // System.err.println(str);
    for (String prop : Arrays.asList("String[] arrayField;", "Boolean boolean$;", "Collection<DomainClass> collection;", "Comparable comparable;", "java.util.Date date;", "DomainClass entityField;", "Integer integer;", "List<DomainClass> listField;", "Map<DomainClass, DomainClass> mapField;", "Set<DomainClass> setField;", "java.sql.Time time;")) {
        assertTrue(prop + " was not contained", str.contains(prop));
    }
}
Also used : JavaWriter(com.querydsl.codegen.utils.JavaWriter) Test(org.junit.Test)

Aggregations

JavaWriter (com.querydsl.codegen.utils.JavaWriter)45 Test (org.junit.Test)41 Matchers.containsString (org.hamcrest.Matchers.containsString)13 SimpleType (com.querydsl.codegen.utils.model.SimpleType)8 ClassType (com.querydsl.codegen.utils.model.ClassType)6 Writer (java.io.Writer)6 StringWriter (java.io.StringWriter)5 Type (com.querydsl.codegen.utils.model.Type)4 CodeWriter (com.querydsl.codegen.utils.CodeWriter)3 ScalaWriter (com.querydsl.codegen.utils.ScalaWriter)2 File (java.io.File)2 OutputStreamWriter (java.io.OutputStreamWriter)2 EnumMap (java.util.EnumMap)2 Map (java.util.Map)2 Property (com.querydsl.codegen.Property)1 SimpleCompiler (com.querydsl.codegen.utils.SimpleCompiler)1 Constructor (com.querydsl.codegen.utils.model.Constructor)1 Parameter (com.querydsl.codegen.utils.model.Parameter)1 ColumnImpl (com.querydsl.sql.ColumnImpl)1 PrimaryKeyData (com.querydsl.sql.codegen.support.PrimaryKeyData)1