Skip to content

Commit

Permalink
#454: Applying bound artifact versions to UpdatePropertiesMojo
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmoniuk committed Oct 10, 2022
1 parent 021e437 commit fb7778c
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class UpdatePropertiesMojo extends AbstractVersionsDependencyUpdaterMojo
*/
@Parameter( property = "allowMajorUpdates",
defaultValue = "true" )
protected boolean allowMajorUpdates;
protected boolean allowMajorUpdates = true;

/**
* Whether to allow the minor version number to be changed.
Expand All @@ -113,7 +113,7 @@ public class UpdatePropertiesMojo extends AbstractVersionsDependencyUpdaterMojo
*/
@Parameter( property = "allowMinorUpdates",
defaultValue = "true" )
protected boolean allowMinorUpdates;
protected boolean allowMinorUpdates = true;

/**
* Whether to allow the incremental version number to be changed.
Expand All @@ -122,7 +122,7 @@ public class UpdatePropertiesMojo extends AbstractVersionsDependencyUpdaterMojo
*/
@Parameter( property = "allowIncrementalUpdates",
defaultValue = "true" )
protected boolean allowIncrementalUpdates;
protected boolean allowIncrementalUpdates = true;

// -------------------------- STATIC METHODS --------------------------

Expand Down
41 changes: 26 additions & 15 deletions src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
import org.apache.maven.artifact.versioning.ArtifactVersion;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
import org.apache.maven.artifact.versioning.Restriction;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.project.MavenProject;
import org.codehaus.mojo.versions.Property;
import org.codehaus.mojo.versions.ordering.BoundArtifactVersion;
import org.codehaus.mojo.versions.ordering.InvalidSegmentException;
import org.codehaus.mojo.versions.ordering.VersionComparator;

import static java.util.Optional.empty;
import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL;

/**
* Manages a property that is associated with one or more artifacts.
Expand Down Expand Up @@ -320,22 +323,21 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert
* Retrieves the newest artifact version for the given property-denoted artifact or {@code null} if no newer
* version could be found.
*
* @param currentVersion current version of the artifact
* @param versionString current version of the artifact
* @param property property name indicating the artifact
* @param allowSnapshots whether snapshots should be considered
* @param reactorProjects collection of reactor projects
* @param helper VersionHelper object
* @param allowDowngrade whether downgrades should be allowed
* @param unchangedSegment indicates the (0-based) most major segment which needs to stay unchanged;
* -1 means that the whole version can be changed
* @param upperBoundSegment the upper bound segment; empty() means no upper bound
* @return newest artifact version fulfilling the criteria or null if no newer version could be found
* @throws InvalidSegmentException thrown if the {@code unchangedSegment} is not valid (e.g. greater than the number
* of segments in the version string)
* @throws InvalidVersionSpecificationException thrown if the version string in the property is not valid
*/
public ArtifactVersion getNewestVersion( String currentVersion, Property property, boolean allowSnapshots,
public ArtifactVersion getNewestVersion( String versionString, Property property, boolean allowSnapshots,
Collection<MavenProject> reactorProjects, VersionsHelper helper,
boolean allowDowngrade, Optional<Segment> unchangedSegment )
boolean allowDowngrade, Optional<Segment> upperBoundSegment )
throws InvalidSegmentException, InvalidVersionSpecificationException
{
final boolean includeSnapshots = !property.isBanSnapshots() && allowSnapshots;
Expand All @@ -346,24 +348,33 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert
? VersionRange.createFromVersionSpec( property.getVersion() ) : null;
helper.getLog().debug( "Property ${" + property.getName() + "}: Restricting results to " + range );

ArtifactVersion lowerBound = helper.createArtifactVersion( currentVersion );
if ( allowDowngrade )
{
Optional<String> updatedVersion = getLowerBound( lowerBound, unchangedSegment );
lowerBound = updatedVersion.map( helper::createArtifactVersion ).orElse( null );
}

ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString );
ArtifactVersion lowerBound = allowDowngrade
? getLowerBound( currentVersion, upperBoundSegment )
.map( DefaultArtifactVersion::new )
.orElse( null )
: currentVersion;
if ( helper.getLog().isDebugEnabled() )
{
helper.getLog().debug( "lowerBoundArtifactVersion: " + lowerBound );
}

ArtifactVersion upperBound = null;
if ( unchangedSegment.isPresent() )
ArtifactVersion upperBound =
!upperBoundSegment.isPresent()
? null
: upperBoundSegment
.map( s -> (ArtifactVersion) new BoundArtifactVersion( currentVersion,
s.isMajorTo( SUBINCREMENTAL )
? Segment.of( s.value() + 1 )
: s ) )
.orElse( null );
if ( helper.getLog().isDebugEnabled() )
{
upperBound = getVersionComparator().incrementSegment( lowerBound, unchangedSegment.get() );
helper.getLog().debug( "Property ${" + property.getName() + "}: upperBound is: " + upperBound );
}
Restriction restriction = new Restriction( lowerBound, false, upperBound, false );

