From 8738c47fce6510040e5aae908416a66e5af3aceb Mon Sep 17 00:00:00 2001 From: Mihir Gune Date: Sat, 9 Nov 2024 17:07:31 -0600 Subject: [PATCH] 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 b1c756786..344bbccc1 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.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 void testTreeTestEnvironment() throws Exception { 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 actualNodes = Arrays.asList( + createArtifactCoordinateString(rootNode.getChildren().get(0)), + createArtifactCoordinateString(rootNode.getChildren().get(1))); + List 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 void testTreeJsonParsing() throws Exception { JsonArray children = root.getJsonArray("children"); assertEquals(children.size(), 2); - JsonObject child0 = children.getJsonObject(0); + List 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 void testTreeJsonParsing() throws Exception { 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 @@ private boolean findString(List contents, String str) { // 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 @@ private void assertNodeEquals( 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(); + } }