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


##########
impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultArtifactManagerTest.java:
##########
@@ -0,0 +1,149 @@
+/*
+ * 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");
+
+        // Before setPath(): the project loop returns the JAR path (the bug)
+        // After fix: paths map is checked first
+        Path pomPath = Path.of("pom.xml").toAbsolutePath();
+
+        // Act: register the POM path explicitly (what maven-install-plugin 
does)
+        manager.setPath(pomArtifact, pomPath);

Review Comment:
   ⚠️ **Test doesn't verify the fix — `setPath()` neutralizes the bug condition 
before `getPath()` is called.**
   
   `setPath(pomArtifact, pomPath)` (line 97) does **two** things:
   1. Puts `pomPath` into the `paths` map.
   2. **Also** iterates `getProjectArtifacts` and calls 
`projectArtifact.setFile(pomPath.toFile())` on the matching legacy artifact 
(line 99 of `DefaultArtifactManager.setPath`).
   
   So by the time `getPath(pomArtifact)` runs on line 100, 
`projectArtifact.getFile()` is already `pomPath.toFile()` — the `fakeJar` has 
been overwritten. Even the **old buggy code** (project-loop first) would return 
`pomPath` at this point. The test passes on both the old and new 
implementations.
   
   To actually demonstrate the regression, you need to call `getPath()` 
**before** `setPath()` to confirm the fakeJar is returned, proving the bug 
exists. Then call `setPath()` + `getPath()` again to confirm the fix. Something 
like:
   
   ```suggestion
           // Verify the bug: before setPath(), the project loop returns the 
JAR path (MINSTALL-315).
           // This assertion FAILS on the unfixed code and is the core 
regression check.
           Path pomPath = Path.of("pom.xml").toAbsolutePath();
           Optional<Path> bugResult = manager.getPath(pomArtifact);
           assertTrue(bugResult.isPresent(), "project artifact has a file set");
           assertEquals(
                   fakeJar.toPath(),
                   bugResult.get(),
                   "Before fix: getPath() returned the project artifact file 
(the JAR), not the paths map");
   
           // Act: register the POM path explicitly (what maven-install-plugin 
does).
           // setPath() also mutates projectArtifact.file as a side-effect, so 
after this call
           // even the old code would return the correct path via the project 
loop.
           // The priority fix (paths-map-first) ensures correctness regardless 
of that side-effect.
           manager.setPath(pomArtifact, pomPath);
   ```



-- 
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