Restriction restriction = new Restriction( lowerBound, allowDowngrade, upperBound, allowDowngrade );
ArtifactVersion result = getNewestVersion( range, restriction, includeSnapshots );

helper.getLog().debug( "Property ${" + property.getName() + "}: Current winner is: " + result );
Expand Down
135 changes: 135 additions & 0 deletions src/test/java/org/codehaus/mojo/versions/UpdatePropertiesMojoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package org.codehaus.mojo.versions;
/*
* 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
*
* http://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.
*/

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Objects;

import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
import org.codehaus.mojo.versions.change.VersionChange;
import org.codehaus.mojo.versions.utils.TestChangeRecorder;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.apache.commons.text.CaseUtils.toCamelCase;
import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Unit tests for {@link UpdatePropertiesMojo}
*/
public class UpdatePropertiesMojoTest extends AbstractMojoTestCase
{
@Rule
public MojoRule mojoRule = new MojoRule( this );
private Path pomDir;
private ArtifactMetadataSource artifactMetadataSource;
private TestChangeRecorder changeRecorder;

@Before
public void setUp() throws Exception
{
super.setUp();
pomDir = Files.createTempDirectory( toCamelCase( getClass().getSimpleName(), false ) );
changeRecorder = new TestChangeRecorder();
artifactMetadataSource = mockArtifactMetadataSource( new HashMap<String, String[]>()
{{
put( "default-artifact", new String[] { "1.0.0", "1.0.1-rc1", "1.1.0-alpha", "2.0.0-M1" } );
}} );
}

@After
public void tearDown() throws Exception
{
try
{
if ( pomDir != null && pomDir.toFile().exists() )
{
Arrays.stream( Objects.requireNonNull( pomDir.toFile().listFiles() ) ).forEach( File::delete );
pomDir.toFile().delete();
}
}
finally
{
super.tearDown();
}
}

private void setUpMojo( UpdatePropertiesMojo mojo ) throws IllegalAccessException
{
mojo.localRepository = new StubArtifactRepository( pomDir.toString() );
mojo.artifactMetadataSource = artifactMetadataSource;
setVariableValueToObject( mojo, "changeRecorder", changeRecorder );
setVariableValueToObject( mojo, "generateBackupPoms", false );
}

@Test
public void testUpdatePropertiesAllowMajorUpdates() throws Exception
{
Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/update-properties/issue-454-pom.xml" ),
Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING );
UpdatePropertiesMojo mojo =
(UpdatePropertiesMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "update-properties" );
setUpMojo( mojo );
mojo.execute();
assertThat( changeRecorder.getChanges(), Matchers.hasItem( new VersionChange( "default-group",
"default-artifact", "1.0.0", "2.0.0-M1" ) ) );
}

@Test
public void testUpdatePropertiesAllowMinorUpdates() throws Exception
{
Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/update-properties/issue-454-pom.xml" ),
Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING );
UpdatePropertiesMojo mojo =
(UpdatePropertiesMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "update-properties" );
setUpMojo( mojo );
mojo.allowMajorUpdates = false;
mojo.execute();
assertThat( changeRecorder.getChanges(), Matchers.hasItem( new VersionChange( "default-group",
"default-artifact", "1.0.0", "1.1.0-alpha" ) ) );
}

@Test
public void testUpdatePropertiesAllowIncrementalUpdates() throws Exception
{
Files.copy( Paths.get( "src/test/resources/org/codehaus/mojo/update-properties/issue-454-pom.xml" ),
Paths.get( pomDir.toString(), "pom.xml" ), REPLACE_EXISTING );
UpdatePropertiesMojo mojo =
(UpdatePropertiesMojo) mojoRule.lookupConfiguredMojo( pomDir.toFile(), "update-properties" );
setUpMojo( mojo );
mojo.allowMajorUpdates = false;
mojo.allowMinorUpdates = false;
mojo.execute();
assertThat( changeRecorder.getChanges(), Matchers.hasItem( new VersionChange( "default-group",
"default-artifact", "1.0.0", "1.0.1-rc1" ) ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>default-group</groupId>
<artifactId>test-artifact</artifactId>
<version>1.0.0</version>

<properties>
<artifact-version>1.0.0</artifact-version>
</properties>

<dependencies>
<dependency>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>${artifact-version}</version>
</dependency>
</dependencies>

</project>

0 comments on commit fb7778c

Please sign in to comment.