Skip to content

Commit

Permalink
Resolves #600: Add allowDowngrade capability to UseLatestReleasesMojo…
Browse files Browse the repository at this point in the history
…, UseNextReleasesMojo, UseNextVersionsMojo
  • Loading branch information
andrzejj0 committed Jan 2, 2023
1 parent 2a755ad commit 364454d
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ public class UseLatestReleasesMojo extends UseLatestVersionsMojoBase {
@Parameter(property = "allowIncrementalUpdates", defaultValue = "true")
protected boolean allowIncrementalUpdates = true;

/**
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
* and there exists a non-snapshot version within the range fulfilling the criteria.</p>
* <p>Only valid if <code>allowSnapshots</code> is <code>false</code>.</p>
*
* @since 2.15.0
*/
@Parameter(property = "allowDowngrade", defaultValue = "false")
protected boolean allowDowngrade;

// ------------------------------ METHODS --------------------------

@Inject
Expand Down Expand Up @@ -148,7 +158,8 @@ private void useLatestReleases(
(dep, versions) -> {
try {
return getLastFiltered(
versions.getNewerVersions(dep.getVersion(), unchangedSegment, false, false), dep);
versions.getNewerVersions(dep.getVersion(), unchangedSegment, false, allowDowngrade),
dep);
} catch (InvalidSegmentException e) {
getLog().warn(String.format(
"Skipping the processing of %s:%s:%s due to: %s",
Expand All @@ -157,7 +168,8 @@ private void useLatestReleases(
return empty();
},
changeKind,
dep -> !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
dep -> allowDowngrade
|| !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import static java.util.Collections.singletonList;
Expand All @@ -51,7 +55,15 @@
@Mojo(name = "use-next-releases", threadSafe = true)
public class UseNextReleasesMojo extends UseLatestVersionsMojoBase {

// ------------------------------ METHODS --------------------------
/**
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
* and there exists a non-snapshot version within the range fulfilling the criteria.</p>
* <p>Only valid if <code>allowSnapshots</code> is <code>false</code>.</p>
*
* @since 2.12.0
*/
@Parameter(property = "allowDowngrade", defaultValue = "false")
protected boolean allowDowngrade;

@Inject
public UseNextReleasesMojo(
Expand Down Expand Up @@ -99,9 +111,10 @@ private void useNextReleases(
useLatestVersions(
pom,
dependencies,
(dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), false))
(dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), Optional.empty(), false, allowDowngrade))
.findFirst(),
changeKind,
dep -> !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
dep -> allowDowngrade
|| !SNAPSHOT_REGEX.matcher(dep.getVersion()).matches());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,21 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.wagon.Wagon;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.api.recording.ChangeRecord;
import org.codehaus.mojo.versions.api.recording.ChangeRecorder;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;

import static java.util.Collections.singletonList;
Expand All @@ -51,6 +54,16 @@
@Mojo(name = "use-next-versions", threadSafe = true)
public class UseNextVersionsMojo extends UseLatestVersionsMojoBase {

/**
* <p>Whether to downgrade a snapshot dependency if <code>allowSnapshots</code> is <code>false</code>
* and there exists a non-snapshot version within the range fulfilling the criteria.</p>
* <p>Only valid if <code>allowSnapshots</code> is <code>false</code>.</p>
*
* @since 2.12.0
*/
@Parameter(property = "allowDowngrade", defaultValue = "false")
protected boolean allowDowngrade;

// ------------------------------ METHODS --------------------------

@Inject
Expand Down Expand Up @@ -97,7 +110,7 @@ private void useNextVersions(
useLatestVersions(
pom,
dependencies,
(dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), allowSnapshots))
(dep, versions) -> Arrays.stream(versions.getNewerVersions(dep.getVersion(), Optional.empty(), allowSnapshots, allowDowngrade))
.findFirst(),
changeKind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.change.DefaultVersionChange;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.hamcrest.Matchers;
Expand All @@ -26,7 +27,10 @@
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession;
import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;

public class UseLatestReleasesMojoTest {
Expand Down Expand Up @@ -98,4 +102,66 @@ public void testDontUpgradeToBeta()
}
assertThat(changeRecorder.getChanges(), Matchers.empty());
}


@Test
public void testAllowDowngrade()
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"});
}
});
mojo.getProject()
.setDependencies(singletonList(DependencyBuilder.newBuilder()
.withGroupId("default-group")
.withArtifactId("artifactA")
.withVersion("1.0.1-SNAPSHOT")
.build()));
mojo.allowDowngrade = true;

try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class)))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange(
"default-group", "artifactA",
"1.0.1-SNAPSHOT", "1.0.0")));
}

