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 215f505
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
package org.codehaus.mojo.versions.api;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* Copyright MojoHaus and Contributors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.*;
Expand Down Expand Up @@ -82,11 +78,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 +113,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 215f505

Please sign in to comment.