From 5fa3693aa7317590f936f9be18edbd62cbcad35d Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Thu, 1 Sep 2022 21:11:46 +0200 Subject: [PATCH] Fixing #251: proper handling of InvalidSegmentException and InvalidVersionSpecificationException --- .../versions/AbstractVersionsUpdaterMojo.java | 23 ++- .../versions/DisplayPropertyUpdatesMojo.java | 73 +++---- .../mojo/versions/PluginUpdatesDetails.java | 15 ++ .../mojo/versions/ResolveRangesMojo.java | 13 +- .../mojo/versions/UpdatePropertiesMojo.java | 29 ++- .../mojo/versions/UpdatePropertyMojo.java | 26 ++- .../mojo/versions/UseLatestReleasesMojo.java | 45 +++-- .../mojo/versions/UseLatestSnapshotsMojo.java | 81 ++++---- .../mojo/versions/UseLatestVersionsMojo.java | 58 +++--- .../mojo/versions/UseNextSnapshotsMojo.java | 39 ++-- .../versions/api/AbstractVersionDetails.java | 65 +++++-- .../mojo/versions/api/PropertyVersions.java | 63 +++--- .../mojo/versions/api/UpdateScope.java | 179 +++++++++++++----- .../mojo/versions/api/VersionDetails.java | 28 +-- .../ordering/AbstractVersionComparator.java | 5 +- .../ordering/InvalidSegmentException.java | 46 ++++- .../ordering/MavenVersionComparator.java | 4 +- .../ordering/MercuryVersionComparator.java | 4 +- .../ordering/NumericVersionComparator.java | 4 +- .../versions/ordering/VersionComparator.java | 2 +- .../versions/UseLatestVersionsMojoTest.java | 63 ++++-- .../ordering/MavenVersionComparatorTest.java | 4 +- .../ordering/VersionComparatorsTest.java | 10 +- 23 files changed, 593 insertions(+), 286 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java index 113f73344..836579f54 100644 --- a/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/AbstractVersionsUpdaterMojo.java @@ -37,6 +37,7 @@ import org.apache.maven.artifact.resolver.ArtifactResolutionException; import org.apache.maven.artifact.resolver.ArtifactResolver; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.VersionRange; import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.AbstractMojo; @@ -54,6 +55,7 @@ import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.PropertyVersions; import org.codehaus.mojo.versions.api.VersionsHelper; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.recording.ChangeRecorder; import org.codehaus.mojo.versions.recording.ChangeRecorderNull; import org.codehaus.mojo.versions.recording.ChangeRecorderXML; @@ -101,7 +103,7 @@ public abstract class AbstractVersionsUpdaterMojo * @since 1.0-alpha-1 */ @Parameter( defaultValue = "${reactorProjects}", required = true, readonly = true ) - protected List reactorProjects; + protected List reactorProjects; /** * The artifact metadata source to use. @@ -216,8 +218,7 @@ public abstract class AbstractVersionsUpdaterMojo // --------------------- GETTER / SETTER METHODS --------------------- - public VersionsHelper getHelper() - throws MojoExecutionException + public VersionsHelper getHelper() throws MojoExecutionException { if ( helper == null ) { @@ -514,13 +515,22 @@ protected boolean shouldApplyUpdate( Artifact artifact, String currentVersion, A } /** - * Based on the passed flags, determines which segment is unchangable. This can be used when determining an upper + *

Based on the passed flags, determines which segment (0-based), which is not to be changed.

+ *

The method will return, depending on the first parameter on the list to be true: + *

