From 7aaf1389a14ebd153a095b63ebd1cdd09db762c5 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Fri, 21 Oct 2022 20:15:45 +0200 Subject: [PATCH] Resolves #776: onlyUpgradable change the filter to versions where the current version is not the latest one --- .../versions/DependencyUpdatesReportMojo.java | 22 ++++++++++++-- .../versions/PluginUpdatesReportMojo.java | 8 ++--- .../DependencyUpdatesReportMojoTest.java | 20 +++++++++++-- .../versions/PluginUpdatesReportMojoTest.java | 30 ++++++++++++++++--- 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java index 6cda7845a4..17da490a42 100644 --- a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java @@ -25,15 +25,18 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import java.util.stream.Collectors; import org.apache.maven.artifact.manager.WagonManager; import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException; import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.artifact.resolver.ArtifactResolver; +import org.apache.maven.artifact.versioning.ArtifactVersion; import org.apache.maven.doxia.sink.Sink; import org.apache.maven.model.Dependency; import org.apache.maven.plugins.annotations.Mojo; @@ -202,8 +205,23 @@ && getProject().getOriginalModel().getDependencyManagement().getDependencies() ! if ( onlyUpgradable ) { - dependencyUpdates = filter( dependencyUpdates, e -> e.getVersions().length > 1 ); - dependencyManagementUpdates = filter( dependencyManagementUpdates, e -> e.getVersions().length > 1 ); + dependencyUpdates = filter( dependencyUpdates, e -> + e.getNewerVersions( e.getVersion(), e.isIncludeSnapshots() ).length > 0 ); + dependencyManagementUpdates = filter( dependencyManagementUpdates, e -> + e.getNewerVersions( e.getVersion(), e.isIncludeSnapshots() ).length > 0 ); + } + + if ( getLog().isDebugEnabled() ) + { + getLog().debug( "Dependency versions:" ); + dependencyUpdates.forEach( ( key, value ) -> getLog().debug( key.toString() + ": " + + Arrays.stream( value.getVersions() ).map( ArtifactVersion::toString ) + .collect( Collectors.joining( ", " ) ) ) ); + + getLog().debug( "Dependency management versions:" ); + dependencyManagementUpdates.forEach( ( key, value ) -> getLog().debug( key.toString() + ": " + + Arrays.stream( value.getVersions() ).map( ArtifactVersion::toString ) + .collect( Collectors.joining( ", " ) ) ) ); } for ( String format : formats ) diff --git a/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java index 1b47c0f7c4..ee7125d951 100644 --- a/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java @@ -163,10 +163,10 @@ protected void doGenerateReport( Locale locale, Sink sink ) throws MavenReportEx if ( onlyUpgradable ) { - pluginUpdates = - filter( pluginUpdates, plugin -> plugin.getVersions().length > 1 ); - pluginManagementUpdates = filter( pluginManagementUpdates, - plugin -> plugin.getVersions().length > 1 ); + pluginUpdates = filter( pluginUpdates, p -> + p.getNewerVersions( p.getVersion(), p.isIncludeSnapshots() ).length > 0 ); + pluginManagementUpdates = filter( pluginManagementUpdates, p -> + p.getNewerVersions( p.getVersion(), p.isIncludeSnapshots() ).length > 0 ); } PluginUpdatesModel model = new PluginUpdatesModel( pluginUpdates, pluginManagementUpdates ); diff --git a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojoTest.java b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojoTest.java index d14013bdc6..3b31c751f4 100644 --- a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojoTest.java @@ -149,6 +149,12 @@ public TestDependencyUpdatesReportMojo withIgnoredVersions( return this; } + public TestDependencyUpdatesReportMojo withAllowSnapshots( boolean allowSnapshots ) + { + this.allowSnapshots = allowSnapshots; + return this; + } + private static RepositorySystem mockRepositorySystem() { RepositorySystem repositorySystem = mock( RepositorySystem.class ); @@ -183,13 +189,21 @@ public void testOnlyUpgradableDependencies() throws IOException, MavenReportExce SinkFactory sinkFactory = new Xhtml5SinkFactory(); new TestDependencyUpdatesReportMojo() .withOnlyUpgradable( true ) + .withArtifactMetadataSource( mockArtifactMetadataSource( new HashMap() + {{ + put( "artifactA", new String[] { "1.0.0", "2.0.0" } ); + put( "artifactB", new String[] { "1.0.0" } ); + put( "artifactC", new String[] { "1.0.0", "2.0.0" } ); + }} ) ) .withDependencies( - dependencyOf( "artifactA" ), dependencyOf( "artifactB" ), - dependencyOf( "artifactC" ) ) + dependencyOf( "artifactA", "1.0.0" ), + dependencyOf( "artifactB", "1.0.0" ), + dependencyOf( "artifactC", "2.0.0" ) ) .generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() ); String output = os.toString(); - assertThat( output, allOf( containsString( "artifactA" ), containsString( "artifactB" ) ) ); + assertThat( output, containsString( "artifactA" ) ); + assertThat( output, not( containsString( "artifactB" ) ) ); assertThat( output, not( containsString( "artifactC" ) ) ); } diff --git a/src/test/java/org/codehaus/mojo/versions/PluginUpdatesReportMojoTest.java b/src/test/java/org/codehaus/mojo/versions/PluginUpdatesReportMojoTest.java index 25f2ea781a..67c5f5c257 100644 --- a/src/test/java/org/codehaus/mojo/versions/PluginUpdatesReportMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/PluginUpdatesReportMojoTest.java @@ -24,10 +24,12 @@ import java.io.OutputStream; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.Locale; import java.util.Set; import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.metadata.ArtifactMetadataSource; import org.apache.maven.doxia.module.xhtml5.Xhtml5SinkFactory; import org.apache.maven.doxia.sink.SinkFactory; import org.apache.maven.model.Build; @@ -84,6 +86,13 @@ public TestPluginUpdatesReportMojo withPlugins( Plugin... plugins ) return this; } + public TestPluginUpdatesReportMojo withArtifactMetadataSource( + ArtifactMetadataSource artifactMetadataSource ) + { + this.artifactMetadataSource = artifactMetadataSource; + return this; + } + public TestPluginUpdatesReportMojo withPluginManagement( Plugin... pluginManagement ) { project.getBuild().getPluginManagement().setPlugins( Arrays.asList( pluginManagement ) ); @@ -131,13 +140,18 @@ private static RepositorySystem mockRepositorySystem() } private static Plugin pluginOf( String artifactId ) + { + return pluginOf( artifactId, "1.0.0" ); + } + + private static Plugin pluginOf( String artifactId, String version ) { return new Plugin() { { setGroupId( "defaultGroup" ); setArtifactId( artifactId ); - setVersion( "1.0.0" ); + setVersion( version ); } }; } @@ -164,13 +178,21 @@ public void testOnlyUpgradableWithPluginManagement() throws IOException, MavenRe OutputStream os = new ByteArrayOutputStream(); SinkFactory sinkFactory = new Xhtml5SinkFactory(); new TestPluginUpdatesReportMojo() - .withPluginManagement( pluginOf( "artifactA" ), pluginOf( "artifactB" ), - pluginOf( "artifactC" ) ) + .withArtifactMetadataSource( mockArtifactMetadataSource( new HashMap() + {{ + put( "artifactA", new String[] { "1.0.0", "2.0.0" } ); + put( "artifactB", new String[] { "1.0.0" } ); + put( "artifactC", new String[] { "1.0.0", "2.0.0" } ); + }} ) ) + .withPluginManagement( pluginOf( "artifactA", "1.0.0" ), + pluginOf( "artifactB", "1.0.0" ), + pluginOf( "artifactC", "2.0.0" ) ) .withOnlyUpgradable( true ) .generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() ); String output = os.toString(); - assertThat( output, allOf( containsString( "artifactA" ), containsString( "artifactB" ) ) ); + assertThat( output, containsString( "artifactA" ) ); + assertThat( output, not( containsString( "artifactB" ) ) ); assertThat( output, not( containsString( "artifactC" ) ) ); }