Skip to content

Commit

Permalink
Resolve default method in QuarkusTestExtension.
Browse files Browse the repository at this point in the history
  • Loading branch information
orekyuu committed Nov 1, 2021
1 parent 24a94d9 commit 30b1e2a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.quarkus.it.main;

import org.junit.jupiter.api.Test;

public interface DefaultMethodInterface {

@Test
default void doTest() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.quarkus.it.main;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class DefaultMethodTestCase implements DefaultMethodInterface {

@Nested
class NestedTestClass implements DefaultMethodInterface {
}

interface InnerTestInterface {
@Test
default void testInnerTestInterface() {

}
}

@Nested
class InnerTestInterfaceTest implements InnerTestInterface {

}

@Nested
class SomeTestInterfaceTest implements DefaultMethodInterface, InnerTestInterface {

}

interface NonTestInterface {
default void simpleMethod() {

}

void abstractMethod();
}

@Nested
class NonTestInterfaceTest implements NonTestInterface {

@Override
public void abstractMethod() {

}
}

interface SuperTestInterface {
@Test
default void superTestMethod() {

}
}

interface ChildTestInterface extends SuperTestInterface {
@Test
default void childTestMethod() {

}
}

@Nested
class HierarchyTest implements ChildTestInterface {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public void afterEach(ExtensionContext context) throws Exception {
Method actualTestMethod = null;

// go up the class hierarchy to fetch the proper test method
Class<?> c = actualTestClass;
Class<?> c = resolveDeclaringClass(originalTestMethod, actualTestClass);
List<Class<?>> parameterTypesFromTccl = new ArrayList<>(originalParameterTypes.length);
for (Class<?> type : originalParameterTypes) {
if (type.isPrimitive()) {
Expand All @@ -487,14 +487,12 @@ public void afterEach(ExtensionContext context) throws Exception {
}
}
Class<?>[] parameterTypes = parameterTypesFromTccl.toArray(new Class[0]);
while (c != Object.class) {
try {
try {
if (c != null) {
actualTestMethod = c.getDeclaredMethod(originalTestMethod.getName(), parameterTypes);
break;
} catch (NoSuchMethodException ignored) {

}
c = c.getSuperclass();
} catch (NoSuchMethodException ignored) {

}
if (actualTestMethod == null) {
throw new RuntimeException("Could not find method " + originalTestMethod + " on test class");
Expand Down Expand Up @@ -932,32 +930,50 @@ private Object runExtensionMethod(ReflectiveInvocationContext<Method> invocation

private Method determineTCCLExtensionMethod(Method originalMethod, Class<?> c)
throws ClassNotFoundException {
Class<?> declaringClass = resolveDeclaringClass(originalMethod, c);
if (declaringClass == null) {
return null;
}
try {
Class<?>[] originalParameterTypes = originalMethod.getParameterTypes();
List<Class<?>> parameterTypesFromTccl = new ArrayList<>(originalParameterTypes.length);
for (Class<?> type : originalParameterTypes) {
if (type.isPrimitive()) {
parameterTypesFromTccl.add(type);
} else {
parameterTypesFromTccl
.add(Class.forName(type.getName(), true,
Thread.currentThread().getContextClassLoader()));
}
}
return declaringClass.getDeclaredMethod(originalMethod.getName(),
parameterTypesFromTccl.toArray(new Class[0]));
} catch (NoSuchMethodException ignored) {

Method newMethod = null;
while (c != Object.class) {
if (c.getName().equals(originalMethod.getDeclaringClass().getName())) {
try {
Class<?>[] originalParameterTypes = originalMethod.getParameterTypes();
List<Class<?>> parameterTypesFromTccl = new ArrayList<>(originalParameterTypes.length);
for (Class<?> type : originalParameterTypes) {
if (type.isPrimitive()) {
parameterTypesFromTccl.add(type);
} else {
parameterTypesFromTccl
.add(Class.forName(type.getName(), true,
Thread.currentThread().getContextClassLoader()));
}
}
newMethod = c.getDeclaredMethod(originalMethod.getName(),
parameterTypesFromTccl.toArray(new Class[0]));
break;
} catch (NoSuchMethodException ignored) {
}

}
return null;
}

private Class<?> resolveDeclaringClass(Method method, Class<?> c) {
if (c == Object.class || c == null) {
return null;
}

if (c.getName().equals(method.getDeclaringClass().getName())) {
return c;
}
Class<?> declaringClass = resolveDeclaringClass(method, c.getSuperclass());
if (declaringClass != null) {
return declaringClass;
}
for (Class<?> anInterface : c.getInterfaces()) {
declaringClass = resolveDeclaringClass(method, anInterface);
if (declaringClass != null) {
return declaringClass;
}
c = c.getSuperclass();
}
return newMethod;
return null;
}

@Override
Expand Down

0 comments on commit 30b1e2a

Please sign in to comment.