fix failing unit test and better coverage

This commit is contained in:
Michael Hoennig 2019-04-26 11:27:29 +02:00
parent ef3e8c968b
commit a10a3a62e5
2 changed files with 41 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
public class ReflectionUtil { public class ReflectionUtil {
public static Field getField(final Class<?> aClass, final String fieldName) { 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' * @param <T> the expected class of the generics parameter at position 'index' in 'rawInterface'
* @return the actual generics parameter * @return the actual generics parameter
*/ */
@SuppressWarnings("unchecked")
public static <T> Class<T> determineGenericInterfaceParameter(final Class<?> clazz, final Class<?> rawInterface, final int paramIndex) { 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()) { for (Type genericInterface : clazz.getGenericInterfaces()) {
if (genericInterface instanceof ParameterizedType) { if (genericInterface instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) genericInterface; final ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
@ -55,16 +64,19 @@ public class ReflectionUtil {
} }
} }
} }
if (clazz.getSuperclass() != Object.class) { if (clazz.getSuperclass() != null) {
return determineGenericInterfaceParameter(clazz.getSuperclass(), rawInterface, paramIndex); final Class<T> found = determineGenericInterfaceParameterImpl(clazz.getSuperclass(), rawInterface, paramIndex);
}
for (Class<?> implementedInterface : clazz.getInterfaces()) {
final Class<T> found = determineGenericInterfaceParameter(implementedInterface, rawInterface, paramIndex);
if (found != null) { if (found != null) {
return found; 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' * @param <T> the expected class of the generics parameter at position 'index' in 'rawClass'
* @return the actual generics parameter * @return the actual generics parameter
*/ */
@SuppressWarnings("unchecked")
public static <T> Class<T> determineGenericClassParameter(final Class<?> clazz, final Class<?> rawClass, final int paramIndex) { 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(); final Type genericClass = clazz.getGenericSuperclass();
if (genericClass instanceof ParameterizedType) { if (genericClass instanceof ParameterizedType) {
final ParameterizedType parameterizedType = (ParameterizedType) genericClass; final ParameterizedType parameterizedType = (ParameterizedType) genericClass;
@ -88,7 +108,7 @@ public class ReflectionUtil {
if (clazz.getSuperclass() != Object.class) { if (clazz.getSuperclass() != Object.class) {
return determineGenericClassParameter(clazz.getSuperclass(), rawClass, paramIndex); return determineGenericClassParameter(clazz.getSuperclass(), rawClass, paramIndex);
} }
throw new AssertionError(clazz.getSimpleName() + " expected to extend " + rawClass.getSimpleName() + "<...>"); return null;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")

View File

@ -2,14 +2,12 @@ package org.hostsharing.hsadminng.service.util;
import org.junit.Test; import org.junit.Test;
import java.awt.*;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.ThrowableAssert.catchThrowable; import static org.assertj.core.api.ThrowableAssert.catchThrowable;
import static org.hostsharing.hsadminng.service.util.ReflectionUtil.unchecked; import static org.hostsharing.hsadminng.service.util.ReflectionUtil.unchecked;
public class ReflectionUtilTest { public class ReflectionUtilUnitTest {
@Test @Test
public void setValue() { public void setValue() {
@ -54,7 +52,7 @@ public class ReflectionUtilTest {
@Test @Test
public void throwsExceptionIfGenericInterfaceNotImplemented() { public void throwsExceptionIfGenericInterfaceNotImplemented() {
final Throwable actual = catchThrowable(() -> ReflectionUtil.determineGenericInterfaceParameter(SomeClass.class, UnusedGenericInterface.class, 1)); 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 @Test
@ -88,7 +86,7 @@ public class ReflectionUtilTest {
@Test @Test
public void asEnumValue() { 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 --- // --- only test fixture below ---
@ -111,19 +109,25 @@ public class ReflectionUtilTest {
private static class SomeClass extends SuperClass { 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 IntermediateInterfaces<Integer, Long> {
private static class UnusedSuperClass extends GenericClass<String, Boolean> implements GenericInterface<Integer, Long> {
} }
private static class GenericClass<T, V> { private static class GenericClass<T, V> {
} }
private interface IntermediateInterfaces<T, V> extends GenericInterface<Integer, Long> {
}
private interface GenericInterface<T, V> { private interface GenericInterface<T, V> {
} }
private interface UnusedGenericInterface<T, V> { private interface UnusedGenericInterface<T, V> {
} }
enum SomeEnum {
RED, BLUE, GREEN
}
} }