Skip to content

Commit

Permalink
Fixing flaky tests in TestTreeMojo (#451)
Browse files Browse the repository at this point in the history
* Fixing flaky tests in TestTreeMojo.java

* Refactor imports: replace wildcards with specific class imports
  • Loading branch information
mihirgune authored Nov 9, 2024
1 parent 192b2c0 commit 8738c47
Showing 1 changed file with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<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));
;
}

/**
Expand Down Expand Up @@ -194,8 +204,13 @@ public void testTreeJsonParsing() throws Exception {
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");
Expand All @@ -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");
Expand Down Expand Up @@ -271,12 +285,6 @@ private boolean findString(List<String> 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(":");

Expand All @@ -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();
}
}

0 comments on commit 8738c47

Please sign in to comment.