/* * Copyright 2011, Mysema Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mysema.query.codegen; import static org.junit.Assert.assertTrue; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.util.Arrays; import java.util.Date; import org.junit.Before; import org.junit.Test; import com.mysema.codegen.JavaWriter; import com.mysema.codegen.StringUtils; import com.mysema.codegen.model.ClassType; import com.mysema.codegen.model.SimpleType; import com.mysema.codegen.model.Type; import com.mysema.codegen.model.TypeCategory; import com.mysema.codegen.model.Types; public class GroovyBeanSerializerTest { private Type typeModel; private EntityType type; private final Writer writer = new StringWriter(); @Before public void setUp() { typeModel = new SimpleType(TypeCategory.ENTITY, "com.mysema.query.DomainClass", "com.mysema.query", "DomainClass", false,false); type = new EntityType(typeModel); } @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.>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 collection;", "Comparable comparable;", "java.util.Date date;", "DomainClass entityField;", "Integer integer;", "List listField;", "Map mapField;", "Set setField;", "java.sql.Time time;")) { assertTrue(prop + " was not contained", str.contains(prop)); } } }