gnodet-bot commented on code in PR #13256:
URL: https://github.com/apache/maven/pull/13256#discussion_r4090467035


##########
impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultArtifactManagerTest.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.internal.impl;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.ProducedArtifact;
+import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.artifact.handler.ArtifactHandler;
+import org.apache.maven.execution.DefaultMavenExecutionRequest;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.project.MavenProject;
+import org.eclipse.aether.DefaultRepositorySystemSession;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Unit tests for {@link DefaultArtifactManager}.
+ */
+class DefaultArtifactManagerTest {
+
+    /**
+     * Regression test for MINSTALL-315: when {@code jar:jar} is run on a 
POM-packaged project,
+     * it sets the project's main artifact file to the produced JAR. Since 
POM-packaged projects share
+     * the same artifact coordinates for both the POM artifact and the project 
artifact, a subsequent
+     * call to {@code setPath(pomArtifact, pomPath)} followed by {@code 
getPath(pomArtifact)} must
+     * return the explicitly registered POM path — NOT the JAR file set by 
{@code jar:jar}.
+     *
+     * <p>The bug was that {@code getPath()} consulted {@code 
project.getArtifact().getFile()} before
+     * checking the explicitly-registered {@code paths} map, so the JAR file 
was returned instead of
+     * the POM path.
+     */
+    @Test
+    void setPathTakesPrecedenceOverProjectArtifactFile() {
+        // Simulate a pom-packaged project where jar:jar has set the project 
artifact's file to a JAR.
+        // In a real build, this happens when the user runs "mvn jar:jar 
install:install" on a pom project.
+        MavenProject mavenProject = new MavenProject();
+        mavenProject.setGroupId("com.example");
+        mavenProject.setArtifactId("pom-project");
+        mavenProject.setVersion("1.0");
+        mavenProject.setPackaging("pom");
+
+        // Create a pom-type artifact for the project (same as what 
ProjectArtifact wraps)
+        ArtifactHandler pomHandler = new PomArtifactHandler();
+        DefaultArtifact projectArtifact =
+                new DefaultArtifact("com.example", "pom-project", "1.0", null, 
"pom", null, pomHandler);
+        // jar:jar sets the project artifact's file to a JAR — this is the bug 
trigger
+        File fakeJar = new File("target/pom-project-1.0.jar");
+        projectArtifact.setFile(fakeJar);
+        mavenProject.setArtifact(projectArtifact);
+
+        // Wire up InternalMavenSession mock
+        InternalMavenSession internalSession = 
mock(InternalMavenSession.class);
+        MavenSession mavenSession = new MavenSession(
+                null, new DefaultRepositorySystemSession(h -> false), new 
DefaultMavenExecutionRequest(), null);
+        mavenSession.setAllProjects(List.of(mavenProject));
+        when(internalSession.getMavenSession()).thenReturn(mavenSession);
+
+        DefaultArtifactManager manager = new 
DefaultArtifactManager(internalSession);
+
+        // Create a ProducedArtifact mock representing the POM artifact.
+        // key() must match the format produced by Artifact.key():
+        //   groupId + ':' + artifactId + ':' + extension + 
(classifier.isEmpty() ? "" : ":" + classifier) + ':' +
+        // version
+        // For a no-classifier POM artifact this is 
"com.example:pom-project:pom:1.0" (single colon before version).
+        ProducedArtifact pomArtifact = mock(ProducedArtifact.class);
+        when(pomArtifact.key()).thenReturn("com.example:pom-project:pom:1.0");
+
+        Path pomPath = Path.of("pom.xml").toAbsolutePath();
+
+        // Verify the bug condition: before setPath(), getPath() hits the 
project-artifact loop and
+        // returns the JAR path that jar:jar set. This is the core regression 
check — it passes only
+        // when the paths map is consulted first (the fix), and would fail on 
the original code.
+        Optional<Path> beforeSet = manager.getPath(pomArtifact);
+        assertTrue(beforeSet.isPresent(), "project artifact has a file set via 
jar:jar");
+        assertEquals(
+                fakeJar.toPath(),
+                beforeSet.get(),
+                "Before setPath(): getPath() must return the project artifact 
file (the JAR set by jar:jar)");
+
+        // Act: register the POM path explicitly (what maven-install-plugin 
does).
+        // Note: setPath() also calls setFile() on the matching legacy 
artifact as a side-effect,
+        // so after this call the project-loop path would also return pomPath.
+        // The priority fix (paths-map-first) ensures correctness regardless 
of that side-effect.
+        manager.setPath(pomArtifact, pomPath);
+
+        // Assert: getPath() must now return the POM path, not the JAR file 
set by jar:jar
+        Optional<Path> result = manager.getPath(pomArtifact);
+        assertTrue(result.isPresent(), "getPath() must return a non-empty 
Optional after setPath()");
+        assertEquals(
+                pomPath,
+                result.get(),
+                "After setPath(): getPath() must return the explicitly-set POM 
path, not the JAR file set by jar:jar");
+    }
+
+    /**
+     * Minimal ArtifactHandler for POM-type artifacts, matching the handler 
used by
+     * {@link org.apache.maven.project.artifact.ProjectArtifact}.
+     */
+    private static class PomArtifactHandler implements ArtifactHandler {
+        @Override
+        public String getClassifier() {
+            return null;
+        }
+
+        @Override
+        public String getDirectory() {
+            return null;
+        }
+
+        @Override
+        public String getExtension() {
+            return "pom";
+        }
+
+        @Override
+        public String getLanguage() {
+            return "none";
+        }
+
+        @Override
+        public String getPackaging() {
+            return "pom";
+        }
+
+        @Override
+        @Deprecated
+        public boolean isAddedToClasspath() {
+            return false;
+        }

Review Comment:
   ⚠️ **Test still passes on both old and new code — regression not caught.**
   
   The comment on line 132-133 is incorrect: the `beforeSet` assertion does 
**not** distinguish old from new code.
   
   Before `setPath()`, `paths` is empty, so `paths.get(id)` returns `null` on 
**both** the old and new implementations — both fall through to the project 
loop, which returns `fakeJar.toPath()`. The assertion passes identically on 
both.
   
   After `setPath(pomArtifact, pomPath)`, `setPath()` calls 
`projectArtifact.setFile(pomPath.toFile())` as a side-effect (line 99 of 
`DefaultArtifactManager.setPath`). So `projectArtifact.getFile()` is now 
`pomPath.toFile()` — the old buggy code (project-loop first) also returns 
`pomPath`, making the `result` assertion pass on both old and new code too.
   
   To actually differentiate the two implementations: call `setPath()` to 
register `pomPath` in the map, then reset `projectArtifact.setFile(fakeJar)` to 
re-arm the bug condition, then call `getPath()`. Only the new code 
(paths-map-first) returns `pomPath`; the old code (project-loop-first) would 
return `fakeJar`.
   
   ```suggestion
           // Verify the fix: register the POM path explicitly (what 
maven-install-plugin does),
           // then re-arm the bug condition by resetting the project artifact's 
file to the JAR.
           // This is the only way to distinguish old code (project-loop-first) 
from new code (paths-map-first),
           // because setPath() also calls projectArtifact.setFile(pomPath) as 
a side-effect — meaning
           // the old buggy code would also pass an assertion made immediately 
after setPath().
           manager.setPath(pomArtifact, pomPath);
   
           // Re-arm the bug: simulate jar:jar running again (or any code that 
overwrites the project artifact file)
           // after install:install has registered the POM path. Old code 
returns fakeJar; new code returns pomPath.
           projectArtifact.setFile(fakeJar);
   
           // Assert: getPath() must return the POM path from the paths map, 
NOT the JAR now in project artifact.
           // This assertion FAILS on the unfixed code (project-loop first → 
returns fakeJar)
           // and PASSES on the fixed code (paths-map first → returns pomPath).
           Optional<Path> result = manager.getPath(pomArtifact);
           assertTrue(result.isPresent(), "getPath() must return a non-empty 
Optional after setPath()");
           assertEquals(
                   pomPath,
                   result.get(),
                   "After setPath(): getPath() must return the explicitly-set 
POM path, not the JAR file set by jar:jar");
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to