@Test
public void testDisallowDowngrade()
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"});
}
});
mojo.getProject()
.setDependencies(singletonList(DependencyBuilder.newBuilder()
.withGroupId("default-group")
.withArtifactId("artifactA")
.withVersion("1.0.1-SNAPSHOT")
.build()));

try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class)))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
}
assertThat(changeRecorder.getChanges(), empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import java.util.Collections;
import java.util.HashMap;

import javax.xml.stream.XMLStreamException;
import org.apache.maven.model.Model;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.codehaus.mojo.versions.api.PomHelper;
import org.codehaus.mojo.versions.api.VersionRetrievalException;
import org.codehaus.mojo.versions.change.DefaultVersionChange;
import org.codehaus.mojo.versions.utils.DependencyBuilder;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
Expand All @@ -34,14 +38,17 @@
import org.mockito.MockedStatic;

import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE;
import static org.apache.maven.plugin.testing.ArtifactStubFactory.setVariableValueToObject;
import static org.codehaus.mojo.versions.utils.MockUtils.mockAetherRepositorySystem;
import static org.codehaus.mojo.versions.utils.MockUtils.mockMavenSession;
import static org.codehaus.mojo.versions.utils.MockUtils.mockRepositorySystem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mockStatic;

/**
Expand Down Expand Up @@ -123,4 +130,66 @@ public void testFindANewerRelease() throws IllegalAccessException {
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange("default-group", "dependency-artifact", "1.1.0", "1.1.1")));
}


@Test
public void testAllowDowngrade()
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"});
}
});
mojo.getProject()
.setDependencies(singletonList(DependencyBuilder.newBuilder()
.withGroupId("default-group")
.withArtifactId("artifactA")
.withVersion("1.0.1-SNAPSHOT")
.build()));
mojo.allowDowngrade = true;

try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class)))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
}
assertThat(
changeRecorder.getChanges(),
hasItem(new DefaultVersionChange(
"default-group", "artifactA",
"1.0.1-SNAPSHOT", "1.0.0")));
}

@Test
public void testDisallowDowngrade()
throws MojoExecutionException, XMLStreamException, MojoFailureException, VersionRetrievalException {
mojo.aetherRepositorySystem = mockAetherRepositorySystem(new HashMap<String, String[]>() {
{
put("artifactA", new String[] {"1.0.0", "1.0.1-SNAPSHOT"});
}
});
mojo.getProject()
.setDependencies(singletonList(DependencyBuilder.newBuilder()
.withGroupId("default-group")
.withArtifactId("artifactA")
.withVersion("1.0.1-SNAPSHOT")
.build()));

try (MockedStatic<PomHelper> pomHelper = mockStatic(PomHelper.class)) {
pomHelper
.when(() -> PomHelper.setDependencyVersion(
any(), anyString(), anyString(), anyString(), anyString(), any(Model.class)))
.thenReturn(true);
pomHelper
.when(() -> PomHelper.getRawModel(any(MavenProject.class)))
.thenReturn(mojo.getProject().getModel());
mojo.update(null);
}
assertThat(changeRecorder.getChanges(), empty());
}
}
Loading

0 comments on commit 364454d

Please sign in to comment.