+ * + * This can be used when determining an upper * bound for the "latest" version. * * @param allowMajorUpdates Allow major updates * @param allowMinorUpdates Allow minor updates * @param allowIncrementalUpdates Allow incremental updates - * @return Returns the segment that is unchangable. If any segment can change, returns -1. + * @return Returns the segment (0-based) that is unchangable. If any segment can change, returns -1. */ protected int determineUnchangedSegment( boolean allowMajorUpdates, boolean allowMinorUpdates, boolean allowIncrementalUpdates ) @@ -553,7 +563,8 @@ else if ( allowIncrementalUpdates ) protected ArtifactVersion updatePropertyToNewestVersion( ModifiedPomXMLEventReader pom, Property property, PropertyVersions version, String currentVersion, boolean allowDowngrade, int segment ) - throws MojoExecutionException, XMLStreamException + throws XMLStreamException, InvalidVersionSpecificationException, + InvalidSegmentException, MojoExecutionException { ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots, this.reactorProjects, diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java index 57dcb4a55..4344a6da0 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java @@ -28,11 +28,13 @@ import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; 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.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; /** @@ -138,44 +140,51 @@ public void execute() } int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); - ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots, - this.reactorProjects, this.getHelper(), false, segment ); - - if ( winner != null && !currentVersion.equals( winner.toString() ) ) + try { - StringBuilder buf = new StringBuilder(); - buf.append( "${" ); - buf.append( property.getName() ); - buf.append( "} " ); - final String newVersion = winner.toString(); - int padding = - INFO_PAD_SIZE - currentVersion.length() - newVersion.length() - 4 + getOutputLineWidthOffset(); - while ( buf.length() < padding ) + ArtifactVersion winner = version.getNewestVersion( currentVersion, property, this.allowSnapshots, + this.reactorProjects, this.getHelper(), false, segment ); + if ( winner != null && !currentVersion.equals( winner.toString() ) ) { - buf.append( '.' ); + StringBuilder buf = new StringBuilder(); + buf.append( "${" ); + buf.append( property.getName() ); + buf.append( "} " ); + final String newVersion = winner.toString(); + int padding = + INFO_PAD_SIZE - currentVersion.length() - newVersion.length() - 4 + + getOutputLineWidthOffset(); + while ( buf.length() < padding ) + { + buf.append( '.' ); + } + buf.append( ' ' ); + buf.append( currentVersion ); + buf.append( " -> " ); + buf.append( newVersion ); + updates.add( buf.toString() ); } - buf.append( ' ' ); - buf.append( currentVersion ); - buf.append( " -> " ); - buf.append( newVersion ); - updates.add( buf.toString() ); - } - else - { - StringBuilder buf = new StringBuilder(); - buf.append( "${" ); - buf.append( property.getName() ); - buf.append( "} " ); - int padding = INFO_PAD_SIZE - currentVersion.length() + getOutputLineWidthOffset(); - while ( buf.length() < padding ) + else { - buf.append( '.' ); + StringBuilder buf = new StringBuilder(); + buf.append( "${" ); + buf.append( property.getName() ); + buf.append( "} " ); + int padding = INFO_PAD_SIZE - currentVersion.length() + getOutputLineWidthOffset(); + while ( buf.length() < padding ) + { + buf.append( '.' ); + } + buf.append( ' ' ); + buf.append( currentVersion ); + current.add( buf.toString() ); } - buf.append( ' ' ); - buf.append( currentVersion ); - current.add( buf.toString() ); } - + catch ( InvalidSegmentException | InvalidVersionSpecificationException e ) + { + getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(), + property.getVersion(), e.getMessage() ) ); + } } logLine( false, "" ); diff --git a/src/main/java/org/codehaus/mojo/versions/PluginUpdatesDetails.java b/src/main/java/org/codehaus/mojo/versions/PluginUpdatesDetails.java index 5ab759516..f8f7af328 100644 --- a/src/main/java/org/codehaus/mojo/versions/PluginUpdatesDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/PluginUpdatesDetails.java @@ -58,11 +58,21 @@ public Map getDependencyVersions() return dependencyVersions; } + /** + * Returns true if a new version of the artifact fulfilling the criteria (whether to include snapshots) can be found + * + * @return true if a new version can be found + */ public boolean isArtifactUpdateAvailable() { return artifactVersions.getAllUpdates( UpdateScope.ANY, includeSnapshots ).length > 0; } + /** + * Returns true if a new version of the dependency can be found + * + * @return true if a new version can be found + */ public boolean isDependencyUpdateAvailable() { for ( ArtifactVersions versions : dependencyVersions.values() ) @@ -76,6 +86,11 @@ public boolean isDependencyUpdateAvailable() return false; } + /** + * Returns true if a new version of the dependency can be found + * + * @return true if a new version can be found + */ public boolean isUpdateAvailable() { return isArtifactUpdateAvailable() || isDependencyUpdateAvailable(); diff --git a/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java b/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java index 193fbc347..937b68d17 100644 --- a/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java @@ -30,6 +30,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.model.Dependency; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -37,6 +38,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; /** @@ -294,8 +296,15 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom ) int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); // TODO: Check if we could add allowDowngrade ? - updatePropertyToNewestVersion( pom, property, version, currentVersion, false, segment ); - + try + { + updatePropertyToNewestVersion( pom, property, version, currentVersion, false, segment ); + } + catch ( InvalidSegmentException | InvalidVersionSpecificationException e ) + { + getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(), + property.getVersion(), e.getMessage() ) ); + } } } diff --git a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java index cb1c8fcee..c53e6eca0 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java @@ -24,12 +24,14 @@ import java.util.Map; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; 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.codehaus.mojo.versions.api.ArtifactAssociation; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; /** @@ -166,21 +168,30 @@ protected void update( ModifiedPomXMLEventReader pom ) { int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); - ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion, - allowDowngrade, segment ); - - if ( targetVersion != null ) + try { - for ( final ArtifactAssociation association : version.getAssociations() ) + ArtifactVersion targetVersion = + updatePropertyToNewestVersion( pom, property, version, currentVersion, + allowDowngrade, segment ); + + if ( targetVersion != null ) { - if ( ( isIncluded( association.getArtifact() ) ) ) + for ( final ArtifactAssociation association : version.getAssociations() ) { - this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(), - association.getArtifactId(), currentVersion, - targetVersion.toString() ); + if ( ( isIncluded( association.getArtifact() ) ) ) + { + this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(), + association.getArtifactId(), currentVersion, + targetVersion.toString() ); + } } } } + catch ( InvalidSegmentException | InvalidVersionSpecificationException e ) + { + getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(), + property.getVersion(), e.getMessage() ) ); + } } } diff --git a/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java index 6390c29a5..5426698a0 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java @@ -24,12 +24,14 @@ import java.util.Map; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; 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.codehaus.mojo.versions.api.ArtifactAssociation; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; /** @@ -146,18 +148,26 @@ protected void update( ModifiedPomXMLEventReader pom ) } int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); - ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion, - allowDowngrade, segment ); - - if ( targetVersion != null ) + try { - for ( final ArtifactAssociation association : version.getAssociations() ) + ArtifactVersion targetVersion = updatePropertyToNewestVersion( pom, property, version, currentVersion, + allowDowngrade, segment ); + + if ( targetVersion != null ) { - this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(), - association.getArtifactId(), currentVersion, - targetVersion.toString() ); + for ( final ArtifactAssociation association : version.getAssociations() ) + { + this.getChangeRecorder().recordUpdate( "updateProperty", association.getGroupId(), + association.getArtifactId(), currentVersion, + targetVersion.toString() ); + } } } + catch ( InvalidSegmentException | InvalidVersionSpecificationException e ) + { + getLog().warn( String.format( "Skipping the processing of %s:%s due to: %s", property.getName(), + property.getVersion(), e.getMessage() ) ); + } } } diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java index 4667cfd78..e4b09dd03 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestReleasesMojo.java @@ -41,6 +41,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -162,33 +163,41 @@ private void useLatestReleases( ModifiedPomXMLEventReader pom, Collection 0 ) + try { - String newVersion = filteredVersions[filteredVersions.length - 1].toString(); - if ( getProject().getParent() != null ) + ArtifactVersion[] newer = versions.getNewerVersions( version, segment, false ); + newer = filterVersionsWithIncludes( newer, artifact ); + + ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion, newer ); + if ( filteredVersions.length > 0 ) { - if ( artifact.getId().equals( getProject().getParentArtifact().getId() ) - && isProcessingParent() ) + String newVersion = filteredVersions[filteredVersions.length - 1].toString(); + if ( getProject().getParent() != null ) { - if ( PomHelper.setProjectParentVersion( pom, newVersion ) ) + if ( artifact.getId().equals( getProject().getParentArtifact().getId() ) + && isProcessingParent() ) { - getLog().debug( "Made parent update from " + version + " to " + newVersion ); + if ( PomHelper.setProjectParentVersion( pom, newVersion ) ) + { + getLog().debug( "Made parent update from " + version + " to " + newVersion ); + } } } - } - if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, - newVersion, getProject().getModel() ) ) - { - getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); + if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, + newVersion, getProject().getModel() ) ) + { + getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); - this.getChangeRecorder().recordUpdate( "useLatestReleases", dep.getGroupId(), - dep.getArtifactId(), version, newVersion ); + this.getChangeRecorder().recordUpdate( "useLatestReleases", dep.getGroupId(), + dep.getArtifactId(), version, newVersion ); + } } } + catch ( InvalidSegmentException e ) + { + getLog().warn( String.format( "Skipping the processing of %s:%s:%s due to: %s", dep.getGroupId(), + dep.getArtifactId(), dep.getVersion(), e.getMessage() ) ); + } } } } diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java index c20ca1602..7f2cc749d 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestSnapshotsMojo.java @@ -39,6 +39,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -167,58 +168,66 @@ private void useLatestSnapshots( ModifiedPomXMLEventReader pom, Collection= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null; - getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); - ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false ); - getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); + try + { + ArtifactVersion upperBound = + segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null; + getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); + ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false ); + getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); - String latestVersion; - ArrayList snapshotsOnly = new ArrayList<>(); + String latestVersion; + ArrayList snapshotsOnly = new ArrayList<>(); - for ( ArtifactVersion artifactVersion : newer ) - { - String newVersion = artifactVersion.toString(); - if ( matchSnapshotRegex.matcher( newVersion ).matches() ) + for ( ArtifactVersion artifactVersion : newer ) { - snapshotsOnly.add( artifactVersion ); + String newVersion = artifactVersion.toString(); + if ( matchSnapshotRegex.matcher( newVersion ).matches() ) + { + snapshotsOnly.add( artifactVersion ); + } } - } - getLog().debug( "Snapshot Only versions " + snapshotsOnly ); + getLog().debug( "Snapshot Only versions " + snapshotsOnly ); - ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( - selectedVersion, snapshotsOnly.toArray( new ArtifactVersion[0] ) ); - getLog().debug( "Filtered versions " + Arrays.asList( filteredVersions ) ); + ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( + selectedVersion, snapshotsOnly.toArray( new ArtifactVersion[0] ) ); + getLog().debug( "Filtered versions " + Arrays.asList( filteredVersions ) ); - if ( filteredVersions.length > 0 ) - { - latestVersion = filteredVersions[filteredVersions.length - 1].toString(); - if ( getProject().getParent() != null ) + if ( filteredVersions.length > 0 ) { - final Artifact parentArtifact = getProject().getParentArtifact(); - if ( artifact.getId().equals( parentArtifact.getId() ) && isProcessingParent() ) + latestVersion = filteredVersions[filteredVersions.length - 1].toString(); + if ( getProject().getParent() != null ) { - if ( PomHelper.setProjectParentVersion( pom, latestVersion ) ) + final Artifact parentArtifact = getProject().getParentArtifact(); + if ( artifact.getId().equals( parentArtifact.getId() ) && isProcessingParent() ) { - getLog().debug( "Made parent update from " + version + " to " + latestVersion ); - - this.getChangeRecorder() - .recordUpdate( "useLatestSnapshots", parentArtifact.getGroupId(), - parentArtifact.getArtifactId(), version, latestVersion ); + if ( PomHelper.setProjectParentVersion( pom, latestVersion ) ) + { + getLog().debug( "Made parent update from " + version + " to " + latestVersion ); + + this.getChangeRecorder() + .recordUpdate( "useLatestSnapshots", parentArtifact.getGroupId(), + parentArtifact.getArtifactId(), version, latestVersion ); + } } } - } - if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, - latestVersion, getProject().getModel() ) ) - { - getLog().info( "Updated " + toString( dep ) + " to version " + latestVersion ); + if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, + latestVersion, getProject().getModel() ) ) + { + getLog().info( "Updated " + toString( dep ) + " to version " + latestVersion ); - this.getChangeRecorder().recordUpdate( "useLatestSnapshots", dep.getGroupId(), - dep.getArtifactId(), version, latestVersion ); + this.getChangeRecorder().recordUpdate( "useLatestSnapshots", dep.getGroupId(), + dep.getArtifactId(), version, latestVersion ); + } } } + catch ( InvalidSegmentException e ) + { + getLog().warn( String.format( "Skipping the processing of %s:%s:%s due to: %s", dep.getGroupId(), + dep.getArtifactId(), dep.getVersion(), e.getMessage() ) ); + } } } } diff --git a/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java index cbfb7adf9..55640b1c9 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseLatestVersionsMojo.java @@ -37,6 +37,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.MajorMinorIncrementalFilter; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -145,7 +146,8 @@ protected void update( ModifiedPomXMLEventReader pom ) private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection dependencies ) throws XMLStreamException, MojoExecutionException, ArtifactMetadataRetrievalException { - int segment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); + int unchangedSegment = determineUnchangedSegment( allowMajorUpdates, allowMinorUpdates, + allowIncrementalUpdates ); MajorMinorIncrementalFilter majorMinorIncfilter = new MajorMinorIncrementalFilter( allowMajorUpdates, allowMinorUpdates, allowIncrementalUpdates ); @@ -176,38 +178,46 @@ private void useLatestVersions( ModifiedPomXMLEventReader pom, Collection 0 ) + try { - String newVersion = filteredVersions[filteredVersions.length - 1].toString(); - if ( getProject().getParent() != null ) + ArtifactVersion[] newerVersions = versions.getNewerVersions( version, unchangedSegment, allowSnapshots, + allowDowngrade ); + + ArtifactVersion[] filteredVersions = majorMinorIncfilter.filter( selectedVersion, newerVersions ); + if ( filteredVersions.length > 0 ) { - final Artifact parentArtifact = getProject().getParentArtifact(); - if ( artifact.getId().equals( parentArtifact.getId() ) && isProcessingParent() ) + String newVersion = filteredVersions[filteredVersions.length - 1].toString(); + if ( getProject().getParent() != null ) { - if ( PomHelper.setProjectParentVersion( pom, newVersion ) ) + final Artifact parentArtifact = getProject().getParentArtifact(); + if ( artifact.getId().equals( parentArtifact.getId() ) && isProcessingParent() ) { - getLog().debug( "Made parent update from " + version + " to " + newVersion ); - - this.getChangeRecorder().recordUpdate( "useLatestVersions", parentArtifact.getGroupId(), - parentArtifact.getArtifactId(), version, - newVersion ); + if ( PomHelper.setProjectParentVersion( pom, newVersion ) ) + { + getLog().debug( "Made parent update from " + version + " to " + newVersion ); + + this.getChangeRecorder().recordUpdate( "useLatestVersions", parentArtifact.getGroupId(), + parentArtifact.getArtifactId(), version, + newVersion ); + } } } - } - if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, newVersion, - getProject().getModel() ) ) - { - getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); + if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, + newVersion, + getProject().getModel() ) ) + { + getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); - this.getChangeRecorder().recordUpdate( "useLatestVersions", dep.getGroupId(), - dep.getArtifactId(), version, newVersion ); + this.getChangeRecorder().recordUpdate( "useLatestVersions", dep.getGroupId(), + dep.getArtifactId(), version, newVersion ); + } } } - + catch ( InvalidSegmentException e ) + { + getLog().warn( String.format( "Skipping the processing of %s:%s:%s due to: %s", dep.getGroupId(), + dep.getArtifactId(), dep.getVersion(), e.getMessage() ) ); + } } } diff --git a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java index a53ab8023..520e12f69 100644 --- a/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UseNextSnapshotsMojo.java @@ -37,6 +37,7 @@ import org.apache.maven.plugins.annotations.Parameter; import org.codehaus.mojo.versions.api.ArtifactVersions; import org.codehaus.mojo.versions.api.PomHelper; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -149,27 +150,35 @@ private void useNextSnapshots( ModifiedPomXMLEventReader pom, Collection= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null; - getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); - ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false ); - getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); - for ( ArtifactVersion artifactVersion : newer ) + try { - String newVersion = artifactVersion.toString(); - if ( matchSnapshotRegex.matcher( newVersion ).matches() ) + ArtifactVersion upperBound = + segment >= 0 ? versionComparator.incrementSegment( lowerBound, segment ) : null; + getLog().info( "Upper bound: " + ( upperBound == null ? "none" : upperBound.toString() ) ); + ArtifactVersion[] newer = versions.getVersions( lowerBound, upperBound, true, false, false ); + getLog().debug( "Candidate versions " + Arrays.asList( newer ) ); + for ( ArtifactVersion artifactVersion : newer ) { - if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, - newVersion, getProject().getModel() ) ) + String newVersion = artifactVersion.toString(); + if ( matchSnapshotRegex.matcher( newVersion ).matches() ) { - getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); - - this.getChangeRecorder().recordUpdate( "useNextSnapshots", dep.getGroupId(), - dep.getArtifactId(), version, newVersion ); + if ( PomHelper.setDependencyVersion( pom, dep.getGroupId(), dep.getArtifactId(), version, + newVersion, getProject().getModel() ) ) + { + getLog().info( "Updated " + toString( dep ) + " to version " + newVersion ); + + this.getChangeRecorder().recordUpdate( "useNextSnapshots", dep.getGroupId(), + dep.getArtifactId(), version, newVersion ); + } + break; } - break; } } + catch ( InvalidSegmentException e ) + { + getLog().warn( String.format( "Skipping the processing of %s:%s:%s due to: %s", dep.getGroupId(), + dep.getArtifactId(), dep.getVersion(), e.getMessage() ) ); + } } } } diff --git a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java index 1cd11640f..9aa9fbf1d 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/AbstractVersionDetails.java @@ -21,6 +21,7 @@ import java.util.Arrays; import java.util.Collections; +import java.util.Optional; import java.util.Set; import java.util.TreeSet; import java.util.stream.Collectors; @@ -32,6 +33,9 @@ import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; +import static java.util.Optional.empty; +import static java.util.Optional.of; + /** * Base class for {@link org.codehaus.mojo.versions.api.VersionDetails}. * @@ -239,9 +243,12 @@ public final ArtifactVersion[] getNewerVersions( String version, boolean include * @param includeSnapshots whether snapshot versions should be included * @deprecated please use {@link AbstractVersionDetails#getNewerVersions(String, int, boolean, boolean)} instead * @return array of newer versions fulfilling the criteria + * @throws InvalidSegmentException if the requested segment is outside the bounds (less than 1 or greater than + * the segment count) */ @Deprecated public final ArtifactVersion[] getNewerVersions( String version, int upperBoundSegment, boolean includeSnapshots ) + throws InvalidSegmentException { return getNewerVersions( version, upperBoundSegment, includeSnapshots, false ); } @@ -256,9 +263,12 @@ public final ArtifactVersion[] getNewerVersions( String version, int upperBoundS * @param allowDowngrade whether to allow downgrading if the current version is a snapshots and snapshots * are disallowed * @return array of newer versions fulfilling the criteria + * @throws InvalidSegmentException if the requested segment is outside the bounds (less than 1 or greater than + * the segment count) */ public final ArtifactVersion[] getNewerVersions( String versionString, int upperBoundSegment, boolean includeSnapshots, boolean allowDowngrade ) + throws InvalidSegmentException { ArtifactVersion currentVersion = new DefaultArtifactVersion( versionString ); ArtifactVersion lowerBound = @@ -368,16 +378,19 @@ public final ArtifactVersion[] getVersions( VersionRange versionRange, ArtifactV } public final ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope ) + throws InvalidSegmentException { return getOldestUpdate( currentVersion, updateScope, isIncludeSnapshots() ); } public final ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope ) + throws InvalidSegmentException { return getNewestUpdate( currentVersion, updateScope, isIncludeSnapshots() ); } public final ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, UpdateScope updateScope ) + throws InvalidSegmentException { return getAllUpdates( currentVersion, updateScope, isIncludeSnapshots() ); } @@ -505,24 +518,48 @@ public ArtifactVersion[] getAllUpdates( VersionRange versionRange, boolean inclu return getVersions( versionRange, getCurrentVersion(), null, includeSnapshots, false, true ); } - protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version, int segment ) + /** + * Returns the lower bound version based on the given artifact version + * and the lowest unchanged segment index (0-based); -1 means that the whole version string can be changed, + * implying that there is also no string designation of the lower bound version. + * + * @param version {@link ArtifactVersion} object specyfing the verion for which the lower bound is being computed + * @param unchangedSegment 0-based index of the first segment not to be changed; -1 means everything can change + * @return {@link ArtifactVersion} the lowest artifact version with the given segment held or null if no such + * version can be found + * @throws InvalidSegmentException if the requested segment is outside the bounds (less than 1 or greater than + * the segment count) + */ + protected ArtifactVersion getLowerBoundArtifactVersion( ArtifactVersion version, int unchangedSegment ) + throws InvalidSegmentException { - String lowerBound = getLowerBound( version, segment ); - return lowerBound != null ? new DefaultArtifactVersion( lowerBound ) : null; + Optional lowerBound = getLowerBound( version, unchangedSegment ); + return lowerBound.map( DefaultArtifactVersion::new ).orElse( null ); } - protected String getLowerBound( ArtifactVersion version, int segment ) + /** + * Returns the string designation of the lower bound version based on the given artifact version + * and the lowest unchanged segment index (0-based); -1 means that the whole version string can be changed, + * implying that there is also no string designation of the lower bound version. + * + * @param version {@link ArtifactVersion} object specyfing the verion for which the lower bound is being computed + * @param unchangedSegment 0-based index of the first segment not to be changed; -1 means everything can change + * @return {@link Optional} string containing the lowest artifact version with the given segment held + * @throws InvalidSegmentException if the requested segment is outside of the bounds (less than 1 or greater than + * the segment count) + */ + protected Optional getLowerBound( ArtifactVersion version, int unchangedSegment ) + throws InvalidSegmentException { - if ( segment < 0 ) + if ( unchangedSegment < 0 ) { - return null; + return empty(); } int segmentCount = getVersionComparator().getSegmentCount( version ); - if ( segment > segmentCount ) + if ( unchangedSegment > segmentCount ) { - throw new InvalidSegmentException( segment, segmentCount, - version.toString() ); + throw new InvalidSegmentException( unchangedSegment, segmentCount, version ); } StringBuilder newVersion = new StringBuilder(); @@ -530,26 +567,26 @@ protected String getLowerBound( ArtifactVersion version, int segment ) if ( segmentCount > 0 ) { newVersion.append( "." ) - .append( segment >= 1 ? version.getMinorVersion() : 0 ); + .append( unchangedSegment >= 1 ? version.getMinorVersion() : 0 ); } if ( segmentCount > 1 ) { newVersion.append( "." ) - .append( segment >= 2 ? version.getIncrementalVersion() : 0 ); + .append( unchangedSegment >= 2 ? version.getIncrementalVersion() : 0 ); } if ( segmentCount > 2 ) { if ( version.getQualifier() != null ) { newVersion.append( "-" ) - .append( segment >= 3 ? version.getQualifier() : "0" ); + .append( unchangedSegment >= 3 ? version.getQualifier() : "0" ); } else { newVersion.append( "-" ) - .append( segment >= 3 ? version.getBuildNumber() : "0" ); + .append( unchangedSegment >= 3 ? version.getBuildNumber() : "0" ); } } - return newVersion.toString(); + return of( newVersion.toString() ); } } diff --git a/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java b/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java index 4ab81204b..be119df8d 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PropertyVersions.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -36,8 +37,9 @@ import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; import org.apache.maven.artifact.versioning.OverConstrainedVersionException; import org.apache.maven.artifact.versioning.VersionRange; -import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; import org.codehaus.mojo.versions.Property; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; /** @@ -154,11 +156,9 @@ private VersionComparator[] lookupComparators() * @return The versions that can be resolved from the supplied Artifact instances or an empty array if no version * can be resolved (i.e. the property is not associated with any of the supplied artifacts or the property * is also associated to an artifact that has not been provided). - * @throws org.apache.maven.plugin.MojoExecutionException When things go wrong. * @since 1.0-alpha-3 */ public ArtifactVersion[] getVersions( Collection artifacts ) - throws MojoExecutionException { List result = new ArrayList<>(); // go through all the associations @@ -307,44 +307,46 @@ public String toString() public ArtifactVersion getNewestVersion( String currentVersion, Property property, boolean allowSnapshots, List reactorProjects, VersionsHelper helper ) - throws MojoExecutionException + throws InvalidVersionSpecificationException, InvalidSegmentException { return getNewestVersion( currentVersion, property, allowSnapshots, reactorProjects, helper, false, -1 ); } + /** + * 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 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 + * @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, - List reactorProjects, VersionsHelper helper, boolean allowDowngrade, - int segment ) - throws MojoExecutionException + Collection reactorProjects, VersionsHelper helper, + boolean allowDowngrade, int unchangedSegment ) + throws InvalidSegmentException, InvalidVersionSpecificationException { final boolean includeSnapshots = !property.isBanSnapshots() && allowSnapshots; helper.getLog().debug( "getNewestVersion(): includeSnapshots='" + includeSnapshots + "'" ); helper.getLog().debug( "Property ${" + property.getName() + "}: Set of valid available versions is " + Arrays.asList( getVersions( includeSnapshots ) ) ); - VersionRange range; - try - { - if ( property.getVersion() != null ) - { - range = VersionRange.createFromVersionSpec( property.getVersion() ); - helper.getLog().debug( "Property ${" + property.getName() + "}: Restricting results to " + range ); - } - else - { - range = null; - helper.getLog().debug( "Property ${" + property.getName() + "}: Restricting results to " + range ); - } - } - catch ( InvalidVersionSpecificationException e ) - { - throw new MojoExecutionException( e.getMessage(), e ); - } + VersionRange range = property.getVersion() != null + ? VersionRange.createFromVersionSpec( property.getVersion() ) : null; + helper.getLog().debug( "Property ${" + property.getName() + "}: Restricting results to " + range ); ArtifactVersion lowerBoundArtifactVersion = helper.createArtifactVersion( currentVersion ); if ( allowDowngrade ) { - String updatedVersion = getLowerBound( lowerBoundArtifactVersion, segment ); - lowerBoundArtifactVersion = updatedVersion != null ? helper.createArtifactVersion( updatedVersion ) : null; + Optional updatedVersion = getLowerBound( lowerBoundArtifactVersion, unchangedSegment ); + lowerBoundArtifactVersion = updatedVersion.map( helper::createArtifactVersion ).orElse( null ); } if ( helper.getLog().isDebugEnabled() ) { @@ -352,9 +354,9 @@ public ArtifactVersion getNewestVersion( String currentVersion, Property propert } ArtifactVersion upperBound = null; - if ( segment != -1 ) + if ( unchangedSegment != -1 ) { - upperBound = getVersionComparator().incrementSegment( lowerBoundArtifactVersion, segment ); + upperBound = getVersionComparator().incrementSegment( lowerBoundArtifactVersion, unchangedSegment ); helper.getLog().debug( "Property ${" + property.getName() + "}: upperBound is: " + upperBound ); } ArtifactVersion result = @@ -417,6 +419,7 @@ else if ( getVersionComparator().compare( result, fromReactor ) < 0 ) private ArtifactVersion getNewestVersion( String currentVersion, VersionsHelper helper, int segment, boolean includeSnapshots, VersionRange range ) + throws InvalidSegmentException { ArtifactVersion lowerBound = helper.createArtifactVersion( currentVersion ); ArtifactVersion upperBound = null; @@ -489,7 +492,7 @@ public int getSegmentCount( ArtifactVersion v ) return result; } - public ArtifactVersion incrementSegment( ArtifactVersion v, int segment ) + public ArtifactVersion incrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException { if ( !isAssociated() ) { diff --git a/src/main/java/org/codehaus/mojo/versions/api/UpdateScope.java b/src/main/java/org/codehaus/mojo/versions/api/UpdateScope.java index 0cefcec07..e3bdb6d16 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/UpdateScope.java +++ b/src/main/java/org/codehaus/mojo/versions/api/UpdateScope.java @@ -26,6 +26,7 @@ import java.util.Map; import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; /** @@ -51,10 +52,18 @@ public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getOldestVersion( currentVersion, - versionComparator.incrementSegment( currentVersion, 2 ), - includeSnapshots, false, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 3 + ? null + : versionDetails.getOldestVersion( currentVersion, + versionComparator.incrementSegment( currentVersion, 2 ), + includeSnapshots, false, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } /** {@inheritDoc} */ @@ -62,10 +71,18 @@ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getNewestVersion( currentVersion, - versionComparator.incrementSegment( currentVersion, 2 ), - includeSnapshots, false, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 3 + ? null + : versionDetails.getNewestVersion( currentVersion, + versionComparator.incrementSegment( currentVersion, 2 ), + includeSnapshots, false, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } /** {@inheritDoc} */ @@ -73,10 +90,17 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getVersions( currentVersion, - versionComparator.incrementSegment( currentVersion, 2 ), - includeSnapshots, false, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 3 ? null + : versionDetails.getVersions( currentVersion, + versionComparator.incrementSegment( currentVersion, 2 ), + includeSnapshots, false, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } }; @@ -94,10 +118,17 @@ public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 2 ), - versionComparator.incrementSegment( currentVersion, 1 ), - includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 3 ? null + : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 2 ), + versionComparator.incrementSegment( currentVersion, 1 ), + includeSnapshots, true, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } /** {@inheritDoc} */ @@ -105,10 +136,17 @@ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 2 ), - versionComparator.incrementSegment( currentVersion, 1 ), - includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 3 ? null + : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 2 ), + versionComparator.incrementSegment( currentVersion, 1 ), + includeSnapshots, true, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } /** {@inheritDoc} */ @@ -116,10 +154,17 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 3 ? null - : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 2 ), - versionComparator.incrementSegment( currentVersion, 1 ), - includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 3 ? null + : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 2 ), + versionComparator.incrementSegment( currentVersion, 1 ), includeSnapshots, true, + false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } }; @@ -137,10 +182,17 @@ public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 2 ? null - : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 1 ), - versionComparator.incrementSegment( currentVersion, 0 ), - includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 2 ? null + : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 1 ), + versionComparator.incrementSegment( currentVersion, 0 ), + includeSnapshots, true, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } /** {@inheritDoc} */ @@ -148,10 +200,17 @@ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 2 ? null - : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 1 ), - versionComparator.incrementSegment( currentVersion, 0 ), - includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 2 ? null + : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 1 ), + versionComparator.incrementSegment( currentVersion, 0 ), includeSnapshots, true, + false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } /** {@inheritDoc} */ @@ -159,10 +218,17 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 2 ? null - : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 1 ), - versionComparator.incrementSegment( currentVersion, 0 ), - includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 2 ? null + : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 1 ), + versionComparator.incrementSegment( currentVersion, 0 ), + includeSnapshots, true, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } }; @@ -180,9 +246,16 @@ public ArtifactVersion getOldestUpdate( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 1 ? null - : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 0 ), - null, includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 1 ? null + : versionDetails.getOldestVersion( versionComparator.incrementSegment( currentVersion, 0 ), + null, includeSnapshots, true, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } /** {@inheritDoc} */ @@ -190,9 +263,16 @@ public ArtifactVersion getNewestUpdate( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 1 ? null - : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 0 ), - null, includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 1 ? null + : versionDetails.getNewestVersion( versionComparator.incrementSegment( currentVersion, 0 ), + null, includeSnapshots, true, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } /** {@inheritDoc} */ @@ -200,9 +280,17 @@ public ArtifactVersion[] getAllUpdates( VersionDetails versionDetails, ArtifactV boolean includeSnapshots ) { VersionComparator versionComparator = versionDetails.getVersionComparator(); - return versionComparator.getSegmentCount( currentVersion ) < 1 ? null - : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 0 ), null, - includeSnapshots, true, false ); + try + { + return versionComparator.getSegmentCount( currentVersion ) < 1 + ? null + : versionDetails.getVersions( versionComparator.incrementSegment( currentVersion, 0 ), + null, includeSnapshots, true, false ); + } + catch ( InvalidSegmentException e ) + { + throw new RuntimeException( e ); + } } }; @@ -432,6 +520,7 @@ public static UpdateScope[] values() * @return The update classification. */ public static UpdateScope classifyUpdate( VersionComparator comparator, ArtifactVersion from, ArtifactVersion to ) + throws InvalidSegmentException { if ( comparator.compare( from, to ) >= 0 ) { diff --git a/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java b/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java index 144121e52..65ea06967 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java +++ b/src/main/java/org/codehaus/mojo/versions/api/VersionDetails.java @@ -21,6 +21,7 @@ import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.artifact.versioning.VersionRange; +import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.ordering.VersionComparator; /** @@ -266,7 +267,8 @@ ArtifactVersion getOldestVersion( VersionRange versionRange, ArtifactVersion low * version is available. * @since 1.0-beta-1 */ - ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope ); + ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope ) + throws InvalidSegmentException; /** * Returns the newest version newer than the specified current version, but within the the specified update scope or @@ -278,7 +280,8 @@ ArtifactVersion getOldestVersion( VersionRange versionRange, ArtifactVersion low * version is available. * @since 1.0-beta-1 */ - ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope ); + ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope ) + throws InvalidSegmentException; /** * Returns the all versions newer than the specified current version, but within the the specified update scope. @@ -288,7 +291,8 @@ ArtifactVersion getOldestVersion( VersionRange versionRange, ArtifactVersion low * @return the all versions after currentVersion within the specified update scope. * @since 1.0-beta-1 */ - ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, UpdateScope updateScope ); + ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, UpdateScope updateScope ) + throws InvalidSegmentException; /** * Returns the oldest version newer than the specified current version, but within the the specified update scope or @@ -302,7 +306,7 @@ ArtifactVersion getOldestVersion( VersionRange versionRange, ArtifactVersion low * @since 1.0-beta-1 */ ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope, - boolean includeSnapshots ); + boolean includeSnapshots ) throws InvalidSegmentException; /** * Returns the newest version newer than the specified current version, but within the the specified update scope or @@ -316,7 +320,7 @@ ArtifactVersion getOldestUpdate( ArtifactVersion currentVersion, UpdateScope upd * @since 1.0-beta-1 */ ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, UpdateScope updateScope, - boolean includeSnapshots ); + boolean includeSnapshots ) throws InvalidSegmentException; /** * Returns the all versions newer than the specified current version, but within the the specified update scope. @@ -328,7 +332,7 @@ ArtifactVersion getNewestUpdate( ArtifactVersion currentVersion, UpdateScope upd * @since 1.0-beta-1 */ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, UpdateScope updateScope, - boolean includeSnapshots ); + boolean includeSnapshots ) throws InvalidSegmentException; /** * Returns the oldest version newer than the specified current version, but within the the specified update scope or @@ -449,7 +453,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, VersionRange ve * version is available. * @since 1.0-beta-1 */ - ArtifactVersion getOldestUpdate( UpdateScope updateScope ); + ArtifactVersion getOldestUpdate( UpdateScope updateScope ) throws InvalidSegmentException; /** * Returns the newest version newer than the specified current version, but within the the specified update scope or @@ -460,7 +464,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, VersionRange ve * version is available. * @since 1.0-beta-1 */ - ArtifactVersion getNewestUpdate( UpdateScope updateScope ); + ArtifactVersion getNewestUpdate( UpdateScope updateScope ) throws InvalidSegmentException; /** * Returns the all versions newer than the specified current version, but within the the specified update scope. @@ -469,7 +473,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, VersionRange ve * @return the all versions after currentVersion within the specified update scope. * @since 1.0-beta-1 */ - ArtifactVersion[] getAllUpdates( UpdateScope updateScope ); + ArtifactVersion[] getAllUpdates( UpdateScope updateScope ) throws InvalidSegmentException; /** * Returns the oldest version newer than the specified current version, but within the the specified update scope or @@ -481,7 +485,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, VersionRange ve * version is available. * @since 1.0-beta-1 */ - ArtifactVersion getOldestUpdate( UpdateScope updateScope, boolean includeSnapshots ); + ArtifactVersion getOldestUpdate( UpdateScope updateScope, boolean includeSnapshots ) throws InvalidSegmentException; /** * Returns the newest version newer than the specified current version, but within the the specified update scope or @@ -493,7 +497,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, VersionRange ve * version is available. * @since 1.0-beta-1 */ - ArtifactVersion getNewestUpdate( UpdateScope updateScope, boolean includeSnapshots ); + ArtifactVersion getNewestUpdate( UpdateScope updateScope, boolean includeSnapshots ) throws InvalidSegmentException; /** * Returns the all versions newer than the specified current version, but within the the specified update scope. @@ -503,7 +507,7 @@ ArtifactVersion[] getAllUpdates( ArtifactVersion currentVersion, VersionRange ve * @return the all versions after currentVersion within the specified update scope. * @since 1.0-beta-1 */ - ArtifactVersion[] getAllUpdates( UpdateScope updateScope, boolean includeSnapshots ); + ArtifactVersion[] getAllUpdates( UpdateScope updateScope, boolean includeSnapshots ) throws InvalidSegmentException; /** * Returns the oldest version newer than the current version, but within the the specified update scope or diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java index 29d005cbc..afed874df 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/AbstractVersionComparator.java @@ -56,7 +56,7 @@ public final int getSegmentCount( ArtifactVersion v ) /** * {@inheritDoc} */ - public final ArtifactVersion incrementSegment( ArtifactVersion v, int segment ) + public final ArtifactVersion incrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException { if ( VersionComparators.isSnapshot( v ) ) { @@ -66,7 +66,8 @@ public final ArtifactVersion incrementSegment( ArtifactVersion v, int segment ) return innerIncrementSegment( v, segment ); } - protected abstract ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ); + protected abstract ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) + throws InvalidSegmentException; /** * Returns a hash code value for the comparator class. diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/InvalidSegmentException.java b/src/main/java/org/codehaus/mojo/versions/ordering/InvalidSegmentException.java index e03c794f4..ce704ff42 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/InvalidSegmentException.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/InvalidSegmentException.java @@ -19,22 +19,56 @@ * under the License. */ +import org.apache.maven.artifact.versioning.ArtifactVersion; + /** * Represents an invalid segment being identified within a version. */ -public class InvalidSegmentException - extends RuntimeException +public class InvalidSegmentException extends Exception { + private final int segment; + + private final int segmentCount; + + private final ArtifactVersion version; + /** - * Constructs a new exception. + * Constructs the exception object * * @param segment the invalid segment index. * @param segmentCount the number of segments. - * @param version the version string. + * @param version the version object. */ - public InvalidSegmentException( int segment, int segmentCount, String version ) + public InvalidSegmentException( int segment, int segmentCount, ArtifactVersion version ) { super( String.format( "Invalid segment, %d, for the %d segment version: '%s'", segment, segmentCount, - version ) ); + version.toString() ) ); + this.segment = segment; + this.segmentCount = segmentCount; + this.version = version; + } + + /** + * @return segment + */ + public int getSegment() + { + return segment; + } + + /** + * @return segment count + */ + public int getSegmentCount() + { + return segmentCount; + } + + /** + * @return version object + */ + public ArtifactVersion getVersion() + { + return version; } } diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java index e13d3da95..b0ee00c9c 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/MavenVersionComparator.java @@ -92,12 +92,12 @@ protected int innerGetSegmentCount( ArtifactVersion v ) /** * {@inheritDoc} */ - protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) + protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException { int segmentCount = innerGetSegmentCount( v ); if ( segment < 0 || segment >= segmentCount ) { - throw new InvalidSegmentException( segment, segmentCount, v.toString() ); + throw new InvalidSegmentException( segment, segmentCount, v ); } String version = v.toString(); if ( segmentCount == 1 ) diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java index b26e88517..335538a0c 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/MercuryVersionComparator.java @@ -52,12 +52,12 @@ protected int innerGetSegmentCount( ArtifactVersion v ) return tok.countTokens(); } - protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) + protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException { final int segmentCount = getSegmentCount( v ); if ( segment < 0 || segment > segmentCount ) { - throw new InvalidSegmentException( segment, segmentCount, v.toString() ); + throw new InvalidSegmentException( segment, segmentCount, v ); } final String version = v.toString(); StringBuilder result = new StringBuilder( version.length() + 10 ); diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java index 1468717b9..85443c276 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/NumericVersionComparator.java @@ -163,12 +163,12 @@ protected int innerGetSegmentCount( ArtifactVersion v ) * {@inheritDoc} */ @SuppressWarnings( "checkstyle:MethodLength" ) - protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) + protected ArtifactVersion innerIncrementSegment( ArtifactVersion v, int segment ) throws InvalidSegmentException { final int segmentCount = innerGetSegmentCount( v ); if ( segment < 0 || segment > segmentCount ) { - throw new InvalidSegmentException( segment, segmentCount, v.toString() ); + throw new InvalidSegmentException( segment, segmentCount, v ); } final String version = v.toString(); StringBuilder buf = new StringBuilder(); diff --git a/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java b/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java index ee2956a7c..fbf6fb14e 100644 --- a/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java +++ b/src/main/java/org/codehaus/mojo/versions/ordering/VersionComparator.java @@ -46,5 +46,5 @@ public interface VersionComparator * @return An artifact version with the specified segment incremented. * @since 1.0-beta-1 */ - ArtifactVersion incrementSegment( ArtifactVersion artifactVersion, int segment ); + ArtifactVersion incrementSegment( ArtifactVersion artifactVersion, int segment ) throws InvalidSegmentException; } diff --git a/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java b/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java index 5ac43e263..fbd94b2c0 100644 --- a/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/UseLatestVersionsMojoTest.java @@ -47,8 +47,8 @@ public void setUp() throws Exception { Dependency dependency = invocation.getArgument( 0 ); return new DefaultArtifact( dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion(), - dependency.getScope(), dependency.getType(), dependency.getClassifier() != null - ? dependency.getClassifier() : "default", null ); + dependency.getScope(), dependency.getType(), + dependency.getClassifier() != null ? dependency.getClassifier() : "default", null ); } ); ArtifactMetadataSource artifactMetadataSourceMock = mock( ArtifactMetadataSource.class ); @@ -63,6 +63,15 @@ public void setUp() throws Exception new DefaultArtifactVersion( "1.0.0" ), new DefaultArtifactVersion( "1.0.0-SNAPSHOT" ), new DefaultArtifactVersion( "0.9.0" ) ); } + else if ( "poison-artifact".equals( artifact.getArtifactId() ) ) + { + return Arrays.asList( new DefaultArtifactVersion( "1.1.1.1-SNAPSHOT" ), + new DefaultArtifactVersion( "1.1.1.0" ), + new DefaultArtifactVersion( "1.1.1.0-SNAPSHOT" ), + new DefaultArtifactVersion( "1.0.0.0" ), + new DefaultArtifactVersion( "1.0.0.0-SNAPSHOT" ), + new DefaultArtifactVersion( "0.9.0.0" ) ); + } fail(); return null; } ); @@ -114,8 +123,7 @@ public void testDependenciesDowngradeIncremental() mojo.update( null ); } assertThat( changeRecorder.getChanges(), - hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", - "1.1.0" ) ) ); + hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", "1.1.0" ) ) ); } @Test @@ -136,8 +144,7 @@ public void testDependenciesDowngradeMinor() mojo.update( null ); } assertThat( changeRecorder.getChanges(), - hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", - "1.0.0" ) ) ); + hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", "1.0.0" ) ) ); } @Test @@ -158,8 +165,7 @@ public void testDependenciesDowngradeMajor() mojo.update( null ); } assertThat( changeRecorder.getChanges(), - hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", - "0.9.0" ) ) ); + hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", "0.9.0" ) ) ); } @Test @@ -182,8 +188,7 @@ public void testDependencyManagementDowngrade() mojo.update( null ); } assertThat( changeRecorder.getChanges(), - hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", - "1.1.0" ) ) ); + hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", "1.1.0" ) ) ); } @Test @@ -197,8 +202,9 @@ public void testParentDowngrade() setVariableValueToObject( mojo, "allowIncrementalUpdates", true ); setVariableValueToObject( mojo, "allowDowngrade", true ); - mojo.getProject().setParentArtifact( new DefaultArtifact( "default-group", "dependency-artifact", - "1.1.1-SNAPSHOT", "compile", "pom", "default", null ) ); + mojo.getProject().setParentArtifact( + new DefaultArtifact( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", "compile", "pom", + "default", null ) ); mojo.getProject().setParent( new MavenProject() {{ setGroupId( mojo.getProject().getParentArtifact().getGroupId() ); @@ -213,8 +219,39 @@ public void testParentDowngrade() .thenReturn( true ); mojo.update( null ); } + assertThat( changeRecorder.getChanges(), + hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", "1.1.0" ) ) ); + } + + @Test + public void testPoisonDependencyVersion() + throws MojoExecutionException, XMLStreamException, MojoFailureException, IllegalAccessException + { + mojo.getProject().getModel().setDependencies( Arrays.asList( + DependencyBuilder.dependencyWith( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", + "default", "pom", SCOPE_COMPILE ), + DependencyBuilder.dependencyWith( "default-group", "poison-artifact", "1.1.1.1-SNAPSHOT", + "default", "pom", SCOPE_COMPILE ) + ) ); + + setVariableValueToObject( mojo, "processDependencies", true ); + setVariableValueToObject( mojo, "allowSnapshots", false ); + setVariableValueToObject( mojo, "allowMajorUpdates", false ); + setVariableValueToObject( mojo, "allowMinorUpdates", true ); + setVariableValueToObject( mojo, "allowIncrementalUpdates", false ); + setVariableValueToObject( mojo, "allowDowngrade", true ); + + try ( MockedStatic pomHelper = mockStatic( PomHelper.class ) ) + { + pomHelper.when( () -> PomHelper.setDependencyVersion( any(), any(), any(), any(), any(), any() ) ) + .thenReturn( true ); + mojo.update( null ); + } + // So, the regular update should take place despite an irregular, or — if I may — "poison", dependency + // being present in the dependency list assertThat( changeRecorder.getChanges(), hasItem( new VersionChange( "default-group", "dependency-artifact", "1.1.1-SNAPSHOT", - "1.1.0" ) ) ); + "1.0.0" ) ) ); } + } \ No newline at end of file diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java index e3aa2dfd2..d69155715 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/MavenVersionComparatorTest.java @@ -42,7 +42,7 @@ public void testSegmentCounting() @Test public void testSegmentIncrementing() - throws Exception + throws InvalidSegmentException { assertIncrement( "6", "5", 0 ); assertIncrement( "6.0", "5.0", 0 ); @@ -58,7 +58,7 @@ public void testSegmentIncrementing() assertIncrement( "1.0-z90-SNAPSHOT", "1.0-z9-SNAPSHOT", 3 ); } - private void assertIncrement( String expected, String initial, int segment ) + private void assertIncrement( String expected, String initial, int segment ) throws InvalidSegmentException { assertEquals( expected, instance.incrementSegment( new DefaultArtifactVersion( initial ), segment ).toString() ); diff --git a/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java b/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java index d6ec074b9..06bdbf7f3 100644 --- a/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java +++ b/src/test/java/org/codehaus/mojo/versions/ordering/VersionComparatorsTest.java @@ -38,24 +38,24 @@ public class VersionComparatorsTest }; @Test - public void testMavenVersionComparator() + public void testMavenVersionComparator() throws InvalidSegmentException { assertVersions( new MavenVersionComparator() ); } @Test - public void testMercuryVersionComparator() + public void testMercuryVersionComparator() throws InvalidSegmentException { assertVersions( new MercuryVersionComparator() ); } @Test - public void testNumericVersionComparator() + public void testNumericVersionComparator() throws InvalidSegmentException { assertVersions( new NumericVersionComparator() ); } - public void assertVersions( VersionComparator instance ) + public void assertVersions( VersionComparator instance ) throws InvalidSegmentException { for ( int i = 0; i < versionDataset.length; i++ ) { @@ -64,7 +64,7 @@ public void assertVersions( VersionComparator instance ) } } - public void assertLater( String version, VersionComparator instance ) + public void assertLater( String version, VersionComparator instance ) throws InvalidSegmentException { ArtifactVersion v1 = new DefaultArtifactVersion( version ); int count = instance.getSegmentCount( v1 );