added tests

This commit is contained in:
Timo Westkämper 2011-03-09 13:43:37 +00:00
parent 55ece4bd0e
commit 021fbef4e5
5 changed files with 84 additions and 52 deletions

View File

@ -26,27 +26,27 @@ import com.mysema.codegen.support.ClassUtils;
public class ClassType implements Type {
private final TypeCategory category;
private final Class<?> javaClass;
private final String className;
private final List<Type> parameters;
@Nullable
private final Class<?> primitiveClass;
@Nullable
private Type arrayType, componentType;
public ClassType(Class<?> javaClass, Type... parameters) {
this(TypeCategory.SIMPLE, javaClass, null, Arrays.asList(parameters));
}
public ClassType(TypeCategory category, Class<?> javaClass, Class<?> primitiveClass) {
this(category, javaClass, primitiveClass, Collections.<Type>emptyList());
}
}
public ClassType(TypeCategory category, Class<?> javaClass, @Nullable Class<?> primitiveClass, List<Type> parameters) {
this.category = category;
this.javaClass = javaClass;
@ -54,15 +54,15 @@ public class ClassType implements Type {
this.parameters = parameters;
this.className = ClassUtils.getFullName(javaClass);
}
public ClassType(TypeCategory category, Class<?> javaClass, List<Type> parameters) {
this(category, javaClass, null, parameters);
}
public ClassType(TypeCategory category, Class<?> clazz, Type... parameters) {
this(category, clazz, null, Arrays.asList(parameters));
}
@Override
public Type as(TypeCategory c) {
if (category == c){
@ -77,7 +77,7 @@ public class ClassType implements Type {
if (arrayType == null){
String fullName = javaClass.getName()+"[]";
String simpleName = javaClass.getSimpleName()+"[]";
arrayType = new SimpleType(TypeCategory.ARRAY, fullName, getPackageName(), simpleName, false, false);
arrayType = new SimpleType(TypeCategory.ARRAY, fullName, getPackageName(), simpleName, false, false);
}
return arrayType;
}
@ -97,7 +97,7 @@ public class ClassType implements Type {
public TypeCategory getCategory() {
return category;
}
@Override
public Type getComponentType() {
Class<?> clazz = javaClass.getComponentType();
@ -106,10 +106,10 @@ public class ClassType implements Type {
}
return componentType;
}
@Override
public String getFullName() {
return className;
return className;
}
@Override
@ -126,15 +126,15 @@ public class ClassType implements Type {
builder.append(ClassUtils.getName(javaClass, packages, classes));
builder.append("<");
boolean first = true;
for (Type parameter : parameters){
for (Type parameter : parameters){
if (!first){
builder.append(", ");
}
if (parameter == null || parameter.getFullName().equals(getFullName())){
builder.append("?");
}else{
builder.append(parameter.getGenericName(false, packages, classes));
}
builder.append(parameter.getGenericName(false, packages, classes));
}
first = false;
}
builder.append(">");
@ -175,7 +175,7 @@ public class ClassType implements Type {
public int hashCode(){
return className.hashCode();
}
@Override
public boolean isFinal() {
return Modifier.isFinal(javaClass.getModifiers());
@ -183,13 +183,13 @@ public class ClassType implements Type {
@Override
public boolean isPrimitive() {
// return javaClass.isPrimitive();
return primitiveClass != null;
return javaClass.isPrimitive() || primitiveClass != null;
}
@Override
public String toString(){
return getGenericName(true);
}
}

View File

@ -19,37 +19,39 @@ import java.util.Set;
*
*/
public final class Types {
public static final ClassType OBJECT = new ClassType(TypeCategory.SIMPLE, Object.class);
public static final ClassType OBJECTS = new ClassType(TypeCategory.ARRAY, Object[].class);
public static final ClassType OBJECTS = new ClassType(TypeCategory.ARRAY, Object[].class);
public static final ClassType BIG_DECIMAL = new ClassType(TypeCategory.NUMERIC, BigDecimal.class);
public static final ClassType BIG_INTEGER = new ClassType(TypeCategory.NUMERIC, BigInteger.class);
public static final ClassType BOOLEAN = new ClassType(TypeCategory.BOOLEAN, Boolean.class, boolean.class);
public static final ClassType BOOLEAN_P = new ClassType(TypeCategory.BOOLEAN, boolean.class);
public static final ClassType BYTE = new ClassType(TypeCategory.NUMERIC, Byte.class, byte.class);
public static final ClassType BYTE_P = new ClassType(TypeCategory.NUMERIC, byte.class);
public static final ClassType CHARACTER = new ClassType(TypeCategory.COMPARABLE, Character.class, char.class);
public static final ClassType CHAR = new ClassType(TypeCategory.COMPARABLE, char.class);
public static final ClassType COLLECTION = new ClassType(TypeCategory.COLLECTION, Collection.class, OBJECT);
public static final ClassType DOUBLE = new ClassType(TypeCategory.NUMERIC, Double.class, double.class);
public static final ClassType DOUBLE_P = new ClassType(TypeCategory.NUMERIC, double.class);
public static final ClassType FLOAT = new ClassType(TypeCategory.NUMERIC, Float.class, float.class);
public static final ClassType INTEGER = new ClassType(TypeCategory.NUMERIC, Integer.class);
public static final ClassType FLOAT_P = new ClassType(TypeCategory.NUMERIC, float.class);
public static final ClassType INTEGER = new ClassType(TypeCategory.NUMERIC, Integer.class, int.class);
public static final ClassType INT = new ClassType(TypeCategory.NUMERIC, int.class);
public static final ClassType ITERABLE = new ClassType(TypeCategory.SIMPLE, Iterable.class, OBJECT);
@ -59,29 +61,29 @@ public final class Types {
public static final ClassType LOCALE = new ClassType(TypeCategory.SIMPLE, Locale.class);
public static final ClassType LONG = new ClassType(TypeCategory.NUMERIC, Long.class, long.class);
public static final ClassType LONG_P = new ClassType(TypeCategory.NUMERIC, long.class);
public static final ClassType MAP = new ClassType(TypeCategory.MAP, Map.class, OBJECT, OBJECT);
public static final ClassType SET = new ClassType(TypeCategory.SET, Set.class, OBJECT);
public static final ClassType SHORT = new ClassType(TypeCategory.NUMERIC, Short.class, short.class);
public static final ClassType SHORT_P = new ClassType(TypeCategory.NUMERIC, short.class);
public static final ClassType STRING = new ClassType(TypeCategory.STRING, String.class);
public static final ClassType URI = new ClassType(TypeCategory.COMPARABLE, URI.class);
public static final ClassType VOID = new ClassType(TypeCategory.SIMPLE, void.class);
public static final SimpleType DATE_TIME = new SimpleType(TypeCategory.DATETIME, "org.joda.time.DateTime", "org.joda.time", "DateTime", false, false);
public static final SimpleType LOCAL_DATE = new SimpleType(TypeCategory.DATE, "org.joda.time.LocalDate", "org.joda.time", "LocalDate", false, false);
public static final SimpleType LOCAL_TIME = new SimpleType(TypeCategory.TIME, "org.joda.time.LocalTime", "org.joda.time", "LocalTime", false, false);
private Types(){}
}

View File

@ -7,6 +7,7 @@ package com.mysema.codegen.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.Map;
@ -16,7 +17,7 @@ import org.junit.Test;
public class ClassTypeTest {
private ClassType stringType = new ClassType(TypeCategory.STRING, String.class);
private final ClassType stringType = new ClassType(TypeCategory.STRING, String.class);
// @Test
// public void asArrayType(){
@ -28,14 +29,14 @@ public class ClassTypeTest {
Type type = new ClassType(TypeCategory.ARRAY,String[].class);
assertEquals("java.lang", type.getPackageName());
}
@Test
public void ArrayType_Equals_SimpleType(){
Type type = new ClassType(TypeCategory.ARRAY,String[].class);
Type type2 = new SimpleType("java.lang.String[]", "java.lang", "String[]");
assertEquals(type, type2);
}
@Test
public void As(){
assertEquals(TypeCategory.COMPARABLE, stringType.as(TypeCategory.COMPARABLE).getCategory());
@ -58,7 +59,7 @@ public class ClassTypeTest {
assertEquals("byte[]", byteArray.getSimpleName());
assertEquals("byte[]", byteArray.getFullName());
}
@Test
public void Array(){
ClassType byteArray = new ClassType(Byte[].class);
@ -66,4 +67,24 @@ public class ClassTypeTest {
assertEquals("Byte[]", byteArray.getSimpleName());
assertEquals("java.lang.Byte[]", byteArray.getFullName());
}
@Test
public void IsPrimitive(){
assertTrue(Types.CHAR.isPrimitive());
assertTrue(Types.DOUBLE_P.isPrimitive());
assertTrue(Types.FLOAT_P.isPrimitive());
assertTrue(Types.INT.isPrimitive());
assertTrue(Types.LONG_P.isPrimitive());
assertTrue(Types.SHORT_P.isPrimitive());
}
@Test
public void GetPrimitiveName(){
assertEquals("char", Types.CHARACTER.getPrimitiveName());
assertEquals("double", Types.DOUBLE.getPrimitiveName());
assertEquals("float", Types.FLOAT.getPrimitiveName());
assertEquals("int", Types.INTEGER.getPrimitiveName());
assertEquals("long", Types.LONG.getPrimitiveName());
assertEquals("short", Types.SHORT.getPrimitiveName());
}
}

View File

@ -28,4 +28,9 @@ public class SimpleTypeTest {
assertEquals("java.lang.String", type.getComponentType().getFullName());
}
@Test
public void getRawName(){
assertEquals("String", new SimpleType(Types.STRING).getRawName(Collections.<String>emptySet(), Collections.singleton(Types.STRING.getFullName())));
}
}

View File

@ -9,6 +9,7 @@ import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -21,9 +22,12 @@ import org.junit.Test;
public class ClassUtilsTest {
@Test
public void GetName() {
assertEquals("int", ClassUtils.getName(int.class));
assertEquals("int", ClassUtils.getName(int.class, Collections.<String>emptySet(), Collections.<String>emptySet()));
assertEquals("Object", ClassUtils.getName(Object.class));
assertEquals("Object[]", ClassUtils.getName(Object[].class));
assertEquals("int", ClassUtils.getName(int.class));