Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.Writer;
import java.nio.file.Files;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -48,6 +47,7 @@
* @since 2.1
*/
public abstract class AbstractHelpMojo extends AbstractMojo {

/** The maximum length of a display line. */
protected static final int LINE_LENGTH = 79;

Expand Down Expand Up @@ -144,7 +144,8 @@ protected org.eclipse.aether.artifact.Artifact getAetherArtifact(String artifact
case 2:
groupId = artifactParts[0];
artifactId = artifactParts[1];
version = Artifact.LATEST_VERSION;
// Not null: Aether normalises a null version to "", not to a metaversion.
version = "LATEST";
break;
case 3:
groupId = artifactParts[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https

*
* 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.plugins.help;

import org.apache.maven.plugin.MojoExecutionException;
import org.eclipse.aether.artifact.Artifact;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Tests the coordinate parsing in {@link AbstractHelpMojo#getAetherArtifact(String, String)}.
*/
class AbstractHelpMojoTest {

private final AbstractHelpMojo mojo = new AbstractHelpMojo(null, null) {
@Override
public void execute() {}
};

@Test
void coordinatesWithoutAVersionGetTheLatestMetaversion() throws Exception {
Artifact artifact = mojo.getAetherArtifact("org.apache.maven:maven-core", "pom");

assertEquals("org.apache.maven", artifact.getGroupId());
assertEquals("maven-core", artifact.getArtifactId());
assertEquals("pom", artifact.getExtension());
// The literal, not the constant: leaving the version out has to keep producing a
// metaversion the resolver recognises. A null version would arrive here as "" instead.
assertEquals("LATEST", artifact.getVersion());
}

@Test
void coordinatesWithAVersionKeepIt() throws Exception {
Artifact artifact = mojo.getAetherArtifact("org.apache.maven:maven-core:3.9.16", "pom");

assertEquals("3.9.16", artifact.getVersion());
}

@Test
void coordinatesWithTooManyPartsAreRejected() {
assertThrows(
MojoExecutionException.class,
() -> mojo.getAetherArtifact("org.apache.maven:maven-core:3.9.16:extra", "pom"));
}

@Test
void emptyCoordinatesAreRejected() {
assertThrows(IllegalArgumentException.class, () -> mojo.getAetherArtifact("", "pom"));
}
}
Loading