DefaultArtifactManagerTest.java

/*
 * 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.RepositoryUtils;
import org.apache.maven.api.Artifact;
import org.apache.maven.api.ProducedArtifact;
import org.apache.maven.api.Version;
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.impl.InternalSession;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.artifact.ProjectArtifact;
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.ArgumentMatchers.anyString;
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();

        // Register the POM path explicitly (what maven-install-plugin does).
        // setPath() also calls projectArtifact.setFile(pomPath.toFile()) as a side-effect,
        // so immediately after this call even the old buggy code (project-loop first) would
        // return pomPath via the project loop ��� both implementations pass at that point.
        manager.setPath(pomArtifact, pomPath);

        // Re-arm the bug condition: simulate jar:jar running again after install:install has
        // already registered the POM path in the paths map. This is the critical state that
        // distinguishes the two implementations:
        //   - old code (project-loop first): returns fakeJar ��� WRONG
        //   - new code (paths-map first):    returns pomPath ��� CORRECT
        projectArtifact.setFile(fakeJar);

        // Assert: getPath() must return the POM path from the paths map,
        // NOT the JAR now in the project artifact's file field.
        // 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(),
                "getPath() must return the explicitly-set POM path from the paths map,"
                        + " not the JAR file in the project artifact (MINSTALL-315)");
    }

    /**
     * MNG-8138: {@code jar:jar} on a pom-packaged project sets the JAR as the file of the project artifact, which has
     * the POM's coordinates. Without any {@code setPath()} call, {@code getPath(pomArtifact)} must still return the
     * project's POM file, so install and deploy never publish the JAR under the POM's coordinates.
     */
    @Test
    void pomArtifactPathIsProjectFileEvenWhenProjectArtifactHasJar() {
        Path pomFile = Path.of("pom.xml").toAbsolutePath();
        MavenProject project = pomProject(pomFile);
        project.getArtifact().setFile(new File("target/pom-project-1.0.jar"));

        DefaultArtifactManager manager = manager(project);

        assertEquals(Optional.of(pomFile), manager.getPath(pomArtifact(project)));
    }

    @Test
    void explicitSetPathOfPomArtifactWinsOverProjectFile() {
        MavenProject project = pomProject(Path.of("pom.xml").toAbsolutePath());
        DefaultArtifactManager manager = manager(project);
        ProducedArtifact pomArtifact = artifact("com.example:pom-project:pom:1.0");
        Path flattened = Path.of("target/flattened-pom.xml").toAbsolutePath();

        manager.setPath(pomArtifact, flattened);

        assertEquals(Optional.of(flattened), manager.getPath(pomArtifact));
    }

    /**
     * An artifact with a reactor project's POM coordinates but its own path, such as the consumer POM of a project
     * not selected with {@code -pl}, keeps that path: only the pom-packaged project's main artifact is ignored.
     */
    @Test
    void artifactWithPomCoordinatesKeepsItsOwnPath() {
        MavenProject project = pomProject(Path.of("pom.xml").toAbsolutePath());
        project.getArtifact().setFile(new File("target/pom-project-1.0.jar"));
        Path repositoryPom = Path.of("target/repo/pom-project-1.0.pom").toAbsolutePath();

        DefaultArtifactManager manager = manager(project);

        assertEquals(
                Optional.of(repositoryPom),
                manager.getPath(new org.apache.maven.impl.DefaultArtifact(
                        versionParsingSession(),
                        new org.eclipse.aether.artifact.DefaultArtifact("com.example", "pom-project", "", "pom", "1.0")
                                .setPath(repositoryPom))));
    }

    @Test
    void mainArtifactOfJarProjectIsStillItsJar() {
        MavenProject project = new MavenProject();
        project.setGroupId("com.example");
        project.setArtifactId("jar-project");
        project.setVersion("1.0");
        project.setPackaging("jar");
        project.setFile(Path.of("pom.xml").toAbsolutePath().toFile());
        DefaultArtifact main = new DefaultArtifact(
                "com.example", "jar-project", "1.0", null, "jar", null, new TestArtifactHandler("jar"));
        File jar = new File("target/jar-project-1.0.jar");
        main.setFile(jar);
        project.setArtifact(main);

        DefaultArtifactManager manager = manager(project);

        assertEquals(Optional.of(jar.toPath()), manager.getPath(artifact("com.example:jar-project:jar:1.0")));
    }

    private static MavenProject pomProject(Path pomFile) {
        MavenProject project = new MavenProject();
        project.setGroupId("com.example");
        project.setArtifactId("pom-project");
        project.setVersion("1.0");
        project.setPackaging("pom");
        if (pomFile != null) {
            project.setFile(pomFile.toFile());
        }
        project.setArtifact(
                new DefaultArtifact("com.example", "pom-project", "1.0", null, "pom", null, new PomArtifactHandler()));
        return project;
    }

    private static Artifact pomArtifact(MavenProject project) {
        return new org.apache.maven.impl.DefaultArtifact(
                versionParsingSession(), RepositoryUtils.toArtifact(new ProjectArtifact(project)));
    }

    /**
     * {@link org.apache.maven.impl.DefaultArtifact#key()} goes through {@code session.parseVersion()}; a bare mock
     * would yield {@code null} and a key that never matches a project.
     */
    private static InternalSession versionParsingSession() {
        InternalSession session = mock(InternalSession.class);
        when(session.parseVersion(anyString())).thenAnswer(invocation -> {
            Version version = mock(Version.class);
            when(version.toString()).thenReturn(invocation.getArgument(0));
            return version;
        });
        return session;
    }

    private static DefaultArtifactManager manager(MavenProject project) {
        InternalMavenSession internalSession = mock(InternalMavenSession.class);
        MavenSession mavenSession = new MavenSession(
                null, new DefaultRepositorySystemSession(h -> false), new DefaultMavenExecutionRequest(), null);
        mavenSession.setAllProjects(List.of(project));
        when(internalSession.getMavenSession()).thenReturn(mavenSession);
        return new DefaultArtifactManager(internalSession);
    }

    private static ProducedArtifact artifact(String key) {
        ProducedArtifact artifact = mock(ProducedArtifact.class);
        when(artifact.key()).thenReturn(key);
        return artifact;
    }

    /**
     * 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;
        }

        @Override
        public boolean isIncludesDependencies() {
            return false;
        }
    }
}