Skip to content

Instantly share code, notes, and snippets.

@ryoasai
Created April 28, 2011 14:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryoasai/946477 to your computer and use it in GitHub Desktop.
Save ryoasai/946477 to your computer and use it in GitHub Desktop.
Spring TypeDescriptor test.
package com.github.ryoasai;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.ReflectionUtils;
public class TypeDescriptorTest {
String[] strArray = { "test1", "test2" };
List<String> strList = Arrays.asList("test", "test2");
Map<String, Integer> map = new HashMap<String, Integer>();
@SuppressWarnings("unchecked")
List<List<String>> strListList = Arrays.asList(strList);
List<String> sampleMethod(List<Integer> intList) {
return null;
}
public static Matcher<Class<?>> isSameClassAs(Class<?> clazz) {
@SuppressWarnings("unchecked") //double cast idiom
Matcher<Class<?>> result = (Matcher<Class<?>>)(Matcher<?>)is((Object)clazz);
return result;
}
// Resolve type from field declaration.
@Test
public void testResolveFieldType() {
TypeDescriptor typeDesc = new TypeDescriptor(ReflectionUtils.findField(getClass(), "strList"));
assertThat(typeDesc.getType(), isSameClassAs(List.class));
assertThat(typeDesc.getElementType(), isSameClassAs(String.class));
assertThat(typeDesc.asString(), is("java.util.List<java.lang.String>"));
assertThat(typeDesc.isCollection(), is(true));
}
@Test
public void testResolveFieldType_NestedType() {
TypeDescriptor typeDesc = new TypeDescriptor(ReflectionUtils.findField(getClass(), "strListList"));
assertThat(typeDesc.getType(), isSameClassAs(List.class));
assertThat(typeDesc.getElementType(), isSameClassAs(List.class));
assertThat(typeDesc.asString(), is("java.util.List<java.util.List<java.lang.String>>"));
assertThat(typeDesc.isCollection(), is(true));
TypeDescriptor elementTypeDesc = typeDesc.getElementTypeDescriptor();
assertThat(elementTypeDesc.getType(), isSameClassAs(List.class));
assertThat(elementTypeDesc.getElementType(), isSameClassAs(String.class));
assertThat(elementTypeDesc.asString(), is("java.util.List<java.lang.String>"));
assertThat(elementTypeDesc.isCollection(), is(true));
}
@Test
public void testResolveFieldType_Map() {
TypeDescriptor typeDesc = new TypeDescriptor(ReflectionUtils.findField(getClass(), "map"));
assertThat(typeDesc.getType(), isSameClassAs(Map.class));
assertThat(typeDesc.getMapKeyType(), isSameClassAs(String.class));
assertThat(typeDesc.getMapValueType(), isSameClassAs(Integer.class));
assertThat(typeDesc.asString(), is("java.util.Map<java.lang.String, java.lang.Integer>"));
assertThat(typeDesc.isMap(), is(true));
}
// Resolve type from method declaration.
@Test
public void testResolveMethodParameterType() {
Method sampleMethod = ReflectionUtils.findMethod(getClass(), "sampleMethod", List.class);
TypeDescriptor typeDesc = new TypeDescriptor(new MethodParameter(sampleMethod, 0));
assertThat(typeDesc.getType(), isSameClassAs(List.class));
assertThat(typeDesc.getElementType(), isSameClassAs(Integer.class));
assertThat(typeDesc.asString(), is("java.util.List<java.lang.Integer>"));
assertThat(typeDesc.isCollection(), is(true));
}
@Test
public void testResolveMethodReturnType() {
Method sampleMethod = ReflectionUtils.findMethod(getClass(), "sampleMethod", List.class);
TypeDescriptor typeDesc = new TypeDescriptor(new MethodParameter(sampleMethod, -1));
assertThat(typeDesc.getType(), isSameClassAs(List.class));
assertThat(typeDesc.getElementType(), isSameClassAs(String.class));
assertThat(typeDesc.asString(), is("java.util.List<java.lang.String>"));
assertThat(typeDesc.isCollection(), is(true));
}
static abstract class Parent<T> {
abstract List<T> getList();
}
static class Child extends Parent<String> {
@Override
List<String> getList() {
return new ArrayList<String>();
}
}
// Resolve covariant return type
@Test
public void testResolveCovariantMethodReturnType() {
Method methodWithCovariantReturnType = ReflectionUtils.findMethod(Child.class, "getList");
TypeDescriptor typeDesc = new TypeDescriptor(new MethodParameter(methodWithCovariantReturnType, -1));
assertThat(typeDesc.getType(), isSameClassAs(List.class));
assertThat(typeDesc.getElementType(), isSameClassAs(String.class));
}
// Resolve type from instance.
@Test
public void testResolveArrayElementType() {
TypeDescriptor typeDesc = TypeDescriptor.forObject(strArray);
assertThat(typeDesc.getElementType(), isSameClassAs(String.class));
assertThat(typeDesc.asString(), is("java.lang.String[]"));
assertThat(typeDesc.isArray(), is(true));
}
@Test
public void testResolveCollectionElementType() {
TypeDescriptor typeDesc = TypeDescriptor.forObject(strList);
assertThat(typeDesc.getElementType(), isSameClassAs(String.class));
assertThat(typeDesc.asString(), is("java.util.Arrays$ArrayList<java.lang.String>"));
assertThat(typeDesc.isCollection(), is(true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment