fix failing unit test and better coverage
This commit is contained in:
parent
ef3e8c968b
commit
a10a3a62e5
@ -4,6 +4,7 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
|
||||
public class ReflectionUtil {
|
||||
|
||||
public static Field getField(final Class<?> aClass, final String fieldName) {
|
||||
@ -45,8 +46,16 @@ public class ReflectionUtil {
|
||||
* @param <T> the expected class of the generics parameter at position 'index' in 'rawInterface'
|
||||
* @return the actual generics parameter
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Class<T> determineGenericInterfaceParameter(final Class<?> clazz, final Class<?> rawInterface, final int paramIndex) {
|
||||
final Class<T> found = determineGenericInterfaceParameterImpl(clazz, rawInterface, paramIndex);
|
||||
if (found == null) {
|
||||
throw new AssertionError(clazz.getSimpleName() + " expected to implement " + rawInterface.getSimpleName() + "<...>");
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> Class<T> determineGenericInterfaceParameterImpl(final Class<?> clazz, final Class<?> rawInterface, final int paramIndex) {
|
||||
for (Type genericInterface : clazz.getGenericInterfaces()) {
|
||||
if (genericInterface instanceof ParameterizedType) {
|
||||
final ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
|
||||
@ -55,16 +64,19 @@ public class ReflectionUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (clazz.getSuperclass() != Object.class) {
|
||||
return determineGenericInterfaceParameter(clazz.getSuperclass(), rawInterface, paramIndex);
|
||||
}
|
||||
for (Class<?> implementedInterface : clazz.getInterfaces()) {
|
||||
final Class<T> found = determineGenericInterfaceParameter(implementedInterface, rawInterface, paramIndex);
|
||||
if (clazz.getSuperclass() != null) {
|
||||
final Class<T> found = determineGenericInterfaceParameterImpl(clazz.getSuperclass(), rawInterface, paramIndex);
|
||||
if (found != null) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
throw new AssertionError(clazz.getSimpleName() + " expected to implement " + rawInterface.getSimpleName() + "<...>");
|
||||
for (Class<?> implementedInterface : clazz.getInterfaces()) {
|
||||
final Class<T> found = determineGenericInterfaceParameterImpl(implementedInterface, rawInterface, paramIndex);
|
||||
if (found != null) {
|
||||
return found;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -76,8 +88,16 @@ public class ReflectionUtil {
|
||||
* @param <T> the expected class of the generics parameter at position 'index' in 'rawClass'
|
||||
* @return the actual generics parameter
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> Class<T> determineGenericClassParameter(final Class<?> clazz, final Class<?> rawClass, final int paramIndex) {
|
||||
final Class<T> found = determineGenericClassParameterImpl(clazz, rawClass, paramIndex);
|
||||
if (found == null) {
|
||||
throw new AssertionError(clazz.getSimpleName() + " expected to extend " + rawClass.getSimpleName() + "<...>");
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <T> Class<T> determineGenericClassParameterImpl(final Class<?> clazz, final Class<?> rawClass, final int paramIndex) {
|
||||
final Type genericClass = clazz.getGenericSuperclass();
|
||||
if (genericClass instanceof ParameterizedType) {
|
||||
final ParameterizedType parameterizedType = (ParameterizedType) genericClass;
|
||||
@ -88,7 +108,7 @@ public class ReflectionUtil {
|
||||
if (clazz.getSuperclass() != Object.class) {
|
||||
return determineGenericClassParameter(clazz.getSuperclass(), rawClass, paramIndex);
|
||||
}
|
||||
throw new AssertionError(clazz.getSimpleName() + " expected to extend " + rawClass.getSimpleName() + "<...>");
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@ -2,14 +2,12 @@ package org.hostsharing.hsadminng.service.util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.ThrowableAssert.catchThrowable;
|
||||
import static org.hostsharing.hsadminng.service.util.ReflectionUtil.unchecked;
|
||||
|
||||
|
||||
public class ReflectionUtilTest {
|
||||
public class ReflectionUtilUnitTest {
|
||||
|
||||
@Test
|
||||
public void setValue() {
|
||||
@ -54,7 +52,7 @@ public class ReflectionUtilTest {
|
||||
@Test
|
||||
public void throwsExceptionIfGenericInterfaceNotImplemented() {
|
||||
final Throwable actual = catchThrowable(() -> ReflectionUtil.determineGenericInterfaceParameter(SomeClass.class, UnusedGenericInterface.class, 1));
|
||||
assertThat(actual).isInstanceOf(AssertionError.class).hasMessageContaining("GenericClass expected to implement UnusedGenericInterface<...>");
|
||||
assertThat(actual).isInstanceOf(AssertionError.class).hasMessageContaining("SomeClass expected to implement UnusedGenericInterface<...>");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -88,7 +86,7 @@ public class ReflectionUtilTest {
|
||||
|
||||
@Test
|
||||
public void asEnumValue() {
|
||||
assertThat(ReflectionUtil.asEnumValue(Color.class, "RED")).isEqualTo(Color.RED);
|
||||
assertThat(ReflectionUtil.asEnumValue(SomeEnum.class, "GREEN")).isEqualTo(SomeEnum.GREEN);
|
||||
}
|
||||
|
||||
// --- only test fixture below ---
|
||||
@ -111,19 +109,25 @@ public class ReflectionUtilTest {
|
||||
private static class SomeClass extends SuperClass {
|
||||
}
|
||||
|
||||
private static class SuperClass extends GenericClass<String, Boolean> implements GenericInterface<Integer, Long> {
|
||||
private static class SuperClass extends GenericClass<String, Boolean> implements IntermediateInterfaces<Integer, Long> {
|
||||
}
|
||||
|
||||
|
||||
private static class UnusedSuperClass extends GenericClass<String, Boolean> implements GenericInterface<Integer, Long> {
|
||||
private static class UnusedSuperClass extends GenericClass<String, Boolean> implements IntermediateInterfaces<Integer, Long> {
|
||||
}
|
||||
|
||||
private static class GenericClass<T, V> {
|
||||
}
|
||||
|
||||
private interface IntermediateInterfaces<T, V> extends GenericInterface<Integer, Long> {
|
||||
}
|
||||
|
||||
private interface GenericInterface<T, V> {
|
||||
}
|
||||
|
||||
private interface UnusedGenericInterface<T, V> {
|
||||
}
|
||||
|
||||
enum SomeEnum {
|
||||
RED, BLUE, GREEN
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user