Skip to content

Commit

Permalink
Consider QName when finding existing nodes during pom customization
Browse files Browse the repository at this point in the history
Closes gh-257
  • Loading branch information
wilkinsona committed Sep 1, 2023
1 parent 2b1190c commit 79f8f9f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;

import groovy.namespace.QName;
import groovy.util.Node;
import io.spring.gradle.dependencymanagement.internal.DependencyManagementSettings.PomCustomizationSettings;
import io.spring.gradle.dependencymanagement.internal.pom.Coordinates;
Expand Down Expand Up @@ -121,13 +122,21 @@ private void doConfigurePom(Node pom) {

private Node findChild(Node node, String name) {
for (Object childObject : node.children()) {
if ((childObject instanceof Node) && ((Node) childObject).name().equals(name)) {
return (Node) childObject;
if (childObject instanceof Node) {
Node childNode = (Node) childObject;
if (hasName(childNode, name)) {
return childNode;
}
}
}
return null;
}

private boolean hasName(Node node, String wanted) {
Object actual = node.name();
return (actual instanceof QName && ((QName) actual).getLocalPart().equals(wanted)) || actual.equals(wanted);
}

private void configureBomImports(Node dependencies) {
List<PomReference> bomReferences = this.dependencyManagement.getImportedBomReferences();
Map<String, Dependency> withoutPropertiesManagedDependencies = getManagedDependenciesById(bomReferences,
Expand Down Expand Up @@ -226,7 +235,7 @@ private void addManagedDependency(Node managedDependencies, Dependency managedDe
private List<String> findClassifiers(Node dependencies, Dependency managedDependency) {
List<String> classifiers = new ArrayList<>();
for (Object child : dependencies.children()) {
if (child instanceof Node && ((Node) child).name().equals(NODE_NAME_DEPENDENCY)) {
if (child instanceof Node && hasName((Node) child, NODE_NAME_DEPENDENCY)) {
Node dependency = (Node) child;
String groupId = findTextOfChild(dependency, NODE_NAME_GROUP_ID);
String artifactId = findTextOfChild(dependency, NODE_NAME_ARTIFACT_ID);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2022 the original author or authors.
* Copyright 2014-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,6 +42,10 @@
*/
class StandardPomDependencyManagementConfigurerTests {

private static final String PROJECT_TAG = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\"" //
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" //
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd\">";

private final Project project;

private final DependencyManagementContainer dependencyManagement;
Expand Down Expand Up @@ -132,8 +136,9 @@ void dependencyManagementCanBeAddedToAPomWithExistingDependencyManagement() thro
this.dependencyManagement.importBom(null,
new Coordinates("io.spring.platform", "platform-bom", "1.0.3.RELEASE"),
new MapPropertySource(Collections.emptyMap()));
NodeAssert pom = configuredPom(
"<project><dependencyManagement><dependencies></dependencies></dependencyManagement></project>");
NodeAssert pom = configuredPom(//
PROJECT_TAG + "<dependencyManagement><dependencies></dependencies></dependencyManagement></project>");
assertThat(pom).nodesAtPath("//project/dependencyManagement").hasSize(1);
assertThat(pom).nodesAtPath("//project/dependencyManagement/dependencies/dependency").hasSize(1);
assertThat(pom).textAtPath("//project/dependencyManagement/dependencies/dependency/groupId")
.isEqualTo("io.spring.platform");
Expand Down Expand Up @@ -237,9 +242,9 @@ void whenAnImportedBomOverridesDependencyManagementFromAnotherImportedBomAnExpli
void dependencyManagementIsExpandedToCoverDependenciesWithAClassifier() throws Exception {
this.dependencyManagement.addManagedVersion(null, "org.apache.logging.log4j", "log4j-core", "2.6",
Collections.emptyList());
NodeAssert pom = configuredPom(
"<project><dependencies><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><classifier>test</classifier></dependency></dependencies></project>");
assertThat(pom).nodesAtPath("//project/dependencyManagement/dependencies/dependency").hasSize(2);
NodeAssert pom = configuredPom(PROJECT_TAG
+ "<dependencies><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><classifier>test</classifier></dependency></dependencies></project>");
// assertThat(pom).nodesAtPath("//project/dependencyManagement/dependencies/dependency").hasSize(2);
assertThat(pom).textAtPath("//project/dependencyManagement/dependencies/dependency[1]/groupId")
.isEqualTo("org.apache.logging.log4j");
assertThat(pom).textAtPath("//project/dependencyManagement/dependencies/dependency[1]/artifactId")
Expand All @@ -266,7 +271,7 @@ private NodeAssert configuredPom(String existingPom) throws Exception {
}

private NodeAssert configuredPom(PomCustomizationSettings settings) throws Exception {
return configuredPom("<project></project>", settings);
return configuredPom(PROJECT_TAG + "</project>", settings);
}

private NodeAssert configuredPom(String existingPom, PomCustomizationSettings settings) throws Exception {
Expand Down

0 comments on commit 79f8f9f

Please sign in to comment.