|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Red Hat Inc. and others |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Red Hat Inc. - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | + |
| 14 | +package org.eclipse.m2e.core; |
| 15 | + |
| 16 | +import static org.junit.Assert.assertNotNull; |
| 17 | +import static org.junit.Assert.assertTrue; |
| 18 | +import static org.junit.Assert.fail; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | + |
| 22 | +import org.eclipse.core.resources.IProject; |
| 23 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 24 | +import org.eclipse.core.runtime.FileLocator; |
| 25 | +import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase; |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +/** |
| 30 | + * Test case for Maven 4 support in m2e. |
| 31 | + * |
| 32 | + * This test demonstrates that m2e currently does NOT support Maven 4 features. |
| 33 | + * As Maven 4 support is implemented, this test can be enhanced to verify: |
| 34 | + * - Model version 4.1.0 parsing |
| 35 | + * - Subprojects support (instead of modules) |
| 36 | + * - Inheritance of groupId and version |
| 37 | + * - Build/Consumer POM split |
| 38 | + * |
| 39 | + * Based on real-world usage from https://github.com/jline/jline3 |
| 40 | + * |
| 41 | + * @see https://maven.apache.org/whatsnewinmaven4.html |
| 42 | + */ |
| 43 | +public class Maven4Test extends AbstractMavenProjectTestCase { |
| 44 | + |
| 45 | + @After |
| 46 | + public void clearWorkspace() throws Exception { |
| 47 | + for(IProject p : ResourcesPlugin.getWorkspace().getRoot().getProjects()) { |
| 48 | + p.delete(true, null); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Test Maven 4 project with model version 4.1.0 and subprojects. |
| 54 | + * |
| 55 | + * This test attempts to import a Maven 4 project that uses: |
| 56 | + * - Model version 4.1.0 |
| 57 | + * - Subprojects instead of modules |
| 58 | + * - Inheritance of groupId and version from parent |
| 59 | + * |
| 60 | + * Expected behavior: |
| 61 | + * - With current m2e (Maven 3 only): This test is expected to fail or show errors |
| 62 | + * - With Maven 4 support: This test should pass and projects should be imported correctly |
| 63 | + */ |
| 64 | + @Test |
| 65 | + public void testMaven4BasicProject() throws Exception { |
| 66 | + // Get the Maven 4 test project |
| 67 | + File sourceDirectory = new File( |
| 68 | + FileLocator.toFileURL(getClass().getResource("/resources/projects/HelloMaven4")).toURI()); |
| 69 | + |
| 70 | + assertNotNull("HelloMaven4 project directory should exist", sourceDirectory); |
| 71 | + assertTrue("HelloMaven4 project directory should be a directory", sourceDirectory.isDirectory()); |
| 72 | + |
| 73 | + File parentPom = new File(sourceDirectory, "pom.xml"); |
| 74 | + assertTrue("Parent pom.xml should exist", parentPom.exists()); |
| 75 | + |
| 76 | + // Attempt to import the Maven 4 project |
| 77 | + // Note: This will likely fail with current m2e since it doesn't support Maven 4 |
| 78 | + try { |
| 79 | + IProject project = importProject("resources/projects/HelloMaven4/pom.xml"); |
| 80 | + |
| 81 | + // If we get here, the project was imported (unexpected with current m2e) |
| 82 | + assertNotNull("Parent project should be imported", project); |
| 83 | + |
| 84 | + // Check if subprojects are recognized |
| 85 | + // Note: With Maven 4 support, these should be imported as child projects |
| 86 | + IProject helloCore = ResourcesPlugin.getWorkspace().getRoot().getProject("hello-core"); |
| 87 | + IProject helloApp = ResourcesPlugin.getWorkspace().getRoot().getProject("hello-app"); |
| 88 | + |
| 89 | + // These assertions will help verify Maven 4 support when implemented |
| 90 | + // For now, they document what should work with Maven 4 |
| 91 | + assertNotNull("hello-core subproject should be recognized", helloCore); |
| 92 | + assertNotNull("hello-app subproject should be recognized", helloApp); |
| 93 | + |
| 94 | + } catch (Exception e) { |
| 95 | + // Expected with current m2e - Maven 4 is not yet supported |
| 96 | + // This documents that m2e needs Maven 4 support |
| 97 | + System.err.println("Expected failure with Maven 3 based m2e: " + e.getMessage()); |
| 98 | + |
| 99 | + // For now, we'll let this exception indicate Maven 4 is not supported |
| 100 | + // When Maven 4 support is added, this test should pass without exceptions |
| 101 | + fail("Maven 4 project import failed (expected with current m2e): " + e.getMessage()); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Test that Maven 4 model version 4.1.0 is recognized. |
| 107 | + * |
| 108 | + * This is a more basic test that just checks if the model version is parsed correctly. |
| 109 | + * When Maven 4 support is added, this should pass. |
| 110 | + */ |
| 111 | + @Test |
| 112 | + public void testMaven4ModelVersion() throws Exception { |
| 113 | + File sourceDirectory = new File( |
| 114 | + FileLocator.toFileURL(getClass().getResource("/resources/projects/HelloMaven4")).toURI()); |
| 115 | + File parentPom = new File(sourceDirectory, "pom.xml"); |
| 116 | + |
| 117 | + assertTrue("Parent pom.xml should exist", parentPom.exists()); |
| 118 | + |
| 119 | + // Try to read the POM |
| 120 | + // With Maven 4 support, this should recognize the 4.1.0 model version |
| 121 | + try { |
| 122 | + // This will likely fail with current Maven 3 based implementation |
| 123 | + // as it doesn't recognize model version 4.1.0 |
| 124 | + @SuppressWarnings("unused") |
| 125 | + IProject project = importProject("resources/projects/HelloMaven4/pom.xml"); |
| 126 | + |
| 127 | + // If we get here, Maven 4 model version was recognized |
| 128 | + // This indicates Maven 4 support is working |
| 129 | + |
| 130 | + } catch (Exception e) { |
| 131 | + // Expected with current m2e |
| 132 | + System.err.println("Maven 4 model version not supported yet: " + e.getMessage()); |
| 133 | + fail("Maven 4 model version 4.1.0 is not supported (expected with current m2e): " + e.getMessage()); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments