package org.opendaylight.mdsal.binding.java.api.generator;
import static com.google.common.base.Preconditions.checkState;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
*/
static void assertContainsRestrictionCheck(final Constructor<?> constructor, final String errorMsg,
final Object... args) throws ReflectiveOperationException {
- try {
- constructor.newInstance(args);
- fail("constructor invocation should fail");
- } catch (InvocationTargetException e) {
- Throwable cause = e.getCause();
- assertTrue(cause instanceof IllegalArgumentException);
- assertEquals(errorMsg, cause.getMessage());
- }
+ final var cause = assertThrows(InvocationTargetException.class, () -> constructor.newInstance(args)).getCause();
+ assertThat(cause, instanceOf(IllegalArgumentException.class));
+ assertEquals(errorMsg, cause.getMessage());
}
/**
*/
static void assertContainsRestrictionCheck(final Object obj, final Method method, final String errorMsg,
final Object... args) throws ReflectiveOperationException {
- try {
- method.invoke(obj, args);
- fail("method invocation should fail");
- } catch (InvocationTargetException e) {
- Throwable cause = e.getCause();
- assertTrue(cause instanceof IllegalArgumentException);
- assertEquals(errorMsg, cause.getMessage());
- }
+ final var cause = assertThrows(InvocationTargetException.class, () -> method.invoke(obj, args)).getCause();
+ assertThat(cause, instanceOf(IllegalArgumentException.class));
+ assertEquals(errorMsg, cause.getMessage());
}
/**
* @param ifc expected interface
*/
static void assertImplementsIfc(final Class<?> clazz, final Class<?> ifc) {
- Class<?>[] interfaces = clazz.getInterfaces();
- List<Class<?>> ifcsList = Arrays.asList(interfaces);
+ List<Class<?>> ifcsList = Arrays.asList(clazz.getInterfaces());
if (!ifcsList.contains(ifc)) {
throw new AssertionError(clazz + " should implement " + ifc);
}
static void assertImplementsParameterizedIfc(final Class<?> clazz, final String ifcName,
final String genericTypeName) {
ParameterizedType ifcType = null;
- for (java.lang.reflect.Type ifc : clazz.getGenericInterfaces()) {
+ for (Type ifc : clazz.getGenericInterfaces()) {
if (ifc instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) ifc;
if (ifcName.equals(pt.getRawType().toString())) {
}
assertNotNull(ifcType);
- java.lang.reflect.Type[] typeArguments = ifcType.getActualTypeArguments();
- assertEquals(1, typeArguments.length);
- assertEquals("interface " + genericTypeName, typeArguments[0].toString());
+ Type[] typeArg = ifcType.getActualTypeArguments();
+ assertEquals(1, typeArg.length);
+ Type typeArgument = typeArg[0];
+ assertThat(typeArgument, instanceOf(Class.class));
+ Class<?> argClass = (Class<?>) typeArgument;
+ assertEquals(genericTypeName, argClass.getName());
+ assertTrue(argClass.isInterface());
}
/**