diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategy.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategy.java new file mode 100644 index 000000000000..df29b1c83227 --- /dev/null +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategy.java @@ -0,0 +1,326 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.apache.maven.cling.invoker.mvnup.goals; + +import java.nio.file.Path; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Stream; + +import eu.maveniverse.domtrip.Document; +import eu.maveniverse.domtrip.Element; +import org.apache.maven.api.cli.mvnup.UpgradeOptions; +import org.apache.maven.api.di.Named; +import org.apache.maven.api.di.Priority; +import org.apache.maven.api.di.Singleton; +import org.apache.maven.cling.invoker.mvnup.UpgradeContext; + +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.ARTIFACT_ID; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.BUILD; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCIES; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCY; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.DEPENDENCY_MANAGEMENT; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.GROUP_ID; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGIN; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGINS; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PLUGIN_MANAGEMENT; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PROFILE; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.PROFILES; +import static eu.maveniverse.domtrip.maven.MavenPomElements.Elements.VERSION; + +/** + * Strategy for migrating deprecated Java EE API dependencies to their Jakarta EE successors. + * + *

Java 11 (JEP 320) removed several Java EE and CORBA modules from the JDK that were + * previously bundled in {@code rt.jar}. Projects that declared these APIs as explicit Maven + * dependencies using their legacy {@code javax.*} groupIds will fail to compile on JDK 11+ + * unless they migrate to the standalone Jakarta EE equivalents. + * + *

This strategy detects legacy Java EE API artifacts in {@code }, + * {@code }, profile dependencies, and plugin dependencies, and + * replaces them with their Jakarta EE equivalents: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
Legacy to Jakarta EE migration map
Legacy (removed in JDK 11)Jakarta EE replacement
{@code javax.xml.bind:jaxb-api}{@code jakarta.xml.bind:jakarta.xml.bind-api:2.3.3}
{@code com.sun.xml.bind:jaxb-core}{@code org.glassfish.jaxb:jaxb-core:2.3.0.1}
{@code com.sun.xml.bind:jaxb-impl}{@code org.glassfish.jaxb:jaxb-runtime:2.3.9}
{@code javax.xml.ws:jaxws-api}{@code jakarta.xml.ws:jakarta.xml.ws-api:2.3.3}
{@code javax.jws:javax.jws-api}{@code jakarta.jws:jakarta.jws-api:1.1.1}
{@code javax.annotation:javax.annotation-api}{@code jakarta.annotation:jakarta.annotation-api:1.3.5}
{@code javax.activation:activation}{@code jakarta.activation:jakarta.activation-api:1.2.2}
{@code javax.activation:javax.activation-api}{@code jakarta.activation:jakarta.activation-api:1.2.2}
+ * + *

The {@code javax.*} API versions listed above are the last Jakarta EE releases that preserve + * the {@code javax.*} package namespace (i.e. they are source-compatible with code written for + * the removed JDK modules), making them a drop-in replacement without requiring import changes. + * The JAXB RI ({@code com.sun.xml.bind:*}) replacements use the {@code org.glassfish.jaxb} + * groupId and retain binary compatibility. + * + * @see JEP 320: Remove the Java EE and CORBA Modules + * @see #14163 + */ +@Named +@Singleton +@Priority(16) +public class JakartaEeApiMigrationStrategy extends AbstractUpgradeStrategy { + + /** + * Maps legacy groupId:artifactId to the Jakarta EE replacement coordinates. + * Values are {@code groupId:artifactId:version} triples for the last compatible + * Jakarta EE release that still uses the {@code javax.*} package namespace. + */ + static final Map LEGACY_TO_JAKARTA = buildMigrationMap(); + + private static Map buildMigrationMap() { + Map map = new LinkedHashMap<>(); + // JAXB — javax.xml.bind + map.put("javax.xml.bind:jaxb-api", new Replacement("jakarta.xml.bind", "jakarta.xml.bind-api", "2.3.3")); + map.put("com.sun.xml.bind:jaxb-core", new Replacement("org.glassfish.jaxb", "jaxb-core", "2.3.0.1")); + map.put("com.sun.xml.bind:jaxb-impl", new Replacement("org.glassfish.jaxb", "jaxb-runtime", "2.3.9")); + // JAX-WS — javax.xml.ws + map.put("javax.xml.ws:jaxws-api", new Replacement("jakarta.xml.ws", "jakarta.xml.ws-api", "2.3.3")); + // JAX-WS annotations — javax.jws + map.put("javax.jws:javax.jws-api", new Replacement("jakarta.jws", "jakarta.jws-api", "1.1.1")); + // javax.annotation — from Common Annotations (JSR-250) + map.put( + "javax.annotation:javax.annotation-api", + new Replacement("jakarta.annotation", "jakarta.annotation-api", "1.3.5")); + // JavaBeans Activation Framework — javax.activation + map.put( + "javax.activation:activation", + new Replacement("jakarta.activation", "jakarta.activation-api", "1.2.2")); + map.put( + "javax.activation:javax.activation-api", + new Replacement("jakarta.activation", "jakarta.activation-api", "1.2.2")); + return Map.copyOf(map); + } + + @Override + public boolean isApplicable(UpgradeContext context) { + UpgradeOptions options = getOptions(context); + + if (options.all().orElse(false)) { + return true; + } + + boolean noOptionsSpecified = options.all().isEmpty() + && options.infer().isEmpty() + && options.model().isEmpty() + && options.plugins().isEmpty() + && options.modelVersion().isEmpty(); + + boolean allOptionsDisabled = options.all().map(v -> !v).orElse(false) + && options.infer().map(v -> !v).orElse(false) + && options.model().map(v -> !v).orElse(false) + && options.plugins().map(v -> !v).orElse(false) + && options.modelVersion().isEmpty(); + + if (noOptionsSpecified || allOptionsDisabled) { + return true; + } + + if (options.model().isPresent()) { + return options.model().get(); + } + + return false; + } + + @Override + public String getDescription() { + return "Migrating legacy Java EE API dependencies to Jakarta EE equivalents (JEP 320)"; + } + + @Override + protected UpgradeResult doApply(UpgradeContext context, Map pomMap) { + Set processedPoms = new HashSet<>(); + Set modifiedPoms = new HashSet<>(); + Set errorPoms = new HashSet<>(); + + for (Map.Entry entry : pomMap.entrySet()) { + Path pomPath = entry.getKey(); + Document pomDocument = entry.getValue(); + processedPoms.add(pomPath); + + context.info(pomPath + " (checking for legacy Java EE API dependencies)"); + context.indent(); + + try { + boolean modified = migrateLegacyJavaEeApis(pomDocument, context); + + if (modified) { + logChange(context, "Legacy Java EE API migration to Jakarta EE equivalents"); + modifiedPoms.add(pomPath); + } else { + context.success("No legacy Java EE API dependencies found"); + } + } catch (Exception e) { + context.failure("Failed to migrate Java EE API dependencies: " + e.getMessage()); + errorPoms.add(pomPath); + } finally { + context.unindent(); + } + } + + return new UpgradeResult(processedPoms, modifiedPoms, errorPoms); + } + + /** + * Scans all dependency sections in the POM and replaces legacy Java EE artifacts + * with their Jakarta EE equivalents. + * + * @param pomDocument the POM document to modify + * @param context the upgrade context for logging + * @return {@code true} if any dependencies were migrated + */ + boolean migrateLegacyJavaEeApis(Document pomDocument, UpgradeContext context) { + Element root = pomDocument.root(); + boolean modified = false; + + // + modified |= migrateDependenciesSection(root.childElement(DEPENDENCIES).orElse(null), context); + + // + modified |= root.childElement(DEPENDENCY_MANAGEMENT).stream() + .flatMap(dm -> dm.childElement(DEPENDENCIES).stream()) + .reduce(false, (acc, deps) -> acc | migrateDependenciesSection(deps, context), Boolean::logicalOr); + + // profiles + Element profiles = root.childElement(PROFILES).orElse(null); + if (profiles != null) { + for (Element profile : profiles.childElements(PROFILE).toList()) { + modified |= migrateDependenciesSection( + profile.childElement(DEPENDENCIES).orElse(null), context); + modified |= profile.childElement(DEPENDENCY_MANAGEMENT).stream() + .flatMap(dm -> dm.childElement(DEPENDENCIES).stream()) + .reduce( + false, + (acc, deps) -> acc | migrateDependenciesSection(deps, context), + Boolean::logicalOr); + } + } + + // plugin-level dependencies (e.g. maven-antrun-plugin dependencies) + Stream pluginContainers = Stream.concat( + root.childElement(BUILD).stream() + .flatMap(build -> Stream.concat( + build.childElement(PLUGINS).stream(), + build.childElement(PLUGIN_MANAGEMENT).stream() + .flatMap(pm -> pm.childElement(PLUGINS).stream()))), + root.childElement(PROFILES).stream() + .flatMap(profs -> profs.childElements(PROFILE)) + .flatMap(profile -> profile.childElement(BUILD).stream()) + .flatMap(build -> Stream.concat( + build.childElement(PLUGINS).stream(), + build.childElement(PLUGIN_MANAGEMENT).stream() + .flatMap(pm -> pm.childElement(PLUGINS).stream())))); + + for (Element pluginsElem : pluginContainers.toList()) { + for (Element plugin : pluginsElem.childElements(PLUGIN).toList()) { + modified |= migrateDependenciesSection( + plugin.childElement(DEPENDENCIES).orElse(null), context); + } + } + + return modified; + } + + private boolean migrateDependenciesSection(Element dependenciesElement, UpgradeContext context) { + if (dependenciesElement == null) { + return false; + } + + boolean modified = false; + for (Element dependency : dependenciesElement.childElements(DEPENDENCY).toList()) { + modified |= migrateDependency(dependency, context); + } + return modified; + } + + /** + * Checks whether a dependency element matches a legacy Java EE artifact and, + * if so, updates its groupId and artifactId in-place. + * + *

The version element is updated only when it is explicitly declared in this + * dependency. If the version is managed via {@code } and + * absent here, it is left untouched. + * + * @param dependency the dependency element to inspect and potentially modify + * @param context the upgrade context for logging + * @return {@code true} if the dependency was migrated + */ + boolean migrateDependency(Element dependency, UpgradeContext context) { + String groupId = dependency.childTextTrimmed(GROUP_ID); + String artifactId = dependency.childTextTrimmed(ARTIFACT_ID); + + if (groupId == null || artifactId == null) { + return false; + } + + String key = groupId + ":" + artifactId; + Replacement replacement = LEGACY_TO_JAKARTA.get(key); + if (replacement == null) { + return false; + } + + // Update groupId + dependency.childElement(GROUP_ID).ifPresent(e -> e.textContent(replacement.groupId())); + + // Update artifactId + dependency.childElement(ARTIFACT_ID).ifPresent(e -> e.textContent(replacement.artifactId())); + + // Update version only if it is explicitly declared in this element + Element versionElement = dependency.childElement(VERSION).orElse(null); + if (versionElement != null) { + String currentVersion = versionElement.textContentTrimmed(); + if (currentVersion != null && currentVersion.startsWith("${") && currentVersion.endsWith("}")) { + context.warning("Version of " + key + " is controlled by property " + + currentVersion + " — update the property value manually to " + + replacement.version()); + } else { + versionElement.textContent(replacement.version()); + } + } + + context.detail(key + " uses APIs that were removed from the JDK in Java 11 (JEP 320)." + + " Replaced with " + replacement.groupId() + ":" + replacement.artifactId() + ":" + + replacement.version() + + " which provides the same API as a standalone dependency."); + + return true; + } + + /** + * Immutable record holding the Jakarta EE replacement coordinates for a legacy Java EE artifact. + */ + record Replacement(String groupId, String artifactId, String version) {} +} diff --git a/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategyTest.java b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategyTest.java new file mode 100644 index 000000000000..d2193ca19213 --- /dev/null +++ b/impl/maven-cli/src/test/java/org/apache/maven/cling/invoker/mvnup/goals/JakartaEeApiMigrationStrategyTest.java @@ -0,0 +1,620 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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.apache.maven.cling.invoker.mvnup.goals; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import eu.maveniverse.domtrip.Document; +import eu.maveniverse.domtrip.Element; +import org.apache.maven.cling.invoker.mvnup.UpgradeContext; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Unit tests for the {@link JakartaEeApiMigrationStrategy} class. + */ +@DisplayName("JakartaEeApiMigrationStrategy") +class JakartaEeApiMigrationStrategyTest { + + private static final Path POM_PATH = Paths.get("/project/pom.xml"); + + @Nested + @DisplayName("Applicability") + class ApplicabilityTests { + + @Test + @DisplayName("should be applicable when --all option is true") + void applicableWithAll() { + JakartaEeApiMigrationStrategy strategy = new JakartaEeApiMigrationStrategy(); + assertTrue(strategy.isApplicable(TestUtils.createMockContext(TestUtils.createOptionsWithAll(true)))); + } + + @Test + @DisplayName("should be applicable with default options (no flags)") + void applicableWithDefaults() { + JakartaEeApiMigrationStrategy strategy = new JakartaEeApiMigrationStrategy(); + assertTrue(strategy.isApplicable(TestUtils.createMockContext())); + } + + @Test + @DisplayName("should be applicable when --model is true") + void applicableWithModel() { + JakartaEeApiMigrationStrategy strategy = new JakartaEeApiMigrationStrategy(); + assertTrue(strategy.isApplicable(TestUtils.createMockContext(TestUtils.createOptionsWithFixModel(true)))); + } + + @Test + @DisplayName("should not be applicable when --model is false") + void notApplicableWithModelFalse() { + JakartaEeApiMigrationStrategy strategy = new JakartaEeApiMigrationStrategy(); + assertFalse(strategy.isApplicable(TestUtils.createMockContext(TestUtils.createOptionsWithFixModel(false)))); + } + } + + @Nested + @DisplayName("Single dependency migration") + class SingleDependencyMigrationTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should migrate javax.xml.bind:jaxb-api to jakarta.xml.bind:jakarta.xml.bind-api") + void migrateJaxbApi() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + javax.xml.bind + jaxb-api + 2.3.1 + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.xml.bind"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.xml.bind-api"), "artifactId should be migrated: " + output); + assertTrue(output.contains("2.3.3"), "version should be updated: " + output); + assertFalse(output.contains("javax.xml.bind"), "old groupId should be gone: " + output); + } + + @Test + @DisplayName("should migrate javax.annotation:javax.annotation-api") + void migrateAnnotationApi() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + javax.annotation + javax.annotation-api + 1.3.2 + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.annotation"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.annotation-api"), "artifactId should be migrated: " + output); + assertTrue(output.contains("1.3.5"), "version should be updated: " + output); + } + + @Test + @DisplayName("should migrate javax.activation:activation") + void migrateActivation() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + javax.activation + activation + 1.1.1 + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.activation"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.activation-api"), "artifactId should be migrated: " + output); + assertTrue(output.contains("1.2.2"), "version should be updated: " + output); + } + + @Test + @DisplayName("should migrate javax.xml.ws:jaxws-api") + void migrateJaxwsApi() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + javax.xml.ws + jaxws-api + 2.3.1 + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.xml.ws"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.xml.ws-api"), "artifactId should be migrated: " + output); + } + + @Test + @DisplayName("should not modify POM with no legacy Java EE dependencies") + void noLegacyDependencies() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + org.slf4j + slf4j-api + 1.7.36 + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(0, result.modifiedPoms().size()); + } + + @Test + @DisplayName("should not modify POM with no dependencies section") + void noDependenciesSection() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(0, result.modifiedPoms().size()); + } + } + + @Nested + @DisplayName("Version handling") + class VersionHandlingTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should update version when explicitly declared") + void updatesExplicitVersion() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + javax.xml.bind + jaxb-api + 2.2.0 + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + strategy.doApply(context, Map.of(POM_PATH, doc)); + + String output = doc.toXml(); + assertTrue(output.contains("2.3.3"), "version should be updated to Jakarta equivalent: " + output); + assertFalse(output.contains("2.2.0"), "old version should be replaced: " + output); + } + + @Test + @DisplayName("should preserve property expression and warn instead of replacing version") + void preservesPropertyExpressionVersion() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + 2.3.1 + + + + javax.xml.bind + jaxb-api + ${jaxb.version} + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.xml.bind"), "groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.xml.bind-api"), "artifactId should be migrated: " + output); + assertTrue( + output.contains("${jaxb.version}"), + "property expression should be preserved, not replaced with literal: " + output); + assertFalse(output.contains("2.3.3"), "literal version should NOT appear: " + output); + } + + @Test + @DisplayName("should not add version when dependency has no version element (managed by depMgmt)") + void preservesManagedDependencyWithoutVersion() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + + javax.xml.bind + jaxb-api + 2.3.1 + + + + + + javax.xml.bind + jaxb-api + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + strategy.doApply(context, Map.of(POM_PATH, doc)); + + String output = doc.toXml(); + // Both the depMgmt entry and the plain dep should have their groupId migrated + assertTrue(output.contains("jakarta.xml.bind"), "groupId should be migrated: " + output); + // The depMgmt entry has 2.3.3; the plain dep had no so none should be added + assertTrue(output.contains("2.3.3"), "dependencyManagement version should be updated: " + output); + // Find the plain section (after ) and verify + // it does NOT contain a element for the migrated dependency + int depMgmtClose = output.indexOf(""); + assertTrue(depMgmtClose > 0, "dependencyManagement should be present: " + output); + String plainDepsSection = output.substring(depMgmtClose); + assertFalse( + plainDepsSection.contains("2.3.3"), + "Plain dependency (no version) should not have a version element added: " + plainDepsSection); + } + } + + @Nested + @DisplayName("Dependency management migration") + class DependencyManagementTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should migrate legacy dependencies in dependencyManagement") + void migratesInDependencyManagement() { + String pomXml = """ + + + 4.0.0 + com.example + parent + 1.0 + pom + + + + javax.xml.bind + jaxb-api + 2.3.1 + + + javax.annotation + javax.annotation-api + 1.3.2 + + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.xml.bind"), "JAXB groupId should be migrated: " + output); + assertTrue(output.contains("jakarta.annotation"), "annotation groupId should be migrated: " + output); + assertFalse(output.contains("javax.xml.bind"), "old JAXB groupId should be gone: " + output); + assertFalse(output.contains("javax.annotation"), "old annotation groupId should be gone: " + output); + } + } + + @Nested + @DisplayName("Profile dependency migration") + class ProfileDependencyTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should migrate legacy dependencies inside profiles") + void migratesInProfiles() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + legacy-jee + + + javax.activation + javax.activation-api + 1.2.0 + + + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.activation"), "groupId should be migrated in profile: " + output); + assertFalse(output.contains("javax.activation"), "old groupId should be gone in profile: " + output); + } + } + + @Nested + @DisplayName("Plugin dependency migration") + class PluginDependencyTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("should migrate legacy dependencies inside plugin dependencies") + void migratesPluginDependencies() { + String pomXml = """ + + + 4.0.0 + com.example + test + 1.0 + + + + org.codehaus.mojo + jaxb2-maven-plugin + 2.5.0 + + + javax.xml.bind + jaxb-api + 2.3.1 + + + + + + + """; + Document doc = Document.of(pomXml); + UpgradeContext context = TestUtils.createMockContext(); + + UpgradeResult result = strategy.doApply(context, Map.of(POM_PATH, doc)); + + assertEquals(1, result.modifiedPoms().size()); + String output = doc.toXml(); + assertTrue(output.contains("jakarta.xml.bind"), "groupId should be migrated in plugin deps: " + output); + assertTrue( + output.contains("jakarta.xml.bind-api"), "artifactId should be migrated in plugin deps: " + output); + assertTrue(output.contains("2.3.3"), "version should be updated in plugin deps: " + output); + assertFalse(output.contains("javax.xml.bind"), "old groupId should be gone from plugin deps: " + output); + } + } + + @Nested + @DisplayName("Single dependency unit tests") + class SingleDependencyUnitTests { + + private JakartaEeApiMigrationStrategy strategy; + + @BeforeEach + void setUp() { + strategy = new JakartaEeApiMigrationStrategy(); + } + + @Test + @DisplayName("migrateDependency returns false for non-legacy groupId") + void noMigrationForUnknownArtifact() { + String depXml = """ + + com.example + my-lib + 1.0 + + """; + Document doc = Document.of("" + depXml + ""); + Element dep = doc.root().childElement("dependency").orElseThrow(); + UpgradeContext context = TestUtils.createMockContext(); + + assertFalse(strategy.migrateDependency(dep, context)); + } + + @Test + @DisplayName("migrateDependency returns true and updates groupId for jaxb-api") + void migratesJaxbApiDependency() { + String depXml = """ + + javax.xml.bind + jaxb-api + 2.3.1 + + """; + Document doc = Document.of("" + depXml + ""); + Element dep = doc.root().childElement("dependency").orElseThrow(); + UpgradeContext context = TestUtils.createMockContext(); + + assertTrue(strategy.migrateDependency(dep, context)); + assertEquals("jakarta.xml.bind", dep.childTextTrimmed("groupId")); + assertEquals("jakarta.xml.bind-api", dep.childTextTrimmed("artifactId")); + assertEquals("2.3.3", dep.childTextTrimmed("version")); + } + } + + @Nested + @DisplayName("Migration map completeness") + class MigrationMapTests { + + @Test + @DisplayName("migration map should contain all JEP-320 removed Java EE APIs") + void migrationMapCompleteness() { + Map map = + JakartaEeApiMigrationStrategy.LEGACY_TO_JAKARTA; + + assertTrue(map.containsKey("javax.xml.bind:jaxb-api"), "JAXB API should be mapped"); + assertTrue(map.containsKey("javax.xml.ws:jaxws-api"), "JAX-WS API should be mapped"); + assertTrue( + map.containsKey("javax.annotation:javax.annotation-api"), "javax.annotation API should be mapped"); + assertTrue(map.containsKey("javax.activation:activation"), "activation API (old) should be mapped"); + assertTrue( + map.containsKey("javax.activation:javax.activation-api"), "activation API (new) should be mapped"); + assertTrue(map.containsKey("javax.jws:javax.jws-api"), "JWS API should be mapped"); + } + + @Test + @DisplayName("all replacement versions should be non-null and non-empty") + void replacementVersionsNonEmpty() { + for (Map.Entry entry : + JakartaEeApiMigrationStrategy.LEGACY_TO_JAKARTA.entrySet()) { + JakartaEeApiMigrationStrategy.Replacement r = entry.getValue(); + assertFalse(r.groupId().isBlank(), entry.getKey() + " should have groupId"); + assertFalse(r.artifactId().isBlank(), entry.getKey() + " should have artifactId"); + assertFalse(r.version().isBlank(), entry.getKey() + " should have version"); + } + } + } +}