gnodet-bot commented on code in PR #13256: URL: https://github.com/apache/maven/pull/13256#discussion_r4087674929
########## impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultArtifactManagerTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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-200/201: 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() = "com.example:pom-project:pom::1.0" + ProducedArtifact pomArtifact = mock(ProducedArtifact.class); + when(pomArtifact.key()).thenReturn("com.example:pom-project:pom::1.0"); Review Comment: ⚠️ **Wrong key format — test doesn't exercise the bug path.** `Artifact.key()` (in `maven-api-core`) builds the key as: ``` groupId + ':' + artifactId + ':' + extension + (classifier.isEmpty() ? "" : ":" + classifier) + ':' + version ``` For an artifact with no classifier the result is `"com.example:pom-project:pom:1.0"` — a **single** colon before the version. The double-colon string `"com.example:pom-project:pom::1.0"` represents a non-empty classifier of `""` which is contradictory and not what the production code produces. Because of this mismatch, `id.equals(id(project.getArtifact()))` in the project loop evaluates to `false` (the loop key is `"pom:1.0"`, the mock key is `"pom::1.0"`), so the loop never matches and the test never reaches the code path that contained the bug. The test validates internal `paths`-map round-trip consistency but not the actual priority fix. Fix: use the key the real `Artifact.key()` would produce: ```suggestion when(pomArtifact.key()).thenReturn("com.example:pom-project:pom:1.0"); ``` With the corrected key the project loop *will* match the project artifact (which still has `getFile() = fakeJar`), demonstrating that the old code returned the JAR and the fixed code returns `pomPath`. ########## impl/maven-core/src/test/java/org/apache/maven/internal/impl/DefaultArtifactManagerTest.java: ########## @@ -0,0 +1,146 @@ +/* + * 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-200/201: when {@code jar:jar} is run on a POM-packaged project, Review Comment: 📝 **Javadoc cites the wrong issue ID.** The PR description and the production-code comment both reference `MINSTALL-315`, but this Javadoc says `MINSTALL-200/201`. Please align: ```suggestion * Regression test for MINSTALL-315: when {@code jar:jar} is run on a POM-packaged project, ``` -- 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]
