DATACMNS-266 - Use new common Maven build infrastructure.
Simplified project setup to be a single module build again. Using Spring Data Build parent POM to simplify project setup. See https://github.com/SpringSource/spring-data-build#spring-data-build-infrastructure
This commit is contained in:
@@ -0,0 +1,378 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.data.util;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.util.ClassTypeInformation.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.Person;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ClassTypeInformation}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class ClassTypeInformationUnitTests {
|
||||
|
||||
@Test
|
||||
public void discoversTypeForSimpleGenericField() {
|
||||
|
||||
TypeInformation<ConcreteType> discoverer = ClassTypeInformation.from(ConcreteType.class);
|
||||
assertEquals(ConcreteType.class, discoverer.getType());
|
||||
TypeInformation<?> content = discoverer.getProperty("content");
|
||||
assertEquals(String.class, content.getType());
|
||||
assertNull(content.getComponentType());
|
||||
assertNull(content.getMapValueType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversTypeForNestedGenericField() {
|
||||
|
||||
TypeInformation<ConcreteWrapper> discoverer = ClassTypeInformation.from(ConcreteWrapper.class);
|
||||
assertEquals(ConcreteWrapper.class, discoverer.getType());
|
||||
TypeInformation<?> wrapper = discoverer.getProperty("wrapped");
|
||||
assertEquals(GenericType.class, wrapper.getType());
|
||||
TypeInformation<?> content = wrapper.getProperty("content");
|
||||
|
||||
assertEquals(String.class, content.getType());
|
||||
assertEquals(String.class, discoverer.getProperty("wrapped").getProperty("content").getType());
|
||||
assertEquals(String.class, discoverer.getProperty("wrapped.content").getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void discoversBoundType() {
|
||||
|
||||
TypeInformation<GenericTypeWithBound> information = ClassTypeInformation.from(GenericTypeWithBound.class);
|
||||
assertEquals(Person.class, information.getProperty("person").getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversBoundTypeForSpecialization() {
|
||||
|
||||
TypeInformation<SpecialGenericTypeWithBound> information = ClassTypeInformation
|
||||
.from(SpecialGenericTypeWithBound.class);
|
||||
assertEquals(SpecialPerson.class, information.getProperty("person").getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void discoversBoundTypeForNested() {
|
||||
|
||||
TypeInformation<AnotherGenericType> information = ClassTypeInformation.from(AnotherGenericType.class);
|
||||
assertEquals(GenericTypeWithBound.class, information.getProperty("nested").getType());
|
||||
assertEquals(Person.class, information.getProperty("nested.person").getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversArraysAndCollections() {
|
||||
TypeInformation<StringCollectionContainer> information = ClassTypeInformation.from(StringCollectionContainer.class);
|
||||
|
||||
TypeInformation<?> property = information.getProperty("array");
|
||||
assertEquals(property.getComponentType().getType(), String.class);
|
||||
|
||||
Class<?> type = property.getType();
|
||||
assertEquals(String[].class, type);
|
||||
assertThat(type.isArray(), is(true));
|
||||
|
||||
property = information.getProperty("foo");
|
||||
assertEquals(Collection[].class, property.getType());
|
||||
assertEquals(Collection.class, property.getComponentType().getType());
|
||||
assertEquals(String.class, property.getComponentType().getComponentType().getType());
|
||||
|
||||
property = information.getProperty("rawSet");
|
||||
assertEquals(Set.class, property.getType());
|
||||
assertEquals(Object.class, property.getComponentType().getType());
|
||||
assertNull(property.getMapValueType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversMapValueType() {
|
||||
|
||||
TypeInformation<StringMapContainer> information = ClassTypeInformation.from(StringMapContainer.class);
|
||||
TypeInformation<?> genericMap = information.getProperty("genericMap");
|
||||
assertEquals(Map.class, genericMap.getType());
|
||||
assertEquals(String.class, genericMap.getMapValueType().getType());
|
||||
|
||||
TypeInformation<?> map = information.getProperty("map");
|
||||
assertEquals(Map.class, map.getType());
|
||||
assertEquals(Calendar.class, map.getMapValueType().getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeInfoDoesNotEqualForGenericTypesWithDifferentParent() {
|
||||
|
||||
TypeInformation<ConcreteWrapper> first = ClassTypeInformation.from(ConcreteWrapper.class);
|
||||
TypeInformation<AnotherConcreteWrapper> second = ClassTypeInformation.from(AnotherConcreteWrapper.class);
|
||||
|
||||
assertFalse(first.getProperty("wrapped").equals(second.getProperty("wrapped")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void handlesPropertyFieldMismatchCorrectly() {
|
||||
|
||||
TypeInformation<PropertyGetter> from = ClassTypeInformation.from(PropertyGetter.class);
|
||||
|
||||
TypeInformation<?> property = from.getProperty("_name");
|
||||
assertThat(property, is(notNullValue()));
|
||||
assertThat(property.getType(), is(typeCompatibleWith(String.class)));
|
||||
|
||||
property = from.getProperty("name");
|
||||
assertThat(property, is(notNullValue()));
|
||||
assertThat(property.getType(), is(typeCompatibleWith(byte[].class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-77
|
||||
*/
|
||||
@Test
|
||||
public void returnsSameInstanceForCachedClass() {
|
||||
|
||||
TypeInformation<PropertyGetter> info = ClassTypeInformation.from(PropertyGetter.class);
|
||||
assertThat(ClassTypeInformation.from(PropertyGetter.class), is(sameInstance(info)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-39
|
||||
*/
|
||||
@Test
|
||||
public void resolvesWildCardTypeCorrectly() {
|
||||
|
||||
TypeInformation<ClassWithWildCardBound> information = ClassTypeInformation.from(ClassWithWildCardBound.class);
|
||||
|
||||
TypeInformation<?> property = information.getProperty("wildcard");
|
||||
assertThat(property.isCollectionLike(), is(true));
|
||||
assertThat(property.getComponentType().getType(), is(typeCompatibleWith(String.class)));
|
||||
|
||||
property = information.getProperty("complexWildcard");
|
||||
assertThat(property.isCollectionLike(), is(true));
|
||||
|
||||
TypeInformation<?> component = property.getComponentType();
|
||||
assertThat(component.isCollectionLike(), is(true));
|
||||
assertThat(component.getComponentType().getType(), is(typeCompatibleWith(String.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvesTypeParametersCorrectly() {
|
||||
|
||||
TypeInformation<ConcreteType> information = ClassTypeInformation.from(ConcreteType.class);
|
||||
TypeInformation<?> superTypeInformation = information.getSuperTypeInformation(GenericType.class);
|
||||
|
||||
List<TypeInformation<?>> parameters = superTypeInformation.getTypeArguments();
|
||||
assertThat(parameters, hasSize(2));
|
||||
assertThat(parameters.get(0).getType(), is((Object) String.class));
|
||||
assertThat(parameters.get(1).getType(), is((Object) Object.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resolvesNestedInheritedTypeParameters() {
|
||||
|
||||
TypeInformation<SecondExtension> information = ClassTypeInformation.from(SecondExtension.class);
|
||||
TypeInformation<?> superTypeInformation = information.getSuperTypeInformation(Base.class);
|
||||
|
||||
List<TypeInformation<?>> parameters = superTypeInformation.getTypeArguments();
|
||||
assertThat(parameters, hasSize(1));
|
||||
assertThat(parameters.get(0).getType(), is((Object) String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoveresMethodParameterTypesCorrectly() throws Exception {
|
||||
|
||||
TypeInformation<SecondExtension> information = ClassTypeInformation.from(SecondExtension.class);
|
||||
Method method = SecondExtension.class.getMethod("foo", Base.class);
|
||||
List<TypeInformation<?>> informations = information.getParameterTypes(method);
|
||||
TypeInformation<?> returnTypeInformation = information.getReturnType(method);
|
||||
|
||||
assertThat(informations, hasSize(1));
|
||||
assertThat(informations.get(0).getType(), is((Object) Base.class));
|
||||
assertThat(informations.get(0), is((Object) returnTypeInformation));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversImplementationBindingCorrectlyForString() throws Exception {
|
||||
|
||||
TypeInformation<TypedClient> information = from(TypedClient.class);
|
||||
Method method = TypedClient.class.getMethod("stringMethod", GenericInterface.class);
|
||||
|
||||
TypeInformation<?> parameterType = information.getParameterTypes(method).get(0);
|
||||
|
||||
TypeInformation<StringImplementation> stringInfo = from(StringImplementation.class);
|
||||
assertThat(parameterType.isAssignableFrom(stringInfo), is(true));
|
||||
assertThat(stringInfo.getSuperTypeInformation(GenericInterface.class), is((Object) parameterType));
|
||||
assertThat(parameterType.isAssignableFrom(from(LongImplementation.class)), is(false));
|
||||
assertThat(parameterType.isAssignableFrom(from(StringImplementation.class).getSuperTypeInformation(
|
||||
GenericInterface.class)), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversImplementationBindingCorrectlyForLong() throws Exception {
|
||||
|
||||
TypeInformation<TypedClient> information = from(TypedClient.class);
|
||||
Method method = TypedClient.class.getMethod("longMethod", GenericInterface.class);
|
||||
|
||||
TypeInformation<?> parameterType = information.getParameterTypes(method).get(0);
|
||||
|
||||
assertThat(parameterType.isAssignableFrom(from(StringImplementation.class)), is(false));
|
||||
assertThat(parameterType.isAssignableFrom(from(LongImplementation.class)), is(true));
|
||||
assertThat(parameterType.isAssignableFrom(from(StringImplementation.class).getSuperTypeInformation(
|
||||
GenericInterface.class)), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void discoversImplementationBindingCorrectlyForNumber() throws Exception {
|
||||
|
||||
TypeInformation<TypedClient> information = from(TypedClient.class);
|
||||
Method method = TypedClient.class.getMethod("boundToNumberMethod", GenericInterface.class);
|
||||
|
||||
TypeInformation<?> parameterType = information.getParameterTypes(method).get(0);
|
||||
|
||||
assertThat(parameterType.isAssignableFrom(from(StringImplementation.class)), is(false));
|
||||
assertThat(parameterType.isAssignableFrom(from(LongImplementation.class)), is(true));
|
||||
assertThat(parameterType.isAssignableFrom(from(StringImplementation.class).getSuperTypeInformation(
|
||||
GenericInterface.class)), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsComponentTypeForMultiDimensionalArrayCorrectly() {
|
||||
|
||||
TypeInformation<?> information = from(String[][].class);
|
||||
assertThat(information.getType(), is((Object) String[][].class));
|
||||
assertThat(information.getComponentType().getType(), is((Object) String[].class));
|
||||
assertThat(information.getActualType().getActualType().getType(), is((Object) String.class));
|
||||
}
|
||||
|
||||
static class StringMapContainer extends MapContainer<String> {
|
||||
|
||||
}
|
||||
|
||||
static class MapContainer<T> {
|
||||
Map<String, T> genericMap;
|
||||
Map<String, Calendar> map;
|
||||
}
|
||||
|
||||
static class StringCollectionContainer extends CollectionContainer<String> {
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
static class CollectionContainer<T> {
|
||||
|
||||
T[] array;
|
||||
Collection<T>[] foo;
|
||||
Set<String> set;
|
||||
Set rawSet;
|
||||
}
|
||||
|
||||
static class GenericTypeWithBound<T extends Person> {
|
||||
|
||||
T person;
|
||||
}
|
||||
|
||||
static class AnotherGenericType<T extends Person, S extends GenericTypeWithBound<T>> {
|
||||
S nested;
|
||||
}
|
||||
|
||||
static class SpecialGenericTypeWithBound extends GenericTypeWithBound<SpecialPerson> {
|
||||
|
||||
}
|
||||
|
||||
static abstract class SpecialPerson extends Person {
|
||||
protected SpecialPerson(Integer ssn, String firstName, String lastName) {
|
||||
super(ssn, firstName, lastName);
|
||||
}
|
||||
}
|
||||
|
||||
static class GenericType<T, S> {
|
||||
|
||||
Long index;
|
||||
T content;
|
||||
}
|
||||
|
||||
static class ConcreteType extends GenericType<String, Object> {
|
||||
|
||||
}
|
||||
|
||||
static class GenericWrapper<S> {
|
||||
|
||||
GenericType<S, Object> wrapped;
|
||||
}
|
||||
|
||||
static class ConcreteWrapper extends GenericWrapper<String> {
|
||||
|
||||
}
|
||||
|
||||
static class AnotherConcreteWrapper extends GenericWrapper<Long> {
|
||||
|
||||
}
|
||||
|
||||
static class PropertyGetter {
|
||||
private String _name;
|
||||
|
||||
public byte[] getName() {
|
||||
return _name.getBytes();
|
||||
}
|
||||
}
|
||||
|
||||
static class ClassWithWildCardBound {
|
||||
List<? extends String> wildcard;
|
||||
List<? extends Collection<? extends String>> complexWildcard;
|
||||
}
|
||||
|
||||
static class Base<T> {
|
||||
|
||||
}
|
||||
|
||||
static class FirstExtension<T> extends Base<String> {
|
||||
|
||||
public Base<GenericWrapper<T>> foo(Base<GenericWrapper<T>> param) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
static class SecondExtension extends FirstExtension<Long> {
|
||||
|
||||
}
|
||||
|
||||
interface GenericInterface<T> {
|
||||
|
||||
}
|
||||
|
||||
interface TypedClient {
|
||||
|
||||
void stringMethod(GenericInterface<String> param);
|
||||
|
||||
void longMethod(GenericInterface<Long> param);
|
||||
|
||||
void boundToNumberMethod(GenericInterface<? extends Number> param);
|
||||
}
|
||||
|
||||
class StringImplementation implements GenericInterface<String> {
|
||||
|
||||
}
|
||||
|
||||
class LongImplementation implements GenericInterface<Long> {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.data.util;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ParameterizedTypeInformation}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ParameterizedTypeUnitTests {
|
||||
|
||||
@Mock
|
||||
ParameterizedType one;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
when(one.getActualTypeArguments()).thenReturn(new Type[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersTypeInformationsWithDifferingParentsNotEqual() {
|
||||
|
||||
TypeDiscoverer<String> stringParent = new TypeDiscoverer<String>(String.class, null);
|
||||
TypeDiscoverer<Object> objectParent = new TypeDiscoverer<Object>(Object.class, null);
|
||||
|
||||
ParameterizedTypeInformation<Object> first = new ParameterizedTypeInformation<Object>(one, stringParent);
|
||||
ParameterizedTypeInformation<Object> second = new ParameterizedTypeInformation<Object>(one, objectParent);
|
||||
|
||||
assertFalse(first.equals(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void considersTypeInformationsWithSameParentsNotEqual() {
|
||||
|
||||
TypeDiscoverer<String> stringParent = new TypeDiscoverer<String>(String.class, null);
|
||||
|
||||
ParameterizedTypeInformation<Object> first = new ParameterizedTypeInformation<Object>(one, stringParent);
|
||||
ParameterizedTypeInformation<Object> second = new ParameterizedTypeInformation<Object>(one, stringParent);
|
||||
|
||||
assertTrue(first.equals(second));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-88
|
||||
*/
|
||||
@Test
|
||||
public void resolvesMapValueTypeCorrectly() {
|
||||
|
||||
TypeInformation<Foo> type = ClassTypeInformation.from(Foo.class);
|
||||
TypeInformation<?> propertyType = type.getProperty("param");
|
||||
assertThat(propertyType.getProperty("value").getType(), is(typeCompatibleWith(String.class)));
|
||||
assertThat(propertyType.getMapValueType().getType(), is(typeCompatibleWith(String.class)));
|
||||
|
||||
propertyType = type.getProperty("param2");
|
||||
assertThat(propertyType.getProperty("value").getType(), is(typeCompatibleWith(String.class)));
|
||||
assertThat(propertyType.getMapValueType().getType(), is(typeCompatibleWith(Locale.class)));
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class Localized<S> extends HashMap<Locale, S> {
|
||||
S value;
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
class Localized2<S> extends HashMap<S, Locale> {
|
||||
S value;
|
||||
}
|
||||
|
||||
class Foo {
|
||||
Localized<String> param;
|
||||
Localized2<String> param2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.data.util;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.util.ReflectionUtils.DescribedFieldFilter;
|
||||
import org.springframework.util.ReflectionUtils.FieldFilter;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class ReflectionUtilsUnitTests {
|
||||
|
||||
Field reference;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
this.reference = Sample.class.getField("field");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsFieldByFilter() {
|
||||
|
||||
Field field = ReflectionUtils.findField(Sample.class, (FieldFilter) new FieldNameFieldFilter("field"));
|
||||
assertThat(field, is(reference));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullIfNoFieldFound() {
|
||||
|
||||
Field field = ReflectionUtils.findField(Sample.class, (FieldFilter) new FieldNameFieldFilter("foo"));
|
||||
assertThat(field, is(nullValue()));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void rejectsNonUniqueField() {
|
||||
ReflectionUtils.findField(Sample.class, new ReflectionUtils.AnnotationFieldFilter(Autowired.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsUniqueField() {
|
||||
|
||||
Field field = ReflectionUtils.findField(Sample.class, new FieldNameFieldFilter("field"), false);
|
||||
assertThat(field, is(reference));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsFieldInSuperclass() {
|
||||
|
||||
class Subclass extends Sample {
|
||||
|
||||
}
|
||||
|
||||
Field field = ReflectionUtils.findField(Subclass.class, new FieldNameFieldFilter("field"));
|
||||
assertThat(field, is(reference));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setsNonPublicField() {
|
||||
|
||||
Sample sample = new Sample();
|
||||
Field field = ReflectionUtils.findField(Sample.class, new FieldNameFieldFilter("first"));
|
||||
ReflectionUtils.setField(field, sample, "foo");
|
||||
assertThat(sample.first, is("foo"));
|
||||
}
|
||||
|
||||
static class Sample {
|
||||
|
||||
public String field;
|
||||
|
||||
@Autowired
|
||||
String first, second;
|
||||
}
|
||||
|
||||
static class FieldNameFieldFilter implements DescribedFieldFilter {
|
||||
|
||||
private final String name;
|
||||
|
||||
public FieldNameFieldFilter(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public boolean matches(Field field) {
|
||||
return field.getName().equals(name);
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return String.format("Filter for fields named %s", name);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.data.util;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link TypeDiscoverer}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class TypeDiscovererUnitTests {
|
||||
|
||||
@Mock
|
||||
@SuppressWarnings("rawtypes")
|
||||
Map<TypeVariable, Type> firstMap;
|
||||
|
||||
@Mock
|
||||
@SuppressWarnings("rawtypes")
|
||||
Map<TypeVariable, Type> secondMap;
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsNullType() {
|
||||
new TypeDiscoverer<Object>(null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEqualIfTypesDiffer() {
|
||||
|
||||
TypeDiscoverer<Object> objectTypeInfo = new TypeDiscoverer<Object>(Object.class, null);
|
||||
TypeDiscoverer<String> stringTypeInfo = new TypeDiscoverer<String>(String.class, null);
|
||||
|
||||
assertFalse(objectTypeInfo.equals(stringTypeInfo));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNotEqualIfTypeVariableMapsDiffer() {
|
||||
|
||||
assertFalse(firstMap.equals(secondMap));
|
||||
|
||||
TypeDiscoverer<Object> first = new TypeDiscoverer<Object>(Object.class, firstMap);
|
||||
TypeDiscoverer<Object> second = new TypeDiscoverer<Object>(Object.class, secondMap);
|
||||
|
||||
assertFalse(first.equals(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dealsWithTypesReferencingThemselves() {
|
||||
|
||||
TypeInformation<SelfReferencing> information = new ClassTypeInformation<SelfReferencing>(SelfReferencing.class);
|
||||
TypeInformation<?> first = information.getProperty("parent").getMapValueType();
|
||||
TypeInformation<?> second = first.getProperty("map").getMapValueType();
|
||||
assertEquals(first, second);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dealsWithTypesReferencingThemselvesInAMap() {
|
||||
|
||||
TypeInformation<SelfReferencingMap> information = new ClassTypeInformation<SelfReferencingMap>(
|
||||
SelfReferencingMap.class);
|
||||
TypeInformation<?> mapValueType = information.getProperty("map").getMapValueType();
|
||||
assertEquals(mapValueType, information);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsComponentAndValueTypesForMapExtensions() {
|
||||
TypeInformation<?> discoverer = new TypeDiscoverer<Object>(CustomMap.class, null);
|
||||
assertEquals(Locale.class, discoverer.getMapValueType().getType());
|
||||
assertEquals(String.class, discoverer.getComponentType().getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsComponentTypeForCollectionExtension() {
|
||||
TypeDiscoverer<CustomCollection> discoverer = new TypeDiscoverer<CustomCollection>(CustomCollection.class, null);
|
||||
assertEquals(String.class, discoverer.getComponentType().getType());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsComponentTypeForArrays() {
|
||||
TypeDiscoverer<String[]> discoverer = new TypeDiscoverer<String[]>(String[].class, null);
|
||||
assertEquals(String.class, discoverer.getComponentType().getType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-57
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void discoveresConstructorParameterTypesCorrectly() throws NoSuchMethodException, SecurityException {
|
||||
|
||||
TypeDiscoverer<GenericConstructors> discoverer = new TypeDiscoverer<GenericConstructors>(GenericConstructors.class,
|
||||
null);
|
||||
Constructor<GenericConstructors> constructor = GenericConstructors.class.getConstructor(List.class, Locale.class);
|
||||
List<TypeInformation<?>> types = discoverer.getParameterTypes(constructor);
|
||||
assertThat(types.size(), is(2));
|
||||
assertThat(types.get(0).getType(), equalTo((Class) List.class));
|
||||
assertThat(types.get(0).getComponentType().getType(), is(equalTo((Class) String.class)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void returnsNullForComponentAndValueTypesForRawMaps() {
|
||||
TypeDiscoverer<Map> discoverer = new TypeDiscoverer<Map>(Map.class, null);
|
||||
assertThat(discoverer.getComponentType(), is(nullValue()));
|
||||
assertThat(discoverer.getMapValueType(), is(nullValue()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-167
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void doesNotConsiderTypeImplementingIterableACollection() {
|
||||
|
||||
TypeDiscoverer<Person> discoverer = new TypeDiscoverer<Person>(Person.class, null);
|
||||
TypeInformation reference = ClassTypeInformation.from(Address.class);
|
||||
|
||||
TypeInformation<?> addresses = discoverer.getProperty("addresses");
|
||||
assertThat(addresses.isCollectionLike(), is(false));
|
||||
assertThat(addresses.getComponentType(), is(reference));
|
||||
|
||||
TypeInformation<?> adressIterable = discoverer.getProperty("addressIterable");
|
||||
assertThat(adressIterable.isCollectionLike(), is(true));
|
||||
assertThat(adressIterable.getComponentType(), is(reference));
|
||||
}
|
||||
|
||||
class Person {
|
||||
|
||||
Addresses addresses;
|
||||
Iterable<Address> addressIterable;
|
||||
}
|
||||
|
||||
abstract class Addresses implements Iterable<Address> {
|
||||
|
||||
}
|
||||
|
||||
class Address {
|
||||
|
||||
}
|
||||
|
||||
class SelfReferencing {
|
||||
|
||||
Map<String, SelfReferencingMap> parent;
|
||||
}
|
||||
|
||||
class SelfReferencingMap {
|
||||
Map<String, SelfReferencingMap> map;
|
||||
}
|
||||
|
||||
interface CustomMap extends Map<String, Locale> {
|
||||
|
||||
}
|
||||
|
||||
interface CustomCollection extends Collection<String> {
|
||||
|
||||
}
|
||||
|
||||
public static class GenericConstructors {
|
||||
|
||||
public GenericConstructors(List<String> first, Locale second) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user