Skip to content

Commit

Permalink
Resolves mojohaus#973: NPE if actual version is null for a dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejj0 committed Jun 9, 2023
1 parent ff26c22 commit f8e0c0c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,12 @@ protected ArtifactVersion getHighestLowerBound(ArtifactVersion lowerBoundVersion
* If the artifact is bound by one or more version ranges, returns the restriction that constitutes
* the version range containing the selected actual version.
* If there are no version ranges, returns the provided version.
* @param selectedVersion actual version used
* @param selectedVersion actual version used, may not be {@code null}
* @return restriction containing the version range selected by the given version,
* or {@link Optional#empty()} if there are no ranges
*/
protected Optional<Restriction> getSelectedRestriction(ArtifactVersion selectedVersion) {
assert selectedVersion != null;
return Optional.ofNullable(getCurrentVersionRange())
.map(VersionRange::getRestrictions)
.flatMap(r -> r.stream()
Expand Down Expand Up @@ -116,7 +117,8 @@ public Restriction restrictionForSelectedSegment(ArtifactVersion lowerBound, Opt
public Restriction restrictionForUnchangedSegment(
ArtifactVersion actualVersion, Optional<Segment> unchangedSegment, boolean allowDowngrade)
throws InvalidSegmentException {
Optional<Restriction> selectedRestriction = getSelectedRestriction(actualVersion);
Optional<Restriction> selectedRestriction =
Optional.ofNullable(actualVersion).flatMap(this::getSelectedRestriction);
ArtifactVersion selectedRestrictionUpperBound =
selectedRestriction.map(Restriction::getUpperBound).orElse(actualVersion);
ArtifactVersion lowerBound = allowDowngrade
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.codehaus.mojo.versions.utils.ArtifactVersionUtils.version;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.fail;

/**
* Unit tests for {@link AbstractVersionDetails}
Expand Down Expand Up @@ -156,4 +157,24 @@ public void testRestrictionForUnchangedSegmentWithTwoNotConnectingRanges4()
.containsVersion(version("2.0.0-1")),
is(true));
}

@Test
public void testRestrictionForUnchangedSegmentWithoutVersionInformation()
throws InvalidSegmentException, InvalidVersionSpecificationException {
instance.setCurrentVersionRange(VersionRange.createFromVersionSpec("[,0]"));
assertThat(
instance.restrictionForUnchangedSegment(null, empty(), false).containsVersion(version("1.0.0")),
is(true));
}

@Test
public void testGetSelectedRestrictionForNoVersion()
throws InvalidVersionSpecificationException, InvalidSegmentException {
instance.setCurrentVersionRange(VersionRange.createFromVersionSpec("[,0]"));
try {
instance.getSelectedRestriction(null);
fail("Expected a NullPointerException to be thrown or assertions are not enabled.");
} catch (AssertionError ignored) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.*;

import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoExecutionException;
Expand Down

0 comments on commit f8e0c0c

Please sign in to comment.