diff --git a/pom.xml b/pom.xml index 27e525abf..f40b44df5 100644 --- a/pom.xml +++ b/pom.xml @@ -313,7 +313,7 @@ modello-maven-plugin - generate-sources + generate-rule generate-sources @@ -323,6 +323,29 @@ java + + + src/main/mdo/rule.mdo + + 2.0.0 + + + + generate-reporting + generate-sources + + + xpp3-writer + + java + + + + src/main/mdo/dependency-updates-report.mdo + src/main/mdo/plugin-updates-report.mdo + + 2.0.0 + site-doc @@ -345,6 +368,8 @@ src/main/mdo/rule.mdo + src/main/mdo/dependency-updates-report.mdo + src/main/mdo/plugin-updates-report.mdo 2.0.0 @@ -392,6 +417,8 @@ org.codehaus.mojo.versions.model, org.codehaus.mojo.versions.model.io.xpp3 + org.codehaus.mojo.versions.reporting.model, + org.codehaus.mojo.versions.reporting.model.io.xpp3, diff --git a/src/it/it-dependency-updates-report-issue-755/invoker.properties b/src/it/it-dependency-updates-report-issue-755/invoker.properties new file mode 100644 index 000000000..09f3f7db0 --- /dev/null +++ b/src/it/it-dependency-updates-report-issue-755/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:dependency-updates-report +invoker.mavenOpts = -DdependencyUpdatesReportFormats=xml diff --git a/src/it/it-dependency-updates-report-issue-755/pom.xml b/src/it/it-dependency-updates-report-issue-755/pom.xml new file mode 100644 index 000000000..c75353ed0 --- /dev/null +++ b/src/it/it-dependency-updates-report-issue-755/pom.xml @@ -0,0 +1,16 @@ + + 4.0.0 + localhost + dummy-artifact + 1.0.0-SNAPSHOT + + + + localhost + dummy-api + 1.0 + + + + diff --git a/src/it/it-dependency-updates-report-issue-755/verify.groovy b/src/it/it-dependency-updates-report-issue-755/verify.groovy new file mode 100644 index 000000000..fc9b89d5c --- /dev/null +++ b/src/it/it-dependency-updates-report-issue-755/verify.groovy @@ -0,0 +1,5 @@ +report = new File( basedir, "target/dependency-updates-report.xml" ).text.replaceAll( '\\s*', '' ) + +assert report.contains( '1.0.1' ) +assert report.contains( '1.1' ) +assert report.contains( '2.0' ) diff --git a/src/it/it-plugin-updates-report-issue-755/invoker.properties b/src/it/it-plugin-updates-report-issue-755/invoker.properties new file mode 100644 index 000000000..5faa3b4b6 --- /dev/null +++ b/src/it/it-plugin-updates-report-issue-755/invoker.properties @@ -0,0 +1,2 @@ +invoker.goals = ${project.groupId}:${project.artifactId}:${project.version}:plugin-updates-report +invoker.mavenOpts = -DpluginUpdatesReportFormats=xml diff --git a/src/it/it-plugin-updates-report-issue-755/pom.xml b/src/it/it-plugin-updates-report-issue-755/pom.xml new file mode 100644 index 000000000..6df231194 --- /dev/null +++ b/src/it/it-plugin-updates-report-issue-755/pom.xml @@ -0,0 +1,18 @@ + + 4.0.0 + localhost + dummy-artifact + 1.0.0-SNAPSHOT + + + + + localhost + dummy-maven-plugin + 1.0 + + + + + diff --git a/src/it/it-plugin-updates-report-issue-755/verify.groovy b/src/it/it-plugin-updates-report-issue-755/verify.groovy new file mode 100644 index 000000000..f68411a70 --- /dev/null +++ b/src/it/it-plugin-updates-report-issue-755/verify.groovy @@ -0,0 +1,5 @@ +report = new File( basedir, "target/plugin-updates-report.xml" ).text.replaceAll( '\\s*', '' ) + +assert report.contains( '3.0.0' ) +assert report.contains( '2.3' ) +assert report.contains( '2.2.2' ) diff --git a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java index 8f40fa0f8..6cda7845a 100644 --- a/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DependencyUpdatesReportMojo.java @@ -21,7 +21,10 @@ import javax.inject.Inject; -import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Locale; import java.util.Map; import java.util.Set; @@ -39,10 +42,10 @@ import org.apache.maven.reporting.MavenReportException; import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.ArtifactVersions; -import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlRenderer; import org.codehaus.mojo.versions.reporting.ReportRendererFactory; import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel; import org.codehaus.mojo.versions.utils.DependencyComparator; +import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlReportRenderer; import org.codehaus.plexus.i18n.I18N; import static java.util.Collections.emptyMap; @@ -205,27 +208,28 @@ && getProject().getOriginalModel().getDependencyManagement().getDependencies() ! for ( String format : formats ) { + DependencyUpdatesModel model = + new DependencyUpdatesModel( dependencyUpdates, dependencyManagementUpdates ); if ( "html".equals( format ) ) { - rendererFactory.createReportRenderer( getOutputName(), sink, locale, - new DependencyUpdatesModel( dependencyUpdates, dependencyManagementUpdates ) ) - .render(); + rendererFactory.createReportRenderer( getOutputName(), sink, locale, model ).render(); } else if ( "xml".equals( format ) ) { - File outputDir = new File( getProject().getBuild().getDirectory() ); - if ( !outputDir.exists() ) + Path outputDir = Paths.get( getProject().getBuild().getDirectory() ); + if ( !Files.exists( outputDir ) ) { - if ( !outputDir.mkdirs() ) + try { - throw new MavenReportException( "Could not create output directory" ); + Files.createDirectories( outputDir ); + } + catch ( IOException e ) + { + throw new MavenReportException( "Could not create the output directory" ); } } - String outputFile = outputDir.getAbsolutePath() + File.separator + getOutputName() + ".xml"; - DependencyUpdatesXmlRenderer xmlGenerator = - new DependencyUpdatesXmlRenderer( dependencyUpdates, dependencyManagementUpdates, - outputFile ); - xmlGenerator.render(); + Path outputFile = outputDir.resolve( getOutputName() + ".xml" ); + new DependencyUpdatesXmlReportRenderer( model, outputFile ).render(); } } } diff --git a/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java index 32503ec04..1b47c0f7c 100644 --- a/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/PluginUpdatesReportMojo.java @@ -21,7 +21,10 @@ import javax.inject.Inject; -import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Locale; import java.util.Map; import java.util.Set; @@ -38,10 +41,10 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.reporting.MavenReportException; import org.apache.maven.repository.RepositorySystem; -import org.codehaus.mojo.versions.xml.PluginUpdatesXmlRenderer; import org.codehaus.mojo.versions.reporting.ReportRendererFactory; import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel; import org.codehaus.mojo.versions.utils.PluginComparator; +import org.codehaus.mojo.versions.xml.PluginUpdatesXmlReportRenderer; import org.codehaus.plexus.i18n.I18N; import static org.codehaus.mojo.versions.utils.MiscUtils.filter; @@ -166,28 +169,29 @@ protected void doGenerateReport( Locale locale, Sink sink ) throws MavenReportEx plugin -> plugin.getVersions().length > 1 ); } + PluginUpdatesModel model = new PluginUpdatesModel( pluginUpdates, pluginManagementUpdates ); for ( String format : formats ) { if ( "html".equals( format ) ) { - rendererFactory.createReportRenderer( getOutputName(), getSink(), locale, - new PluginUpdatesModel( pluginUpdates, pluginManagementUpdates ) ) - .render(); + rendererFactory.createReportRenderer( getOutputName(), getSink(), locale, model ).render(); } else if ( "xml".equals( format ) ) { - File outputDir = new File( getProject().getBuild().getDirectory() ); - if ( !outputDir.exists() ) + Path outputDir = Paths.get( getProject().getBuild().getDirectory() ); + if ( !Files.exists( outputDir ) ) { - if ( !outputDir.mkdirs() ) + try { - throw new MavenReportException( "Could not create output directory" ); + Files.createDirectories( outputDir ); + } + catch ( IOException e ) + { + throw new MavenReportException( "Could not create the output directory" ); } } - String outputFile = outputDir.getAbsolutePath() + File.separator + getOutputName() + ".xml"; - PluginUpdatesXmlRenderer xmlGenerator = - new PluginUpdatesXmlRenderer( pluginUpdates, pluginManagementUpdates, outputFile ); - xmlGenerator.render(); + Path outputFile = outputDir.resolve( getOutputName() + ".xml" ); + new PluginUpdatesXmlReportRenderer( model, outputFile ).render(); } } } diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java index 8c1f3d42e..c26c7ca78 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/DependencyUpdatesReportRenderer.java @@ -29,12 +29,6 @@ import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel; import org.codehaus.plexus.i18n.I18N; -import static java.util.Optional.of; -import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; -import static org.codehaus.mojo.versions.api.Segment.MAJOR; -import static org.codehaus.mojo.versions.api.Segment.MINOR; -import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; - /** * @param type of the model * @since 1.0-beta-1 @@ -92,31 +86,7 @@ protected void renderTable( String titleKey, Map c @Override protected OverviewStats computeOverviewStats() { - OverviewStats stats = new OverviewStats(); - model.getAllUpdates().values().forEach( details -> - { - if ( oldestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null ) - { - stats.incrementAny(); - } - else if ( oldestUpdateCache.get( details, of( INCREMENTAL ) ) != null ) - { - stats.incrementIncremental(); - } - else if ( oldestUpdateCache.get( details, of( MINOR ) ) != null ) - { - stats.incrementMinor(); - } - else if ( oldestUpdateCache.get( details, of( MAJOR ) ) != null ) - { - stats.incrementMajor(); - } - else - { - stats.incrementUpToDate(); - } - } ); - return stats; + return OverviewStats.fromUpdates( model.getAllUpdates().values(), oldestUpdateCache ); } protected void renderDependencyDetail( Dependency artifact, ArtifactVersions details ) diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java b/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java index 4cff6950a..bde72a921 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/OverviewStats.java @@ -20,12 +20,26 @@ * under the License. */ +import java.util.Collection; +import java.util.Optional; + +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.codehaus.mojo.versions.api.ArtifactVersions; +import org.codehaus.mojo.versions.api.ArtifactVersionsCache; +import org.codehaus.mojo.versions.api.Segment; + +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; +import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; + /** * Represents summary stats * * @author Andrzej Jarmoniuk */ -class OverviewStats +public class OverviewStats { private int major; @@ -37,6 +51,55 @@ class OverviewStats private int upToDate; + /** + * Creates a {@linkplain OverviewStats} instance based on the collection of version updates in + * the argument + * + * @param updates collection of all version updates, typically from + * {@linkplain org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel#getAllUpdates()} + * @param cache if not null, cache to retrieve the version information, initialised with + * the {@link ArtifactVersions#getOldestUpdate(Optional)} update information + * @param subclass of {@linkplain OverviewStats} + * @param subclass of {@linkplain ArtifactVersions} + * @return instance of the {@linkplain OverviewStats} + */ + public static T fromUpdates( Collection updates, + ArtifactVersionsCache cache ) + { + OverviewStats stats = new OverviewStats(); + updates.forEach( details -> + { + if ( getOldestUpdate( cache, details, of( SUBINCREMENTAL ) ) != null ) + { + stats.incrementAny(); + } + else if ( getOldestUpdate( cache, details, of( INCREMENTAL ) ) != null ) + { + stats.incrementIncremental(); + } + else if ( getOldestUpdate( cache, details, of( MINOR ) ) != null ) + { + stats.incrementMinor(); + } + else if ( getOldestUpdate( cache, details, of( MAJOR ) ) != null ) + { + stats.incrementMajor(); + } + else + { + stats.incrementUpToDate(); + } + } ); + return (T) stats; + } + + protected static ArtifactVersion getOldestUpdate( ArtifactVersionsCache cache, + V details, + Optional segment ) + { + return cache != null ? cache.get( details, segment ) : details.getOldestUpdate( segment ); + } + public int getMajor() { return major; diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java b/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java new file mode 100644 index 000000000..5275c17e4 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/versions/reporting/PluginOverviewStats.java @@ -0,0 +1,96 @@ +package org.codehaus.mojo.versions.reporting; +/* + * 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.util.Collection; +import java.util.Optional; + +import org.codehaus.mojo.versions.PluginUpdatesDetails; +import org.codehaus.mojo.versions.api.ArtifactVersions; +import org.codehaus.mojo.versions.api.ArtifactVersionsCache; +import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel; + +import static java.util.Optional.of; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; +import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; + +public class PluginOverviewStats extends OverviewStats +{ + /** + * Extension of the {@linkplain OverviewStats} adding dependency stats + */ + private int dependencies; + + public int getDependencies() + { + return dependencies; + } + + public void incrementDependencies() + { + dependencies++; + } + + /** + * Creates a {@linkplain PluginOverviewStats} instance based on the collection of version updates in + * the argument + * + * @param updates collection of all version updates, typically from {@linkplain PluginUpdatesModel#getAllUpdates()} + * @param cache if not null, cache to retrieve the version information, initialised with + * the {@link ArtifactVersions#getOldestUpdate(Optional)} update information + * @param always equal to {@linkplain PluginOverviewStats} + * @param always equal to {@linkplain org.codehaus.mojo.versions.PluginUpdatesDetails} + * @return instance of the {@linkplain PluginOverviewStats}, initialised with the update information + */ + public static T fromUpdates( Collection updates, + ArtifactVersionsCache cache ) + { + PluginOverviewStats stats = new PluginOverviewStats(); + updates.forEach( details -> + { + if ( getOldestUpdate( cache, details, of( SUBINCREMENTAL ) ) != null ) + { + stats.incrementAny(); + } + else if ( getOldestUpdate( cache, details, of( INCREMENTAL ) ) != null ) + { + stats.incrementIncremental(); + } + else if ( getOldestUpdate( cache, details, of( MINOR ) ) != null ) + { + stats.incrementMinor(); + } + else if ( getOldestUpdate( cache, details, of( MAJOR ) ) != null ) + { + stats.incrementMajor(); + } + else + { + stats.incrementUpToDate(); + } + if ( ( ( PluginUpdatesDetails ) details ).isDependencyUpdateAvailable() ) + { + stats.incrementDependencies(); + } + } ); + return (T) stats; + } +} diff --git a/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java index 1df2288be..e1327dd56 100644 --- a/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java +++ b/src/main/java/org/codehaus/mojo/versions/reporting/PluginUpdatesReportRenderer.java @@ -127,59 +127,13 @@ protected void renderSummaryTable( Map content sink.table_(); } - /** - * Extension of the {@linkplain OverviewStats} adding dependency stats - */ - static class PluginOverviewStats extends OverviewStats - { - private int dependencies; - - public int getDependencies() - { - return dependencies; - } - - public void incrementDependencies() - { - dependencies++; - } - } - /** * {@inheritDoc} */ @Override protected PluginOverviewStats computeOverviewStats() { - PluginOverviewStats stats = new PluginOverviewStats(); - model.getAllUpdates().values().forEach( details -> - { - if ( oldestUpdateCache.get( details, of( SUBINCREMENTAL ) ) != null ) - { - stats.incrementAny(); - } - else if ( oldestUpdateCache.get( details, of( INCREMENTAL ) ) != null ) - { - stats.incrementIncremental(); - } - else if ( oldestUpdateCache.get( details, of( MINOR ) ) != null ) - { - stats.incrementMinor(); - } - else if ( oldestUpdateCache.get( details, of( MAJOR ) ) != null ) - { - stats.incrementMajor(); - } - else - { - stats.incrementUpToDate(); - } - if ( details.isDependencyUpdateAvailable() ) - { - stats.incrementDependencies(); - } - } ); - return stats; + return PluginOverviewStats.fromUpdates( model.getAllUpdates().values(), oldestUpdateCache ); } @Override diff --git a/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlRenderer.java b/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlRenderer.java deleted file mode 100644 index 6b890c7b5..000000000 --- a/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlRenderer.java +++ /dev/null @@ -1,292 +0,0 @@ -package org.codehaus.mojo.versions.xml; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.Collection; -import java.util.Map; -import java.util.Optional; -import java.util.TreeMap; - -import org.apache.maven.artifact.versioning.ArtifactVersion; -import org.apache.maven.model.Dependency; -import org.apache.maven.reporting.MavenReportException; -import org.codehaus.mojo.versions.api.ArtifactVersions; -import org.codehaus.mojo.versions.api.Segment; -import org.codehaus.mojo.versions.utils.DependencyComparator; - -import static java.util.Optional.empty; -import static java.util.Optional.of; -import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; -import static org.codehaus.mojo.versions.api.Segment.MAJOR; -import static org.codehaus.mojo.versions.api.Segment.MINOR; -import static org.codehaus.mojo.versions.api.Segment.SUBINCREMENTAL; - -/** - * XML renderer for DependencyUpdatesReport creates an xml file in target directory and writes report about available - * dependency/dependency management updates. - * - * @author Illia Dubinin - * @since 2.4 - */ -public class DependencyUpdatesXmlRenderer -{ - - private static final String GROUP_ID = "groupId"; - - private static final String ARTIFACT_ID = "artifactId"; - - private static final String SCOPE = "scope"; - - private static final String CLASSIFIER = "classifier"; - - private static final String TYPE = "type"; - - private static final String CURRENT_VERSION = "currentVersion"; - - private static final String NEXT_VERSION = "nextVersion"; - - private static final String STATUS = "status"; - - private static final String OPEN_TAG = "<"; - - private static final String CLOSE_TAG = ">"; - - private static final String OPEN_CLOSING_TAG = " dependencyUpdates; - - private final Map dependencyManagementUpdates; - - private final String outputFileName; - - public DependencyUpdatesXmlRenderer( Map dependencyUpdates, - Map dependencyManagementUpdates, - String outputFileName ) - { - this.dependencyUpdates = dependencyUpdates; - this.dependencyManagementUpdates = dependencyManagementUpdates; - this.outputFileName = outputFileName; - } - - /** - * Makes report file with given name in target directory. - * - * @throws MavenReportException if something went wrong - */ - public void render() - throws MavenReportException - { - StringBuilder sb = new StringBuilder(); - sb.append( "" ).append( NL ); - Map allUpdates = new TreeMap<>( DependencyComparator.INSTANCE ); - allUpdates.putAll( dependencyManagementUpdates ); - allUpdates.putAll( dependencyUpdates ); - sb.append( getSummaryBlock( allUpdates.values() ) ); - sb.append( getDependencyInfoBlock( dependencyManagementUpdates, "dependencyManagements", - "dependencyManagement" ) ); - sb.append( getDependencyInfoBlock( dependencyUpdates, "dependencies", "dependency" ) ); - sb.append( "" ).append( NL ); - PrintWriter pw; - try - { - pw = new PrintWriter( outputFileName, "UTF8" ); - pw.print( sb ); - pw.close(); - } - catch ( IOException e ) - { - throw new MavenReportException( "Cannot create xml report.", e ); - } - } - - /** - * Method wraps value in xml tag. In ex: to wrap foo in tag bar you have to pass foo as value and bar as tag. As a - * result you will get: <bar>foo</bar> - * - * @param value - string to wrap - * @param tag - name of tag - * @return value wrapped in xml tag - */ - - public static String wrapElement( String value, String tag ) - { - return OPEN_TAG + tag + CLOSE_TAG + value + OPEN_CLOSING_TAG + tag + CLOSE_TAG; - } - - /** - * Returns summary of dependency analysis result in xml format: current version, next available, next incremental, - * next minor and next major versions. - * - * @param allUpdates all dependencies versions - * @return summary in xml format - */ - public static String getSummaryBlock( Collection allUpdates ) - { - int numInc = 0; - int numMin = 0; - int numMaj = 0; - int numAny = 0; - int numCur = 0; - for ( ArtifactVersions details : allUpdates ) - { - if ( details.getOldestUpdate( of( SUBINCREMENTAL ) ) != null ) - { - numAny++; - } - else if ( details.getOldestUpdate( of( INCREMENTAL ) ) != null ) - { - numInc++; - } - else if ( details.getOldestUpdate( of( MINOR ) ) != null ) - { - numMin++; - } - else if ( details.getOldestUpdate( of( MAJOR ) ) != null ) - { - numMaj++; - } - else - { - numCur++; - } - } - - String result = "\t%n" + "\t\t%d%n" - + "\t\t%d%n" - + "\t\t%d%n" - + "\t\t%d%n" + "\t\t%d%n" - + "\t%n"; - - return String.format( result, numCur, numAny, numInc, numMin, numMaj ); - } - - /** - * Returns xml report for current dependency state with following info: current version, next available version, - * next incremental/minor/major if available and status ('incremental available', 'minor available', 'major - * available' or 'no new available') - * - * @param versions version info for dependency - * @return xml reports about current possible updates. - */ - public static String getVersionsBlocks( ArtifactVersions versions ) - { - StringBuilder sBuilder = new StringBuilder(); - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( versions.isCurrentVersionDefined() - ? versions.getCurrentVersion().toString() : versions.getArtifact().getVersionRange().toString(), - CURRENT_VERSION ) ).append( NL ); - ArtifactVersion nextVersion = versions.getOldestUpdate( empty() ); - if ( nextVersion != null ) - { - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( nextVersion.toString(), - NEXT_VERSION ) ).append( NL ); - - String incrementalsBlock = getVersionsInScopeBlock( versions, of( INCREMENTAL ) ); - sBuilder.append( incrementalsBlock ); - String minorsBlock = getVersionsInScopeBlock( versions, of( MINOR ) ); - sBuilder.append( minorsBlock ); - String majorsBlock = getVersionsInScopeBlock( versions, of( MAJOR ) ); - sBuilder.append( majorsBlock ); - - String status = null; - if ( incrementalsBlock.length() > 0 ) - { - status = "incremental available"; - } - else if ( minorsBlock.length() > 0 ) - { - status = "minor available"; - } - else if ( majorsBlock.length() > 0 ) - { - status = "major available"; - } - - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( status, STATUS ) ).append( NL ); - } - else - { - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( "no new available", - STATUS ) ).append( NL ); - } - - return sBuilder.toString(); - } - - private static String getDependencyInfoBlock( Map dependencyUpdates, String blockName, - String subblockName ) - { - StringBuilder sBuilder = new StringBuilder(); - sBuilder.append( TAB ).append( OPEN_TAG ).append( blockName ).append( CLOSE_TAG ).append( NL ); - dependencyUpdates.forEach( ( dep, value ) -> - { - sBuilder.append( TAB ).append( TAB ).append( OPEN_TAG ).append( subblockName ).append( CLOSE_TAG ) - .append( NL ); - - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getGroupId(), - GROUP_ID ) ).append( NL ); - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getArtifactId(), - ARTIFACT_ID ) ).append( NL ); - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getScope(), - SCOPE ) ).append( NL ); - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getClassifier(), - CLASSIFIER ) ).append( NL ); - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( dep.getType(), - TYPE ) ).append( NL ); - - sBuilder.append( getVersionsBlocks( value ) ); - - sBuilder.append( TAB ).append( TAB ).append( OPEN_CLOSING_TAG ).append( subblockName ).append( CLOSE_TAG ) - .append( NL ); - } ); - sBuilder.append( TAB ).append( OPEN_CLOSING_TAG ).append( blockName ).append( CLOSE_TAG ).append( NL ); - return sBuilder.toString(); - } - - private static String getVersionsInScopeBlock( ArtifactVersions av, Optional scope ) - { - String versionsTag = scope.map( s -> s.name().toLowerCase() + "s" ).orElse( "any" ); - StringBuilder sBuilder = new StringBuilder(); - - ArtifactVersion nextVersion = av.getOldestUpdate( scope ); - if ( nextVersion != null ) - { - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( OPEN_TAG ).append( versionsTag ) - .append( CLOSE_TAG ).append( NL ); - ArtifactVersion[] versions = av.getAllUpdates( scope ); - for ( ArtifactVersion version : versions ) - { - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( TAB ) - .append( wrapElement( version.toString(), - scope.toString().toLowerCase() ) ).append( NL ); - } - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( OPEN_CLOSING_TAG ).append( versionsTag ) - .append( CLOSE_TAG ).append( NL ); - } - - return sBuilder.toString(); - } - -} diff --git a/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java new file mode 100644 index 000000000..25dfb3dcb --- /dev/null +++ b/src/main/java/org/codehaus/mojo/versions/xml/DependencyUpdatesXmlReportRenderer.java @@ -0,0 +1,146 @@ +package org.codehaus.mojo.versions.xml; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.model.Dependency; +import org.codehaus.mojo.versions.api.AbstractVersionDetails; +import org.codehaus.mojo.versions.api.ArtifactVersions; +import org.codehaus.mojo.versions.api.ArtifactVersionsCache; +import org.codehaus.mojo.versions.api.ReportRenderer; +import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.reporting.OverviewStats; +import org.codehaus.mojo.versions.reporting.model.DependencyInfo; +import org.codehaus.mojo.versions.reporting.model.DependencyReportSummary; +import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel; +import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesReport; +import org.codehaus.mojo.versions.reporting.model.io.xpp3.DependencyUpdatesReportXpp3Writer; + +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static java.util.Optional.ofNullable; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; + +/** + * XML renderer for DependencyUpdatesReport creates an xml file in target directory and writes report about available + * dependency/dependency management updates. + * + * @author Illia Dubinin + * @since 2.4 + */ +public class DependencyUpdatesXmlReportRenderer implements ReportRenderer +{ + private final DependencyUpdatesModel model; + private final Path outputFile; + private final ArtifactVersionsCache oldestUpdateCache + = new ArtifactVersionsCache( AbstractVersionDetails::getOldestUpdate ); + /** + * Creates a new instance + * @param model object containing the updates model + * @param outputFile output file for the report + */ + public DependencyUpdatesXmlReportRenderer( DependencyUpdatesModel model, Path outputFile ) + { + this.model = model; + this.outputFile = outputFile; + } + + /** + * Creates an XML report + */ + @Override + public void render() + { + try ( BufferedWriter writer = Files.newBufferedWriter( outputFile, + StandardCharsets.UTF_8 ) ) + { + new DependencyUpdatesReportXpp3Writer().write( writer, new DependencyUpdatesReport() + {{ + setSummary( new DependencyReportSummary() + {{ + OverviewStats overviewStats = OverviewStats.fromUpdates( model.getAllUpdates().values(), + oldestUpdateCache ); + setUsingLastVersion( String.valueOf( overviewStats.getUpToDate() ) ); + setNextVersionAvailable( String.valueOf( overviewStats.getAny() ) ); + setNextIncrementalAvailable( String.valueOf( overviewStats.getIncremental() ) ); + setNextMinorAvailable( String.valueOf( overviewStats.getMinor() ) ); + setNextMajorAvailable( String.valueOf( overviewStats.getMajor() ) ); + }} ); + setDependencyManagements( createDependencyInfo( model.getArtifactManagementUpdates() ) ); + setDependencies( createDependencyInfo( model.getArtifactUpdates() ) ); + }} ); + } + catch ( IOException e ) + { + throw new RuntimeException( e ); + } + } + + private static void setSection( ArtifactVersions versions, Segment segment, Consumer> setterFunction ) + { + ofNullable( versions.getAllUpdates( of( segment ) ) ) + .map( v -> Arrays.stream( v ) + .map( ArtifactVersion::toString ) + .collect( Collectors.toList() ) ) + .ifPresent( setterFunction ); + } + + private static List createDependencyInfo( Map versions ) + { + return versions.entrySet().stream().map( e -> + new DependencyInfo() + {{ + setGroupId( e.getKey().getGroupId() ); + setArtifactId( e.getKey().getArtifactId() ); + setCurrentVersion( e.getKey().getVersion() ); + setScope( e.getKey().getScope() ); + setType( e.getKey().getType() ); + setClassifier( e.getKey().getClassifier() ); + + ofNullable( e.getValue().getOldestUpdate( empty() ) ) + .map( ArtifactVersion::toString ).ifPresent( this::setNextVersion ); + + setSection( e.getValue(), INCREMENTAL, this::setIncrementals ); + setSection( e.getValue(), MINOR, this::setMinors ); + setSection( e.getValue(), MAJOR, this::setMajors ); + + setStatus( getNextVersion() == null + ? "no new available" + : getIncrementals() != null + ? "incremental available" + : getMinors() != null + ? "minor available" + : "major available" ); + }} ).collect( Collectors.toList() ); + } +} diff --git a/src/main/java/org/codehaus/mojo/versions/xml/PluginUpdatesXmlRenderer.java b/src/main/java/org/codehaus/mojo/versions/xml/PluginUpdatesXmlRenderer.java deleted file mode 100644 index 7459323b3..000000000 --- a/src/main/java/org/codehaus/mojo/versions/xml/PluginUpdatesXmlRenderer.java +++ /dev/null @@ -1,132 +0,0 @@ -package org.codehaus.mojo.versions.xml; - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import java.io.IOException; -import java.io.PrintWriter; -import java.util.Map; -import java.util.TreeMap; - -import org.apache.maven.model.Plugin; -import org.apache.maven.reporting.MavenReportException; -import org.codehaus.mojo.versions.PluginUpdatesDetails; -import org.codehaus.mojo.versions.utils.PluginComparator; - -import static org.codehaus.mojo.versions.xml.DependencyUpdatesXmlRenderer.getVersionsBlocks; -import static org.codehaus.mojo.versions.xml.DependencyUpdatesXmlRenderer.wrapElement; - -/** - * XML renderer for PluginUpdatesReport creates an xml file in target directory and writes report about available - * plugin/plugin management updates. - * - * @author Illia Dubinin - * @since 2.4 - */ -public class PluginUpdatesXmlRenderer -{ - - private static final String GROUP_ID = "groupId"; - - private static final String ARTIFACT_ID = "artifactId"; - - private static final String OPEN_TAG = "<"; - - private static final String CLOSE_TAG = ">"; - - private static final String OPEN_CLOSING_TAG = " pluginUpdates; - - private Map pluginManagementUpdates; - - private String outputFileName; - - public PluginUpdatesXmlRenderer( Map pluginUpdates, - Map pluginManagementUpdates, String outputFileName ) - { - this.pluginUpdates = pluginUpdates; - this.pluginManagementUpdates = pluginManagementUpdates; - this.outputFileName = outputFileName; - } - - /** - * Makes report file with given name in target directory. - * - * @throws MavenReportException if something went wrong - */ - public void render() - throws MavenReportException - { - StringBuilder sb = new StringBuilder(); - sb.append( "" ).append( NL ); - Map allUpdates = new TreeMap<>( PluginComparator.INSTANCE ); - allUpdates.putAll( pluginManagementUpdates ); - allUpdates.putAll( pluginUpdates ); - sb.append( getSummaryBlock( allUpdates ) ); - sb.append( getPluginsInfoBlock( pluginManagementUpdates, "pluginManagements", "pluginManagement" ) ); - sb.append( getPluginsInfoBlock( pluginUpdates, "plugins", "plugin" ) ); - sb.append( "" ).append( NL ); - PrintWriter pw; - try - { - pw = new PrintWriter( outputFileName, "UTF8" ); - pw.print( sb ); - pw.close(); - } - catch ( IOException e ) - { - throw new MavenReportException( "Cannot create xml report.", e ); - } - } - - private static String getSummaryBlock( Map allUpdates ) - { - return DependencyUpdatesXmlRenderer.getSummaryBlock( allUpdates.values() ); - } - - private static String getPluginsInfoBlock( Map pluginUpdates, String blockName, - String subblockName ) - { - StringBuilder sBuilder = new StringBuilder(); - sBuilder.append( TAB ).append( OPEN_TAG ).append( blockName ).append( CLOSE_TAG ).append( NL ); - pluginUpdates.forEach( ( plugin, value ) -> - { - sBuilder.append( TAB ).append( TAB ).append( OPEN_TAG ).append( subblockName ).append( CLOSE_TAG ) - .append( NL ); - - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( plugin.getGroupId(), - GROUP_ID ) ).append( NL ); - sBuilder.append( TAB ).append( TAB ).append( TAB ).append( wrapElement( plugin.getArtifactId(), - ARTIFACT_ID ) ).append( NL ); - - sBuilder.append( getVersionsBlocks( value ) ); - - sBuilder.append( TAB ).append( TAB ).append( OPEN_CLOSING_TAG ).append( subblockName ).append( CLOSE_TAG ) - .append( NL ); - } ); - sBuilder.append( TAB ).append( OPEN_CLOSING_TAG ).append( blockName ).append( CLOSE_TAG ).append( NL ); - return sBuilder.toString(); - } - -} diff --git a/src/main/java/org/codehaus/mojo/versions/xml/PluginUpdatesXmlReportRenderer.java b/src/main/java/org/codehaus/mojo/versions/xml/PluginUpdatesXmlReportRenderer.java new file mode 100644 index 000000000..1f4d66802 --- /dev/null +++ b/src/main/java/org/codehaus/mojo/versions/xml/PluginUpdatesXmlReportRenderer.java @@ -0,0 +1,148 @@ +package org.codehaus.mojo.versions.xml; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import org.apache.maven.artifact.versioning.ArtifactVersion; +import org.apache.maven.model.Dependency; +import org.codehaus.mojo.versions.PluginUpdatesDetails; +import org.codehaus.mojo.versions.api.AbstractVersionDetails; +import org.codehaus.mojo.versions.api.ArtifactVersions; +import org.codehaus.mojo.versions.api.ArtifactVersionsCache; +import org.codehaus.mojo.versions.api.ReportRenderer; +import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.reporting.PluginOverviewStats; +import org.codehaus.mojo.versions.reporting.model.PluginInfo; +import org.codehaus.mojo.versions.reporting.model.PluginReportSummary; +import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel; +import org.codehaus.mojo.versions.reporting.model.PluginUpdatesReport; +import org.codehaus.mojo.versions.reporting.model.io.xpp3.PluginUpdatesReportXpp3Writer; + +import static java.util.Optional.empty; +import static java.util.Optional.of; +import static java.util.Optional.ofNullable; +import static org.codehaus.mojo.versions.api.Segment.INCREMENTAL; +import static org.codehaus.mojo.versions.api.Segment.MAJOR; +import static org.codehaus.mojo.versions.api.Segment.MINOR; + +/** + * XML renderer for DependencyUpdatesReport creates an xml file in target directory and writes report about available + * dependency/dependency management updates. + * + * @author Illia Dubinin + * @since 2.4 + */ +public class PluginUpdatesXmlReportRenderer implements ReportRenderer +{ + private final PluginUpdatesModel model; + private final Path outputFile; + private final ArtifactVersionsCache oldestUpdateCache + = new ArtifactVersionsCache( AbstractVersionDetails::getOldestUpdate ); + /** + * Creates a new instance + * @param model object containing the updates model + * @param outputFile output file for the report + */ + public PluginUpdatesXmlReportRenderer( PluginUpdatesModel model, Path outputFile ) + { + this.model = model; + this.outputFile = outputFile; + } + + /** + * Creates an XML report + */ + @Override + public void render() + { + try ( BufferedWriter writer = Files.newBufferedWriter( outputFile, + StandardCharsets.UTF_8 ) ) + { + new PluginUpdatesReportXpp3Writer().write( writer, new PluginUpdatesReport() + {{ + setSummary( new PluginReportSummary() + {{ + PluginOverviewStats overviewStats = PluginOverviewStats.fromUpdates( + model.getAllUpdates().values(), oldestUpdateCache ); + setUsingLastVersion( String.valueOf( overviewStats.getUpToDate() ) ); + setNextVersionAvailable( String.valueOf( overviewStats.getAny() ) ); + setNextIncrementalAvailable( String.valueOf( overviewStats.getIncremental() ) ); + setNextMinorAvailable( String.valueOf( overviewStats.getMinor() ) ); + setNextMajorAvailable( String.valueOf( overviewStats.getMajor() ) ); + setDependencyUpdates( String.valueOf( overviewStats.getDependencies() ) ); + }} ); + setPluginManagements( createPluginInfo( model.getArtifactManagementUpdates() ) ); + setPlugins( createPluginInfo( model.getArtifactUpdates() ) ); + }} ); + } + catch ( IOException e ) + { + throw new RuntimeException( e ); + } + } + + private static void setSection( ArtifactVersions versions, Segment segment, Consumer> setterFunction ) + { + ofNullable( versions.getAllUpdates( of( segment ) ) ) + .map( v -> Arrays.stream( v ) + .map( ArtifactVersion::toString ) + .collect( Collectors.toList() ) ) + .ifPresent( setterFunction ); + } + + private static List createPluginInfo( Map versions ) + { + return versions.entrySet().stream().map( e -> + new PluginInfo() + {{ + setGroupId( e.getKey().getGroupId() ); + setArtifactId( e.getKey().getArtifactId() ); + setCurrentVersion( e.getKey().getVersion() ); + setScope( e.getKey().getScope() ); + setType( e.getKey().getType() ); + setClassifier( e.getKey().getClassifier() ); + + ofNullable( e.getValue().getOldestUpdate( empty() ) ) + .map( ArtifactVersion::toString ).ifPresent( this::setNextVersion ); + + setSection( e.getValue(), INCREMENTAL, this::setIncrementals ); + setSection( e.getValue(), MINOR, this::setMinors ); + setSection( e.getValue(), MAJOR, this::setMajors ); + + setStatus( getNextVersion() == null + ? "no new available" + : getIncrementals() != null + ? "incremental available" + : getMinors() != null + ? "minor available" + : "major available" ); + }} ).collect( Collectors.toList() ); + } +} diff --git a/src/main/mdo/dependency-updates-report.mdo b/src/main/mdo/dependency-updates-report.mdo new file mode 100644 index 000000000..28b723ee5 --- /dev/null +++ b/src/main/mdo/dependency-updates-report.mdo @@ -0,0 +1,177 @@ + + + + + + dependency-updates-report + DependencyUpdatesReport + + XML version of the Dependency Updates Report + + + + + package + org.codehaus.mojo.versions.reporting.model + + + + + namespace + + + + + + DependencyUpdatesReport + + + summary + true + + DependencyReportSummary + 1 + + + + dependencyManagements + + DependencyInfo + * + + + + dependencies + + DependencyInfo + * + + + + + + + DependencyReportSummary + + + usingLastVersion + true + String + + + nextVersionAvailable + true + String + + + nextIncrementalAvailable + 1.0.0+ + true + String + + + nextMinorAvailable + true + String + + + nextMajorAvailable + true + String + + + + + + DependencyInfo + + + groupId + true + String + + + artifactId + true + String + + + scope + true + String + + + classifier + String + + + type + true + String + + + currentVersion + String + + + nextVersion + String + + + any + + String + * + + + + incrementals + + String + * + + + + minors + + String + * + + + + majors + + String + * + + + + status + true + String + + + + + + diff --git a/src/main/mdo/plugin-updates-report.mdo b/src/main/mdo/plugin-updates-report.mdo new file mode 100644 index 000000000..25c7fa428 --- /dev/null +++ b/src/main/mdo/plugin-updates-report.mdo @@ -0,0 +1,182 @@ + + + + + + plugin-updates-report + PluginUpdatesReport + + XML version of the Plugin Updates Report + + + + + package + org.codehaus.mojo.versions.reporting.model + + + + + namespace + + + + + + PluginUpdatesReport + + + summary + true + + PluginReportSummary + 1 + + + + pluginManagements + + PluginInfo + * + + + + plugins + + PluginInfo + * + + + + + + + PluginReportSummary + + + usingLastVersion + true + String + + + nextVersionAvailable + true + String + + + nextIncrementalAvailable + 1.0.0+ + true + String + + + nextMinorAvailable + true + String + + + nextMajorAvailable + true + String + + + dependencyUpdates + true + String + + + + + + PluginInfo + + + groupId + true + String + + + artifactId + true + String + + + scope + true + String + + + classifier + String + + + type + true + String + + + currentVersion + String + + + nextVersion + String + + + any + + String + * + + + + incrementals + + String + * + + + + minors + + String + * + + + + majors + + String + * + + + + status + true + String + + + + + + diff --git a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesXmlRendererTest.java b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesXmlRendererTest.java index 69889a7e9..1d8994aa8 100644 --- a/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesXmlRendererTest.java +++ b/src/test/java/org/codehaus/mojo/versions/DependencyUpdatesXmlRendererTest.java @@ -19,49 +19,86 @@ * under the License. */ +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; -import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; -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.VersionRange; import org.codehaus.mojo.versions.api.ArtifactVersions; -import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlRenderer; -import org.hamcrest.core.Is; +import org.codehaus.mojo.versions.ordering.MavenVersionComparator; +import org.codehaus.mojo.versions.reporting.model.DependencyUpdatesModel; +import org.codehaus.mojo.versions.utils.DependencyBuilder; +import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlReportRenderer; +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; +import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.core.IsNull.nullValue; /** - * Basic tests for {@linkplain DependencyUpdatesXmlRenderer}. + * Basic tests for {@linkplain DependencyUpdatesXmlReportRenderer}. * * @author Andrzej Jarmoniuk */ public class DependencyUpdatesXmlRendererTest { - @Test - public void testGetVersionsBlocks() throws InvalidVersionSpecificationException + private Path tempFile; + + @Before + public void setUp() throws IOException + { + tempFile = Files.createTempFile( "xml-dependency-report", "" ); + } + + @After + public void tearDown() throws IOException { - List versions = - versionsOf( "1.0.0.1", "1.0.0.2", "2.121.2.1", "2.100.0.1", "3.1.0.1", "1.1.1" ); - final Artifact artifact = - new DefaultArtifact( "group", "artifact", VersionRange.createFromVersionSpec( "[1.0,3.0]" ), "foo", - "bar", "jar", null ); - ArtifactVersions artifactVersions = new ArtifactVersions( artifact, versions, null ); - assertThat( artifactVersions.getCurrentVersion(), nullValue() ); - assertThat( artifactVersions.isCurrentVersionDefined(), Is.is( false ) ); - String versionsBlocks = DependencyUpdatesXmlRenderer.getVersionsBlocks( artifactVersions ); - assertThat( versionsBlocks, containsString( "[1.0,3.0]" ) ); + if ( tempFile != null && Files.exists( tempFile ) ) + { + Files.delete( tempFile ); + } } - private static List versionsOf( String... versions ) + @Test + public void testReportGeneration() throws IOException { - return Arrays.stream( versions ).map( DefaultArtifactVersion::new ).collect( Collectors.toList() ); + new DependencyUpdatesXmlReportRenderer( new DependencyUpdatesModel( + singletonMap( DependencyBuilder.newBuilder() + .withGroupId( "default-group" ) + .withArtifactId( "artifactA" ) + .withVersion( "1.0.0" ) + .build(), new ArtifactVersions( + new DefaultArtifact( "default-group", "artifactA", + "1.0.0", SCOPE_COMPILE, "jar", "default", + null ), + Arrays.asList( + new DefaultArtifactVersion( "1.0.0" ), + new DefaultArtifactVersion( "1.0.1" ), + new DefaultArtifactVersion( "1.1.0" ), + new DefaultArtifactVersion( "2.0.0" ) + ), + new MavenVersionComparator() ) ), + emptyMap() ), tempFile ).render(); + String output = String.join( "", Files.readAllLines( tempFile ) ) + .replaceAll( ">\\s*<", "><" ); + + assertThat( output, containsString( "0" ) ); + assertThat( output, containsString( "0" ) ); + assertThat( output, containsString( "1" ) ); + assertThat( output, containsString( "0" ) ); + assertThat( output, containsString( "0" ) ); + + assertThat( output, containsString( "1.0.0" ) ); + assertThat( output, containsString( "1.0.1" ) ); + assertThat( output, containsString( "1.0.1" ) ); + assertThat( output, containsString( "1.1.0" ) ); + assertThat( output, containsString( "2.0.0" ) ); + assertThat( output, containsString( "incremental available" ) ); } } diff --git a/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java b/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java new file mode 100644 index 000000000..5d653ae5e --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/PluginUpdatesXmlRendererTest.java @@ -0,0 +1,128 @@ +package org.codehaus.mojo.versions; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.apache.maven.artifact.DefaultArtifact; +import org.apache.maven.artifact.versioning.DefaultArtifactVersion; +import org.apache.maven.model.Plugin; +import org.codehaus.mojo.versions.api.ArtifactVersions; +import org.codehaus.mojo.versions.ordering.MavenVersionComparator; +import org.codehaus.mojo.versions.reporting.model.PluginUpdatesModel; +import org.codehaus.mojo.versions.utils.DependencyBuilder; +import org.codehaus.mojo.versions.xml.DependencyUpdatesXmlReportRenderer; +import org.codehaus.mojo.versions.xml.PluginUpdatesXmlReportRenderer; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; +import static org.apache.maven.artifact.Artifact.SCOPE_COMPILE; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; + +/** + * Basic tests for {@linkplain DependencyUpdatesXmlReportRenderer}. + * + * @author Andrzej Jarmoniuk + */ +public class PluginUpdatesXmlRendererTest +{ + private Path tempFile; + + @Before + public void setUp() throws IOException + { + tempFile = Files.createTempFile( "xml-plugin-report", "" ); + } + + @After + public void tearDown() throws IOException + { + if ( tempFile != null && Files.exists( tempFile ) ) + { + Files.delete( tempFile ); + } + } + + @Test + public void testReportGeneration() throws IOException + { + PluginUpdatesModel pluginUpdates = new PluginUpdatesModel( + singletonMap( + pluginOf( "default-group", "artifactA", "1.0.0" ), + new PluginUpdatesDetails( + new ArtifactVersions( + artifactOf( "default-group", "artifactA", "1.0.0" ), + Stream.of( "1.0.0", "1.0.1", "1.1.0", "2.0.0" ) + .map( DefaultArtifactVersion::new ) + .collect( Collectors.toList() ), + new MavenVersionComparator() ), + singletonMap( DependencyBuilder.dependencyWith( "default-group", "artifactB", + "1.0.0" ), + new ArtifactVersions( + artifactOf( "default-group", "artifactB", "1.0.0" ), + Stream.of( "1.0.0", "1.0.1-SNAPSHOT", "1.1.0-rc1", "2.0.0" ) + .map( DefaultArtifactVersion::new ) + .collect( Collectors.toList() ), + new MavenVersionComparator() ) ), false ) ), + emptyMap() ); + new PluginUpdatesXmlReportRenderer( pluginUpdates, tempFile ).render(); + + String output = String.join( "", Files.readAllLines( tempFile ) ) + .replaceAll( ">\\s*<", "><" ); + + assertThat( output, containsString( "0" ) ); + assertThat( output, containsString( "0" ) ); + assertThat( output, containsString( "1" ) ); + assertThat( output, containsString( "0" ) ); + assertThat( output, containsString( "0" ) ); + assertThat( output, containsString( "1" ) ); + + assertThat( output, containsString( "1.0.0" ) ); + assertThat( output, containsString( "1.0.1" ) ); + assertThat( output, containsString( "1.0.1" ) ); + assertThat( output, containsString( "1.1.0" ) ); + assertThat( output, containsString( "2.0.0" ) ); + assertThat( output, containsString( "incremental available" ) ); + } + + private static DefaultArtifact artifactOf( String groupId, String artifactId, String version ) + { + return new DefaultArtifact( groupId, artifactId, version, SCOPE_COMPILE, "jar", "default", + null ); + } + + private static Plugin pluginOf( String groupId, String artifactId, String version ) + { + return new Plugin() + {{ + setGroupId( groupId ); + setArtifactId( artifactId ); + setVersion( version ); + }}; + } +} diff --git a/src/test/java/org/codehaus/mojo/versions/utils/TestUtils.java b/src/test/java/org/codehaus/mojo/versions/utils/TestUtils.java new file mode 100644 index 000000000..6e45c91b9 --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/utils/TestUtils.java @@ -0,0 +1,74 @@ +package org.codehaus.mojo.versions.utils; +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; + +import static java.nio.file.FileVisitResult.CONTINUE; +import static org.apache.commons.text.CaseUtils.toCamelCase; + +/** + * Auxiliary test utilities + */ +public class TestUtils +{ + /** + * Creates a temporary directory with the given name + * @param name name of the directory to create + * @return {@linkplain Path} object pointing to the directory + * @throws IOException should the I/O operation fail + */ + public static Path createTempDir( String name ) throws IOException + { + return Files.createTempDirectory( toCamelCase( name, false ) ); + } + + /** + * Deletes the given directory together with all its contents + * @param dir directory to delete + * @throws IOException should an I/O operation fail + */ + public static void tearDownTempDir( Path dir ) throws IOException + { + if ( dir != null && Files.exists( dir ) ) + { + Files.walkFileTree( dir, new SimpleFileVisitor() + { + @Override + public FileVisitResult visitFile( Path file, BasicFileAttributes attrs ) throws IOException + { + Files.delete( file ); + return CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory( Path dir, IOException exc ) throws IOException + { + Files.delete( dir ); + return CONTINUE; + } + } ); + } + } +}