Skip to content

Commit f51b9d5

Browse files
authored
#1331: Fix NPE in restrictionForUnchangedSegment if actual version is null (#1332)
1 parent 8d209b3 commit f51b9d5

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

versions-common/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,15 @@ public Restriction restrictionForSelectedSegment(ArtifactVersion lowerBound, Opt
131131
ArtifactVersion nextVersion = selectedSegment
132132
.filter(s -> s.isMajorTo(SUBINCREMENTAL))
133133
.map(Segment::minorTo)
134+
.filter(__ -> highestLowerBound != null)
134135
.map(s -> (ArtifactVersion) new BoundArtifactVersion(highestLowerBound, s))
135136
.orElse(highestLowerBound);
136137
return new Restriction(
137138
nextVersion,
138139
false,
139140
selectedSegment
140141
.filter(MAJOR::isMajorTo)
142+
.filter(__ -> highestLowerBound != null)
141143
.map(s -> (ArtifactVersion) new BoundArtifactVersion(highestLowerBound, s))
142144
.orElse(null),
143145
false);
@@ -157,6 +159,7 @@ public Restriction restrictionForUnchangedSegment(
157159
.orElse(null)
158160
: selectedRestrictionUpperBound;
159161
ArtifactVersion upperBound = unchangedSegment
162+
.filter(__ -> selectedRestrictionUpperBound != null)
160163
.map(s -> (ArtifactVersion) new BoundArtifactVersion(
161164
selectedRestrictionUpperBound, s.isMajorTo(SUBINCREMENTAL) ? Segment.minorTo(s) : s))
162165
.orElse(null);
@@ -174,7 +177,8 @@ public Restriction restrictionForUnchangedSegment(
174177
@Override
175178
public Restriction restrictionForIgnoreScope(ArtifactVersion lowerBound, Optional<Segment> ignored) {
176179
ArtifactVersion highestLowerBound = getHighestLowerBound(lowerBound);
177-
ArtifactVersion nextVersion = ignored.map(s -> (ArtifactVersion) new BoundArtifactVersion(highestLowerBound, s))
180+
ArtifactVersion nextVersion = ignored.filter(__ -> highestLowerBound != null)
181+
.map(s -> (ArtifactVersion) new BoundArtifactVersion(highestLowerBound, s))
178182
.orElse(highestLowerBound);
179183
return new Restriction(nextVersion, false, null, false);
180184
}

versions-common/src/test/java/org/codehaus/mojo/versions/api/ArtifactVersionsTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static org.hamcrest.Matchers.arrayContaining;
5151
import static org.hamcrest.Matchers.arrayWithSize;
5252
import static org.hamcrest.Matchers.emptyArray;
53+
import static org.hamcrest.Matchers.equalTo;
5354
import static org.hamcrest.Matchers.hasItem;
5455
import static org.hamcrest.Matchers.hasToString;
5556
import static org.hamcrest.Matchers.is;
@@ -344,4 +345,34 @@ void testPredicate() {
344345
assertThat(noSnapshots.getVersions(true), arrayWithSize(15));
345346
assertThat(noSnapshots.getCurrentVersion(), nullValue());
346347
}
348+
349+
@Test
350+
void testRestrictionForUnchangedSegmentIfActualVersionNullAndUpperRestrictionNull() throws InvalidSegmentException {
351+
ArtifactVersions versions = new ArtifactVersions(
352+
new DefaultArtifact("default-group", "dummy-api", "(,2)", "foo", "bar", "jar", null),
353+
Arrays.asList(versions("1.0.0")));
354+
assertThat(
355+
versions.restrictionForUnchangedSegment(null, Optional.of(MAJOR), false),
356+
is(equalTo(new Restriction(null, false, null, false))));
357+
}
358+
359+
@Test
360+
void testRestrictionForSelectedSegmentIfLowerBoundNull() throws InvalidSegmentException {
361+
ArtifactVersions versions = new ArtifactVersions(
362+
new DefaultArtifact("default-group", "dummy-api", "(,)", "foo", "bar", "jar", null),
363+
Arrays.asList(versions("1.0.0")));
364+
assertThat(
365+
versions.restrictionForSelectedSegment(null, Optional.of(MAJOR)),
366+
is(equalTo(new Restriction(null, false, null, false))));
367+
}
368+
369+
@Test
370+
void testRestrictionForIgnoreScopeLowerBoundNull() throws InvalidSegmentException {
371+
ArtifactVersions versions = new ArtifactVersions(
372+
new DefaultArtifact("default-group", "dummy-api", "(,)", "foo", "bar", "jar", null),
373+
Arrays.asList(versions("1.0.0")));
374+
assertThat(
375+
versions.restrictionForIgnoreScope(null, Optional.of(MAJOR)),
376+
is(equalTo(new Restriction(null, false, null, false))));
377+
}
347378
}

0 commit comments

Comments
 (0)