From abedad1acb91ed45d228d3d88a4b4b54f03fedbe Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Wed, 26 Jun 2024 14:07:03 -0300 Subject: [PATCH 01/20] [WIP] Adding AddParentPom recipe Tests fail with "Recipe was expected to make a change but made no changes." --- .../org/openrewrite/maven/AddParentPom.java | 277 ++++++ .../openrewrite/maven/AddParentPomTest.java | 824 ++++++++++++++++++ 2 files changed, 1101 insertions(+) create mode 100644 rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java create mode 100644 rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java new file mode 100644 index 00000000000..0ea8f12a53d --- /dev/null +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -0,0 +1,277 @@ +/* + * Copyright 2021 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. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.maven; + +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.*; +import org.openrewrite.internal.StringUtils; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.maven.internal.MavenPomDownloader; +import org.openrewrite.maven.table.MavenMetadataFailures; +import org.openrewrite.maven.tree.*; +import org.openrewrite.semver.Semver; +import org.openrewrite.semver.VersionComparator; +import org.openrewrite.xml.AddToTagVisitor; +import org.openrewrite.xml.ChangeTagValueVisitor; +import org.openrewrite.xml.TagNameComparator; +import org.openrewrite.xml.tree.Xml; + +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +import static java.util.Collections.emptyList; + +@Value +@EqualsAndHashCode(callSuper = false) +public class AddParentPom extends Recipe { + transient MavenMetadataFailures metadataFailures = new MavenMetadataFailures(this); + + @Option(displayName = "Group ID", + description = "The group ID of the maven parent pom to be adopted.", + example = "org.springframework.boot") + String groupId; + + @Option(displayName = "Artifact ID", + description = "The artifact ID of the maven parent pom to be adopted.", + example = "spring-boot-starter-parent") + String artifactId; + + @Option(displayName = "Version", + description = "An exact version number or node-style semver selector used to select the version number.", + example = "29.X") + String version; + + @Option(displayName = "Relative path", + description = "New relative path attribute for parent lookup.", + example = "../pom.xml") + @Nullable + String relativePath; + + @Option(displayName = "Version pattern", + description = "Allows version selection to be extended beyond the original Node Semver semantics. So for example," + + "Setting 'version' to \"25-29\" can be paired with a metadata pattern of \"-jre\" to select Guava 29.0-jre", + example = "-jre", + required = false) + @Nullable + String versionPattern; + + @Option(displayName = "File pattern", + description = "A glob expression that can be used to constrain which directories or source files should be searched. " + + "Multiple patterns may be specified, separated by a semicolon `;`. " + + "If multiple patterns are supplied any of the patterns matching will be interpreted as a match. " + + "When not set, all source files are searched. ", + required = false, + example = "**/*-parent/grpc-*/pom.xml") + @Nullable + String filePattern; + + @Override + public String getDisplayName() { + return "Change Maven parent"; + } + + @Override + public String getInstanceNameSuffix() { + return String.format("`%s:%s:%s`", groupId, artifactId, version); + } + + @Override + public String getDescription() { + return "Change the parent pom of a Maven pom.xml. Identifies the parent pom to be changed by its groupId and artifactId."; + } + + @Override + public Validated validate() { + Validated validated = super.validate(); + //noinspection ConstantConditions + if (version != null) { + validated = validated.and(Semver.validate(version, versionPattern)); + } + return validated; + } + + @Override + public TreeVisitor getVisitor() { + VersionComparator versionComparator = Semver.validate(version, versionPattern).getValue(); + assert versionComparator != null; + + return Preconditions.check(new MavenVisitor() { + @Override + public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { + if (filePattern != null) { + return PathUtils.matchesGlob(sourceFile.getSourcePath(), filePattern) && super.isAcceptable(sourceFile, ctx); + } + return super.isAcceptable(sourceFile, ctx); + } + }, new MavenIsoVisitor() { + @Nullable + private Collection availableVersions; + + @Override + public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { + Xml.Tag root = document.getRoot(); + // TODO What to do when a parent is already present? + if (!root.getChild("parent").isPresent()) { + document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build("")) + .visitNonNull(document, ctx, getCursor().getParentOrThrow()); + } + return document; + } + + @SuppressWarnings("OptionalGetWithoutIsPresent") + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { + Xml.Tag t = super.visitTag(tag, ctx); + + List> changeParentTagVisitors = new ArrayList<>(); + + changeParentTagVisitors.add(new ChangeTagValueVisitor<>(t.getChild("groupId").get(), groupId)); + changeParentTagVisitors.add(new ChangeTagValueVisitor<>(t.getChild("artifactId").get(), artifactId)); + changeParentTagVisitors.add(new ChangeTagValueVisitor<>(t.getChild("version").get(), version)); + + final Xml.Tag relativePathTag; + if (StringUtils.isBlank(relativePath)) { + relativePathTag = Xml.Tag.build(""); + } else { + relativePathTag = Xml.Tag.build("" + relativePath + ""); + } + doAfterVisit(new AddToTagVisitor<>(t, relativePathTag, new MavenTagInsertionComparator(t.getChildren()))); + maybeUpdateModel(); + + for (TreeVisitor visitor : changeParentTagVisitors) { + doAfterVisit(visitor); + } + maybeUpdateModel(); + doAfterVisit(new RemoveRedundantDependencyVersions(null, null, + RemoveRedundantDependencyVersions.Comparator.GTE, null).getVisitor()); + return t; + } + + }); + } + + private static Pattern PROPERTY_PATTERN = Pattern.compile("\\$\\{([^}]+)}"); + + private static Map getPropertiesInUse(Xml.Document pomXml, ExecutionContext ctx) { + Map properties = new HashMap<>(); + new MavenIsoVisitor() { + + @Nullable + ResolvedPom resolvedPom = null; + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { + Xml.Tag t = super.visitTag(tag, ctx); + if(t.getContent() != null && t.getContent().size() == 1 && t.getContent().get(0) instanceof Xml.CharData) { + String text = ((Xml.CharData) t.getContent().get(0)).getText().trim(); + Matcher m = PROPERTY_PATTERN.matcher(text); + while(m.find()) { + if(resolvedPom == null) { + resolvedPom = getResolutionResult().getPom(); + } + String propertyName = m.group(1).trim(); + if (resolvedPom.getProperties().containsKey(propertyName) && !isGlobalProperty(propertyName)) { + properties.put(m.group(1).trim(), resolvedPom.getProperties().get(propertyName)); + } + } + } + return t; + } + + private boolean isGlobalProperty(String propertyName) { + return propertyName.startsWith("project.") || propertyName.startsWith("env.") + || propertyName.startsWith("settings.") || propertyName.equals("basedir"); + } + }.visit(pomXml, ctx); + return properties; + } + + private List getDependenciesUnmanagedByNewParent(MavenResolutionResult mrr, ResolvedPom newParent) { + ResolvedPom resolvedPom = mrr.getPom(); + + // Dependencies managed by the current pom's own dependency management are irrelevant to parent upgrade + List locallyManaged = mrr.getPom().getRequested().getDependencyManagement(); + + Set requestedWithoutExplicitVersion = resolvedPom.getRequested().getDependencies().stream() + .filter(dep -> dep.getVersion() == null) + // Dependencies explicitly managed by the current pom require no changes + .filter(dep -> locallyManaged.stream() + .noneMatch(localManagedDep -> localManagedDep.getGroupId().equals(dep.getGroupId()) && localManagedDep.getArtifactId().equals(dep.getArtifactId()))) + .map(dep -> new GroupArtifactVersion(dep.getGroupId(), dep.getArtifactId(), null)) + .collect(Collectors.toCollection(LinkedHashSet::new)); + + if(requestedWithoutExplicitVersion.isEmpty()) { + return emptyList(); + } + + List depsWithoutExplicitVersion = resolvedPom.getDependencyManagement().stream() + .filter(dep -> requestedWithoutExplicitVersion.contains(dep.getGav().withVersion(null))) + // Exclude dependencies managed by a bom imported by the current pom + .filter(dep -> dep.getBomGav() == null || locallyManaged.stream() + .noneMatch(localManagedDep -> localManagedDep.getGroupId().equals(dep.getBomGav().getGroupId()) && localManagedDep.getArtifactId().equals(dep.getBomGav().getArtifactId()))) + .collect(Collectors.toList()); + + if(depsWithoutExplicitVersion.isEmpty()) { + return emptyList(); + } + + // Remove from the list any that would still be managed under the new parent + Set newParentManagedGa = newParent.getDependencyManagement().stream() + .map(dep -> new GroupArtifact(dep.getGav().getGroupId(), dep.getGav().getArtifactId())) + .collect(Collectors.toSet()); + + depsWithoutExplicitVersion = depsWithoutExplicitVersion.stream() + .filter(it -> !newParentManagedGa.contains(new GroupArtifact(it.getGav().getGroupId(), it.getGav().getArtifactId()))) + .collect(Collectors.toList()); + return depsWithoutExplicitVersion; + } + + @Value + @EqualsAndHashCode(callSuper = false) + private static class UnconditionalAddProperty extends MavenIsoVisitor { + String key; + String value; + @Override + public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { + Xml.Document d = super.visitDocument(document, ctx); + Xml.Tag root = d.getRoot(); + Optional properties = root.getChild("properties"); + if (!properties.isPresent()) { + Xml.Tag propertiesTag = Xml.Tag.build("\n<" + key + ">" + value + "\n"); + d = (Xml.Document) new AddToTagVisitor(root, propertiesTag, new MavenTagInsertionComparator(root.getChildren())).visitNonNull(d, ctx); + } else if (!properties.get().getChildValue(key).isPresent()) { + Xml.Tag propertyTag = Xml.Tag.build("<" + key + ">" + value + ""); + d = (Xml.Document) new AddToTagVisitor<>(properties.get(), propertyTag, new TagNameComparator()).visitNonNull(d, ctx); + } + if (d != document) { + maybeUpdateModel(); + } + return d; + } + + @Override + public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { + Xml.Tag t = super.visitTag(tag, ctx); + if (isPropertyTag() && key.equals(tag.getName()) + && !value.equals(tag.getValue().orElse(null))) { + t = (Xml.Tag) new ChangeTagValueVisitor<>(tag, value).visitNonNull(t, ctx); + } + return t; + } + } +} diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java new file mode 100644 index 00000000000..5222759b3d7 --- /dev/null +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -0,0 +1,824 @@ +/* + * Copyright 2022 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. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.maven; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.RepeatedTest; +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.Issue; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.mavenProject; +import static org.openrewrite.maven.Assertions.pomXml; + +class AddParentPomTest implements RewriteTest { + + @DocumentExample + @Test + void addParent() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.springframework.boot", + "spring-boot-starter-parent", + "1.5.12.RELEASE", + null, + null, + null + )), + pomXml( + """ + + 4.0.0 + + com.mycompany.app + my-app + 1 + + """, + """ + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.12.RELEASE + + + com.mycompany.app + my-app + 1 + + """ + ) + ); + } + + @Test + void addParentWithRelativePath() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.springframework.boot", + "spring-boot-starter-parent", + "1.5.12.RELEASE", + "../../pom.xml", + null, + null + )), + pomXml( + """ + + 4.0.0 + + com.mycompany.app + my-app + 1 + + """, + """ + + 4.0.0 + + + com.fasterxml.jackson + jackson-parent + 2.12 + ../../pom.xml + + + com.mycompany.app + my-app + 1 + + """ + ) + ); + } + + @Test + void addParentWithRelativePathEmptyValue() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.springframework.boot", + "spring-boot-starter-parent", + "1.5.12.RELEASE", + "", + null, + null + )), + pomXml( + """ + + 4.0.0 + + com.mycompany.app + my-app + 1 + + """, + """ + + 4.0.0 + + + com.fasterxml.jackson + jackson-parent + 2.12 + + + + com.mycompany.app + my-app + 1 + + """ + ) + ); + } + + @RepeatedTest(10) + void multiModuleRelativePath() { + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null,null); + rewriteRun( + spec -> spec.recipe(recipe), + mavenProject("parent", + pomXml( + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + module1 + module2 + + + """, + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + + + module1 + module2 + + + """ + ), + mavenProject("module1", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module1 + + """ + )), + mavenProject("module2", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module2 + + """ + ) + ) + ) + ); + } + + @RepeatedTest(10) + void multiModuleRelativePathChangeChildrens() { + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null,null); + rewriteRun( + spec -> spec.recipe(recipe), + mavenProject("parent", + pomXml( + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + module1 + module2 + + + """ + ), + mavenProject("module1", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module1 + + """, + """ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.0 + + + module1 + + """ + )), + mavenProject("module2", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module2 + + """, + """ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.0 + + + module2 + + """ + ) + ) + ) + ); +} + + @Test + void addParentWithVersionPattern() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.springframework.boot", + "spring-boot-starter-parent", + null, + null, + "~1.5", + null + )), + pomXml( + """ + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.12.RELEASE + + + + com.mycompany.app + my-app + 1 + + """, + """ + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.22.RELEASE + + + + com.mycompany.app + my-app + 1 + + """ + ) + ); + } + + @Test + void wildcardVersion() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.springframework.boot", + "spring-boot-starter-parent", + null, + null, + "~1.5", + null + )), + pomXml( + """ + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.12.RELEASE + + + + com.mycompany.app + my-app + 1 + + """, + """ + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.5.22.RELEASE + + + + com.mycompany.app + my-app + 1 + + """ + ) + ); + } + + @Test + void removesRedundantExplicitVersionsMatchingNewParent() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.junit", + "junit-bom", + "5.9.1", + "", + null, + null + )), + pomXml( + """ + + 4.0.0 + + com.mycompany.app + my-app + 1 + + + + org.junit.jupiter + junit-jupiter-api + 5.9.1 + + + + """, + """ + + 4.0.0 + + + org.junit + junit-bom + 5.9.1 + + + + com.mycompany.app + my-app + 1 + + + + org.junit.jupiter + junit-jupiter-api + + + + """ + ) + ); + } + + @Test + void takesNewVersionFromParent() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.junit", + "junit-bom", + "5.9.1", + "", + null, + null + )), + pomXml( + """ + + 4.0.0 + + com.mycompany.app + my-app + 1 + + + + org.junit.jupiter + junit-jupiter-api + 5.8.0 + + + + """, + """ + + 4.0.0 + + + org.junit + junit-bom + 5.9.1 + + + + com.mycompany.app + my-app + 1 + + + + org.junit.jupiter + junit-jupiter-api + + + + """ + ) + ); + } + + @RepeatedTest(10) + @Issue("https://github.com/openrewrite/rewrite/issues/1753") + void multiModule() { + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.6.7", null, null, null); + rewriteRun( + spec -> spec.recipe(recipe), + mavenProject("parent", + pomXml( + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + module1 + module2 + + + """, + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.6.7 + + + + module1 + module2 + + + """ + ), + mavenProject("module1", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module1 + + """ + )), + mavenProject("module2", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module2 + + """ + ) + ) + ) + ); + } + + @Test + void shouldNotAddToDependencyManagement() { + rewriteRun( + spec -> spec.recipe(new AddParentPom("org.jenkins-ci.plugins", "plugin", "4.81", "", null, null)), + // language=xml + pomXml( + """ + + example + 4.0.0 + + 2.387.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + + io.jenkins.tools.bom + bom-2.387.x + 2516.v113cb_3d00317 + pom + import + + + + + + org.jenkins-ci.plugins + junit + + + + """, + """ + + example + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.81 + + + + 2.387.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + + io.jenkins.tools.bom + bom-2.387.x + 2516.v113cb_3d00317 + pom + import + + + + + + org.jenkins-ci.plugins + junit + + + + """ + )); + } + + @Test + void doesNotAddMavenDefaultProperties() { + rewriteRun( + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), + pomXml( + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + ${project.build.directory} + + + """, + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.7.18 + + + + ${project.build.directory} + + + """ + ) + ); + } + + @Test + void doesNotAddGrandparentProperties() { + rewriteRun( + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), + pomXml( + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + ${junit.version} + + + """, + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.7.18 + + + + ${junit.version} + + + """ + ) + ); + } + + @Test + void doesNotAddGlobalProperties() { + rewriteRun( + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), + pomXml( + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + ${basedir} + ${project.basedir} + ${project.build.directory} + ${project.version} + ${env.GIT_HOME} + ${settings.offline} + + + """, + """ + + + 4.0.0 + org.sample + sample + 1.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.7.18 + + + + ${basedir} + ${project.basedir} + ${project.build.directory} + ${project.version} + ${env.GIT_HOME} + ${settings.offline} + + + """ + ) + ); + } +} From 30d63b3f1cb43977f09acd9a251356d46aae2ed0 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 26 Jun 2024 22:30:21 +0200 Subject: [PATCH 02/20] Apply formatter; drop RepeatedTest; fix precondition --- .../org/openrewrite/maven/AddParentPom.java | 35 +- .../openrewrite/maven/AddParentPomTest.java | 495 +++++++++--------- 2 files changed, 265 insertions(+), 265 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index 0ea8f12a53d..2ba4d45b962 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2024 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. @@ -20,7 +20,7 @@ import org.openrewrite.*; import org.openrewrite.internal.StringUtils; import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.maven.internal.MavenPomDownloader; +import org.openrewrite.marker.SearchResult; import org.openrewrite.maven.table.MavenMetadataFailures; import org.openrewrite.maven.tree.*; import org.openrewrite.semver.Semver; @@ -73,9 +73,9 @@ public class AddParentPom extends Recipe { @Option(displayName = "File pattern", description = "A glob expression that can be used to constrain which directories or source files should be searched. " + - "Multiple patterns may be specified, separated by a semicolon `;`. " + - "If multiple patterns are supplied any of the patterns matching will be interpreted as a match. " + - "When not set, all source files are searched. ", + "Multiple patterns may be specified, separated by a semicolon `;`. " + + "If multiple patterns are supplied any of the patterns matching will be interpreted as a match. " + + "When not set, all source files are searched. ", required = false, example = "**/*-parent/grpc-*/pom.xml") @Nullable @@ -113,11 +113,12 @@ public TreeVisitor getVisitor() { return Preconditions.check(new MavenVisitor() { @Override - public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) { - if (filePattern != null) { - return PathUtils.matchesGlob(sourceFile.getSourcePath(), filePattern) && super.isAcceptable(sourceFile, ctx); + public Xml visitDocument(Xml.Document document, ExecutionContext executionContext) { + if (filePattern == null || PathUtils.matchesGlob(document.getSourcePath(), filePattern)) { + return SearchResult.found(document); } - return super.isAcceptable(sourceFile, ctx); + return document; + } }, new MavenIsoVisitor() { @Nullable @@ -174,14 +175,15 @@ private static Map getPropertiesInUse(Xml.Document pomXml, Execu @Nullable ResolvedPom resolvedPom = null; + @Override public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { Xml.Tag t = super.visitTag(tag, ctx); - if(t.getContent() != null && t.getContent().size() == 1 && t.getContent().get(0) instanceof Xml.CharData) { + if (t.getContent() != null && t.getContent().size() == 1 && t.getContent().get(0) instanceof Xml.CharData) { String text = ((Xml.CharData) t.getContent().get(0)).getText().trim(); Matcher m = PROPERTY_PATTERN.matcher(text); - while(m.find()) { - if(resolvedPom == null) { + while (m.find()) { + if (resolvedPom == null) { resolvedPom = getResolutionResult().getPom(); } String propertyName = m.group(1).trim(); @@ -195,7 +197,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { private boolean isGlobalProperty(String propertyName) { return propertyName.startsWith("project.") || propertyName.startsWith("env.") - || propertyName.startsWith("settings.") || propertyName.equals("basedir"); + || propertyName.startsWith("settings.") || propertyName.equals("basedir"); } }.visit(pomXml, ctx); return properties; @@ -215,7 +217,7 @@ private List getDependenciesUnmanagedByNewParent(Mave .map(dep -> new GroupArtifactVersion(dep.getGroupId(), dep.getArtifactId(), null)) .collect(Collectors.toCollection(LinkedHashSet::new)); - if(requestedWithoutExplicitVersion.isEmpty()) { + if (requestedWithoutExplicitVersion.isEmpty()) { return emptyList(); } @@ -226,7 +228,7 @@ private List getDependenciesUnmanagedByNewParent(Mave .noneMatch(localManagedDep -> localManagedDep.getGroupId().equals(dep.getBomGav().getGroupId()) && localManagedDep.getArtifactId().equals(dep.getBomGav().getArtifactId()))) .collect(Collectors.toList()); - if(depsWithoutExplicitVersion.isEmpty()) { + if (depsWithoutExplicitVersion.isEmpty()) { return emptyList(); } @@ -246,6 +248,7 @@ private List getDependenciesUnmanagedByNewParent(Mave private static class UnconditionalAddProperty extends MavenIsoVisitor { String key; String value; + @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { Xml.Document d = super.visitDocument(document, ctx); @@ -268,7 +271,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { Xml.Tag t = super.visitTag(tag, ctx); if (isPropertyTag() && key.equals(tag.getName()) - && !value.equals(tag.getValue().orElse(null))) { + && !value.equals(tag.getValue().orElse(null))) { t = (Xml.Tag) new ChangeTagValueVisitor<>(tag, value).visitNonNull(t, ctx); } return t; diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index 5222759b3d7..b01ee4d423c 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 the original author or authors. + * Copyright 2024 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. @@ -15,12 +15,9 @@ */ package org.openrewrite.maven; -import org.junit.jupiter.api.Nested; -import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.Issue; -import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.mavenProject; @@ -44,7 +41,7 @@ void addParent() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -53,13 +50,13 @@ void addParent() { """ 4.0.0 - + org.springframework.boot spring-boot-starter-parent 1.5.12.RELEASE - + com.mycompany.app my-app 1 @@ -84,7 +81,7 @@ void addParentWithRelativePath() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -93,14 +90,14 @@ void addParentWithRelativePath() { """ 4.0.0 - + com.fasterxml.jackson jackson-parent 2.12 ../../pom.xml - + com.mycompany.app my-app 1 @@ -125,7 +122,7 @@ void addParentWithRelativePathEmptyValue() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -134,14 +131,14 @@ void addParentWithRelativePathEmptyValue() { """ 4.0.0 - + com.fasterxml.jackson jackson-parent 2.12 - + com.mycompany.app my-app 1 @@ -151,166 +148,166 @@ void addParentWithRelativePathEmptyValue() { ); } - @RepeatedTest(10) + @Test void multiModuleRelativePath() { - AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null,null); - rewriteRun( - spec -> spec.recipe(recipe), - mavenProject("parent", - pomXml( - """ - - - 4.0.0 - org.sample - sample - 1.0.0 - - - module1 - module2 - - - """, - """ - - - 4.0.0 - org.sample - sample - 1.0.0 - - - org.springframework.boot - spring-boot-starter-parent - 2.6.7 - - - - - module1 - module2 - - - """ - ), - mavenProject("module1", + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null, null); + rewriteRun( + spec -> spec.recipe(recipe), + mavenProject("parent", pomXml( """ 4.0.0 - - org.sample - sample - 1.0.0 - - module1 + org.sample + sample + 1.0.0 + + + module1 + module2 + - """ - )), - mavenProject("module2", - pomXml( + """, """ 4.0.0 - - org.sample - sample - 1.0.0 - - module2 - - """ - ) - ) - ) - ); - } - - @RepeatedTest(10) - void multiModuleRelativePathChangeChildrens() { - AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null,null); - rewriteRun( - spec -> spec.recipe(recipe), - mavenProject("parent", - pomXml( - """ - - - 4.0.0 - org.sample - sample - 1.0.0 - - - module1 - module2 - - - """ - ), - mavenProject("module1", - pomXml( - """ - - - 4.0.0 - org.sample sample 1.0.0 - - module1 - - """, - """ - - - 4.0.0 + org.springframework.boot spring-boot-starter-parent - 2.5.0 + 2.6.7 - module1 + + + module1 + module2 + """ - )), - mavenProject("module2", - pomXml( - """ - - - 4.0.0 - - org.sample - sample - 1.0.0 - - module2 - - """, + ), + mavenProject("module1", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module1 + + """ + )), + mavenProject("module2", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module2 + + """ + ) + ) + ) + ); + } + + @Test + void multiModuleRelativePathChangeChildrens() { + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null, null); + rewriteRun( + spec -> spec.recipe(recipe), + mavenProject("parent", + pomXml( """ 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.5.0 - - - module2 + org.sample + sample + 1.0.0 + + + module1 + module2 + """ + ), + mavenProject("module1", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module1 + + """, + """ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.0 + + + module1 + + """ + )), + mavenProject("module2", + pomXml( + """ + + + 4.0.0 + + org.sample + sample + 1.0.0 + + module2 + + """, + """ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.5.0 + + + module2 + + """ + ) + ) ) - ) - ) - ); -} + ); + } @Test void addParentWithVersionPattern() { @@ -327,14 +324,14 @@ void addParentWithVersionPattern() { """ 4.0.0 - + org.springframework.boot spring-boot-starter-parent 1.5.12.RELEASE - + com.mycompany.app my-app 1 @@ -343,14 +340,14 @@ void addParentWithVersionPattern() { """ 4.0.0 - + org.springframework.boot spring-boot-starter-parent 1.5.22.RELEASE - + com.mycompany.app my-app 1 @@ -375,14 +372,14 @@ void wildcardVersion() { """ 4.0.0 - + org.springframework.boot spring-boot-starter-parent 1.5.12.RELEASE - + com.mycompany.app my-app 1 @@ -391,14 +388,14 @@ void wildcardVersion() { """ 4.0.0 - + org.springframework.boot spring-boot-starter-parent 1.5.22.RELEASE - + com.mycompany.app my-app 1 @@ -423,11 +420,11 @@ void removesRedundantExplicitVersionsMatchingNewParent() { """ 4.0.0 - + com.mycompany.app my-app 1 - + org.junit.jupiter @@ -440,18 +437,18 @@ void removesRedundantExplicitVersionsMatchingNewParent() { """ 4.0.0 - + org.junit junit-bom 5.9.1 - + com.mycompany.app my-app 1 - + org.junit.jupiter @@ -479,11 +476,11 @@ void takesNewVersionFromParent() { """ 4.0.0 - + com.mycompany.app my-app 1 - + org.junit.jupiter @@ -496,18 +493,18 @@ void takesNewVersionFromParent() { """ 4.0.0 - + org.junit junit-bom 5.9.1 - + com.mycompany.app my-app 1 - + org.junit.jupiter @@ -520,7 +517,7 @@ void takesNewVersionFromParent() { ); } - @RepeatedTest(10) + @Test @Issue("https://github.com/openrewrite/rewrite/issues/1753") void multiModule() { AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.6.7", null, null, null); @@ -535,7 +532,7 @@ void multiModule() { org.sample sample 1.0.0 - + module1 module2 @@ -604,89 +601,89 @@ void shouldNotAddToDependencyManagement() { spec -> spec.recipe(new AddParentPom("org.jenkins-ci.plugins", "plugin", "4.81", "", null, null)), // language=xml pomXml( - """ - - example - 4.0.0 - - 2.387.3 - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - + """ + + example + 4.0.0 + + 2.387.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + + io.jenkins.tools.bom + bom-2.387.x + 2516.v113cb_3d00317 + pom + import + + + - io.jenkins.tools.bom - bom-2.387.x - 2516.v113cb_3d00317 - pom - import + org.jenkins-ci.plugins + junit - - - - org.jenkins-ci.plugins - junit - - - - """, - """ - - example - 4.0.0 - - org.jenkins-ci.plugins - plugin - 4.81 - - - - 2.387.3 - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - + + """, + """ + + example + 4.0.0 + + org.jenkins-ci.plugins + plugin + 4.81 + + + + 2.387.3 + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + repo.jenkins-ci.org + https://repo.jenkins-ci.org/public/ + + + + + + io.jenkins.tools.bom + bom-2.387.x + 2516.v113cb_3d00317 + pom + import + + + - io.jenkins.tools.bom - bom-2.387.x - 2516.v113cb_3d00317 - pom - import + org.jenkins-ci.plugins + junit - - - - org.jenkins-ci.plugins - junit - - - - """ - )); + + """ + )); } @Test @@ -701,7 +698,7 @@ void doesNotAddMavenDefaultProperties() { org.sample sample 1.0.0 - + ${project.build.directory} @@ -714,13 +711,13 @@ void doesNotAddMavenDefaultProperties() { org.sample sample 1.0.0 - + org.springframework.boot spring-boot-starter-parent 2.7.18 - + ${project.build.directory} @@ -742,7 +739,7 @@ void doesNotAddGrandparentProperties() { org.sample sample 1.0.0 - + ${junit.version} @@ -755,13 +752,13 @@ void doesNotAddGrandparentProperties() { org.sample sample 1.0.0 - + org.springframework.boot spring-boot-starter-parent 2.7.18 - + ${junit.version} @@ -783,7 +780,7 @@ void doesNotAddGlobalProperties() { org.sample sample 1.0.0 - + ${basedir} ${project.basedir} @@ -801,13 +798,13 @@ void doesNotAddGlobalProperties() { org.sample sample 1.0.0 - + org.springframework.boot spring-boot-starter-parent 2.7.18 - + ${basedir} ${project.basedir} From ab360c716c26ca42adb7e484406b500a02a97141 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 26 Jun 2024 22:59:53 +0200 Subject: [PATCH 03/20] Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- .../src/main/java/org/openrewrite/maven/AddParentPom.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index 2ba4d45b962..59cf9d40080 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -113,7 +113,7 @@ public TreeVisitor getVisitor() { return Preconditions.check(new MavenVisitor() { @Override - public Xml visitDocument(Xml.Document document, ExecutionContext executionContext) { + public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { if (filePattern == null || PathUtils.matchesGlob(document.getSourcePath(), filePattern)) { return SearchResult.found(document); } From 1204724a69bff7567bad98a43a542457dc079def Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Thu, 27 Jun 2024 13:50:15 -0300 Subject: [PATCH 04/20] Fixing some test expectations on AddParentPomTest should come right after --- .../openrewrite/maven/AddParentPomTest.java | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index b01ee4d423c..da41aa6c41c 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -41,6 +41,11 @@ void addParent() { """ 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 1.5.12.RELEASE + com.mycompany.app my-app @@ -51,12 +56,6 @@ void addParent() { 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 1.5.12.RELEASE - - com.mycompany.app my-app 1 @@ -90,7 +89,6 @@ void addParentWithRelativePath() { """ 4.0.0 - com.fasterxml.jackson jackson-parent @@ -131,7 +129,6 @@ void addParentWithRelativePathEmptyValue() { """ 4.0.0 - com.fasterxml.jackson jackson-parent @@ -173,16 +170,15 @@ void multiModuleRelativePath() { 4.0.0 - org.sample - sample - 1.0.0 - org.springframework.boot spring-boot-starter-parent 2.6.7 + org.sample + sample + 1.0.0 module1 @@ -437,7 +433,6 @@ void removesRedundantExplicitVersionsMatchingNewParent() { """ 4.0.0 - org.junit junit-bom @@ -493,7 +488,6 @@ void takesNewVersionFromParent() { """ 4.0.0 - org.junit junit-bom @@ -543,15 +537,14 @@ void multiModule() { 4.0.0 - org.sample - sample - 1.0.0 - org.springframework.boot spring-boot-starter-parent 2.6.7 + org.sample + sample + 1.0.0 module1 @@ -708,15 +701,14 @@ void doesNotAddMavenDefaultProperties() { 4.0.0 - org.sample - sample - 1.0.0 - org.springframework.boot spring-boot-starter-parent 2.7.18 + org.sample + sample + 1.0.0 ${project.build.directory} @@ -749,15 +741,14 @@ void doesNotAddGrandparentProperties() { 4.0.0 - org.sample - sample - 1.0.0 - org.springframework.boot spring-boot-starter-parent 2.7.18 + org.sample + sample + 1.0.0 ${junit.version} @@ -795,15 +786,14 @@ void doesNotAddGlobalProperties() { 4.0.0 - org.sample - sample - 1.0.0 - org.springframework.boot spring-boot-starter-parent 2.7.18 + org.sample + sample + 1.0.0 ${basedir} From ea1e369c8dc13882eca105ffee2ebce0d9db02ec Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Thu, 27 Jun 2024 13:50:48 -0300 Subject: [PATCH 05/20] Fixing multiple issues on AddParentPom recipe --- .../org/openrewrite/maven/AddParentPom.java | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index 59cf9d40080..3afd4d34f5b 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -114,7 +114,9 @@ public TreeVisitor getVisitor() { return Preconditions.check(new MavenVisitor() { @Override public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { - if (filePattern == null || PathUtils.matchesGlob(document.getSourcePath(), filePattern)) { + Xml.Tag root = document.getRoot(); + if (!root.getChild("parent").isPresent() && + (filePattern == null || PathUtils.matchesGlob(document.getSourcePath(), filePattern))) { return SearchResult.found(document); } return document; @@ -127,43 +129,44 @@ public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { @Override public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { Xml.Tag root = document.getRoot(); - // TODO What to do when a parent is already present? - if (!root.getChild("parent").isPresent()) { - document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build("")) + assert !root.getChild("parent").isPresent(); + + document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build(""), new MavenTagInsertionComparator(root.getChildren())) .visitNonNull(document, ctx, getCursor().getParentOrThrow()); - } - return document; + + return super.visitDocument(document, ctx); } - @SuppressWarnings("OptionalGetWithoutIsPresent") @Override public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { Xml.Tag t = super.visitTag(tag, ctx); - List> changeParentTagVisitors = new ArrayList<>(); + if (isParentTag()) { + List> addToTagVisitors = new ArrayList<>(); - changeParentTagVisitors.add(new ChangeTagValueVisitor<>(t.getChild("groupId").get(), groupId)); - changeParentTagVisitors.add(new ChangeTagValueVisitor<>(t.getChild("artifactId").get(), artifactId)); - changeParentTagVisitors.add(new ChangeTagValueVisitor<>(t.getChild("version").get(), version)); + addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + groupId + ""))); + addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + artifactId + ""))); + addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + version + ""))); - final Xml.Tag relativePathTag; - if (StringUtils.isBlank(relativePath)) { - relativePathTag = Xml.Tag.build(""); - } else { - relativePathTag = Xml.Tag.build("" + relativePath + ""); - } - doAfterVisit(new AddToTagVisitor<>(t, relativePathTag, new MavenTagInsertionComparator(t.getChildren()))); - maybeUpdateModel(); + if (relativePath != null) { + final Xml.Tag relativePathTag; + if (StringUtils.isBlank(relativePath)) { + relativePathTag = Xml.Tag.build(""); + } else { + relativePathTag = Xml.Tag.build("" + relativePath + ""); + } + addToTagVisitors.add(new AddToTagVisitor<>(t, relativePathTag)); + } - for (TreeVisitor visitor : changeParentTagVisitors) { - doAfterVisit(visitor); + for (TreeVisitor visitor : addToTagVisitors) { + doAfterVisit(visitor); + } + maybeUpdateModel(); + doAfterVisit(new RemoveRedundantDependencyVersions(null, null, + RemoveRedundantDependencyVersions.Comparator.GTE, null).getVisitor()); } - maybeUpdateModel(); - doAfterVisit(new RemoveRedundantDependencyVersions(null, null, - RemoveRedundantDependencyVersions.Comparator.GTE, null).getVisitor()); return t; } - }); } From c1cce07f2a96edd4e7fd89283a4656fbaf6a0c04 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Thu, 27 Jun 2024 17:39:39 -0300 Subject: [PATCH 06/20] Fixing expectations on AddParentPomTest --- .../openrewrite/maven/AddParentPomTest.java | 75 +++++++++---------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index da41aa6c41c..b2feb3ebfc4 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -41,11 +41,6 @@ void addParent() { """ 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 1.5.12.RELEASE - com.mycompany.app my-app @@ -55,6 +50,11 @@ void addParent() { """ 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 1.5.12.RELEASE + com.mycompany.app my-app @@ -90,9 +90,9 @@ void addParentWithRelativePath() { 4.0.0 - com.fasterxml.jackson - jackson-parent - 2.12 + org.springframework.boot + spring-boot-starter-parent + 1.5.12.RELEASE ../../pom.xml @@ -130,10 +130,10 @@ void addParentWithRelativePathEmptyValue() { 4.0.0 - com.fasterxml.jackson - jackson-parent - 2.12 - + org.springframework.boot + spring-boot-starter-parent + 1.5.12.RELEASE + com.mycompany.app @@ -173,8 +173,8 @@ void multiModuleRelativePath() { org.springframework.boot spring-boot-starter-parent - 2.6.7 - + 1.5.12.RELEASE + org.sample sample @@ -224,7 +224,7 @@ void multiModuleRelativePath() { @Test void multiModuleRelativePathChangeChildrens() { - AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null, null); + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", null, null, null); rewriteRun( spec -> spec.recipe(recipe), mavenProject("parent", @@ -242,6 +242,25 @@ void multiModuleRelativePathChangeChildrens() { module2 + """, + """ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 1.5.12.RELEASE + + org.sample + sample + 1.0.0 + + + module1 + module2 + + """ ), mavenProject("module1", @@ -257,19 +276,6 @@ void multiModuleRelativePathChangeChildrens() { module1 - """, - """ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.5.0 - - - module1 - """ )), mavenProject("module2", @@ -285,19 +291,6 @@ void multiModuleRelativePathChangeChildrens() { module2 - """, - """ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.5.0 - - - module2 - """ ) ) From fb121e8eae2e20426ddee55c1886572e400c4713 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Thu, 27 Jun 2024 17:52:19 -0300 Subject: [PATCH 07/20] Fixing expectations on AddParentPomTest --- .../openrewrite/maven/AddParentPomTest.java | 67 ++----------------- 1 file changed, 7 insertions(+), 60 deletions(-) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index b2feb3ebfc4..5d8901796be 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -298,63 +298,15 @@ void multiModuleRelativePathChangeChildrens() { ); } - @Test - void addParentWithVersionPattern() { - rewriteRun( - spec -> spec.recipe(new AddParentPom( - "org.springframework.boot", - "spring-boot-starter-parent", - null, - null, - "~1.5", - null - )), - pomXml( - """ - - 4.0.0 - - - org.springframework.boot - spring-boot-starter-parent - 1.5.12.RELEASE - - - - com.mycompany.app - my-app - 1 - - """, - """ - - 4.0.0 - - - org.springframework.boot - spring-boot-starter-parent - 1.5.22.RELEASE - - - - com.mycompany.app - my-app - 1 - - """ - ) - ); - } - @Test void wildcardVersion() { rewriteRun( spec -> spec.recipe(new AddParentPom( "org.springframework.boot", "spring-boot-starter-parent", + "~1.5", null, null, - "~1.5", null )), pomXml( @@ -362,13 +314,6 @@ void wildcardVersion() { 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 1.5.12.RELEASE - - - com.mycompany.app my-app 1 @@ -377,12 +322,10 @@ void wildcardVersion() { """ 4.0.0 - org.springframework.boot spring-boot-starter-parent 1.5.22.RELEASE - com.mycompany.app @@ -589,8 +532,10 @@ void shouldNotAddToDependencyManagement() { pomXml( """ - example 4.0.0 + org.sample + sample + 1.0.0 2.387.3 @@ -627,7 +572,6 @@ void shouldNotAddToDependencyManagement() { """, """ - example 4.0.0 org.jenkins-ci.plugins @@ -635,6 +579,9 @@ void shouldNotAddToDependencyManagement() { 4.81 + org.sample + sample + 1.0.0 2.387.3 From 5a5634f84722f8efc16a5ec2b611ba32dd80ca0a Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Thu, 27 Jun 2024 18:02:05 -0300 Subject: [PATCH 08/20] Testing FilePattern param on AddParentPom recipe --- .../openrewrite/maven/AddParentPomTest.java | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index 5d8901796be..d9f3af87799 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -622,7 +622,7 @@ void shouldNotAddToDependencyManagement() { @Test void doesNotAddMavenDefaultProperties() { rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, "dir/pom.xml")), pomXml( """ @@ -662,7 +662,7 @@ void doesNotAddMavenDefaultProperties() { @Test void doesNotAddGrandparentProperties() { rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, "dir/pom.xml")), pomXml( """ @@ -748,4 +748,53 @@ void doesNotAddGlobalProperties() { ) ); } + + @Test + void addParentPomWithMatchingFilePattern() { + rewriteRun( + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + """, + """ + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.7.18 + + com.mycompany.app + my-app + 1 + + """, + spec -> spec.path("dir/pom.xml") + ) + ); + } + + @Test + void addParentPomWithNonMatchingFilePattern() { + rewriteRun( + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, "dir/pom.xml")), + pomXml( + """ + + 4.0.0 + com.mycompany.app + my-app + 1 + + """, + spec -> spec.path("pom.xml") + ) + ); + } } From 1da518cac7059cebd264279d2bbf3dd0a7210509 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Fri, 28 Jun 2024 11:20:56 -0300 Subject: [PATCH 09/20] Fix wildcard version --- .../org/openrewrite/maven/AddParentPom.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index 3afd4d34f5b..fba91493f14 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -146,7 +146,15 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + groupId + ""))); addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + artifactId + ""))); - addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + version + ""))); + + try { + Optional targetVersion = findAcceptableVersion(groupId, artifactId, ctx); + targetVersion.ifPresent((v) -> + addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + v + ""))) + ); + } catch (MavenDownloadingException e) { + throw new RuntimeException(e); + } if (relativePath != null) { final Xml.Tag relativePathTag; @@ -167,6 +175,19 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { } return t; } + + private Optional findAcceptableVersion(String groupId, String artifactId, ExecutionContext ctx) + throws MavenDownloadingException { + + if (availableVersions == null) { + MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, ctx)); + //noinspection EqualsWithItself + availableVersions = mavenMetadata.getVersioning().getVersions().stream() + .filter(v -> versionComparator.isValid(null, v)) + .collect(Collectors.toList()); + } + return availableVersions.stream().max(versionComparator); + } }); } From 66d716ae470c4bad74c67a8b712a72c730201ae8 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Fri, 28 Jun 2024 11:36:04 -0300 Subject: [PATCH 10/20] Fixing tests --- .../test/java/org/openrewrite/maven/AddParentPomTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index d9f3af87799..9556ad17523 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -622,7 +622,7 @@ void shouldNotAddToDependencyManagement() { @Test void doesNotAddMavenDefaultProperties() { rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, "dir/pom.xml")), + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), pomXml( """ @@ -662,7 +662,7 @@ void doesNotAddMavenDefaultProperties() { @Test void doesNotAddGrandparentProperties() { rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, "dir/pom.xml")), + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), pomXml( """ @@ -752,7 +752,7 @@ void doesNotAddGlobalProperties() { @Test void addParentPomWithMatchingFilePattern() { rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), + spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, "dir/pom.xml")), pomXml( """ From 314b0ddda9c3275bca3f76ef7c549d847bc444a4 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Fri, 28 Jun 2024 12:00:42 -0300 Subject: [PATCH 11/20] Better error handling --- .../src/main/java/org/openrewrite/maven/AddParentPom.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index fba91493f14..b825b1c600a 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -149,11 +149,17 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { try { Optional targetVersion = findAcceptableVersion(groupId, artifactId, ctx); + // TODO What to do if not present? targetVersion.ifPresent((v) -> addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + v + ""))) ); } catch (MavenDownloadingException e) { - throw new RuntimeException(e); + for (Map.Entry repositoryResponse : e.getRepositoryResponses().entrySet()) { + MavenRepository repository = repositoryResponse.getKey(); + metadataFailures.insertRow(ctx, new MavenMetadataFailures.Row(groupId, artifactId, version, + repository.getUri(), repository.getSnapshots(), repository.getReleases(), repositoryResponse.getValue())); + } + return e.warn(tag); } if (relativePath != null) { From 4f0e60f496bd5527828ffa1fdd0c7a54e1af97ba Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 28 Jun 2024 23:27:38 +0200 Subject: [PATCH 12/20] Apply formatter and drop unused code --- .../org/openrewrite/maven/AddParentPom.java | 124 +----------------- .../openrewrite/maven/AddParentPomTest.java | 52 ++++---- 2 files changed, 30 insertions(+), 146 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index b825b1c600a..802e51027a4 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -22,21 +22,16 @@ import org.openrewrite.internal.lang.Nullable; import org.openrewrite.marker.SearchResult; import org.openrewrite.maven.table.MavenMetadataFailures; -import org.openrewrite.maven.tree.*; +import org.openrewrite.maven.tree.MavenMetadata; +import org.openrewrite.maven.tree.MavenRepository; import org.openrewrite.semver.Semver; import org.openrewrite.semver.VersionComparator; import org.openrewrite.xml.AddToTagVisitor; -import org.openrewrite.xml.ChangeTagValueVisitor; -import org.openrewrite.xml.TagNameComparator; import org.openrewrite.xml.tree.Xml; import java.util.*; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; -import static java.util.Collections.emptyList; - @Value @EqualsAndHashCode(callSuper = false) public class AddParentPom extends Recipe { @@ -116,7 +111,7 @@ public TreeVisitor getVisitor() { public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { Xml.Tag root = document.getRoot(); if (!root.getChild("parent").isPresent() && - (filePattern == null || PathUtils.matchesGlob(document.getSourcePath(), filePattern))) { + (filePattern == null || PathUtils.matchesGlob(document.getSourcePath(), filePattern))) { return SearchResult.found(document); } return document; @@ -132,7 +127,7 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { assert !root.getChild("parent").isPresent(); document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build(""), new MavenTagInsertionComparator(root.getChildren())) - .visitNonNull(document, ctx, getCursor().getParentOrThrow()); + .visitNonNull(document, ctx, getCursor().getParentOrThrow()); return super.visitDocument(document, ctx); } @@ -196,115 +191,4 @@ private Optional findAcceptableVersion(String groupId, String artifactId } }); } - - private static Pattern PROPERTY_PATTERN = Pattern.compile("\\$\\{([^}]+)}"); - - private static Map getPropertiesInUse(Xml.Document pomXml, ExecutionContext ctx) { - Map properties = new HashMap<>(); - new MavenIsoVisitor() { - - @Nullable - ResolvedPom resolvedPom = null; - - @Override - public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { - Xml.Tag t = super.visitTag(tag, ctx); - if (t.getContent() != null && t.getContent().size() == 1 && t.getContent().get(0) instanceof Xml.CharData) { - String text = ((Xml.CharData) t.getContent().get(0)).getText().trim(); - Matcher m = PROPERTY_PATTERN.matcher(text); - while (m.find()) { - if (resolvedPom == null) { - resolvedPom = getResolutionResult().getPom(); - } - String propertyName = m.group(1).trim(); - if (resolvedPom.getProperties().containsKey(propertyName) && !isGlobalProperty(propertyName)) { - properties.put(m.group(1).trim(), resolvedPom.getProperties().get(propertyName)); - } - } - } - return t; - } - - private boolean isGlobalProperty(String propertyName) { - return propertyName.startsWith("project.") || propertyName.startsWith("env.") - || propertyName.startsWith("settings.") || propertyName.equals("basedir"); - } - }.visit(pomXml, ctx); - return properties; - } - - private List getDependenciesUnmanagedByNewParent(MavenResolutionResult mrr, ResolvedPom newParent) { - ResolvedPom resolvedPom = mrr.getPom(); - - // Dependencies managed by the current pom's own dependency management are irrelevant to parent upgrade - List locallyManaged = mrr.getPom().getRequested().getDependencyManagement(); - - Set requestedWithoutExplicitVersion = resolvedPom.getRequested().getDependencies().stream() - .filter(dep -> dep.getVersion() == null) - // Dependencies explicitly managed by the current pom require no changes - .filter(dep -> locallyManaged.stream() - .noneMatch(localManagedDep -> localManagedDep.getGroupId().equals(dep.getGroupId()) && localManagedDep.getArtifactId().equals(dep.getArtifactId()))) - .map(dep -> new GroupArtifactVersion(dep.getGroupId(), dep.getArtifactId(), null)) - .collect(Collectors.toCollection(LinkedHashSet::new)); - - if (requestedWithoutExplicitVersion.isEmpty()) { - return emptyList(); - } - - List depsWithoutExplicitVersion = resolvedPom.getDependencyManagement().stream() - .filter(dep -> requestedWithoutExplicitVersion.contains(dep.getGav().withVersion(null))) - // Exclude dependencies managed by a bom imported by the current pom - .filter(dep -> dep.getBomGav() == null || locallyManaged.stream() - .noneMatch(localManagedDep -> localManagedDep.getGroupId().equals(dep.getBomGav().getGroupId()) && localManagedDep.getArtifactId().equals(dep.getBomGav().getArtifactId()))) - .collect(Collectors.toList()); - - if (depsWithoutExplicitVersion.isEmpty()) { - return emptyList(); - } - - // Remove from the list any that would still be managed under the new parent - Set newParentManagedGa = newParent.getDependencyManagement().stream() - .map(dep -> new GroupArtifact(dep.getGav().getGroupId(), dep.getGav().getArtifactId())) - .collect(Collectors.toSet()); - - depsWithoutExplicitVersion = depsWithoutExplicitVersion.stream() - .filter(it -> !newParentManagedGa.contains(new GroupArtifact(it.getGav().getGroupId(), it.getGav().getArtifactId()))) - .collect(Collectors.toList()); - return depsWithoutExplicitVersion; - } - - @Value - @EqualsAndHashCode(callSuper = false) - private static class UnconditionalAddProperty extends MavenIsoVisitor { - String key; - String value; - - @Override - public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { - Xml.Document d = super.visitDocument(document, ctx); - Xml.Tag root = d.getRoot(); - Optional properties = root.getChild("properties"); - if (!properties.isPresent()) { - Xml.Tag propertiesTag = Xml.Tag.build("\n<" + key + ">" + value + "\n"); - d = (Xml.Document) new AddToTagVisitor(root, propertiesTag, new MavenTagInsertionComparator(root.getChildren())).visitNonNull(d, ctx); - } else if (!properties.get().getChildValue(key).isPresent()) { - Xml.Tag propertyTag = Xml.Tag.build("<" + key + ">" + value + ""); - d = (Xml.Document) new AddToTagVisitor<>(properties.get(), propertyTag, new TagNameComparator()).visitNonNull(d, ctx); - } - if (d != document) { - maybeUpdateModel(); - } - return d; - } - - @Override - public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { - Xml.Tag t = super.visitTag(tag, ctx); - if (isPropertyTag() && key.equals(tag.getName()) - && !value.equals(tag.getValue().orElse(null))) { - t = (Xml.Tag) new ChangeTagValueVisitor<>(tag, value).visitNonNull(t, ctx); - } - return t; - } - } } diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index 9556ad17523..66d1401ec7d 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -41,7 +41,7 @@ void addParent() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -55,7 +55,7 @@ void addParent() { spring-boot-starter-parent 1.5.12.RELEASE - + com.mycompany.app my-app 1 @@ -80,7 +80,7 @@ void addParentWithRelativePath() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -95,7 +95,7 @@ void addParentWithRelativePath() { 1.5.12.RELEASE ../../pom.xml - + com.mycompany.app my-app 1 @@ -120,7 +120,7 @@ void addParentWithRelativePathEmptyValue() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -135,7 +135,7 @@ void addParentWithRelativePathEmptyValue() { 1.5.12.RELEASE - + com.mycompany.app my-app 1 @@ -159,7 +159,7 @@ void multiModuleRelativePath() { org.sample sample 1.0.0 - + module1 module2 @@ -236,7 +236,7 @@ void multiModuleRelativePathChangeChildrens() { org.sample sample 1.0.0 - + module1 module2 @@ -255,7 +255,7 @@ void multiModuleRelativePathChangeChildrens() { org.sample sample 1.0.0 - + module1 module2 @@ -313,7 +313,7 @@ void wildcardVersion() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -327,7 +327,7 @@ void wildcardVersion() { spring-boot-starter-parent 1.5.22.RELEASE - + com.mycompany.app my-app 1 @@ -352,11 +352,11 @@ void removesRedundantExplicitVersionsMatchingNewParent() { """ 4.0.0 - + com.mycompany.app my-app 1 - + org.junit.jupiter @@ -375,11 +375,11 @@ void removesRedundantExplicitVersionsMatchingNewParent() { 5.9.1 - + com.mycompany.app my-app 1 - + org.junit.jupiter @@ -407,11 +407,11 @@ void takesNewVersionFromParent() { """ 4.0.0 - + com.mycompany.app my-app 1 - + org.junit.jupiter @@ -430,11 +430,11 @@ void takesNewVersionFromParent() { 5.9.1 - + com.mycompany.app my-app 1 - + org.junit.jupiter @@ -462,7 +462,7 @@ void multiModule() { org.sample sample 1.0.0 - + module1 module2 @@ -631,7 +631,7 @@ void doesNotAddMavenDefaultProperties() { org.sample sample 1.0.0 - + ${project.build.directory} @@ -649,7 +649,7 @@ void doesNotAddMavenDefaultProperties() { org.sample sample 1.0.0 - + ${project.build.directory} @@ -671,7 +671,7 @@ void doesNotAddGrandparentProperties() { org.sample sample 1.0.0 - + ${junit.version} @@ -689,7 +689,7 @@ void doesNotAddGrandparentProperties() { org.sample sample 1.0.0 - + ${junit.version} @@ -711,7 +711,7 @@ void doesNotAddGlobalProperties() { org.sample sample 1.0.0 - + ${basedir} ${project.basedir} @@ -734,7 +734,7 @@ void doesNotAddGlobalProperties() { org.sample sample 1.0.0 - + ${basedir} ${project.basedir} From 4efbc9149baaaeda9cd134da630a5d0c0c14dc32 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Tue, 2 Jul 2024 21:03:58 -0300 Subject: [PATCH 13/20] Adding tests with non-existing version They are failing --- .../openrewrite/maven/AddParentPomTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index 66d1401ec7d..c01fc330d71 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -65,6 +65,58 @@ void addParent() { ); } + @DocumentExample + @Test + void addParentWithNonExistingVersion() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.springframework.boot", + "spring-boot-starter-parent", + "1.5.99.RELEASE", + null, + null, + null + )), + pomXml( + """ + + 4.0.0 + + com.mycompany.app + my-app + 1 + + """ + ) + ); + } + + @DocumentExample + @Test + void addParentWithNonExistingVersionWildcard() { + rewriteRun( + spec -> spec.recipe(new AddParentPom( + "org.springframework.boot", + "spring-boot-starter-parent", + "~1.9", + null, + null, + null + )), + pomXml( + """ + + 4.0.0 + + com.mycompany.app + my-app + 1 + + """ + ) + ); + } + @Test void addParentWithRelativePath() { rewriteRun( From a6cb88566394d7f4e656c8a55239e2625b502d98 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Tue, 2 Jul 2024 22:19:59 -0300 Subject: [PATCH 14/20] Moved logic to `visitDocument` method to fix the empty parent tag when version is invalid Now `RemoveRedundantDependencyVersions` doesn't work... --- .../org/openrewrite/maven/AddParentPom.java | 71 ++++++++----------- 1 file changed, 28 insertions(+), 43 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index 802e51027a4..39f08c2a49e 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -126,55 +126,40 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { Xml.Tag root = document.getRoot(); assert !root.getChild("parent").isPresent(); - document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build(""), new MavenTagInsertionComparator(root.getChildren())) - .visitNonNull(document, ctx, getCursor().getParentOrThrow()); + try { + Optional targetVersion = findAcceptableVersion(groupId, artifactId, ctx); + if (targetVersion.isPresent()) { - return super.visitDocument(document, ctx); - } + Xml.Tag parentTag = Xml.Tag.build("\n" + + "" + groupId + "\n" + + "" + artifactId + "\n" + + "" + targetVersion.get() + "\n" + + (relativePath != null ? + StringUtils.isBlank(relativePath) ? + "" + : "" + relativePath + "" + : "" + ) + + ""); - @Override - public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { - Xml.Tag t = super.visitTag(tag, ctx); - - if (isParentTag()) { - List> addToTagVisitors = new ArrayList<>(); - - addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + groupId + ""))); - addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + artifactId + ""))); - - try { - Optional targetVersion = findAcceptableVersion(groupId, artifactId, ctx); - // TODO What to do if not present? - targetVersion.ifPresent((v) -> - addToTagVisitors.add(new AddToTagVisitor<>(t, Xml.Tag.build("" + v + ""))) - ); - } catch (MavenDownloadingException e) { - for (Map.Entry repositoryResponse : e.getRepositoryResponses().entrySet()) { - MavenRepository repository = repositoryResponse.getKey(); - metadataFailures.insertRow(ctx, new MavenMetadataFailures.Row(groupId, artifactId, version, - repository.getUri(), repository.getSnapshots(), repository.getReleases(), repositoryResponse.getValue())); - } - return e.warn(tag); - } - if (relativePath != null) { - final Xml.Tag relativePathTag; - if (StringUtils.isBlank(relativePath)) { - relativePathTag = Xml.Tag.build(""); - } else { - relativePathTag = Xml.Tag.build("" + relativePath + ""); - } - addToTagVisitors.add(new AddToTagVisitor<>(t, relativePathTag)); - } + document = (Xml.Document) new AddToTagVisitor<>(root, parentTag, new MavenTagInsertionComparator(root.getChildren())) + .visitNonNull(document, ctx, getCursor().getParentOrThrow()); - for (TreeVisitor visitor : addToTagVisitors) { - doAfterVisit(visitor); + doAfterVisit(new RemoveRedundantDependencyVersions(null, null, + RemoveRedundantDependencyVersions.Comparator.GTE, null).getVisitor()); + + } + } catch(MavenDownloadingException e) { + for (Map.Entry repositoryResponse : e.getRepositoryResponses().entrySet()) { + MavenRepository repository = repositoryResponse.getKey(); + metadataFailures.insertRow(ctx, new MavenMetadataFailures.Row(groupId, artifactId, version, + repository.getUri(), repository.getSnapshots(), repository.getReleases(), repositoryResponse.getValue())); } - maybeUpdateModel(); - doAfterVisit(new RemoveRedundantDependencyVersions(null, null, - RemoveRedundantDependencyVersions.Comparator.GTE, null).getVisitor()); + return e.warn(document); } - return t; + + return super.visitDocument(document, ctx); } private Optional findAcceptableVersion(String groupId, String artifactId, ExecutionContext ctx) From 296c771ed182e34a8ddac65d9031db9f69198106 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Tue, 2 Jul 2024 22:36:32 -0300 Subject: [PATCH 15/20] Remove useless test cases Co-authored-by: Tim te Beek --- .../openrewrite/maven/AddParentPomTest.java | 130 ------------------ 1 file changed, 130 deletions(-) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index c01fc330d71..7c2f9309013 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -671,136 +671,6 @@ void shouldNotAddToDependencyManagement() { )); } - @Test - void doesNotAddMavenDefaultProperties() { - rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), - pomXml( - """ - - - 4.0.0 - org.sample - sample - 1.0.0 - - - ${project.build.directory} - - - """, - """ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.7.18 - - org.sample - sample - 1.0.0 - - - ${project.build.directory} - - - """ - ) - ); - } - - @Test - void doesNotAddGrandparentProperties() { - rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), - pomXml( - """ - - - 4.0.0 - org.sample - sample - 1.0.0 - - - ${junit.version} - - - """, - """ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.7.18 - - org.sample - sample - 1.0.0 - - - ${junit.version} - - - """ - ) - ); - } - - @Test - void doesNotAddGlobalProperties() { - rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, null)), - pomXml( - """ - - - 4.0.0 - org.sample - sample - 1.0.0 - - - ${basedir} - ${project.basedir} - ${project.build.directory} - ${project.version} - ${env.GIT_HOME} - ${settings.offline} - - - """, - """ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.7.18 - - org.sample - sample - 1.0.0 - - - ${basedir} - ${project.basedir} - ${project.build.directory} - ${project.version} - ${env.GIT_HOME} - ${settings.offline} - - - """ - ) - ); - } - @Test void addParentPomWithMatchingFilePattern() { rewriteRun( From 91ca361fa59eda116afc93c6bf90b72f9b12d32f Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Tue, 2 Jul 2024 22:25:33 -0300 Subject: [PATCH 16/20] Removing filePattern option in favor of Preconditions. --- .../org/openrewrite/maven/AddParentPom.java | 13 +--- .../openrewrite/maven/AddParentPomTest.java | 65 ++----------------- 2 files changed, 5 insertions(+), 73 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index 39f08c2a49e..c15e8a0aa87 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -66,16 +66,6 @@ public class AddParentPom extends Recipe { @Nullable String versionPattern; - @Option(displayName = "File pattern", - description = "A glob expression that can be used to constrain which directories or source files should be searched. " + - "Multiple patterns may be specified, separated by a semicolon `;`. " + - "If multiple patterns are supplied any of the patterns matching will be interpreted as a match. " + - "When not set, all source files are searched. ", - required = false, - example = "**/*-parent/grpc-*/pom.xml") - @Nullable - String filePattern; - @Override public String getDisplayName() { return "Change Maven parent"; @@ -110,8 +100,7 @@ public TreeVisitor getVisitor() { @Override public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { Xml.Tag root = document.getRoot(); - if (!root.getChild("parent").isPresent() && - (filePattern == null || PathUtils.matchesGlob(document.getSourcePath(), filePattern))) { + if (!root.getChild("parent").isPresent()) { return SearchResult.found(document); } return document; diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index 7c2f9309013..62fbe620199 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -34,7 +34,6 @@ void addParent() { "spring-boot-starter-parent", "1.5.12.RELEASE", null, - null, null )), pomXml( @@ -74,7 +73,6 @@ void addParentWithNonExistingVersion() { "spring-boot-starter-parent", "1.5.99.RELEASE", null, - null, null )), pomXml( @@ -100,7 +98,6 @@ void addParentWithNonExistingVersionWildcard() { "spring-boot-starter-parent", "~1.9", null, - null, null )), pomXml( @@ -125,7 +122,6 @@ void addParentWithRelativePath() { "spring-boot-starter-parent", "1.5.12.RELEASE", "../../pom.xml", - null, null )), pomXml( @@ -165,7 +161,6 @@ void addParentWithRelativePathEmptyValue() { "spring-boot-starter-parent", "1.5.12.RELEASE", "", - null, null )), pomXml( @@ -199,7 +194,7 @@ void addParentWithRelativePathEmptyValue() { @Test void multiModuleRelativePath() { - AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null, null); + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", "", null); rewriteRun( spec -> spec.recipe(recipe), mavenProject("parent", @@ -276,7 +271,7 @@ void multiModuleRelativePath() { @Test void multiModuleRelativePathChangeChildrens() { - AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", null, null, null); + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", null, null); rewriteRun( spec -> spec.recipe(recipe), mavenProject("parent", @@ -358,7 +353,6 @@ void wildcardVersion() { "spring-boot-starter-parent", "~1.5", null, - null, null )), pomXml( @@ -397,7 +391,6 @@ void removesRedundantExplicitVersionsMatchingNewParent() { "junit-bom", "5.9.1", "", - null, null )), pomXml( @@ -452,7 +445,6 @@ void takesNewVersionFromParent() { "junit-bom", "5.9.1", "", - null, null )), pomXml( @@ -502,7 +494,7 @@ void takesNewVersionFromParent() { @Test @Issue("https://github.com/openrewrite/rewrite/issues/1753") void multiModule() { - AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.6.7", null, null, null); + AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.6.7", null, null); rewriteRun( spec -> spec.recipe(recipe), mavenProject("parent", @@ -579,7 +571,7 @@ void multiModule() { @Test void shouldNotAddToDependencyManagement() { rewriteRun( - spec -> spec.recipe(new AddParentPom("org.jenkins-ci.plugins", "plugin", "4.81", "", null, null)), + spec -> spec.recipe(new AddParentPom("org.jenkins-ci.plugins", "plugin", "4.81", "", null)), // language=xml pomXml( """ @@ -670,53 +662,4 @@ void shouldNotAddToDependencyManagement() { """ )); } - - @Test - void addParentPomWithMatchingFilePattern() { - rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, "dir/pom.xml")), - pomXml( - """ - - 4.0.0 - com.mycompany.app - my-app - 1 - - """, - """ - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.7.18 - - com.mycompany.app - my-app - 1 - - """, - spec -> spec.path("dir/pom.xml") - ) - ); - } - - @Test - void addParentPomWithNonMatchingFilePattern() { - rewriteRun( - spec -> spec.recipe(new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.7.18", null, null, "dir/pom.xml")), - pomXml( - """ - - 4.0.0 - com.mycompany.app - my-app - 1 - - """, - spec -> spec.path("pom.xml") - ) - ); - } } From 305e3fb848bab58d77cc287895f69c185ac41f98 Mon Sep 17 00:00:00 2001 From: Rodrigo Carvalho Silva Date: Tue, 2 Jul 2024 22:43:06 -0300 Subject: [PATCH 17/20] Fixing description --- .../src/main/java/org/openrewrite/maven/AddParentPom.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index c15e8a0aa87..ffb14fa7d88 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -78,7 +78,7 @@ public String getInstanceNameSuffix() { @Override public String getDescription() { - return "Change the parent pom of a Maven pom.xml. Identifies the parent pom to be changed by its groupId and artifactId."; + return "Add a parent pom to a Maven pom.xml. Doesn't add in case of a parent already present."; } @Override From 1f13889ab35dbf483189626aba64bddceb4246cc Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 3 Jul 2024 10:22:55 +0200 Subject: [PATCH 18/20] Restore `maybeUpdateModel()` before `RemoveRedundantDependencyVersions` --- .../org/openrewrite/maven/AddParentPom.java | 20 +++++++++---------- .../openrewrite/maven/AddParentPomTest.java | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index ffb14fa7d88..38f0b88d332 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -29,7 +29,9 @@ import org.openrewrite.xml.AddToTagVisitor; import org.openrewrite.xml.tree.Xml; -import java.util.*; +import java.util.Collection; +import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; @Value @@ -119,27 +121,24 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { Optional targetVersion = findAcceptableVersion(groupId, artifactId, ctx); if (targetVersion.isPresent()) { - Xml.Tag parentTag = Xml.Tag.build("\n" + + Xml.Tag parentTag = Xml.Tag.build( + "\n" + "" + groupId + "\n" + "" + artifactId + "\n" + "" + targetVersion.get() + "\n" + - (relativePath != null ? - StringUtils.isBlank(relativePath) ? - "" - : "" + relativePath + "" - : "" - ) + + (relativePath == null ? "" : StringUtils.isBlank(relativePath) ? + "" : "" + relativePath + "") + ""); - document = (Xml.Document) new AddToTagVisitor<>(root, parentTag, new MavenTagInsertionComparator(root.getChildren())) .visitNonNull(document, ctx, getCursor().getParentOrThrow()); + maybeUpdateModel(); doAfterVisit(new RemoveRedundantDependencyVersions(null, null, RemoveRedundantDependencyVersions.Comparator.GTE, null).getVisitor()); } - } catch(MavenDownloadingException e) { + } catch (MavenDownloadingException e) { for (Map.Entry repositoryResponse : e.getRepositoryResponses().entrySet()) { MavenRepository repository = repositoryResponse.getKey(); metadataFailures.insertRow(ctx, new MavenMetadataFailures.Row(groupId, artifactId, version, @@ -156,7 +155,6 @@ private Optional findAcceptableVersion(String groupId, String artifactId if (availableVersions == null) { MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, ctx)); - //noinspection EqualsWithItself availableVersions = mavenMetadata.getVersioning().getVersions().stream() .filter(v -> versionComparator.isValid(null, v)) .collect(Collectors.toList()); diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index 62fbe620199..4f62e7aa43b 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -79,7 +79,7 @@ void addParentWithNonExistingVersion() { """ 4.0.0 - + com.mycompany.app my-app 1 @@ -104,7 +104,7 @@ void addParentWithNonExistingVersionWildcard() { """ 4.0.0 - + com.mycompany.app my-app 1 From 09ce19a1547a37a4a5264e7cf5310b1f43b82fd1 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 3 Jul 2024 10:56:00 +0200 Subject: [PATCH 19/20] Update name and description; move variable inside visitor --- .../java/org/openrewrite/maven/AddParentPom.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java index 38f0b88d332..d495affc70f 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/AddParentPom.java @@ -31,6 +31,7 @@ import java.util.Collection; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; @@ -70,7 +71,7 @@ public class AddParentPom extends Recipe { @Override public String getDisplayName() { - return "Change Maven parent"; + return "Add Maven parent"; } @Override @@ -80,7 +81,7 @@ public String getInstanceNameSuffix() { @Override public String getDescription() { - return "Add a parent pom to a Maven pom.xml. Doesn't add in case of a parent already present."; + return "Add a parent pom to a Maven pom.xml. Does nothing if a parent pom is already present."; } @Override @@ -95,9 +96,6 @@ public Validated validate() { @Override public TreeVisitor getVisitor() { - VersionComparator versionComparator = Semver.validate(version, versionPattern).getValue(); - assert versionComparator != null; - return Preconditions.check(new MavenVisitor() { @Override public Xml visitDocument(Xml.Document document, ExecutionContext ctx) { @@ -120,7 +118,6 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { try { Optional targetVersion = findAcceptableVersion(groupId, artifactId, ctx); if (targetVersion.isPresent()) { - Xml.Tag parentTag = Xml.Tag.build( "\n" + "" + groupId + "\n" + @@ -136,7 +133,6 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { maybeUpdateModel(); doAfterVisit(new RemoveRedundantDependencyVersions(null, null, RemoveRedundantDependencyVersions.Comparator.GTE, null).getVisitor()); - } } catch (MavenDownloadingException e) { for (Map.Entry repositoryResponse : e.getRepositoryResponses().entrySet()) { @@ -150,9 +146,10 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { return super.visitDocument(document, ctx); } + private final VersionComparator versionComparator = Objects.requireNonNull(Semver.validate(version, versionPattern).getValue()); + private Optional findAcceptableVersion(String groupId, String artifactId, ExecutionContext ctx) throws MavenDownloadingException { - if (availableVersions == null) { MavenMetadata mavenMetadata = metadataFailures.insertRows(ctx, () -> downloadMetadata(groupId, artifactId, ctx)); availableVersions = mavenMetadata.getVersioning().getVersions().stream() From d5d643326c00cdfebe5ed8395e302fbb245ed65f Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 3 Jul 2024 11:01:55 +0200 Subject: [PATCH 20/20] Condense and remove duplicate tests --- .../openrewrite/maven/AddParentPomTest.java | 222 ++---------------- 1 file changed, 16 insertions(+), 206 deletions(-) diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java index 4f62e7aa43b..a2575e1e656 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/AddParentPomTest.java @@ -16,8 +16,9 @@ package org.openrewrite.maven; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.openrewrite.DocumentExample; -import org.openrewrite.Issue; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.mavenProject; @@ -64,39 +65,17 @@ void addParent() { ); } - @DocumentExample - @Test - void addParentWithNonExistingVersion() { - rewriteRun( - spec -> spec.recipe(new AddParentPom( - "org.springframework.boot", - "spring-boot-starter-parent", - "1.5.99.RELEASE", - null, - null - )), - pomXml( - """ - - 4.0.0 - - com.mycompany.app - my-app - 1 - - """ - ) - ); - } - - @DocumentExample - @Test - void addParentWithNonExistingVersionWildcard() { + @ParameterizedTest + @ValueSource(strings = { + "1.5.99.RELEASE", + "~1.9" + }) + void shoudNotAddInvalidParentPomVersion(String version) { rewriteRun( spec -> spec.recipe(new AddParentPom( "org.springframework.boot", "spring-boot-starter-parent", - "~1.9", + version, null, null )), @@ -270,10 +249,13 @@ void multiModuleRelativePath() { } @Test - void multiModuleRelativePathChangeChildrens() { - AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "1.5.12.RELEASE", null, null); + void multiModuleShouldOnlyChangeRoot() { rewriteRun( - spec -> spec.recipe(recipe), + spec -> spec.recipe(new AddParentPom( + "org.springframework.boot", + "spring-boot-starter-parent", + "1.5.12.RELEASE", + null, null)), mavenProject("parent", pomXml( """ @@ -346,7 +328,7 @@ void multiModuleRelativePathChangeChildrens() { } @Test - void wildcardVersion() { + void shouldResolveWildcardVersion() { rewriteRun( spec -> spec.recipe(new AddParentPom( "org.springframework.boot", @@ -490,176 +472,4 @@ void takesNewVersionFromParent() { ) ); } - - @Test - @Issue("https://github.com/openrewrite/rewrite/issues/1753") - void multiModule() { - AddParentPom recipe = new AddParentPom("org.springframework.boot", "spring-boot-starter-parent", "2.6.7", null, null); - rewriteRun( - spec -> spec.recipe(recipe), - mavenProject("parent", - pomXml( - """ - - - 4.0.0 - org.sample - sample - 1.0.0 - - - module1 - module2 - - - """, - """ - - - 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - 2.6.7 - - org.sample - sample - 1.0.0 - - - module1 - module2 - - - """ - ), - mavenProject("module1", - pomXml( - """ - - - 4.0.0 - - org.sample - sample - 1.0.0 - - module1 - - """ - )), - mavenProject("module2", - pomXml( - """ - - - 4.0.0 - - org.sample - sample - 1.0.0 - - module2 - - """ - ) - ) - ) - ); - } - - @Test - void shouldNotAddToDependencyManagement() { - rewriteRun( - spec -> spec.recipe(new AddParentPom("org.jenkins-ci.plugins", "plugin", "4.81", "", null)), - // language=xml - pomXml( - """ - - 4.0.0 - org.sample - sample - 1.0.0 - - 2.387.3 - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - - io.jenkins.tools.bom - bom-2.387.x - 2516.v113cb_3d00317 - pom - import - - - - - - org.jenkins-ci.plugins - junit - - - - """, - """ - - 4.0.0 - - org.jenkins-ci.plugins - plugin - 4.81 - - - org.sample - sample - 1.0.0 - - 2.387.3 - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - repo.jenkins-ci.org - https://repo.jenkins-ci.org/public/ - - - - - - io.jenkins.tools.bom - bom-2.387.x - 2516.v113cb_3d00317 - pom - import - - - - - - org.jenkins-ci.plugins - junit - - - - """ - )); - } }