Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-8192] Consistently throw InvalidArtifactRTException for invalid #1637

Merged
merged 1 commit into from
Aug 10, 2024
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
5 changes: 5 additions & 0 deletions maven-artifact/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ under the License.
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,22 @@ private void validateIdentity() {
groupId, artifactId, getVersion(), type, "The groupId cannot be empty.");
}

if (artifactId == null) {
if (empty(artifactId)) {
throw new InvalidArtifactRTException(
groupId, artifactId, getVersion(), type, "The artifactId cannot be empty.");
}

if (type == null) {
if (empty(type)) {
throw new InvalidArtifactRTException(groupId, artifactId, getVersion(), type, "The type cannot be empty.");
}

if ((version == null) && (versionRange == null)) {
if ((empty(version)) && (versionRange == null)) {
throw new InvalidArtifactRTException(
groupId, artifactId, getVersion(), type, "The version cannot be empty.");
}
}

private boolean empty(String value) {
public static boolean empty(String value) {
return (value == null) || (value.trim().length() < 1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.WeakHashMap;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;

/**
* Construct a version range from a specification.
Expand Down Expand Up @@ -202,6 +203,9 @@ private static Restriction parseRestriction(String spec) throws InvalidVersionSp
}

public static VersionRange createFromVersion(String version) {
if (DefaultArtifact.empty(version)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Or should we throw here for null/empty string? If so, which exception?

Copy link
Member

Choose a reason for hiding this comment

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

Null as in line 95

return null;
}
VersionRange cached = CACHE_VERSION.get(version);
if (cached == null) {
List<Restriction> restrictions = Collections.emptyList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
*/
package org.apache.maven.artifact;

import java.util.stream.Stream;

import org.apache.maven.artifact.handler.ArtifactHandlerMock;
import org.apache.maven.artifact.versioning.VersionRange;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

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

class DefaultArtifactTest {
Expand Down Expand Up @@ -152,4 +158,31 @@ void testMNG7780() throws Exception {
new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
assertEquals(artifact, nullVersionArtifact);
}

@ParameterizedTest
@MethodSource("invalidMavenCoordinates")
void testIllegalCoordinatesInConstructor(String groupId, String artifactId, String version) {
assertThrows(
InvalidArtifactRTException.class,
() -> new DefaultArtifact(
groupId, artifactId, version, scope, type, classifier, artifactHandler, false));
if (version == null) {
assertThrows(
InvalidArtifactRTException.class,
() -> new DefaultArtifact(
groupId, artifactId, (VersionRange) null, scope, type, classifier, artifactHandler, false));

This comment was marked as outdated.

}
}

static Stream<Arguments> invalidMavenCoordinates() {
return Stream.of(
Arguments.of(null, null, null),
Arguments.of("", "", ""),
Arguments.of(null, "artifactId", "1.0"),
Arguments.of("", "artifactId", "1.0"),
Arguments.of("groupId", null, "1.0"),
Arguments.of("groupId", "", "1.0"),
Arguments.of("groupId", "artifactId", null),
Arguments.of("groupId", "artifactId", ""));
}
}