This is an automated email from the ASF dual-hosted git repository. elharo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
The following commit(s) were added to refs/heads/master by this push: new 8738c47f Fixing flaky tests in TestTreeMojo (#451) 8738c47f is described below commit 8738c47fce6510040e5aae908416a66e5af3aceb Author: Mihir Gune <mihirg...@gmail.com> AuthorDate: Sat Nov 9 17:07:31 2024 -0600 Fixing flaky tests in TestTreeMojo (#451) * Fixing flaky tests in TestTreeMojo.java * Refactor imports: replace wildcards with specific class imports --- .../plugins/dependency/tree/TestTreeMojo.java | 38 +++++++++++++++------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java b/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java index b1c75678..344bbccc 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/tree/TestTreeMojo.java @@ -32,8 +32,11 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; @@ -101,8 +104,15 @@ public class TestTreeMojo extends AbstractDependencyMojoTestCase { DependencyNode rootNode = mojo.getDependencyGraph(); assertNodeEquals("testGroupId:project:jar:1.0:compile", rootNode); assertEquals(2, rootNode.getChildren().size()); - assertChildNodeEquals("testGroupId:release:jar:1.0:compile", rootNode, 0); - assertChildNodeEquals("testGroupId:snapshot:jar:2.0-SNAPSHOT:compile", rootNode, 1); + + List<String> actualNodes = Arrays.asList( + createArtifactCoordinateString(rootNode.getChildren().get(0)), + createArtifactCoordinateString(rootNode.getChildren().get(1))); + List<String> expectedNodes = + Arrays.asList("testGroupId:release:jar:1.0:compile", "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile"); + + assertTrue(expectedNodes.containsAll(actualNodes)); + ; } /** @@ -194,8 +204,13 @@ public class TestTreeMojo extends AbstractDependencyMojoTestCase { JsonArray children = root.getJsonArray("children"); assertEquals(children.size(), 2); - JsonObject child0 = children.getJsonObject(0); + List<JsonObject> sortedChildren = children.stream() + .map(JsonObject.class::cast) + .sorted(Comparator.comparing(child -> child.getString("artifactId"))) + .collect(Collectors.toList()); + // Now that children are sorted, we can assert their values in a fixed order + JsonObject child0 = sortedChildren.get(0); assertEquals(child0.getString("groupId"), "testGroupId"); assertEquals(child0.getString("artifactId"), "release"); assertEquals(child0.getString("version"), "1.0"); @@ -204,8 +219,7 @@ public class TestTreeMojo extends AbstractDependencyMojoTestCase { assertEquals(child0.getString("classifier"), ""); assertEquals(child0.getString("optional"), "false"); - JsonObject child1 = children.getJsonObject(1); - + JsonObject child1 = sortedChildren.get(1); assertEquals(child1.getString("groupId"), "testGroupId"); assertEquals(child1.getString("artifactId"), "snapshot"); assertEquals(child1.getString("version"), "2.0-SNAPSHOT"); @@ -271,12 +285,6 @@ public class TestTreeMojo extends AbstractDependencyMojoTestCase { // private methods -------------------------------------------------------- - private void assertChildNodeEquals(String expectedNode, DependencyNode actualParentNode, int actualChildIndex) { - DependencyNode actualNode = actualParentNode.getChildren().get(actualChildIndex); - - assertNodeEquals(expectedNode, actualNode); - } - private void assertNodeEquals(String expectedNode, DependencyNode actualNode) { String[] tokens = expectedNode.split(":"); @@ -298,4 +306,12 @@ public class TestTreeMojo extends AbstractDependencyMojoTestCase { assertEquals("version", expectedVersion, actualArtifact.getVersion()); assertEquals("scope", expectedScope, actualArtifact.getScope()); } + + private String createArtifactCoordinateString(DependencyNode actualNode) { + return actualNode.getArtifact().getGroupId() + ":" + + actualNode.getArtifact().getArtifactId() + ":" + + actualNode.getArtifact().getType() + ":" + + actualNode.getArtifact().getVersion() + ":" + + actualNode.getArtifact().getScope(); + } }