Skip to content

Commit

Permalink
Fix regression for test class hierarchies without bridge methods
Browse files Browse the repository at this point in the history
Code introduced by [1] broke standard support for test class hierarchies
without bridge or synthetic methods present.
This commit fixes the regression by inspecting the return and parameter
types of the upper method for usage of generic-related types.

[1] 82174c1
  • Loading branch information
sormuras committed Mar 11, 2017
1 parent 27d1e25 commit 0eccc25
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2015-2017 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.jupiter.engine.bridge;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

/**
* @since 5.0
*/
@ExtendWith(NumberResolver.class)
abstract class AbstractNonGenericTests {

@Test
void mA() {
BridgeMethodTests.sequence.add("mA()");
}

@Test
void test(Number value) {
BridgeMethodTests.sequence.add("A.test(Number)");
Assertions.assertEquals(42, value);
}

static class B extends AbstractNonGenericTests {

@Test
void mB() {
BridgeMethodTests.sequence.add("mB()");
}

@Test
void test(Byte value) {
BridgeMethodTests.sequence.add("B.test(Byte)");
Assertions.assertEquals(123, value.intValue());
}

}

static class C extends B {

@Test
void mC() {
BridgeMethodTests.sequence.add("mC()");
}

@Override
@Test
void test(Byte value) {
BridgeMethodTests.sequence.add("C.test(Byte)");
Assertions.assertEquals(123, value.intValue());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.junit.jupiter.engine.bridge;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -58,8 +59,8 @@ void childHasNoBridgeMethods() throws Exception {

@Test
void compareMethodExecutionSequenceOrder() {
String withoutBridgeMethods = execute(ChildWithoutBridgeMethods.class);
String withBridgeMethods = execute(ChildWithBridgeMethods.class);
String withoutBridgeMethods = execute(1, ChildWithoutBridgeMethods.class);
String withBridgeMethods = execute(1, ChildWithBridgeMethods.class);
assertEquals(withoutBridgeMethods, withBridgeMethods);
}

Expand All @@ -68,16 +69,35 @@ List<DynamicTest> ensureSingleTestMethodsExecute() {
return Arrays.asList(
dynamicTest("Byte", //
() -> assertEquals("[test(Byte) BEGIN, test(N), test(Byte) END.]", //
execute(ByteTestCase.class))),
execute(1, ByteTestCase.class))),
dynamicTest("Short", //
() -> assertEquals("[test(Short) BEGIN, test(N), test(Short) END.]", //
execute(ShortTestCase.class))));
execute(1, ShortTestCase.class))));
}

private String execute(Class<?> testClass) {
@Test
void inheritedNonGenericMethodsAreExecuted() {
String b = execute(4, AbstractNonGenericTests.B.class);
assertAll("Missing expected test(s) in sequence: " + b, //
() -> assertTrue(b.contains("A.test(Number)")), //
() -> assertTrue(b.contains("mA()")), //
() -> assertTrue(b.contains("mB()")), //
() -> assertTrue(b.contains("B.test(Byte)")) //
);
String c = execute(5, AbstractNonGenericTests.C.class);
assertAll("Missing expected test(s) in sequence: " + c, //
() -> assertTrue(c.contains("A.test(Number)")), //
() -> assertTrue(c.contains("mA()")), //
() -> assertTrue(c.contains("mB()")), //
() -> assertTrue(c.contains("mC()")), //
() -> assertTrue(c.contains("C.test(Byte)")) //
);
}

private String execute(int expectedTestFinishedCount, Class<?> testClass) {
sequence.clear();
ExecutionEventRecorder recorder = executeTestsForClass(testClass);
assertEquals(1, recorder.getTestFinishedCount());
assertEquals(expectedTestFinishedCount, recorder.getTestFinishedCount());
return sequence.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public Object resolve(ParameterContext parameterContext, ExtensionContext extens
throws ParameterResolutionException {

Class<?> type = parameterContext.getParameter().getType();
if (type == Number.class) {
return 42;
}
try {
return type.getMethod("valueOf", String.class).invoke(null, "123");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -723,7 +726,11 @@ private static boolean isMethodShadowedBy(Method upper, Method lower) {
if (lower.getParameterCount() != upper.getParameterCount()) {
return false;
}
// Check for method sub-signatures.
// trivial case: parameter types exactly match
if (Arrays.equals(lower.getParameterTypes(), upper.getParameterTypes())) {
return true;
}
// param count is equal, but types do not match exactly: check for method sub-signatures
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.2
for (int i = 0; i < lower.getParameterCount(); i++) {
Class<?> lowerType = lower.getParameterTypes()[i];
Expand All @@ -732,7 +739,27 @@ private static boolean isMethodShadowedBy(Method upper, Method lower) {
return false;
}
}
return true;
// lower is sub-signature of upper: check for generics in upper method
if (isGeneric(upper)) {
return true;
}
return false;
}

static boolean isGeneric(Method method) {
if (isGeneric(method.getGenericReturnType())) {
return true;
}
for (Type type : method.getGenericParameterTypes()) {
if (isGeneric(type)) {
return true;
}
}
return false;
}

private static boolean isGeneric(Type type) {
return type instanceof TypeVariable || type instanceof GenericArrayType;
}

private static <T extends AccessibleObject> T makeAccessible(T object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,12 +673,34 @@ void findMethodsIgnoresBridgeMethods() throws Exception {
assertTrue(methods.stream().filter(Method::isBridge).count() == 0);
}

@Test
void isGeneric() throws Exception {
for (Method method : Generic.class.getMethods()) {
assertTrue(ReflectionUtils.isGeneric(method));
}
for (Method method : PublicClass.class.getMethods()) {
assertFalse(ReflectionUtils.isGeneric(method));
}
}

private static void createDirectories(Path... paths) throws IOException {
for (Path path : paths) {
Files.createDirectory(path);
}
}

interface Generic<A, B, C extends A> {
A foo();

B foo(A a, B b);

C foo(C[][] cs);

<X> int foo(X x);

<X> X foo(int i);
}

class ClassWithSyntheticMethod {
Runnable foo = InterfaceWithStaticMethod::foo;
Runnable bar = StaticClass::staticMethod;
Expand Down

0 comments on commit 0eccc25

Please sign in to comment.