[core] Convert more tests to JUnit5

This commit is contained in:
Andreas Dangel committed 2022-06-10 20:06:54 +02:00
1 parent 3e68d334df
commit 7ff4327ef2
6 files changed
+229 -242

No files matched your search

@@ -4,7 +4,7 @@
package net.sourceforge.pmd.document;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.BufferedWriter;
import java.io.File;
@@ -12,31 +12,33 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
public class DocumentFileTest {
class DocumentFileTest {
private static final String FILE_PATH = "psvm.java";
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@TempDir
private Path temporaryFolder;
private File temporaryFile;
@Before
public void setUpTemporaryFiles() throws IOException {
temporaryFile = temporaryFolder.newFile(FILE_PATH);
@BeforeEach
private void setUpTemporaryFiles() throws IOException {
temporaryFile = temporaryFolder.resolve(FILE_PATH).toFile();
Assertions.assertTrue(temporaryFile.createNewFile());
}
@Test
public void insertAtStartOfTheFileShouldSucceed() throws IOException {
void insertAtStartOfTheFileShouldSucceed() throws IOException {
writeContentToTemporaryFile("static void main(String[] args) {}");
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
@@ -50,7 +52,7 @@ public class DocumentFileTest {
}
@Test
public void shouldPreserveNewlines() throws IOException {
void shouldPreserveNewlines() throws IOException {
final String testFileContent = IOUtils.toString(
DocumentFileTest.class.getResource("ShouldPreserveNewlines.java"), StandardCharsets.UTF_8);
writeContentToTemporaryFile(testFileContent);
@@ -99,7 +101,7 @@ public class DocumentFileTest {
}
@Test
public void insertVariousTokensIntoTheFileShouldSucceed() throws IOException {
void insertVariousTokensIntoTheFileShouldSucceed() throws IOException {
writeContentToTemporaryFile("static void main(String[] args) {}");
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
@@ -114,7 +116,7 @@ public class DocumentFileTest {
}
@Test
public void insertAtTheEndOfTheFileShouldSucceed() throws IOException {
void insertAtTheEndOfTheFileShouldSucceed() throws IOException {
final String code = "public static void main(String[] args)";
writeContentToTemporaryFile(code);
@@ -129,7 +131,7 @@ public class DocumentFileTest {
}
@Test
public void removeTokenShouldSucceed() throws IOException {
void removeTokenShouldSucceed() throws IOException {
final String code = "public static void main(final String[] args) {}";
writeContentToTemporaryFile(code);
@@ -144,7 +146,7 @@ public class DocumentFileTest {
}
@Test
public void insertAndRemoveTokensShouldSucceed() throws IOException {
void insertAndRemoveTokensShouldSucceed() throws IOException {
final String code = "static void main(final String[] args) {}";
writeContentToTemporaryFile(code);
@@ -160,7 +162,7 @@ public class DocumentFileTest {
}
@Test
public void insertAndDeleteVariousTokensShouldSucceed() throws IOException {
void insertAndDeleteVariousTokensShouldSucceed() throws IOException {
final String code = "void main(String[] args) {}";
writeContentToTemporaryFile(code);
@@ -179,7 +181,7 @@ public class DocumentFileTest {
}
@Test
public void replaceATokenShouldSucceed() throws IOException {
void replaceATokenShouldSucceed() throws IOException {
final String code = "int main(String[] args) {}";
writeContentToTemporaryFile(code);
@@ -194,7 +196,7 @@ public class DocumentFileTest {
}
@Test
public void replaceVariousTokensShouldSucceed() throws IOException {
void replaceVariousTokensShouldSucceed() throws IOException {
final String code = "int main(String[] args) {}";
writeContentToTemporaryFile(code);
@@ -211,7 +213,7 @@ public class DocumentFileTest {
}
@Test
public void insertDeleteAndReplaceVariousTokensShouldSucceed() throws IOException {
void insertDeleteAndReplaceVariousTokensShouldSucceed() throws IOException {
final String code = "static int main(CharSequence[] args) {}";
writeContentToTemporaryFile(code);
@@ -230,7 +232,7 @@ public class DocumentFileTest {
}
@Test
public void lineToOffsetMappingWithLineFeedShouldSucceed() throws IOException {
void lineToOffsetMappingWithLineFeedShouldSucceed() throws IOException {
final String code = "public static int main(String[] args) {" + '\n'
+ "int var;" + '\n'
+ "}";
@@ -247,7 +249,7 @@ public class DocumentFileTest {
}
@Test
public void lineToOffsetMappingWithCarriageReturnFeedLineFeedShouldSucceed() throws IOException {
void lineToOffsetMappingWithCarriageReturnFeedLineFeedShouldSucceed() throws IOException {
final String code = "public static int main(String[] args) {" + "\r\n"
+ "int var;" + "\r\n"
+ "}";
@@ -264,7 +266,7 @@ public class DocumentFileTest {
}
@Test
public void lineToOffsetMappingWithMixedLineSeparatorsShouldSucceed() throws IOException {
void lineToOffsetMappingWithMixedLineSeparatorsShouldSucceed() throws IOException {
final String code = "public static int main(String[] args) {" + "\r\n"
+ "int var;" + "\n"
+ "}";
@@ -4,40 +4,42 @@
package net.sourceforge.pmd.document;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTest {
class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTest {
private static final String FILE_PATH = "psvm.java";
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
@TempDir
private Path temporaryFolder;
private File temporaryFile;
private DocumentOperationsApplierForNonOverlappingRegions applier;
@Before
public void setUpTemporaryFiles() throws IOException {
temporaryFile = temporaryFolder.newFile(FILE_PATH);
@BeforeEach
private void setUpTemporaryFiles() throws IOException {
temporaryFile = temporaryFolder.resolve(FILE_PATH).toFile();
Assertions.assertTrue(temporaryFile.createNewFile());
}
@Test
public void insertAtStartOfTheDocumentShouldSucceed() throws IOException {
void insertAtStartOfTheDocumentShouldSucceed() throws IOException {
writeContentToTemporaryFile("static void main(String[] args) {}");
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
@@ -87,7 +89,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
}
@Test
public void removeTokenShouldSucceed() throws IOException {
void removeTokenShouldSucceed() throws IOException {
writeContentToTemporaryFile("public static void main(String[] args) {}");
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
@@ -104,7 +106,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
}
@Test
public void insertAndRemoveTokensShouldSucceed() throws IOException {
void insertAndRemoveTokensShouldSucceed() throws IOException {
final String code = "static void main(final String[] args) {}";
writeContentToTemporaryFile(code);
@@ -123,7 +125,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
}
@Test
public void insertAndDeleteVariousTokensShouldSucceed() throws IOException {
void insertAndDeleteVariousTokensShouldSucceed() throws IOException {
final String code = "void main(String[] args) {}";
writeContentToTemporaryFile(code);
@@ -146,7 +148,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
}
@Test
public void replaceATokenShouldSucceed() throws IOException {
void replaceATokenShouldSucceed() throws IOException {
final String code = "int main(String[] args) {}";
writeContentToTemporaryFile(code);
@@ -165,7 +167,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
}
@Test
public void replaceVariousTokensShouldSucceed() throws IOException {
void replaceVariousTokensShouldSucceed() throws IOException {
final String code = "int main(String[] args) {}";
writeContentToTemporaryFile(code);
@@ -197,7 +199,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
}
@Test
public void insertDeleteAndReplaceVariousTokensShouldSucceed() throws IOException {
void insertDeleteAndReplaceVariousTokensShouldSucceed() throws IOException {
final String code = "static int main(CharSequence[] args) {}";
writeContentToTemporaryFile(code);
File diff suppressed because it is too large. Load diff
@@ -7,40 +7,36 @@ package net.sourceforge.pmd.lang.ast.impl;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.node;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.root;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.tree;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import net.sourceforge.pmd.lang.ast.DummyNode;
import net.sourceforge.pmd.lang.ast.DummyNode.DummyRootNode;
import net.sourceforge.pmd.lang.ast.Node;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
/**
* Unit test for {@link AbstractNode}.
*/
@RunWith(JUnitParamsRunner.class)
public class AbstractNodeTest {
class AbstractNodeTest {
private static final int NUM_CHILDREN = 3;
private static final int NUM_GRAND_CHILDREN = 3;
// Note that in order to successfully run JUnitParams, we need to explicitly use `Integer` instead of `int`
private Integer[] childrenIndexes() {
static Integer[] childrenIndexes() {
return getIntRange(NUM_CHILDREN);
}
private Integer[] grandChildrenIndexes() {
static Integer[] grandChildrenIndexes() {
return getIntRange(NUM_GRAND_CHILDREN);
}
@@ -52,7 +48,7 @@ public class AbstractNodeTest {
return childIndexes;
}
public Object childrenAndGrandChildrenIndexes() {
static Object childrenAndGrandChildrenIndexes() {
final Integer[] childrenIndexes = childrenIndexes();
final Integer[] grandChildrenIndexes = grandChildrenIndexes();
final Object[] indexes = new Object[childrenIndexes.length * grandChildrenIndexes.length];
@@ -67,7 +63,7 @@ public class AbstractNodeTest {
private DummyRootNode rootNode;
@Before
@BeforeEach
public void setUpSampleNodeTree() {
rootNode = tree(
() -> {
@@ -89,9 +85,9 @@ public class AbstractNodeTest {
/**
* Explicitly tests the {@code remove} method, and implicitly the {@code removeChildAtIndex} method
*/
@Test
@Parameters(method = "childrenIndexes")
public void testRemoveChildOfRootNode(final int childIndex) {
@ParameterizedTest
@MethodSource("childrenIndexes")
void testRemoveChildOfRootNode(final int childIndex) {
final DummyNode child = rootNode.getChild(childIndex);
final List<? extends DummyNode> grandChildren = child.children().toList();
@@ -109,7 +105,7 @@ public class AbstractNodeTest {
}
@Test
public void testPrevNextSiblings() {
void testPrevNextSiblings() {
DummyRootNode root = tree(() -> root(node(), node()));
assertNull(root.getNextSibling());
@@ -129,7 +125,7 @@ public class AbstractNodeTest {
* This is a border case as the root node does not have any parent.
*/
@Test
public void testRemoveRootNode() {
void testRemoveRootNode() {
// Check that the root node has the expected properties
final List<? extends DummyNode> children = rootNode.children().toList();
@@ -149,9 +145,9 @@ public class AbstractNodeTest {
* Explicitly tests the {@code remove} method, and implicitly the {@code removeChildAtIndex} method.
* These are border cases as grandchildren nodes do not have any child.
*/
@Test
@Parameters(method = "childrenAndGrandChildrenIndexes")
public void testRemoveGrandChildNode(final int childIndex, final int grandChildIndex) {
@ParameterizedTest
@MethodSource("childrenAndGrandChildrenIndexes")
void testRemoveGrandChildNode(final int childIndex, final int grandChildIndex) {
final DummyNode child = rootNode.getChild(childIndex);
final DummyNode grandChild = child.getChild(grandChildIndex);
@@ -167,9 +163,9 @@ public class AbstractNodeTest {
/**
* Explicitly tests the {@code removeChildAtIndex} method.
*/
@Test
@Parameters(method = "childrenIndexes")
public void testRemoveRootNodeChildAtIndex(final int childIndex) {
@ParameterizedTest
@MethodSource("childrenIndexes")
void testRemoveRootNodeChildAtIndex(final int childIndex) {
final List<? extends DummyNode> originalChildren = rootNode.children().toList();
// Do the actual removal
@@ -195,12 +191,12 @@ public class AbstractNodeTest {
* Test that invalid indexes cases are handled without exception.
*/
@Test
public void testRemoveChildAtIndexWithInvalidIndex() {
void testRemoveChildAtIndexWithInvalidIndex() {
try {
rootNode.removeChildAtIndex(-1);
rootNode.removeChildAtIndex(rootNode.getNumChildren());
} catch (final Exception e) {
fail("No exception was expected.");
Assertions.fail("No exception was expected.");
}
}
@@ -208,9 +204,9 @@ public class AbstractNodeTest {
* Explicitly tests the {@code removeChildAtIndex} method.
* This is a border case as the method invocation should do nothing.
*/
@Test
@Parameters(method = "grandChildrenIndexes")
public void testRemoveChildAtIndexOnNodeWithNoChildren(final int grandChildIndex) {
@ParameterizedTest
@MethodSource("grandChildrenIndexes")
void testRemoveChildAtIndexOnNodeWithNoChildren(final int grandChildIndex) {
// grandChild does not have any child
final DummyNode grandChild = rootNode.getChild(grandChildIndex).getChild(grandChildIndex);
@@ -4,12 +4,14 @@
package net.sourceforge.pmd.lang.ast.internal;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertSame;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.node;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.nodeB;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.root;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.tree;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Arrays;
@@ -19,13 +21,9 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import net.sourceforge.pmd.lang.ast.DummyNode;
import net.sourceforge.pmd.lang.ast.DummyNode.DummyNodeTypeB;
@@ -36,8 +34,7 @@ import net.sourceforge.pmd.lang.ast.NodeStream;
* Asserts invariants independent of the NodeStream implementation. Error
* messages are not great but coverage is.
*/
@RunWith(Parameterized.class)
public class NodeStreamBlanketTest<T extends Node> {
class NodeStreamBlanketTest<T extends Node> {
private static final List<Node> ASTS = Arrays.asList(
tree(
@@ -70,17 +67,9 @@ public class NodeStreamBlanketTest<T extends Node> {
)
);
@Rule
public ExpectedException expect = ExpectedException.none();
private final NodeStream<T> stream;
public NodeStreamBlanketTest(NodeStream<T> stream) {
this.stream = stream;
}
@Test
public void testToListConsistency() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testToListConsistency(NodeStream<T> stream) {
List<T> toList = stream.toList();
List<T> collected = stream.collect(Collectors.toList());
List<T> fromStream = stream.toStream().collect(Collectors.toList());
@@ -91,16 +80,18 @@ public class NodeStreamBlanketTest<T extends Node> {
assertEquals(toList, cached);
}
@Test
public void testToListSize() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testToListSize(NodeStream<T> stream) {
List<T> toList = stream.toList();
assertEquals(toList.size(), stream.count());
}
@Test
public void testLast() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testLast(NodeStream<T> stream) {
assertImplication(
stream,
prop("nonEmpty", NodeStream::nonEmpty),
@@ -108,8 +99,9 @@ public class NodeStreamBlanketTest<T extends Node> {
);
}
@Test
public void testFirst() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testFirst(NodeStream<T> stream) {
assertImplication(
stream,
prop("nonEmpty", NodeStream::nonEmpty),
@@ -118,8 +110,9 @@ public class NodeStreamBlanketTest<T extends Node> {
}
@Test
public void testDrop() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testDrop(NodeStream<T> stream) {
assertImplication(
stream,
prop("nonEmpty", NodeStream::nonEmpty),
@@ -129,8 +122,9 @@ public class NodeStreamBlanketTest<T extends Node> {
);
}
@Test
public void testDropLast() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testDropLast(NodeStream<T> stream) {
assertImplication(
stream,
prop("nonEmpty", NodeStream::nonEmpty),
@@ -140,8 +134,9 @@ public class NodeStreamBlanketTest<T extends Node> {
);
}
@Test
public void testDropMoreThan1() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testDropMoreThan1(NodeStream<T> stream) {
assertImplication(
stream,
prop("count() > 1", it -> it.count() > 1),
@@ -150,8 +145,9 @@ public class NodeStreamBlanketTest<T extends Node> {
);
}
@Test
public void testTake() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testTake(NodeStream<T> stream) {
assertImplication(
stream,
prop("nonEmpty", NodeStream::nonEmpty),
@@ -161,33 +157,35 @@ public class NodeStreamBlanketTest<T extends Node> {
);
}
@Test
public void testGet() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testGet(NodeStream<T> stream) {
for (int i = 0; i < 100; i++) {
assertSame("stream.get(i) == stream.drop(i).first()", stream.get(i), stream.drop(i).first());
assertSame(stream.get(i), stream.drop(i).first(), "stream.get(i) == stream.drop(i).first()");
}
}
@Test
public void testGetNegative() {
expect.expect(IllegalArgumentException.class);
stream.get(-1);
@ParameterizedTest
@MethodSource("primeNumbers")
void testGetNegative(NodeStream<T> stream) {
assertThrows(IllegalArgumentException.class, () -> stream.get(-1));
}
@Test
public void testDropNegative() {
expect.expect(IllegalArgumentException.class);
stream.drop(-1);
@ParameterizedTest
@MethodSource("primeNumbers")
void testDropNegative(NodeStream<T> stream) {
assertThrows(IllegalArgumentException.class, () -> stream.drop(-1));
}
@Test
public void testTakeNegative() {
expect.expect(IllegalArgumentException.class);
stream.take(-1);
@ParameterizedTest
@MethodSource("primeNumbers")
void testTakeNegative(NodeStream<T> stream) {
assertThrows(IllegalArgumentException.class, () -> stream.take(-1));
}
@Test
public void testEmpty() {
@ParameterizedTest
@MethodSource("primeNumbers")
void testEmpty(NodeStream<T> stream) {
assertEquivalence(
stream,
prop("isEmpty", NodeStream::isEmpty),
@@ -203,8 +201,7 @@ public class NodeStreamBlanketTest<T extends Node> {
);
}
@Parameterized.Parameters(name = "{index} On {0}")
public static Collection<?> primeNumbers() {
static Collection<?> primeNumbers() {
return ASTS.stream().flatMap(
root -> Stream.of(
root.asStream(),
@@ -251,10 +248,10 @@ public class NodeStreamBlanketTest<T extends Node> {
for (Prop<? super T> prop1 : properties) {
for (Prop<? super T> prop2 : properties) {
boolean p1 = prop1.test(input);
Assert.assertEquals(
assertEquals(
p1, prop2.test(input),
"Expected (" + prop1.description + ") === (" + prop2.description
+ "), but the LHS was " + p1 + " and the RHS was " + !p1,
p1, prop2.test(input)
+ "), but the LHS was " + p1 + " and the RHS was " + !p1
);
}
}
@@ -262,13 +259,13 @@ public class NodeStreamBlanketTest<T extends Node> {
@SafeVarargs
private static <T> void assertImplication(T input, Prop<? super T> precond, Prop<? super T>... properties) {
Assume.assumeTrue(precond.test(input));
Assumptions.assumeTrue(precond.test(input));
for (Prop<? super T> prop2 : properties) {
Assert.assertTrue(
assertTrue(
prop2.test(input),
"Expected (" + precond.description + ") to entail (" + prop2.description
+ "), but the latter was false",
prop2.test(input)
+ "), but the latter was false"
);
}
}
@@ -11,18 +11,18 @@ import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.pathsOf;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.root;
import static net.sourceforge.pmd.lang.ast.impl.DummyTreeUtil.tree;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.Optional;
import org.apache.commons.lang3.mutable.MutableInt;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import net.sourceforge.pmd.lang.ast.DummyNode;
import net.sourceforge.pmd.lang.ast.DummyNode.DummyNodeTypeB;
@@ -34,7 +34,7 @@ import net.sourceforge.pmd.lang.ast.NodeStream.DescendantNodeStream;
/**
* @author Clément Fournier
*/
public class NodeStreamTest {
class NodeStreamTest {
private final DummyNode tree1 = tree(
@@ -70,7 +70,7 @@ public class NodeStreamTest {
@Test
public void testStreamConstructionIsNullSafe() {
void testStreamConstructionIsNullSafe() {
assertTrue(NodeStream.of((Node) null).isEmpty());
assertThat(NodeStream.of(null, null, tree1).count(), equalTo(1));
assertThat(NodeStream.fromIterable(Arrays.asList(tree1, null, null)).count(), equalTo(1));
@@ -79,26 +79,26 @@ public class NodeStreamTest {
@Test
public void testMapIsNullSafe() {
void testMapIsNullSafe() {
assertTrue(tree1.descendantsOrSelf().map(n -> null).isEmpty());
}
@Test
public void testFlatMapIsNullSafe() {
void testFlatMapIsNullSafe() {
assertTrue(tree1.descendantsOrSelf().flatMap(n -> null).isEmpty());
}
@Test
public void testChildrenStream() {
void testChildrenStream() {
assertThat(pathsOf(tree1.children()), contains("0", "1"));
assertThat(pathsOf(tree1.asStream().children()), contains("0", "1"));
}
@Test
public void testChildrenEagerEvaluation() {
void testChildrenEagerEvaluation() {
NodeStream<? extends Node> children = tree1.children();
assertEquals(AxisStream.ChildrenStream.class, children.getClass());
NodeStream<Node> children1 = children.children();
@@ -108,27 +108,27 @@ public class NodeStreamTest {
@Test
public void testDescendantStream() {
void testDescendantStream() {
assertThat(pathsOf(tree1.descendants()), contains("0", "00", "01", "010", "011", "0110", "012", "013", "1"));
assertThat(pathsOf(tree1.asStream().descendants()), contains("0", "00", "01", "010", "011", "0110", "012", "013", "1"));
}
@Test
public void testSingletonStream() {
void testSingletonStream() {
assertThat(pathsOf(tree1.asStream()), contains(""));
assertThat(pathsOf(NodeStream.of(tree1)), contains(""));
}
@Test
public void testDescendantOrSelfStream() {
void testDescendantOrSelfStream() {
assertThat(pathsOf(tree1.descendantsOrSelf()), contains("", "0", "00", "01", "010", "011", "0110", "012", "013", "1"));
assertThat(pathsOf(NodeStream.of(tree1).descendantsOrSelf()), contains("", "0", "00", "01", "010", "011", "0110", "012", "013", "1"));
assertThat(pathsOf(followPath(tree1, "0110").descendantsOrSelf()), contains("0110")); // with a leaf node
}
@Test
public void testAncestors() {
void testAncestors() {
// 010
Node node = tree1.children().children().children().first();
assertEquals("010", node.getImage());
@@ -142,7 +142,7 @@ public class NodeStreamTest {
}
@Test
public void testAncestorsFiltered() {
void testAncestorsFiltered() {
// 0110
Node node = tree1.children().children().children().children().first();
assertEquals("0110", node.getImage());
@@ -152,7 +152,7 @@ public class NodeStreamTest {
}
@Test
public void testAncestorsFilteredDrop() {
void testAncestorsFilteredDrop() {
// 0110
Node node = tree1.children().children().children().children().first();
assertEquals("0110", node.getImage());
@@ -163,7 +163,7 @@ public class NodeStreamTest {
@Test
public void testFollowingSiblings() {
void testFollowingSiblings() {
assertThat(pathsOf(followPath(tree2, "2").asStream().followingSiblings()), contains("3"));
assertThat(pathsOf(followPath(tree2, "0").asStream().followingSiblings()), contains("1", "2", "3"));
assertTrue(pathsOf(followPath(tree2, "3").asStream().followingSiblings()).isEmpty());
@@ -171,47 +171,47 @@ public class NodeStreamTest {
@Test
public void testPrecedingSiblings() {
void testPrecedingSiblings() {
assertThat(pathsOf(followPath(tree2, "2").asStream().precedingSiblings()), contains("0", "1"));
assertThat(pathsOf(followPath(tree2, "3").asStream().precedingSiblings()), contains("0", "1", "2"));
assertTrue(pathsOf(followPath(tree2, "0").asStream().precedingSiblings()).isEmpty());
}
@Test
public void testRootSiblings() {
void testRootSiblings() {
assertTrue(tree2.asStream().precedingSiblings().isEmpty());
assertTrue(tree2.asStream().followingSiblings().isEmpty());
}
@Test
public void testAncestorStream() {
void testAncestorStream() {
assertThat(pathsOf(followPath(tree1, "01").ancestors()), contains("0", ""));
assertThat(pathsOf(followPath(tree1, "01").asStream().ancestors()), contains("0", ""));
}
@Test
public void testParentStream() {
void testParentStream() {
assertThat(pathsOf(followPath(tree1, "01").asStream().parents()), contains("0"));
}
@Test
public void testAncestorStreamUnion() {
void testAncestorStreamUnion() {
assertThat(pathsOf(NodeStream.union(followPath(tree1, "01").ancestors(),
tree2.children().ancestors())), contains("0", "", "", "", "", ""));
}
@Test
public void testDistinct() {
void testDistinct() {
assertThat(pathsOf(NodeStream.union(followPath(tree1, "01").ancestors(),
tree2.children().ancestors()).distinct()), contains("0", "", "")); // roots of both trees
}
@Test
public void testGet() {
void testGet() {
// ("0", "00", "01", "010", "011", "0110", "012", "013", "1")
DescendantNodeStream<DummyNode> stream = tree1.descendants();
@@ -224,7 +224,7 @@ public class NodeStreamTest {
}
@Test
public void testNodeStreamsCanBeIteratedSeveralTimes() {
void testNodeStreamsCanBeIteratedSeveralTimes() {
DescendantNodeStream<DummyNode> stream = tree1.descendants();
assertThat(stream.count(), equalTo(9));
@@ -237,7 +237,7 @@ public class NodeStreamTest {
@Test
public void testNodeStreamPipelineIsLazy() {
void testNodeStreamPipelineIsLazy() {
MutableInt numEvals = new MutableInt();
@@ -251,7 +251,7 @@ public class NodeStreamTest {
@Test
public void testForkJoinUpstreamPipelineIsExecutedAtMostOnce() {
void testForkJoinUpstreamPipelineIsExecutedAtMostOnce() {
MutableInt numEvals = new MutableInt();
NodeStream<Node> stream =
@@ -275,7 +275,7 @@ public class NodeStreamTest {
@Test
public void testCachedStreamUpstreamPipelineIsExecutedAtMostOnce() {
void testCachedStreamUpstreamPipelineIsExecutedAtMostOnce() {
MutableInt upstreamEvals = new MutableInt();
MutableInt downstreamEvals = new MutableInt();
@@ -304,7 +304,7 @@ public class NodeStreamTest {
@Test
public void testUnionIsLazy() {
void testUnionIsLazy() {
MutableInt tree1Evals = new MutableInt();
MutableInt tree2Evals = new MutableInt();
@@ -323,7 +323,7 @@ public class NodeStreamTest {
@Test
public void testSomeOperationsAreLazy() {
void testSomeOperationsAreLazy() {
MutableInt tree1Evals = new MutableInt();
@@ -365,7 +365,7 @@ public class NodeStreamTest {
@Test
public void testFollowingSiblingsNonEmpty() {
void testFollowingSiblingsNonEmpty() {
DummyNode node = followPath(tree1, "012");
NodeStream<Node> nodes = node.asStream().followingSiblings();
@@ -375,7 +375,7 @@ public class NodeStreamTest {
}
@Test
public void testPrecedingSiblingsNonEmpty() {
void testPrecedingSiblingsNonEmpty() {
DummyNode node = followPath(tree1, "011");
NodeStream<Node> nodes = node.asStream().precedingSiblings();
@@ -385,7 +385,7 @@ public class NodeStreamTest {
}
@Test
public void testPrecedingSiblingsDrop() {
void testPrecedingSiblingsDrop() {
DummyNode node = followPath(tree1, "012");
NodeStream<Node> nodes = node.asStream().precedingSiblings().drop(1);
@@ -394,7 +394,7 @@ public class NodeStreamTest {
}
@Test
public void testFollowingSiblingsDrop() {
void testFollowingSiblingsDrop() {
DummyNode node = followPath(tree1, "011");
NodeStream<Node> nodes = node.asStream().followingSiblings().drop(1);