Skip to content

Commit

Permalink
Resolves mojohaus#776: onlyUpgradable change the filter to versions w…
Browse files Browse the repository at this point in the history
…here the current version is not the latest one
  • Loading branch information
andrzejj0 committed Oct 21, 2022
1 parent 98c703e commit 7aaf138
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -183,13 +189,21 @@ public void testOnlyUpgradableDependencies() throws IOException, MavenReportExce
SinkFactory sinkFactory = new Xhtml5SinkFactory();
new TestDependencyUpdatesReportMojo()
.withOnlyUpgradable( true )
.withArtifactMetadataSource( mockArtifactMetadataSource( new HashMap<String, String[]>()
{{
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" ) ) );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 );
}
};
}
Expand All @@ -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<String, String[]>()
{{
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" ) ) );
}

Expand Down

0 comments on commit 7aaf138

Please sign in to comment.