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-7780] DefaultArtifact.equals throws NullPointerException if o.version is null #1108

Merged
merged 3 commits into from
May 22, 2023
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 @@ -19,11 +19,7 @@
package org.apache.maven.artifact;

import java.io.File;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.metadata.ArtifactMetadata;
Expand Down Expand Up @@ -287,44 +283,25 @@ public String toString() {
return sb.toString();
}

public int hashCode() {
int result = 17;
result = 37 * result + groupId.hashCode();
result = 37 * result + artifactId.hashCode();
result = 37 * result + type.hashCode();
if (version != null) {
result = 37 * result + version.hashCode();
}
result = 37 * result + (classifier != null ? classifier.hashCode() : 0);
return result;
}

@Override
public boolean equals(Object o) {
if (o == this) {
if (this == o) {
return true;
}

if (!(o instanceof Artifact)) {
return false;
}

Artifact a = (Artifact) o;

if (!a.getGroupId().equals(groupId)) {
return false;
} else if (!a.getArtifactId().equals(artifactId)) {
return false;
} else if (!a.getVersion().equals(version)) {
if (o == null || getClass() != o.getClass()) {
return false;
} else if (!a.getType().equals(type)) {
return false;
} else {
return a.getClassifier() == null
? classifier == null
: a.getClassifier().equals(classifier);
}
DefaultArtifact that = (DefaultArtifact) o;
return Objects.equals(groupId, that.groupId)
&& Objects.equals(artifactId, that.artifactId)
&& Objects.equals(type, that.type)
&& Objects.equals(classifier, that.classifier)
&& Objects.equals(version, that.version);
}

// We don't consider the version range in the comparison, just the resolved version
@Override
public int hashCode() {
return Objects.hash(groupId, artifactId, type, classifier, version);
}

public String getBaseVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ void testNonResolvedVersionRangeConsistentlyYieldsNullVersions() throws Exceptio
assertNull(artifact.getVersion());
assertNull(artifact.getBaseVersion());
}

@Test
void testMNG7780() throws Exception {
VersionRange vr = VersionRange.createFromVersionSpec("[1.0,2.0)");
artifact = new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
assertNull(artifact.getVersion());
assertNull(artifact.getBaseVersion());

DefaultArtifact nullVersionArtifact =
new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
assertEquals(artifact, nullVersionArtifact);
}
}