Skip to content

Commit

Permalink
Fix stack overflow error in assertIterableEquals
Browse files Browse the repository at this point in the history
Prior to this commit an iterable of iterables (here instances
of java.nio.file.Path) passed to method `assertIterableEquals`
in class `Assertions` of the JUnit Jupiter API yielded a
`StackOverflowError`.

This commit prevent the eternal while-loop by leveraging the
`equals()`-implementation of the non-null expected element.

Fixes #2157
  • Loading branch information
sormuras committed Jan 20, 2020
1 parent 12ee207 commit 530bb44
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ private static void assertIterableEquals(Iterable<?> expected, Iterable<?> actua
if (expectedElement == actualElement) {
continue;
}
// Prevent stack overflow error.
// See https://github.com/junit-team/junit5/issues/2157 for details.
if (expectedElement != null && expectedElement.equals(actualElement)) {
continue;
}

indexes.addLast(processed - 1);
assertIterableElementsEqual(expectedElement, actualElement, indexes, messageOrSupplier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals;
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith;
import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.IterableFactory.listOf;
import static org.junit.jupiter.api.IterableFactory.setOf;

import java.nio.file.Path;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -439,4 +441,12 @@ void assertIterableEqualsDifferentNestedIterablesAndMessageSupplier() {
}
}

@Test
// https://github.com/junit-team/junit5/issues/2157
void assertIterableEqualsWithListOfPath() {
var expected = listOf(Path.of("1"));
var actual = listOf(Path.of("1"));
assertDoesNotThrow(() -> assertIterableEquals(expected, actual));
}

}

0 comments on commit 530bb44

Please sign in to comment.