From 79f8f9fb33fe0f477752b0f07f9a8ab8e14818c3 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 1 Sep 2023 10:11:53 +0100 Subject: [PATCH] Consider QName when finding existing nodes during pom customization Closes gh-257 --- ...dardPomDependencyManagementConfigurer.java | 15 ++++++++++++--- ...omDependencyManagementConfigurerTests.java | 19 ++++++++++++------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/spring/gradle/dependencymanagement/internal/StandardPomDependencyManagementConfigurer.java b/src/main/java/io/spring/gradle/dependencymanagement/internal/StandardPomDependencyManagementConfigurer.java index 39acc30..db484b6 100644 --- a/src/main/java/io/spring/gradle/dependencymanagement/internal/StandardPomDependencyManagementConfigurer.java +++ b/src/main/java/io/spring/gradle/dependencymanagement/internal/StandardPomDependencyManagementConfigurer.java @@ -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; @@ -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 bomReferences = this.dependencyManagement.getImportedBomReferences(); Map withoutPropertiesManagedDependencies = getManagedDependenciesById(bomReferences, @@ -226,7 +235,7 @@ private void addManagedDependency(Node managedDependencies, Dependency managedDe private List findClassifiers(Node dependencies, Dependency managedDependency) { List 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); diff --git a/src/test/java/io/spring/gradle/dependencymanagement/internal/StandardPomDependencyManagementConfigurerTests.java b/src/test/java/io/spring/gradle/dependencymanagement/internal/StandardPomDependencyManagementConfigurerTests.java index 49ef536..6b2f122 100644 --- a/src/test/java/io/spring/gradle/dependencymanagement/internal/StandardPomDependencyManagementConfigurerTests.java +++ b/src/test/java/io/spring/gradle/dependencymanagement/internal/StandardPomDependencyManagementConfigurerTests.java @@ -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. @@ -42,6 +42,10 @@ */ class StandardPomDependencyManagementConfigurerTests { + private static final String PROJECT_TAG = ""; + private final Project project; private final DependencyManagementContainer dependencyManagement; @@ -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( - ""); + NodeAssert pom = configuredPom(// + PROJECT_TAG + ""); + 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"); @@ -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( - "org.apache.logging.log4jlog4j-coretest"); - assertThat(pom).nodesAtPath("//project/dependencyManagement/dependencies/dependency").hasSize(2); + NodeAssert pom = configuredPom(PROJECT_TAG + + "org.apache.logging.log4jlog4j-coretest"); + // 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") @@ -266,7 +271,7 @@ private NodeAssert configuredPom(String existingPom) throws Exception { } private NodeAssert configuredPom(PomCustomizationSettings settings) throws Exception { - return configuredPom("", settings); + return configuredPom(PROJECT_TAG + "", settings); } private NodeAssert configuredPom(String existingPom, PomCustomizationSettings settings) throws Exception {