Skip to content

Commit

Permalink
Fix exception when ClassSelectors contain interfaces (#2)
Browse files Browse the repository at this point in the history
+ Avoid computing the entire class hierarchy unnecessarily

Fixes #1.
  • Loading branch information
marcphilipp authored Jul 9, 2021
1 parent c7a4f5f commit 7350608
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

package org.junit.support.testng.engine;

import static java.util.Spliterators.spliteratorUnknownSize;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.testng.ITestNGMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -74,16 +77,27 @@ static Stream<String> collectGroups(Class<?> testClass) {
}

private static Stream<Test> collectTestAnnotations(Class<?> testClass) {
return getClassHierarchy(testClass).stream() //
return getClassHierarchy(testClass) //
.map(clazz -> clazz.getAnnotation(Test.class)) //
.filter(Objects::nonNull);
}

private static List<Class<?>> getClassHierarchy(Class<?> testClass) {
List<Class<?>> result = new ArrayList<>();
for (Class<?> clazz = testClass; clazz != Object.class; clazz = clazz.getSuperclass()) {
result.add(clazz);
}
return result;
private static Stream<Class<?>> getClassHierarchy(Class<?> testClass) {
Iterator<Class<?>> iterator = new Iterator<Class<?>>() {
Class<?> next = testClass;

@Override
public boolean hasNext() {
return next != Object.class && next != null;
}

@Override
public Class<?> next() {
Class<?> result = next;
next = next.getSuperclass();
return result;
}
};
return StreamSupport.stream(spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,27 @@ void ignoresIgnoredTests() {
TestDescriptor methodDescriptor = getOnlyElement(classDescriptor.getChildren());
assertThat(methodDescriptor.getDisplayName()).isEqualTo("test");
}

@ParameterizedTest
@ValueSource(classes = { InterfaceTestCase.class, AbstractTestCase.class, RecordTestCase.class,
EnumTestCase.class })
void doesNotThrowExceptionWhenNonExecutableTypeOfClassIsSelected(Class<?> testClass) {
var request = request().selectors(selectClass(testClass)).build();

var rootDescriptor = testEngine.discover(request, engineId);

assertThat(rootDescriptor.getChildren()).isEmpty();
}

interface InterfaceTestCase {
}

static abstract class AbstractTestCase {
}

record RecordTestCase() {
}

enum EnumTestCase {
}
}

0 comments on commit 7350608

Please sign in to comment.