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
jarmoniuk committed Jun 9, 2023
1 parent ff26c22 commit a27af46
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 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) {
}
}
}

0 comments on commit a27af46

Please sign in to comment.