Skip to content

Commit

Permalink
mojohaus#454: Applying bound artifact versions to UpdatePropertiesMoj…
Browse files Browse the repository at this point in the history
…o, UpdatePropertyMojo + adding it tests for resolve-ranges and update-parent
  • Loading branch information
andrzejj0 committed Oct 11, 2022
1 parent 021e437 commit 418137e
Show file tree
Hide file tree
Showing 17 changed files with 450 additions and 19 deletions.
10 changes: 10 additions & 0 deletions src/it-repo/dummy-parent5-1.0.0.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<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>localhost</groupId>
<artifactId>dummy-parent5</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>

</project>
10 changes: 10 additions & 0 deletions src/it-repo/dummy-parent5-2.0.0-rc1.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<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>localhost</groupId>
<artifactId>dummy-parent5</artifactId>
<version>2.0.0-rc1</version>
<packaging>pom</packaging>

</project>
2 changes: 2 additions & 0 deletions src/it/it-resolve-ranges-issue-454/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:resolve-ranges
invoker.mavenOpts = -DallowMajorUpdates=false
20 changes: 20 additions & 0 deletions src/it/it-resolve-ranges-issue-454/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<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>localhost</groupId>
<artifactId>test-artifact</artifactId>
<version>1.0.0</version>

<properties>
<ver>[1.0.0,)</ver>
</properties>
<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-parent5</artifactId>
<version>${ver}</version>
</dependency>
</dependencies>

</project>
3 changes: 3 additions & 0 deletions src/it/it-resolve-ranges-issue-454/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pom = new File( basedir, "pom.xml" ).text

assert !( pom =~ /2.0.0-rc1/ )
2 changes: 2 additions & 0 deletions src/it/it-update-parent-issue-454/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:update-parent
invoker.mavenOpts = -DallowMajorUpdates=false
14 changes: 14 additions & 0 deletions src/it/it-update-parent-issue-454/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<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>

<artifactId>test-artifact</artifactId>
<version>1.0.0</version>

<parent>
<groupId>localhost</groupId>
<artifactId>dummy-parent5</artifactId>
<version>1.0.0</version>
</parent>

</project>
3 changes: 3 additions & 0 deletions src/it/it-update-parent-issue-454/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pom = new File( basedir, "pom.xml" ).text

assert !( pom =~ /2.0.0-rc1/ )
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
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class UpdatePropertyMojo
* @since 1.3
*/
@Parameter( property = "property" )
private String property = null;
protected String property = null;

/**
* The new version to set the property to (can be a version range to find a version within).
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 org.junit.Test;

/**
* Unit tests for {@link ResolveRangesMojo}
*/
public class ResolveRangesMojoTest
{
@Test
public void testDependencyRangesMajorUpdatesFalse()
{

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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.nio.file.Files;
import java.nio.file.Paths;

import org.codehaus.mojo.versions.change.VersionChange;
import org.hamcrest.Matchers;
import org.junit.Test;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Unit tests for {@link UpdatePropertiesMojo}
*/
public class UpdatePropertiesMojoTest extends UpdatePropertiesMojoTestBase
{
@Test
public void testAllowMajorUpdates() 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 );
setUpMojo( "update-properties" ).execute();
assertThat( changeRecorder.getChanges(), Matchers.hasItem( new VersionChange( "default-group",
"default-artifact", "1.0.0", "2.0.0-M1" ) ) );
}

@Test
public void testAllowMinorUpdates() 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 = setUpMojo( "update-properties" );
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 testAllowIncrementalUpdates() 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 = setUpMojo( "update-properties" );
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,84 @@
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.nio.file.Path;
import java.util.HashMap;

import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
import org.apache.maven.plugin.Mojo;
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.utils.TestChangeRecorder;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;

import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource;
import static org.codehaus.mojo.versions.utils.TestUtils.createTempDir;
import static org.codehaus.mojo.versions.utils.TestUtils.tearDownTempDir;

/**
* Base class for {@link UpdatePropertiesMojo} and {@link UpdatePropertyMojo} test suites
*/
public abstract class UpdatePropertiesMojoTestBase extends AbstractMojoTestCase
{
@Rule
public MojoRule mojoRule = new MojoRule( this );
protected Path pomDir;
protected ArtifactMetadataSource artifactMetadataSource;
protected TestChangeRecorder changeRecorder;

@Before
public void setUp() throws Exception
{
super.setUp();
pomDir = createTempDir( "update-property" );
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
{
tearDownTempDir( pomDir );
}
finally
{
super.tearDown();
}
}

@SuppressWarnings( "unchecked" )
protected <T extends Mojo> T setUpMojo( String goal ) throws Exception
{
T mojo = (T) mojoRule.lookupConfiguredMojo( pomDir.toFile(), goal );
setVariableValueToObject( mojo, "localRepository", new StubArtifactRepository( pomDir.toString() ) );
setVariableValueToObject( mojo, "artifactMetadataSource", artifactMetadataSource );
setVariableValueToObject( mojo, "changeRecorder", changeRecorder );
setVariableValueToObject( mojo, "generateBackupPoms", false );
return (T) mojo;
}
}
Loading

0 comments on commit 418137e

Please sign in to comment.