Skip to content

Commit

Permalink
Order wrappers without transferring between collections
Browse files Browse the repository at this point in the history
  • Loading branch information
mpkorstanje committed Feb 3, 2025
1 parent a96fcd6 commit 0112631
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

package org.junit.jupiter.engine.discovery;

import static java.util.stream.Collectors.toCollection;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;
Expand Down Expand Up @@ -60,12 +60,12 @@ protected void doWithMatchingDescriptor(Class<PARENT> parentTestDescriptorType,
protected void orderChildrenTestDescriptors(TestDescriptor parentTestDescriptor, Class<CHILD> matchingChildrenType,
Function<CHILD, WRAPPER> descriptorWrapperFactory, DescriptorWrapperOrderer descriptorWrapperOrderer) {

Set<WRAPPER> matchingDescriptorWrappers = parentTestDescriptor.getChildren()//
List<WRAPPER> matchingDescriptorWrappers = parentTestDescriptor.getChildren()//
.stream()//
.filter(matchingChildrenType::isInstance)//
.map(matchingChildrenType::cast)//
.map(descriptorWrapperFactory)//
.collect(toCollection(LinkedHashSet::new));
.collect(toList());

// If there are no children to order, abort early.
if (matchingDescriptorWrappers.isEmpty()) {
Expand Down Expand Up @@ -149,33 +149,33 @@ private boolean canOrderWrappers() {
return this.orderingAction != null;
}

private void orderWrappers(Set<WRAPPER> wrappers) {
private void orderWrappers(List<WRAPPER> wrappers) {
List<WRAPPER> orderedWrappers = new ArrayList<>(wrappers);
this.orderingAction.accept(orderedWrappers);
Set<Object> distinctOrderedWrappers = new LinkedHashSet<>(orderedWrappers);
Map<Object, Integer> distinctWrappersToIndex = distinctWrappersToIndex(orderedWrappers);

int difference = orderedWrappers.size() - wrappers.size();
int distinctDifference = distinctOrderedWrappers.size() - wrappers.size();
int distinctDifference = distinctWrappersToIndex.size() - wrappers.size();
if (difference > 0) { // difference >= distinctDifference
logDescriptorsAddedWarning(difference);
}
if (distinctDifference < 0) { // distinctDifference <= difference
logDescriptorsRemovedWarning(distinctDifference);
}

orderWrappersWithOrderFrom(wrappers, distinctOrderedWrappers);
wrappers.sort(comparing(wrapper -> distinctWrappersToIndex.getOrDefault(wrapper, -1)));
}

@SuppressWarnings("unchecked")
private void orderWrappersWithOrderFrom(Set<WRAPPER> wrappers, Set<Object> orderedWrappers) {
// Avoid ClassCastException if a misbehaving ordering action added a non-WRAPPER
for (Object wrapper : orderedWrappers) {
//noinspection SuspiciousMethodCalls
if (wrappers.remove(wrapper)) {
// guaranteed by wrappers.contains
wrappers.add((WRAPPER) wrapper);
private Map<Object, Integer> distinctWrappersToIndex(List<?> wrappers) {
Map<Object, Integer> toIndex = new HashMap<>();
for (int i = 0; i < wrappers.size(); i++) {
// Avoid ClassCastException if a misbehaving ordering action added a non-WRAPPER
Object wrapper = wrappers.get(i);
if (!toIndex.containsKey(wrapper)) {
toIndex.put(wrapper, i);
}
}
return toIndex;
}

private void logDescriptorsAddedWarning(int number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
import static org.junit.jupiter.engine.Constants.PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.LogRecord;
Expand Down Expand Up @@ -279,6 +283,15 @@ void misbehavingMethodOrdererThatAddsElements(@TrackLogRecords LogRecordListener
assertExpectedLogMessage(listener, expectedMessage);
}

@Test
void misbehavingMethodOrdererThatImpersonatesElements() {
Class<?> testClass = MisbehavingByImpersonatingTestCase.class;

executeTestsInParallel(testClass).assertStatistics(stats -> stats.succeeded(2));

assertThat(callSequence).containsExactlyInAnyOrder("test1()", "test2()");
}

@Test
void misbehavingMethodOrdererThatRemovesElements(@TrackLogRecords LogRecordListener listener) {
Class<?> testClass = MisbehavingByRemovingTestCase.class;
Expand Down Expand Up @@ -655,6 +668,24 @@ void test1() {
}
}

@SuppressWarnings("JUnitMalformedDeclaration")
@TestMethodOrder(MisbehavingByImpersonating.class)
static class MisbehavingByImpersonatingTestCase {

@BeforeEach
void trackInvocations(TestInfo testInfo) {
callSequence.add(testInfo.getDisplayName());
}

@Test
void test2() {
}

@Test
void test1() {
}
}

@SuppressWarnings("JUnitMalformedDeclaration")
@TestMethodOrder(MisbehavingByRemoving.class)
static class MisbehavingByRemovingTestCase {
Expand Down Expand Up @@ -721,6 +752,61 @@ static <T> T mockMethodDescriptor() {

}

static class MisbehavingByImpersonating implements MethodOrderer {

@Override
public void orderMethods(MethodOrdererContext context) {
context.getMethodDescriptors().sort(comparing(MethodDescriptor::getDisplayName));
MethodDescriptor method1 = context.getMethodDescriptors().get(0);
MethodDescriptor method2 = context.getMethodDescriptors().get(1);

context.getMethodDescriptors().set(0, createMethodDescriptorImpersonator(method1));
context.getMethodDescriptors().set(1, createMethodDescriptorImpersonator(method2));
}

@SuppressWarnings("unchecked")
static <T> T createMethodDescriptorImpersonator(MethodDescriptor method) {
MethodDescriptor stub = new MethodDescriptor() {
@Override
public Method getMethod() {
return null;
}

@Override
public String getDisplayName() {
return null;
}

@Override
public boolean isAnnotated(Class<? extends Annotation> annotationType) {
return false;
}

@Override
public <A extends Annotation> Optional<A> findAnnotation(Class<A> annotationType) {
return null;
}

@Override
public <A extends Annotation> List<A> findRepeatableAnnotations(Class<A> annotationType) {
return null;
}

@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@Override
public boolean equals(Object obj) {
return method.equals(obj);
}

@Override
public int hashCode() {
return method.hashCode();
}
};
return (T) stub;
}
}

static class MisbehavingByRemoving implements MethodOrderer {

@Override
Expand Down

0 comments on commit 0112631

Please sign in to comment.