From 17b4a106c5e13910730e8c3623eadf8af4a932d1 Mon Sep 17 00:00:00 2001 From: Tyler Van Gorder Date: Mon, 14 Nov 2022 12:29:49 -0800 Subject: [PATCH] Rewrite recipes that upgrade jaxb/jax-ws and add explicit runtimes. Fixes #145 --- .../j2ee/UpdateJaxbRuntimeToJakartaEE8.java | 178 +++++++++++++ .../j2ee/UpdateJaxwsRuntimeToJakartaEE8.java | 155 +++++++++++ .../migrate/{javax => j2ee}/package-info.java | 2 +- .../java/migrate/javax/AddJaxbRuntime.java | 160 ------------ .../java/migrate/javax/AddJaxwsRuntime.java | 131 ---------- .../javax/ReplaceJavaxJaxbWithJakarta.java | 61 ----- .../javax/ReplaceJavaxJaxwsWithJakarta.java | 61 ----- .../rewrite/add-jaxb-dependencies.yml | 44 ---- .../rewrite/add-jaxws-dependencies.yml | 44 ---- .../META-INF/rewrite/java-version-11.yml | 7 +- .../UpdateJaxbRuntimeToJakartaEE8Test.java | 244 ++++++++++++++++++ .../UpdateJaxwsRuntimeToJakartaEE8Test.java | 239 +++++++++++++++++ .../jakarta/EhcacheJavaxtoJakartaTest.java | 6 +- 13 files changed, 824 insertions(+), 508 deletions(-) create mode 100644 src/main/java/org/openrewrite/java/migrate/j2ee/UpdateJaxbRuntimeToJakartaEE8.java create mode 100644 src/main/java/org/openrewrite/java/migrate/j2ee/UpdateJaxwsRuntimeToJakartaEE8.java rename src/main/java/org/openrewrite/java/migrate/{javax => j2ee}/package-info.java (94%) delete mode 100644 src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java delete mode 100644 src/main/java/org/openrewrite/java/migrate/javax/AddJaxwsRuntime.java delete mode 100644 src/main/java/org/openrewrite/java/migrate/javax/ReplaceJavaxJaxbWithJakarta.java delete mode 100644 src/main/java/org/openrewrite/java/migrate/javax/ReplaceJavaxJaxwsWithJakarta.java delete mode 100644 src/main/resources/META-INF/rewrite/add-jaxb-dependencies.yml delete mode 100644 src/main/resources/META-INF/rewrite/add-jaxws-dependencies.yml create mode 100644 src/test/java/org/openrewrite/java/migrate/j2ee/UpdateJaxbRuntimeToJakartaEE8Test.java create mode 100644 src/test/java/org/openrewrite/java/migrate/j2ee/UpdateJaxwsRuntimeToJakartaEE8Test.java diff --git a/src/main/java/org/openrewrite/java/migrate/j2ee/UpdateJaxbRuntimeToJakartaEE8.java b/src/main/java/org/openrewrite/java/migrate/j2ee/UpdateJaxbRuntimeToJakartaEE8.java new file mode 100644 index 0000000000..0e9b132239 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/j2ee/UpdateJaxbRuntimeToJakartaEE8.java @@ -0,0 +1,178 @@ +/* + * 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.java.migrate.j2ee; + +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.*; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.maven.*; +import org.openrewrite.maven.tree.*; +import org.openrewrite.xml.tree.Xml; + +import java.time.Duration; +import java.util.*; + +@Value +@EqualsAndHashCode(callSuper = true) +public class UpdateJaxbRuntimeToJakartaEE8 extends Recipe { + + private static final String LEGACY_JAVA_JAXB_API_GROUP = "javax.xml.bind"; + private static final String LEGACY_JAVA_JAXB_API_ARTIFACT = "jaxb-api"; + + private static final String JAKARTA_API_GROUP = "jakarta.xml.bind"; + private static final String JAKARTA_API_ARTIFACT = "jakarta.xml.bind-api"; + + private static final String SUN_JAXB_RUNTIME_GROUP = "com.sun.xml.bind"; + private static final String SUN_JAXB_RUNTIME_ARTIFACT = "jaxb-impl"; + + private static final String GLASSFISH_JAXB_RUNTIME_GROUP = "org.glassfish.jaxb"; + private static final String GLASSFISH_JAXB_RUNTIME_ARTIFACT = "jaxb-runtime"; + + @Option(displayName = "JAXB run-time", + description = "Which implementation of the JAXB run-time that will be added to maven projects that have transitive dependencies on the JAXB API", + valid = {"glassfish", "sun"}, + example = "glassfish") + String runtime; + + @Override + public String getDisplayName() { + return "Use latest JAXB API and runtime for Jakarta EE 8"; + } + + @Override + public String getDescription() { + return "Update maven build files to use the latest JAXB API from Jakarta EE8 and add a compatible runtime " + + "dependency to maintain compatibility with Java versions greater than Java 8. This recipe will change " + + "existing dependencies on `javax.xml.bind:jax-api` to `jakarta.xml.bind:jakarta.xml.bind-api`. The " + + "recipe will also add a JAXB run-time, in `provided` scope, to any project that has a transitive dependency " + + "on the JAXB API. **The resulting dependencies still use the `javax` namespace, despite the move " + + "to the Jakarta artifact.**"; + } + + @Override + public Duration getEstimatedEffortPerOccurrence() { + return Duration.ofMinutes(30); + } + + @Override + public Set getTags() { + return new HashSet<>(Arrays.asList("javax", "jakarta", "j2ee", "jaxb", "glassfish", "java11")); + } + + @Override + protected TreeVisitor getVisitor() { + return new MavenIsoVisitor() { + @SuppressWarnings("ConstantConditions") + @Override + public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { + //remove legacy jaxb-core, regardless of which runtime is being used. + doAfterVisit(new RemoveDependency("com.sun.xml.bind", "jaxb-core", null)); + Xml.Document d = super.visitDocument(document, ctx); + + d = (Xml.Document) new ChangeDependencyGroupIdAndArtifactId( + LEGACY_JAVA_JAXB_API_GROUP, LEGACY_JAVA_JAXB_API_ARTIFACT, + JAKARTA_API_GROUP, JAKARTA_API_ARTIFACT, "2.3.2", null + ).getVisitor().visit(d, ctx); + d = (Xml.Document) new ChangeManagedDependencyGroupIdAndArtifactId( + LEGACY_JAVA_JAXB_API_GROUP, LEGACY_JAVA_JAXB_API_ARTIFACT, + JAKARTA_API_GROUP, JAKARTA_API_ARTIFACT, "2.3.2" + ).getVisitor().visit(d, ctx); + + //Normalize any existing runtimes to the one selected in this recipe. + if ("sun".equals(runtime)) { + d = (Xml.Document) new ChangeDependencyGroupIdAndArtifactId( + GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, + SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, "2.3.2", null + ).getVisitor().visit(d, ctx); + d = (Xml.Document) new ChangeManagedDependencyGroupIdAndArtifactId( + GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, + SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, "2.3.2" + ).getVisitor().visit(d, ctx); + } else { + d = (Xml.Document) new ChangeDependencyGroupIdAndArtifactId( + SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, + GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, "2.3.2", null + ).getVisitor().visit(d, ctx); + d = (Xml.Document) new ChangeManagedDependencyGroupIdAndArtifactId( + SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, + GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, "2.3.2" + ).getVisitor().visit(d, ctx); + } + if (d != document) { + //If any changes to dependencies were made, return now to allow the maven model to be updated. + //The logic for adding missing dependencies will happen in the next cycle. + return d; + } + d = maybeAddRuntimeDependency(d, ctx); + if (d != document) { + doAfterVisit(new RemoveRedundantDependencyVersions("org.glassfish.jaxb", "*", true)); + doAfterVisit(new RemoveRedundantDependencyVersions("com.sun.xml.bind", "*", true)); + doAfterVisit(new RemoveRedundantDependencyVersions("jakarta.xml.bind", "*", true)); + } + return d; + } + + @SuppressWarnings("ConstantConditions") + private Xml.Document maybeAddRuntimeDependency(Xml.Document d, ExecutionContext ctx) { + + MavenResolutionResult mavenModel = getResolutionResult(); + + Scope apiScope = getTransitiveDependencyScope(mavenModel, JAKARTA_API_GROUP, JAKARTA_API_ARTIFACT); + Scope runtimeScope = "sun".equals(runtime) ? + getTransitiveDependencyScope(mavenModel, SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT) : + getTransitiveDependencyScope(mavenModel, GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT); + + if (apiScope != null && (runtimeScope == null || !apiScope.isInClasspathOf(runtimeScope))) { + String resolvedScope = apiScope == Scope.Test ? "test" : "provided"; + AddDependencyVisitor addDependency = "sun".equals(runtime) ? + new AddDependencyVisitor(SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, "2.3.2", null, resolvedScope, null, null, null, null, null) : + new AddDependencyVisitor(GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, "2.3.2", null, resolvedScope, null, null, null, null, null); + return (Xml.Document) addDependency.visit(d, ctx); + } + + return d; + } + + }; + } + + /** + * Finds the highest scope for a given group/artifact. + * + * @param mavenModel The maven model to search for a dependency. + * @param groupId The group ID of the dependency + * @param artifactId The artifact ID of the dependency + * @return The highest scope of the given dependency or null if the dependency does not exist. + */ + @Nullable + private Scope getTransitiveDependencyScope(MavenResolutionResult mavenModel, String groupId, String artifactId) { + + Scope maxScope = null; + for (Map.Entry> entry : mavenModel.getDependencies().entrySet()) { + for (ResolvedDependency dependency : entry.getValue()) { + if (groupId.equals(dependency.getGroupId()) && artifactId.equals(dependency.getArtifactId())) { + maxScope = Scope.maxPrecedence(maxScope, entry.getKey()); + if (Scope.Compile.equals(maxScope)) { + return maxScope; + } + break; + } + } + } + return maxScope; + } +} diff --git a/src/main/java/org/openrewrite/java/migrate/j2ee/UpdateJaxwsRuntimeToJakartaEE8.java b/src/main/java/org/openrewrite/java/migrate/j2ee/UpdateJaxwsRuntimeToJakartaEE8.java new file mode 100644 index 0000000000..de4028a957 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/j2ee/UpdateJaxwsRuntimeToJakartaEE8.java @@ -0,0 +1,155 @@ +/* + * 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.java.migrate.j2ee; + +import lombok.EqualsAndHashCode; +import lombok.Value; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.maven.*; +import org.openrewrite.maven.tree.MavenResolutionResult; +import org.openrewrite.maven.tree.ResolvedDependency; +import org.openrewrite.maven.tree.Scope; +import org.openrewrite.xml.tree.Xml; + +import java.time.Duration; +import java.util.*; + +@Value +@EqualsAndHashCode(callSuper = true) +public class UpdateJaxwsRuntimeToJakartaEE8 extends Recipe { + + private static final String LEGACY_JAVA_JAXWS_API_GROUP = "javax.xml.ws"; + private static final String LEGACY_JAVA_JAXWS_API_ARTIFACT = "jaxws-api"; + + private static final String JAKARTA_JAXWS_API_GROUP = "jakarta.xml.ws"; + private static final String JAKARTA_JAXWS_API_ARTIFACT = "jakarta.xml.ws-api"; + + private static final String SUN_JAXWS_RUNTIME_GROUP = "com.sun.xml.ws"; + private static final String SUN_JAXWS_RUNTIME_ARTIFACT = "jaxws-rt"; + + @Override + public String getDisplayName() { + return "Use the latest JAX-WS API and runtime for Jakarta EE 8"; + } + + @Override + public String getDescription() { + return "Update maven build files to use the latest JAX-WS API from Jakarta EE 8 and add a compatible runtime " + + "dependency to maintain compatibility with Java version greater than Java 8. This recipe will change " + + "existing dependencies on `javax.xml.ws:jaxws-api` to `jakarta.xml.ws:jakarta.xml.ws-api`. The " + + "recipe will also add a JAXWS run-time, in `provided` scope, to any project that has a transitive dependency " + + "on the JAXWS API. **The resulting dependencies still use the `javax` namespace, despite the move " + + "to the Jakarta artifact.**"; + } + + @Override + public Duration getEstimatedEffortPerOccurrence() { + return Duration.ofMinutes(30); + } + + @Override + public Set getTags() { + return new HashSet<>(Arrays.asList("javax", "jakarta", "j2ee", "jax-ws", "java11")); + } + + + @Override + protected TreeVisitor getVisitor() { + return new MavenIsoVisitor() { + @SuppressWarnings({"ReassignedVariable", "ConstantConditions"}) + @Override + public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) { + // Remove legacy jaxws-ri library (in favor of the jaxws-rt) + doAfterVisit(new RemoveDependency(SUN_JAXWS_RUNTIME_GROUP, "jaxws-ri", null)); + doAfterVisit(new RemoveManagedDependency(SUN_JAXWS_RUNTIME_GROUP, "jaxws-ri", null)); + Xml.Document d = super.visitDocument(document, ctx); + + d = (Xml.Document) new ChangeDependencyGroupIdAndArtifactId( + LEGACY_JAVA_JAXWS_API_GROUP, LEGACY_JAVA_JAXWS_API_ARTIFACT, + JAKARTA_JAXWS_API_GROUP, JAKARTA_JAXWS_API_ARTIFACT, "2.3.2", null + ).getVisitor().visit(d, ctx); + // Change javax.xml.ws:jaxws-api to jakarta.xml.ws:jakarta.xml.ws-api + d = (Xml.Document) new ChangeManagedDependencyGroupIdAndArtifactId( + LEGACY_JAVA_JAXWS_API_GROUP, LEGACY_JAVA_JAXWS_API_ARTIFACT, + JAKARTA_JAXWS_API_GROUP, JAKARTA_JAXWS_API_ARTIFACT, "2.3.2" + ).getVisitor().visit(d, ctx); + + if (d != document) { + //If any changes to dependencies were made, return now to allow the maven model to be updated. + //The logic for adding missing dependencies will happen in the next cycle. + return d; + } + + d = maybeAddRuntimeDependency(d, ctx); + if (d != document) { + doAfterVisit(new RemoveRedundantDependencyVersions("jakarta.xml.ws", "*", true)); + doAfterVisit(new RemoveRedundantDependencyVersions("com.sun.xml.ws", "*", true)); + } + return d; + } + + @SuppressWarnings("ConstantConditions") + private Xml.Document maybeAddRuntimeDependency(Xml.Document d, ExecutionContext ctx) { + + MavenResolutionResult mavenModel = getResolutionResult(); + + //Find the highest scope of a transitive dependency on the JAX-WS API (if it exists at all) + Scope apiScope = getTransitiveDependencyScope(mavenModel, JAKARTA_JAXWS_API_GROUP, JAKARTA_JAXWS_API_ARTIFACT); + //Find the highest scope of a transitive dependency on the JAX-WS runtime (if it exists at all) + Scope runtimeScope = getTransitiveDependencyScope(mavenModel, SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT); + + if (apiScope != null && (runtimeScope == null || !apiScope.isInClasspathOf(runtimeScope))) { + String resolvedScope = apiScope == Scope.Test ? "test" : "provided"; + d = (Xml.Document) new AddDependencyVisitor(SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, + "2.3.2", null, resolvedScope, null, null, null, null, null).visit(d, ctx); + } + + return d; + } + + }; + } + + /** + * Finds the highest scope for a given group/artifact. + * + * @param mavenModel The maven model to search for a dependency. + * @param groupId The group ID of the dependency + * @param artifactId The artifact ID of the dependency + * @return The highest scope of the given dependency or null if the dependency does not exist. + */ + @Nullable + private Scope getTransitiveDependencyScope(MavenResolutionResult mavenModel, String groupId, String artifactId) { + + Scope maxScope = null; + for (Map.Entry> entry : mavenModel.getDependencies().entrySet()) { + for (ResolvedDependency dependency : entry.getValue()) { + if (groupId.equals(dependency.getGroupId()) && artifactId.equals(dependency.getArtifactId())) { + maxScope = Scope.maxPrecedence(maxScope, entry.getKey()); + if (Scope.Compile.equals(maxScope)) { + return maxScope; + } + break; + } + } + } + return maxScope; + } + +} diff --git a/src/main/java/org/openrewrite/java/migrate/javax/package-info.java b/src/main/java/org/openrewrite/java/migrate/j2ee/package-info.java similarity index 94% rename from src/main/java/org/openrewrite/java/migrate/javax/package-info.java rename to src/main/java/org/openrewrite/java/migrate/j2ee/package-info.java index e0579ae0f3..4e8967cb2a 100644 --- a/src/main/java/org/openrewrite/java/migrate/javax/package-info.java +++ b/src/main/java/org/openrewrite/java/migrate/j2ee/package-info.java @@ -15,7 +15,7 @@ */ @NonNullApi @NonNullFields -package org.openrewrite.java.migrate.javax; +package org.openrewrite.java.migrate.j2ee; import org.openrewrite.internal.lang.NonNullApi; import org.openrewrite.internal.lang.NonNullFields; diff --git a/src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java b/src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java deleted file mode 100644 index cfe659c130..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/javax/AddJaxbRuntime.java +++ /dev/null @@ -1,160 +0,0 @@ -/* - * 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.java.migrate.javax; - -import lombok.EqualsAndHashCode; -import lombok.Value; -import org.openrewrite.*; -import org.openrewrite.internal.ListUtils; -import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.maven.AddDependencyVisitor; -import org.openrewrite.maven.MavenIsoVisitor; -import org.openrewrite.maven.RemoveDependency; -import org.openrewrite.maven.tree.*; -import org.openrewrite.xml.tree.Xml; - -import java.time.Duration; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; - -import static org.openrewrite.java.migrate.MavenUtils.getMavenModel; -import static org.openrewrite.java.migrate.MavenUtils.isMavenSource; - -@Value -@EqualsAndHashCode(callSuper = true) -public class AddJaxbRuntime extends Recipe { - - private static final String JAXB_API_GROUP = "jakarta.xml.bind"; - private static final String JAXB_API_ARTIFACT = "jakarta.xml.bind-api"; - - private static final String SUN_JAXB_RUNTIME_GROUP = "com.sun.xml.bind"; - private static final String SUN_JAXB_RUNTIME_ARTIFACT = "jaxb-impl"; - - private static final String GLASSFISH_JAXB_RUNTIME_GROUP = "org.glassfish.jaxb"; - private static final String GLASSFISH_JAXB_RUNTIME_ARTIFACT = "jaxb-runtime"; - - @Option(displayName = "JAXB run-time", - description = "Which implementation of the JAXB run-time that will be added to maven projects that have transitive dependencies on the JAXB API", - valid = {"glassfish", "sun"}, - example = "glassfish") - String runtime; - - @Override - public String getDisplayName() { - return "Add JAXB run-time dependency to a Maven project"; - } - - @Override - public String getDescription() { - return "This recipe will add a JAXB run-time dependency to any maven project that has a transitive dependency on JAXB."; - } - - @Override - public Duration getEstimatedEffortPerOccurrence() { - return Duration.ofMinutes(5); - } - - @Override - protected List visit(List before, ExecutionContext ctx) { - //remove legacy jaxb-core, regardless of which runtime is being used. - doNext(new RemoveDependency("com.sun.xml.bind", "jaxb-core", null)); - ReplaceRuntimeVisitor replaceRuntime = "sun".equals(runtime) ? - new ReplaceRuntimeVisitor(GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, "2.3.2") : - new ReplaceRuntimeVisitor(SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, "2.3.2"); - - Map gavToModel = new HashMap<>(); - List sources = ListUtils.map(before, s -> { - if (isMavenSource(s)) { - Xml.Document mavenSource = (Xml.Document) replaceRuntime.visitNonNull(s, ctx); - MavenResolutionResult mavenModel = getMavenModel(mavenSource); - gavToModel.put(new GroupArtifactVersion(mavenModel.getPom().getGroupId(),mavenModel.getPom().getArtifactId(), mavenModel.getPom().getVersion()), mavenModel); - return mavenSource; - } - return s; - }); - - sources = ListUtils.map(sources, s -> { - if (isMavenSource(s)) { - MavenResolutionResult mavenModel = getMavenModel(s); - Scope apiScope = getTransitiveDependencyScope(mavenModel, JAXB_API_GROUP, JAXB_API_ARTIFACT, gavToModel); - Scope runtimeScope = "sun".equals(runtime) ? - getTransitiveDependencyScope(mavenModel, SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, gavToModel) : - getTransitiveDependencyScope(mavenModel, GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, gavToModel); - if (apiScope != null && (runtimeScope == null || !apiScope.isInClasspathOf(runtimeScope))) { - String resolvedScope = apiScope == Scope.Test ? "test" : "provided"; - AddDependencyVisitor addDependency = "sun".equals(runtime) ? - new AddDependencyVisitor(SUN_JAXB_RUNTIME_GROUP, SUN_JAXB_RUNTIME_ARTIFACT, "2.3.2", null, resolvedScope, null, null, null, null, null) : - new AddDependencyVisitor(GLASSFISH_JAXB_RUNTIME_GROUP, GLASSFISH_JAXB_RUNTIME_ARTIFACT, "2.3.2", null, resolvedScope, null, null, null, null, null); - return (SourceFile) addDependency.visit(s, ctx); - } - } - return s; - }); - - return sources; - } - - /** - * Finds the highest scope for a given group/artifact. - * - * @param mavenModel The maven model to search for a dependency. - * @param groupId The group ID of the dependency - * @param artifactId The artifact ID of the dependency - * @param gavToModels A map of gav coordinates to "poms" that exist in the source set, these may have been manipulated by other visitors - * @return The highest scope of the given dependency or null if the dependency does not exist. - */ - @Nullable - private Scope getTransitiveDependencyScope(MavenResolutionResult mavenModel, String groupId, String artifactId, Map gavToModels) { - - Scope maxScope = null; - for (Map.Entry> entry : mavenModel.getDependencies().entrySet()) { - for (ResolvedDependency dependency : entry.getValue()) { - if (groupId.equals(dependency.getGroupId()) && artifactId.equals(dependency.getArtifactId())) { - maxScope = Scope.maxPrecedence(maxScope, entry.getKey()); - if (Scope.Compile.equals(maxScope)) { - return maxScope; - } - break; - } - } - } - return null; - } - - @Value - @EqualsAndHashCode(callSuper = true) - private static class ReplaceRuntimeVisitor extends MavenIsoVisitor { - - String oldGroupId; - String oldArtifactId; - String newGroupId; - String newArtifactId; - String newVersion; - - @Override - public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { - if (isDependencyTag(oldGroupId, oldArtifactId)) { - Optional scopeTag = tag.getChild("scope"); - String scope = scopeTag.isPresent() && scopeTag.get().getValue().isPresent() ? scopeTag.get().getValue().get() : null; - doAfterVisit(new RemoveDependency(oldGroupId, oldArtifactId, scope)); - doAfterVisit(new AddDependencyVisitor(newGroupId, newArtifactId, newVersion, null, scope, null, null, null, null, null)); - } - return tag; - } - } -} diff --git a/src/main/java/org/openrewrite/java/migrate/javax/AddJaxwsRuntime.java b/src/main/java/org/openrewrite/java/migrate/javax/AddJaxwsRuntime.java deleted file mode 100644 index c7b3e4476b..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/javax/AddJaxwsRuntime.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * 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.java.migrate.javax; - -import lombok.EqualsAndHashCode; -import lombok.Value; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.SourceFile; -import org.openrewrite.internal.ListUtils; -import org.openrewrite.internal.lang.Nullable; -import org.openrewrite.java.migrate.MavenUtils; -import org.openrewrite.maven.AddDependencyVisitor; -import org.openrewrite.maven.RemoveDependency; -import org.openrewrite.maven.tree.GroupArtifactVersion; -import org.openrewrite.maven.tree.MavenResolutionResult; -import org.openrewrite.maven.tree.ResolvedDependency; -import org.openrewrite.maven.tree.Scope; - -import java.time.Duration; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -import static org.openrewrite.java.migrate.MavenUtils.getMavenModel; -import static org.openrewrite.java.migrate.MavenUtils.isMavenSource; - -@Value -@EqualsAndHashCode(callSuper = true) -public class AddJaxwsRuntime extends Recipe { - - private static final String JAKARTA_JAXWS_API_GROUP = "jakarta.xml.ws"; - private static final String JAKARTA_JAXWS_API_ARTIFACT = "jakarta.xml.ws-api"; - - private static final String SUN_JAXWS_RUNTIME_GROUP = "com.sun.xml.ws"; - private static final String SUN_JAXWS_RUNTIME_ARTIFACT = "jaxws-rt"; - - @Override - public String getDisplayName() { - return "Add JAX-WS run-time dependency to a Maven project"; - } - - @Override - public String getDescription() { - return "This recipe will add a JAX-WS run-time dependency to any maven project that has a transitive dependency on JAX-WS APIs."; - } - - @Override - public Duration getEstimatedEffortPerOccurrence() { - return Duration.ofMinutes(5); - } - - @Override - protected List visit(List before, ExecutionContext ctx) { - //remove legacy jaxws-ri library (in favor of the jakarta runtime) - doNext(new RemoveDependency(SUN_JAXWS_RUNTIME_GROUP, "jaxws-ri", null)); - - //Collect a map of gav coordinates to pom models for any maven files in the source set (other visitors may have - //made changes to those models) - Map gavToModel = before.stream() - .filter(MavenUtils::isMavenSource) - .map(MavenUtils::getMavenModel) - .collect(Collectors.toMap( - mavenModel -> new GroupArtifactVersion( - mavenModel.getPom().getGroupId(), - mavenModel.getPom().getArtifactId(), - mavenModel.getPom().getVersion()), - Function.identity())); - return ListUtils.map(before, s -> { - if (isMavenSource(s)) { - MavenResolutionResult mavenModel = getMavenModel(s); - - //Find the highest scope of a transitive dependency on the JAX-WS API (if it exists at all) - Scope apiScope = getTransitiveDependencyScope(mavenModel, JAKARTA_JAXWS_API_GROUP, JAKARTA_JAXWS_API_ARTIFACT, gavToModel); - //Find the highest scope of a transitive dependency on the JAX-WS runtime (if it exists at all) - Scope runtimeScope = getTransitiveDependencyScope(mavenModel, SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, gavToModel); - - if (apiScope != null && (runtimeScope == null || !apiScope.isInClasspathOf(runtimeScope))) { - //If the API is present and there is not a matching runtime in the transitive scope of the api, add the runtime. - String resolvedScope = apiScope == Scope.Test ? "test" : "provided"; - return (SourceFile) new AddDependencyVisitor( - SUN_JAXWS_RUNTIME_GROUP, SUN_JAXWS_RUNTIME_ARTIFACT, "2.3.2", - null, resolvedScope, null, null, null, - null, null).visit(s, ctx); - } - } - return s; - }); - } - - /** - * Finds the highest scope for a given group/artifact. - * - * @param mavenModel The maven model to search for a dependency. - * @param groupId The group ID of the dependency - * @param artifactId The artifact ID of the dependency - * @param gavToModels A map of gav coordinates to "poms" that exist in the source set, these may have been manipulated by other visitors - * @return The highest scope of the given dependency or null if the dependency does not exist. - */ - @Nullable - private static Scope getTransitiveDependencyScope(MavenResolutionResult mavenModel, String groupId, String artifactId, Map gavToModels) { - Scope maxScope = null; - for (Map.Entry> entry : mavenModel.getDependencies().entrySet()) { - for (ResolvedDependency dependency : entry.getValue()) { - if (groupId.equals(dependency.getGroupId()) && artifactId.equals(dependency.getArtifactId())) { - maxScope = Scope.maxPrecedence(maxScope, entry.getKey()); - if (Scope.Compile.equals(maxScope)) { - return maxScope; - } - break; - } - } - } - return null; - } - -} diff --git a/src/main/java/org/openrewrite/java/migrate/javax/ReplaceJavaxJaxbWithJakarta.java b/src/main/java/org/openrewrite/java/migrate/javax/ReplaceJavaxJaxbWithJakarta.java deleted file mode 100644 index cd88347237..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/javax/ReplaceJavaxJaxbWithJakarta.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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.java.migrate.javax; - -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; -import org.openrewrite.maven.AddDependencyVisitor; -import org.openrewrite.maven.MavenIsoVisitor; -import org.openrewrite.maven.RemoveDependency; -import org.openrewrite.xml.tree.Xml; - -import java.time.Duration; -import java.util.Optional; - -public class ReplaceJavaxJaxbWithJakarta extends Recipe { - - @Override - public String getDisplayName() { - return "Replace `javax.xml.bind:jaxb-api with `jakarta.xml.bind:jakarta.xml.bind-api`"; - } - - @Override - public String getDescription() { - return "This recipe will replace the legacy `javax-api` artifact with the Jakarta EE equivalent."; - } - - @Override - public Duration getEstimatedEffortPerOccurrence() { - return Duration.ofMinutes(5); - } - - @Override - protected TreeVisitor getVisitor() { - return new MavenIsoVisitor() { - @Override - public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { - if (isDependencyTag("javax.xml.bind", "jaxb-api")) { - Optional scopeTag = tag.getChild("scope"); - String scope = scopeTag.isPresent() && scopeTag.get().getValue().isPresent() ? scopeTag.get().getValue().get() : null; - doAfterVisit(new RemoveDependency("javax.xml.bind", "jaxb-api", scope)); - doAfterVisit(new AddDependencyVisitor("jakarta.xml.bind", "jakarta.xml.bind-api", "2.3.2", null, scope, null, null, null, null, null)); - } - return super.visitTag(tag, ctx); - } - }; - } -} diff --git a/src/main/java/org/openrewrite/java/migrate/javax/ReplaceJavaxJaxwsWithJakarta.java b/src/main/java/org/openrewrite/java/migrate/javax/ReplaceJavaxJaxwsWithJakarta.java deleted file mode 100644 index d9fc35d478..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/javax/ReplaceJavaxJaxwsWithJakarta.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * 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.java.migrate.javax; - -import org.openrewrite.ExecutionContext; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; -import org.openrewrite.maven.AddDependencyVisitor; -import org.openrewrite.maven.MavenIsoVisitor; -import org.openrewrite.maven.RemoveDependency; -import org.openrewrite.xml.tree.Xml; - -import java.time.Duration; -import java.util.Optional; - -public class ReplaceJavaxJaxwsWithJakarta extends Recipe { - - @Override - public String getDisplayName() { - return "Replace `javax.xml.ws:jaxws-api` with `jakarta.xml.ws:jakarta.xml.ws-api`"; - } - - @Override - public String getDescription() { - return "This recipe will replace the legacy javax `jaxws-api` artifact with the Jakarta EE equivalent. The jakarta JAX-WS API 2.3.x is part of JakartaEE 8 and still uses `javax` packaging."; - } - - @Override - public Duration getEstimatedEffortPerOccurrence() { - return Duration.ofMinutes(5); - } - - @Override - protected TreeVisitor getVisitor() { - return new MavenIsoVisitor() { - @Override - public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { - if (isDependencyTag("javax.xml.ws", "jaxws-api")) { - Optional scopeTag = tag.getChild("scope"); - String scope = scopeTag.isPresent() && scopeTag.get().getValue().isPresent() ? scopeTag.get().getValue().get() : null; - doAfterVisit(new RemoveDependency("javax.xml.ws", "jaxws-api", scope)); - doAfterVisit(new AddDependencyVisitor("jakarta.xml.ws", "jakarta.xml.ws-api", "2.3.2", null, scope, null, null, null, null, null)); - } - return super.visitTag(tag, ctx); - } - }; - } -} diff --git a/src/main/resources/META-INF/rewrite/add-jaxb-dependencies.yml b/src/main/resources/META-INF/rewrite/add-jaxb-dependencies.yml deleted file mode 100644 index 0d41b11d66..0000000000 --- a/src/main/resources/META-INF/rewrite/add-jaxb-dependencies.yml +++ /dev/null @@ -1,44 +0,0 @@ -# -# 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. -# ---- -type: specs.openrewrite.org/v1beta/recipe -name: org.openrewrite.java.migrate.javax.AddJaxbDependencies -displayName: Add explicit JAXB dependencies -description: This recipe will add the necessary JAXB dependencies for those projects migrating to Java 11. -tags: - - javax - - java11 - - jaxb - - jakarta - - glassfish - -recipeList: - # Replace the legacy, compile-time Javax JAXB API in favor of the jakarta artifact. - - org.openrewrite.java.migrate.javax.ReplaceJavaxJaxbWithJakarta - # Add jakarta.xml.bind-api to a maven project that uses the jaxb binding API - - org.openrewrite.maven.AddDependency: - groupId: jakarta.xml.bind - artifactId: jakarta.xml.bind-api - version: 2.3.2 - onlyIfUsing: javax.xml.bind.* - # Update the jaxb api and runtime to the most current version of 2.3.x - - org.openrewrite.maven.UpgradeDependencyVersion: - groupId: jakarta.xml.bind - artifactId: jakarta.xml.bind-api - newVersion: 2.3.2 - # Add JAXB Runtime if there is a transitive dependency on the JAXB API. - - org.openrewrite.java.migrate.javax.AddJaxbRuntime: - runtime: sun diff --git a/src/main/resources/META-INF/rewrite/add-jaxws-dependencies.yml b/src/main/resources/META-INF/rewrite/add-jaxws-dependencies.yml deleted file mode 100644 index 21b3a2c288..0000000000 --- a/src/main/resources/META-INF/rewrite/add-jaxws-dependencies.yml +++ /dev/null @@ -1,44 +0,0 @@ -# -# 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. -# ---- -type: specs.openrewrite.org/v1beta/recipe -name: org.openrewrite.java.migrate.javax.AddJaxwsDependencies -displayName: Add explicit JAX-WS dependencies -description: This recipe will add the necessary JAX-WS dependencies for those projects migrating to Java 11. -tags: - - javax - - java11 - - jaxws - - jakarta - - glassfish - -recipeList: - # Replace the legacy, compile-time Javax JAXWS API in favor of the jakarta artifact. - - org.openrewrite.java.migrate.javax.ReplaceJavaxJaxwsWithJakarta - # Add the jakarta.xml.ws-api to a maven project that uses the jws API - - org.openrewrite.maven.AddDependency: - groupId: jakarta.xml.ws - artifactId: jakarta.xml.ws-api - version: 2.3.2 - onlyIfUsing: javax.jws.* - # Update the jaxws api and runtime to the most current version of 2.3.x - - org.openrewrite.maven.UpgradeDependencyVersion: - groupId: jakarta.xml.ws - artifactId: jakarta.xml.ws-api - newVersion: 2.3.2 - # Add JAXWS Runtime if there is a transitive dependency on the JAXB API. - - org.openrewrite.java.migrate.javax.AddJaxwsRuntime: - runtime: sun diff --git a/src/main/resources/META-INF/rewrite/java-version-11.yml b/src/main/resources/META-INF/rewrite/java-version-11.yml index f9fc068ff7..5c8a390c33 100644 --- a/src/main/resources/META-INF/rewrite/java-version-11.yml +++ b/src/main/resources/META-INF/rewrite/java-version-11.yml @@ -27,9 +27,10 @@ tags: recipeList: - org.openrewrite.java.migrate.UseJavaUtilBase64 - # Add javax dependencies if they are used by a project. - - org.openrewrite.java.migrate.javax.AddJaxwsDependencies - - org.openrewrite.java.migrate.javax.AddJaxbDependencies + # Add an explicit JAXB/JAX-WS runtime and upgrade the dependencies to Jakarta EE 8 + - org.openrewrite.java.migrate.j2ee.UpdateJaxbRuntimeToJakartaEE8: + runtime: sun + - org.openrewrite.java.migrate.j2ee.UpdateJaxwsRuntimeToJakartaEE8 - org.openrewrite.java.migrate.javax.AddInjectDependencies # Add jdeprscan plugin to a maven-based build. - org.openrewrite.java.migrate.AddJDeprScanPlugin diff --git a/src/test/java/org/openrewrite/java/migrate/j2ee/UpdateJaxbRuntimeToJakartaEE8Test.java b/src/test/java/org/openrewrite/java/migrate/j2ee/UpdateJaxbRuntimeToJakartaEE8Test.java new file mode 100644 index 0000000000..6eb88f54f5 --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/j2ee/UpdateJaxbRuntimeToJakartaEE8Test.java @@ -0,0 +1,244 @@ +/* + * 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.java.migrate.j2ee; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.maven.Assertions.pomXml; + +public class UpdateJaxbRuntimeToJakartaEE8Test implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .recipe(new UpdateJaxbRuntimeToJakartaEE8("sun")) + ; + } + + @Test + void addJaxbRuntimeOnce() { + + rewriteRun( + pomXml( + """ + + com.example.jaxb + jaxb-example + 1.0.0 + + + jakarta.xml.bind + jakarta.xml.bind-api + 2.3.2 + + + + """, + """ + + com.example.jaxb + jaxb-example + 1.0.0 + + + jakarta.xml.bind + jakarta.xml.bind-api + 2.3.2 + + + com.sun.xml.bind + jaxb-impl + 2.3.2 + provided + + + + """ + ) + ); + } + + @Test + void renameRuntime() { + + rewriteRun( + pomXml( + """ + + com.example.jaxb + jaxb-example + 1.0.0 + + + jakarta.xml.bind + jakarta.xml.bind-api + 2.3.2 + + + org.glassfish.jaxb + jaxb-runtime + 2.3.2 + provided + + + + """, + """ + + com.example.jaxb + jaxb-example + 1.0.0 + + + jakarta.xml.bind + jakarta.xml.bind-api + 2.3.2 + + + com.sun.xml.bind + jaxb-impl + 2.3.2 + provided + + + + """ + ) + ); + } + + @Test + void renameAndUpdateApiAndRuntime() { + + rewriteRun( + pomXml( + """ + + com.example.jaxb + jaxb-example + 1.0.0 + + + javax.xml.bind + jaxb-api + 2.3.1 + + + org.glassfish.jaxb + jaxb-runtime + 2.3.1 + provided + + + + """, + """ + + com.example.jaxb + jaxb-example + 1.0.0 + + + jakarta.xml.bind + jakarta.xml.bind-api + 2.3.2 + + + com.sun.xml.bind + jaxb-impl + 2.3.2 + provided + + + + """ + ) + ); + } + + @Test + void renameAndUpdateApiAndAddRuntimeManagedDependencies() { + + rewriteRun( + spec -> spec.cycles(3), + pomXml( + """ + + com.example.jaxb + jaxb-example + 1.0.0 + + + + javax.xml.bind + jaxb-api + 2.3.1 + + + org.glassfish.jaxb + jaxb-runtime + 2.3.1 + provided + + + + + + javax.xml.bind + jaxb-api + + + + """, + """ + + com.example.jaxb + jaxb-example + 1.0.0 + + + + jakarta.xml.bind + jakarta.xml.bind-api + 2.3.2 + + + com.sun.xml.bind + jaxb-impl + 2.3.2 + provided + + + + + + jakarta.xml.bind + jakarta.xml.bind-api + + + com.sun.xml.bind + jaxb-impl + provided + + + + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/java/migrate/j2ee/UpdateJaxwsRuntimeToJakartaEE8Test.java b/src/test/java/org/openrewrite/java/migrate/j2ee/UpdateJaxwsRuntimeToJakartaEE8Test.java new file mode 100644 index 0000000000..59c44b5b7b --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/j2ee/UpdateJaxwsRuntimeToJakartaEE8Test.java @@ -0,0 +1,239 @@ +/* + * 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.java.migrate.j2ee; + +import org.junit.jupiter.api.Test; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.maven.Assertions.pomXml; + +public class UpdateJaxwsRuntimeToJakartaEE8Test implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .recipe(new UpdateJaxwsRuntimeToJakartaEE8()) + .cycles(3) + ; + } + + @Test + void addJaxwsRuntimeOnce() { + + rewriteRun( + spec -> spec.cycles(2), + pomXml( + """ + + com.example.jaxws + jaxws-example + 1.0.0 + + + jakarta.xml.ws + jakarta.xml.ws-api + 2.3.2 + + + + """, + """ + + com.example.jaxws + jaxws-example + 1.0.0 + + + jakarta.xml.ws + jakarta.xml.ws-api + 2.3.2 + + + com.sun.xml.ws + jaxws-rt + 2.3.2 + provided + + + + """ + ) + ); + } + + @Test + void removeReferenceImplementationRuntime() { + + rewriteRun( + pomXml( + """ + + com.example.jaxws + jaxws-example + 1.0.0 + + + javax.xml.ws + jaxws-api + 2.3.1 + + + com.sun.xml.ws + jaxws-ri + 2.3.2 + provided + + + + """, + """ + + com.example.jaxws + jaxws-example + 1.0.0 + + + jakarta.xml.ws + jakarta.xml.ws-api + 2.3.2 + + + com.sun.xml.ws + jaxws-rt + 2.3.2 + provided + + + + """ + ) + ); + } + + @Test + void renameAndUpdateApiAndRuntime() { + + rewriteRun( + pomXml( + """ + + com.example.jaxws + jaxws-example + 1.0.0 + + + jakarta.xml.ws + jakarta.xml.ws-api + 2.3.2 + + + com.sun.xml.ws + jaxws-ri + 2.3.2 + provided + + + + """, + """ + + com.example.jaxws + jaxws-example + 1.0.0 + + + jakarta.xml.ws + jakarta.xml.ws-api + 2.3.2 + + + com.sun.xml.ws + jaxws-rt + 2.3.2 + provided + + + + """ + ) + ); + } + + @Test + void renameAndUpdateApiAndAddRuntimeManagedDependencies() { + + rewriteRun( + pomXml( + """ + + com.example.jaxws + jaxws-example + 1.0.0 + + + + javax.xml.ws + jaxws-api + 2.3.1 + + + com.sun.xml.ws + jaxws-ri + 2.3.2 + + + + + + javax.xml.ws + jaxws-api + + + + """, + """ + + com.example.jaxws + jaxws-example + 1.0.0 + + + + jakarta.xml.ws + jakarta.xml.ws-api + 2.3.2 + + + + + + jakarta.xml.ws + jakarta.xml.ws-api + + + com.sun.xml.ws + jaxws-rt + 2.3.2 + provided + + + + """ + ) + ); + } +} diff --git a/src/test/java/org/openrewrite/java/migrate/jakarta/EhcacheJavaxtoJakartaTest.java b/src/test/java/org/openrewrite/java/migrate/jakarta/EhcacheJavaxtoJakartaTest.java index c8b72432f7..fb8587116f 100644 --- a/src/test/java/org/openrewrite/java/migrate/jakarta/EhcacheJavaxtoJakartaTest.java +++ b/src/test/java/org/openrewrite/java/migrate/jakarta/EhcacheJavaxtoJakartaTest.java @@ -69,18 +69,18 @@ void migrateEhcacheDependencies() { org.ehcache ehcache - 3.10.2 + 3.10.4 jakarta org.ehcache ehcache-clustered - 3.10.2 + 3.10.4 org.ehcache ehcache-transactions - 3.10.2 + 3.10.4 jakarta