From 555f9058f588d5ffd064f707497c7b9f20ad52b0 Mon Sep 17 00:00:00 2001 From: Andrzej Jarmoniuk Date: Tue, 25 Oct 2022 08:01:02 +0200 Subject: [PATCH] Resolves #793: Added includeParent to DisplayPropertyUpdates and PropertyUpdatesReport --- .../versions/CompareDependenciesMojo.java | 4 +- .../versions/DisplayPropertyUpdatesMojo.java | 19 +- .../versions/PropertyUpdatesReportMojo.java | 19 +- .../mojo/versions/ResolveRangesMojo.java | 7 +- .../mojo/versions/SetPropertyMojo.java | 9 +- .../mojo/versions/UpdatePropertiesMojo.java | 14 +- .../mojo/versions/UpdatePropertyMojo.java | 10 +- .../versions/api/DefaultVersionsHelper.java | 39 ++-- .../codehaus/mojo/versions/api/PomHelper.java | 13 +- .../mojo/versions/api/VersionsHelper.java | 189 +++++++++++++++++- .../DisplayPropertyUpdatesMojoTest.java | 22 ++ .../PropertyUpdatesReportMojoTest.java | 98 +++++++++ .../api/DefaultVersionsHelperTest.java | 9 +- 13 files changed, 400 insertions(+), 52 deletions(-) create mode 100644 src/test/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojoTest.java diff --git a/src/main/java/org/codehaus/mojo/versions/CompareDependenciesMojo.java b/src/main/java/org/codehaus/mojo/versions/CompareDependenciesMojo.java index 1c015fd82..eb7d80d8b 100644 --- a/src/main/java/org/codehaus/mojo/versions/CompareDependenciesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/CompareDependenciesMojo.java @@ -47,6 +47,7 @@ import org.codehaus.mojo.versions.api.ArtifactAssociation; 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.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.DependencyBuilder; @@ -206,7 +207,8 @@ protected void update( ModifiedPomXMLEventReader pom ) if ( updatePropertyVersions ) { Map versionProperties = - this.getHelper().getVersionPropertiesMap( getProject(), null, null, null, true ); + this.getHelper().getVersionPropertiesMap( VersionsHelper.VersionPropertiesMapRequest.builder() + .withMavenProject( getProject() ).build() ); List diff = updatePropertyVersions( pom, versionProperties, remoteDepsMap ); propertyDiffs.addAll( diff ); } diff --git a/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java b/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java index 2120aac1c..f0f11a7a5 100644 --- a/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojo.java @@ -42,6 +42,7 @@ import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.PropertyVersions; import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.api.VersionsHelper; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.SegmentUtils; @@ -128,6 +129,14 @@ public class DisplayPropertyUpdatesMojo @Parameter( property = "allowIncrementalUpdates", defaultValue = "true" ) private boolean allowIncrementalUpdates; + /** + *

Whether to include property updates from parent.

+ * + * @since 2.14.0 + */ + @Parameter( property = "includeParent", defaultValue = "true" ) + protected boolean includeParent = true; + // -------------------------- STATIC METHODS -------------------------- // -------------------------- OTHER METHODS -------------------------- @@ -150,8 +159,14 @@ public void execute() List updates = new ArrayList<>(); Map propertyVersions = - this.getHelper().getVersionPropertiesMap( getProject(), properties, includeProperties, excludeProperties, - autoLinkItems ); + this.getHelper().getVersionPropertiesMap( VersionsHelper.VersionPropertiesMapRequest.builder() + .withMavenProject( getProject() ) + .withPropertyDefinitions( properties ) + .withIncludeProperties( includeProperties ) + .withExcludeProperties( excludeProperties ) + .withIncludeParent( includeParent ) + .withAutoLinkItems( autoLinkItems ) + .build() ); for ( Map.Entry entry : propertyVersions.entrySet() ) { Property property = entry.getKey(); diff --git a/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java b/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java index 3299d0e0e..60f758678 100644 --- a/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojo.java @@ -36,6 +36,7 @@ import org.apache.maven.reporting.MavenReportException; import org.apache.maven.repository.RepositorySystem; import org.codehaus.mojo.versions.api.PropertyVersions; +import org.codehaus.mojo.versions.api.VersionsHelper; import org.codehaus.mojo.versions.reporting.ReportRendererFactory; import org.codehaus.mojo.versions.reporting.model.PropertyUpdatesModel; import org.codehaus.mojo.versions.utils.PropertyComparator; @@ -84,6 +85,14 @@ public class PropertyUpdatesReportMojo extends AbstractVersionsReportWhether to include property updates from parent.

+ * + * @since 2.14.0 + */ + @Parameter( property = "includeParent", defaultValue = "true" ) + private boolean includeParent = true; + @Inject protected PropertyUpdatesReportMojo( I18N i18n, RepositorySystem repositorySystem, ArtifactResolver artifactResolver, @@ -120,8 +129,14 @@ protected void doGenerateReport( Locale locale, Sink sink ) final Map updateSet = new TreeMap<>( PropertyComparator.INSTANCE ); try { - updateSet.putAll( getHelper().getVersionPropertiesMap( getProject(), properties, includeProperties, - excludeProperties, autoLinkItems ) ); + updateSet.putAll( getHelper().getVersionPropertiesMap( VersionsHelper.VersionPropertiesMapRequest.builder() + .withMavenProject( getProject() ) + .withPropertyDefinitions( properties ) + .withIncludeProperties( includeProperties ) + .withExcludeProperties( excludeProperties ) + .withIncludeParent( includeParent ) + .withAutoLinkItems( autoLinkItems ) + .build() ) ); } catch ( MojoExecutionException e ) { diff --git a/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java b/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java index db997e1b0..5c05c8b08 100644 --- a/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/ResolveRangesMojo.java @@ -46,6 +46,7 @@ import org.codehaus.mojo.versions.api.PomHelper; import org.codehaus.mojo.versions.api.PropertyVersions; import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.api.VersionsHelper; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.SegmentUtils; @@ -304,7 +305,11 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom ) { Map propertyVersions = - this.getHelper().getVersionPropertiesMap( getProject(), null, includeProperties, excludeProperties, true ); + this.getHelper().getVersionPropertiesMap( VersionsHelper.VersionPropertiesMapRequest.builder() + .withMavenProject( getProject() ) + .withIncludeProperties( includeProperties ) + .withExcludeProperties( excludeProperties ) + .build() ); for ( Map.Entry entry : propertyVersions.entrySet() ) { Property property = entry.getKey(); diff --git a/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java b/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java index 6ab591534..da046d430 100644 --- a/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java @@ -37,6 +37,7 @@ import org.apache.maven.repository.RepositorySystem; 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.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.PropertiesVersionsFileReader; @@ -154,8 +155,12 @@ private void update( ModifiedPomXMLEventReader pom, Property[] propertiesConfig, throws MojoExecutionException, XMLStreamException { Map propertyVersions = - this.getHelper().getVersionPropertiesMap( getProject(), propertiesConfig, properties, "", - autoLinkItems ); + this.getHelper().getVersionPropertiesMap( VersionsHelper.VersionPropertiesMapRequest.builder() + .withMavenProject( getProject() ) + .withPropertyDefinitions( propertiesConfig ) + .withIncludeProperties( properties ) + .withAutoLinkItems( autoLinkItems ) + .build() ); for ( Map.Entry entry : propertyVersions.entrySet() ) { Property currentProperty = entry.getKey(); diff --git a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java index 9e20ce076..bd692db5a 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertiesMojo.java @@ -39,6 +39,7 @@ import org.codehaus.mojo.versions.api.ArtifactAssociation; import org.codehaus.mojo.versions.api.PropertyVersions; import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.api.VersionsHelper; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; @@ -156,11 +157,14 @@ public UpdatePropertiesMojo( RepositorySystem repositorySystem, protected void update( ModifiedPomXMLEventReader pom ) throws MojoExecutionException, MojoFailureException, XMLStreamException { - Map propertyVersions = this.getHelper().getVersionPropertiesMap( getProject(), - properties, - includeProperties, - excludeProperties, - autoLinkItems ); + Map propertyVersions = getHelper().getVersionPropertiesMap( + VersionsHelper.VersionPropertiesMapRequest.builder() + .withMavenProject( getProject() ) + .withPropertyDefinitions( properties ) + .withIncludeProperties( includeProperties ) + .withExcludeProperties( excludeProperties ) + .withAutoLinkItems( autoLinkItems ) + .build() ); for ( Map.Entry entry : propertyVersions.entrySet() ) { Property property = entry.getKey(); diff --git a/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java b/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java index 4e8cecc7e..ed9d27961 100644 --- a/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/UpdatePropertyMojo.java @@ -39,6 +39,7 @@ import org.codehaus.mojo.versions.api.ArtifactAssociation; import org.codehaus.mojo.versions.api.PropertyVersions; import org.codehaus.mojo.versions.api.Segment; +import org.codehaus.mojo.versions.api.VersionsHelper; import org.codehaus.mojo.versions.ordering.InvalidSegmentException; import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader; import org.codehaus.mojo.versions.utils.SegmentUtils; @@ -158,8 +159,13 @@ protected void update( ModifiedPomXMLEventReader pom ) Property propertyConfig = new Property( property ); propertyConfig.setVersion( newVersion ); Map propertyVersions = - this.getHelper().getVersionPropertiesMap( getProject(), new Property[] {propertyConfig}, property, "", - autoLinkItems ); + this.getHelper().getVersionPropertiesMap( + VersionsHelper.VersionPropertiesMapRequest.builder() + .withMavenProject( getProject() ) + .withPropertyDefinitions( new Property[] {propertyConfig} ) + .withIncludeProperties( property ) + .withAutoLinkItems( autoLinkItems ) + .build() ); for ( Map.Entry entry : propertyVersions.entrySet() ) { Property property = entry.getKey(); diff --git a/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java b/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java index ed959429d..ce0f210c9 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/DefaultVersionsHelper.java @@ -783,27 +783,22 @@ public ExpressionEvaluator getExpressionEvaluator( MavenProject project ) } @Override - public Map getVersionPropertiesMap( MavenProject project, - Property[] propertyDefinitions, - String includeProperties, String excludeProperties, - boolean autoLinkItems ) + public Map getVersionPropertiesMap( VersionPropertiesMapRequest request ) throws MojoExecutionException { Map properties = new HashMap<>(); - if ( propertyDefinitions != null ) + if ( request.getPropertyDefinitions() != null ) { - for ( Property propertyDefinition : propertyDefinitions ) - { - properties.put( propertyDefinition.getName(), propertyDefinition ); - } + Arrays.stream( request.getPropertyDefinitions() ).forEach( p -> properties.put( p.getName(), p ) ); } Map builders = new HashMap<>(); - if ( autoLinkItems ) + if ( request.isAutoLinkItems() ) { final PropertyVersionsBuilder[] propertyVersionsBuilders; try { - propertyVersionsBuilders = PomHelper.getPropertyVersionsBuilders( this, project ); + propertyVersionsBuilders = PomHelper.getPropertyVersionsBuilders( this, request.getMavenProject(), + request.isIncludeParent() ); } catch ( ExpressionEvaluationException | IOException e ) { @@ -825,8 +820,12 @@ public Map getVersionPropertiesMap( MavenProject pro } } - List includePropertiesList = getSplitProperties( includeProperties ); - List excludePropertiesList = getSplitProperties( excludeProperties ); + List includePropertiesList = request.getIncludeProperties() != null + ? Arrays.asList( request.getIncludeProperties().split( "\\s*,\\s*" ) ) + : Collections.emptyList(); + List excludePropertiesList = request.getExcludeProperties() != null + ? Arrays.asList( request.getExcludeProperties().split( "\\s*,\\s*" ) ) + : Collections.emptyList(); getLog().debug( "Searching for properties associated with builders" ); Iterator i = properties.values().iterator(); @@ -884,7 +883,8 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr + builder.getVersionRange() ); property.setVersion( builder.getVersionRange() ); } - versions.setCurrentVersion( project.getProperties().getProperty( property.getName() ) ); + versions.setCurrentVersion( request.getMavenProject().getProperties() + .getProperty( property.getName() ) ); propertyVersions.put( property, versions ); } catch ( ArtifactMetadataRetrievalException e ) @@ -895,17 +895,6 @@ else if ( !excludePropertiesList.isEmpty() && excludePropertiesList.contains( pr return propertyVersions; } - private List getSplitProperties( String commaSeparatedProperties ) - { - List propertiesList = Collections.emptyList(); - if ( StringUtils.isNotEmpty( commaSeparatedProperties ) ) - { - String[] splittedProps = StringUtils.split( commaSeparatedProperties, "," ); - propertiesList = Arrays.asList( StringUtils.stripAll( splittedProps ) ); - } - return propertiesList; - } - /** * Builder class for {@linkplain DefaultVersionsHelper} */ diff --git a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java index 40bb3cac0..557f0eac0 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java @@ -72,6 +72,7 @@ import org.codehaus.plexus.util.StringUtils; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; +import static java.util.Collections.singletonMap; import static java.util.stream.IntStream.range; /** @@ -1018,16 +1019,20 @@ private static Map getRawModelWithParents( MavenProject pro * * @param helper Our versions helper. * @param project The project to examine. + * @param includeParent whether parent POMs should be included * @return An array of properties that are associated within the project. * @throws ExpressionEvaluationException if an expression cannot be evaluated. * @throws IOException if the project's pom file cannot be parsed. * @since 1.0-alpha-3 */ - public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHelper helper, MavenProject project ) + public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHelper helper, MavenProject project, + boolean includeParent ) throws ExpressionEvaluationException, IOException { ExpressionEvaluator expressionEvaluator = helper.getExpressionEvaluator( project ); - Map reactorModels = getRawModelWithParents( project ); + Map reactorModels = includeParent + ? getRawModelWithParents( project ) + : singletonMap( project, getRawModel( project ) ); Map propertiesMap = new TreeMap<>(); @@ -1081,7 +1086,9 @@ public static PropertyVersionsBuilder[] getPropertyVersionsBuilders( VersionsHel reactorModels.values().forEach( model -> addProperties( helper, propertiesMap, null, model.getProperties() ) ); - for ( MavenProject currentPrj = project; currentPrj != null; currentPrj = currentPrj.getParent() ) + for ( MavenProject currentPrj = project; currentPrj != null; currentPrj = includeParent + ? currentPrj.getParent() + : null ) { Model model = reactorModels.get( currentPrj ); diff --git a/src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java b/src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java index 3d5777967..57f03e453 100644 --- a/src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java +++ b/src/main/java/org/codehaus/mojo/versions/api/VersionsHelper.java @@ -221,20 +221,193 @@ PluginUpdatesDetails lookupPluginUpdates( Plugin plugin, boolean allowSnapshots * {@link org.codehaus.mojo.versions.Property} instances consisting of the properties defined in the project which * are associated with version information. * - * @param project The project. - * @param propertyDefinitions Any extra hints about properties. - * @param includeProperties A comma separated list of properties to include. - * @param excludeProperties A comma separated list of properties to exclude. - * @param autoLinkItems whether to automatically infer associations + * @param request {@link VersionPropertiesMapRequest} instance containing the arguments * @return a map of {@link org.codehaus.mojo.versions.api.PropertyVersions} values keyed by * {@link org.codehaus.mojo.versions.Property} instances. * @throws MojoExecutionException if something goes wrong. */ - Map getVersionPropertiesMap( MavenProject project, Property[] propertyDefinitions, - String includeProperties, String excludeProperties, - boolean autoLinkItems ) + Map getVersionPropertiesMap( VersionPropertiesMapRequest request ) throws MojoExecutionException; + /** + * Argument builder class for + * {@link VersionsHelper#getVersionPropertiesMap(VersionPropertiesMapRequest)}. + */ + class VersionPropertiesMapRequest + { + private MavenProject mavenProject; + private Property[] propertyDefinitions; + private String includeProperties; + private String excludeProperties; + private boolean includeParent; + private boolean autoLinkItems; + + /** + * Returns the {@link MavenProject} object + * @return {@link MavenProject} object + */ + protected MavenProject getMavenProject() + { + return mavenProject; + } + + /** + * Returns the {@link Property} array + * @return {@link Property} array + */ + protected Property[] getPropertyDefinitions() + { + return propertyDefinitions; + } + + /** + * Returns the value of {@link #includeProperties} + * @return value of {@link #includeProperties} + */ + protected String getIncludeProperties() + { + return includeProperties; + } + + /** + * Returns the value of {@link #excludeProperties} + * @return value of {@link #excludeProperties} + */ + protected String getExcludeProperties() + { + return excludeProperties; + } + + /** + * Returns the value of {@link #includeParent}. + * If not set, it is assumed to be {@code true} + * + * @return value of {@link #includeParent} + */ + protected boolean isIncludeParent() + { + return includeParent; + } + + /** + * Returns the value of {@link #autoLinkItems} + * If not set, it is assumed to be {@code true} + * @return value of {@link #autoLinkItems} + */ + protected boolean isAutoLinkItems() + { + return autoLinkItems; + } + + /** + * Returns a new {@link Builder} instance + * @return new {@link Builder} instance + */ + public static Builder builder() + { + return new Builder(); + } + + /** + * Builder class for {@link VersionPropertiesMapRequest} + */ + public static class Builder + { + private MavenProject mavenProject; + private Property[] propertyDefinitions; + private String includeProperties; + private String excludeProperties; + private Boolean includeParent; + private Boolean autoLinkItems; + + private Builder() + { + } + + /** + * Supplies the {@link MavenProject} instance + * @param mavenProject {@link MavenProject} instance + * @return {@link Builder} instance + */ + public Builder withMavenProject( MavenProject mavenProject ) + { + this.mavenProject = mavenProject; + return this; + } + + /** + * Supplies the {@link MavenProject} instance + * @param propertyDefinitions array of property definitions + * @return {@link Builder} instance + */ + public Builder withPropertyDefinitions( Property[] propertyDefinitions ) + { + this.propertyDefinitions = propertyDefinitions; + return this; + } + + /** + * Supplies the properties to include + * @param includeProperties comma-delimited properties to include + * @return {@link Builder} instance + */ + public Builder withIncludeProperties( String includeProperties ) + { + this.includeProperties = includeProperties; + return this; + } + + /** + * Supplies the properties to exclude + * @param excludeProperties comma-delimited properties to exclude + * @return {@link Builder} instance + */ + public Builder withExcludeProperties( String excludeProperties ) + { + this.excludeProperties = excludeProperties; + return this; + } + + /** + * Supplies the includeParent parameter (whether parent POMs should be included) + * @param includeParent whether parent POMs should be included + * @return {@link Builder} instance + */ + public Builder withIncludeParent( boolean includeParent ) + { + this.includeParent = includeParent; + return this; + } + + /** + * Supplies the information whether to automatically infer associations + * @param autoLinkItems whether to automatically infer associations + * @return {@link Builder} instance + */ + public Builder withAutoLinkItems( boolean autoLinkItems ) + { + this.autoLinkItems = autoLinkItems; + return this; + } + + /** + * Returns the {@link VersionPropertiesMapRequest} instance + * @return {@link VersionPropertiesMapRequest} instance + */ + public VersionPropertiesMapRequest build() + { + VersionPropertiesMapRequest instance = new VersionPropertiesMapRequest(); + instance.mavenProject = this.mavenProject; + instance.propertyDefinitions = propertyDefinitions; + instance.includeProperties = includeProperties; + instance.excludeProperties = excludeProperties; + instance.includeParent = includeParent == null || includeParent; + instance.autoLinkItems = autoLinkItems == null || autoLinkItems; + return instance; + } + } + } + /** * Attempts to resolve the artifact. * diff --git a/src/test/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojoTest.java b/src/test/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojoTest.java index be01de3bd..f306faed9 100644 --- a/src/test/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojoTest.java +++ b/src/test/java/org/codehaus/mojo/versions/DisplayPropertyUpdatesMojoTest.java @@ -35,6 +35,7 @@ import static org.apache.commons.codec.CharEncoding.UTF_8; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.matchesPattern; +import static org.hamcrest.Matchers.not; /** * Unit tests for {@link DisplayPropertyUpdatesMojo} @@ -85,4 +86,25 @@ public void testPropertiesFromParent() throws Exception assertThat( String.join( "", Files.readAllLines( tempFile ) ), matchesPattern( ".*\\$\\{ver} \\.* 1\\.0\\.0 -> 2\\.0\\.0.*" ) ); } + + @Test + public void testDisablePropertiesFromParent() throws Exception + { + Path tempFile = Files.createTempFile( tempDir, "output", "" ); + + TestUtils.copyDir( Paths.get( "src/test/resources/org/codehaus/mojo/display-property-updates/issue-367" ), + tempDir ); + DisplayPropertyUpdatesMojo mojo = + (DisplayPropertyUpdatesMojo) mojoRule.lookupConfiguredMojo( tempDir.resolve( "child" ).toFile(), + "display-property-updates" ); + mojo.outputEncoding = UTF_8; + mojo.outputFile = tempFile.toFile(); + mojo.setPluginContext( new HashMap<>() ); + mojo.artifactMetadataSource = MockUtils.mockArtifactMetadataSource(); + mojo.includeParent = false; + mojo.execute(); + + assertThat( String.join( "", Files.readAllLines( tempFile ) ), + not( matchesPattern( ".*\\$\\{ver} \\.* 1\\.0\\.0 -> 2\\.0\\.0.*" ) ) ); + } } diff --git a/src/test/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojoTest.java b/src/test/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojoTest.java new file mode 100644 index 000000000..4d498592d --- /dev/null +++ b/src/test/java/org/codehaus/mojo/versions/PropertyUpdatesReportMojoTest.java @@ -0,0 +1,98 @@ +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.ByteArrayOutputStream; +import java.io.File; +import java.io.OutputStream; +import java.util.Locale; + +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.doxia.tools.SiteTool; +import org.apache.maven.plugin.testing.AbstractMojoTestCase; +import org.apache.maven.plugin.testing.MojoRule; +import org.apache.maven.plugin.testing.stubs.StubArtifactRepository; +import org.junit.Rule; +import org.junit.Test; + +import static org.codehaus.mojo.versions.utils.MockUtils.mockArtifactMetadataSource; +import static org.codehaus.mojo.versions.utils.MockUtils.mockSiteTool; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.not; + +/** + * Unit tests for {@link PropertyUpdatesReportMojo} + */ +public class PropertyUpdatesReportMojoTest extends AbstractMojoTestCase +{ + @Rule + public MojoRule mojoRule = new MojoRule( this ); + private static final ArtifactMetadataSource ARTIFACT_METADATA_SOURCE = mockArtifactMetadataSource(); + private static final SiteTool SITE_TOOL = mockSiteTool(); + private static final StubArtifactRepository LOCAL_REPOSITORY = new StubArtifactRepository( "" ); + + @Test + public void testIncludeParentTrueShouldContainProperty() throws Exception + { + OutputStream os = new ByteArrayOutputStream(); + SinkFactory sinkFactory = new Xhtml5SinkFactory(); + + PropertyUpdatesReportMojo mojo = + (PropertyUpdatesReportMojo) mojoRule.lookupConfiguredMojo( + new File( "src/test/resources/org/codehaus/mojo/display-property-updates/issue-367/child" ), + "property-updates-report" ); + setVariableValueToObject( mojo, "localRepository", LOCAL_REPOSITORY ); + setVariableValueToObject( mojo, "siteTool", SITE_TOOL ); + setVariableValueToObject( mojo, "artifactMetadataSource", ARTIFACT_METADATA_SOURCE ); + setVariableValueToObject( mojo, "includeParent", true ); + mojo.generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() ); + + String output = os.toString() + .replaceAll( "<[^>]+>", " " ) + .replaceAll( "&[^;]+;", " " ) + .replaceAll( "\\s+", " " ); + assertThat( output, containsString( "${ver}" ) ); + } + + @Test + public void testIncludeParentFalseShouldNotCountainProperty() throws Exception + { + OutputStream os = new ByteArrayOutputStream(); + SinkFactory sinkFactory = new Xhtml5SinkFactory(); + + PropertyUpdatesReportMojo mojo = + (PropertyUpdatesReportMojo) mojoRule.lookupConfiguredMojo( + new File( "src/test/resources/org/codehaus/mojo/display-property-updates/issue-367/child" ), + "property-updates-report" ); + setVariableValueToObject( mojo, "localRepository", new StubArtifactRepository( "" ) ); + setVariableValueToObject( mojo, "siteTool", SITE_TOOL ); + setVariableValueToObject( mojo, "artifactMetadataSource", ARTIFACT_METADATA_SOURCE ); + setVariableValueToObject( mojo, "includeParent", false ); + mojo.generate( sinkFactory.createSink( os ), sinkFactory, Locale.getDefault() ); + + String output = os.toString() + .replaceAll( "<[^>]+>", " " ) + .replaceAll( "&[^;]+;", " " ) + .replaceAll( "\\s+", " " ); + assertThat( output, not( containsString( "${ver}" ) ) ); + } +} diff --git a/src/test/java/org/codehaus/mojo/versions/api/DefaultVersionsHelperTest.java b/src/test/java/org/codehaus/mojo/versions/api/DefaultVersionsHelperTest.java index 89fcc92b4..0ccf473e9 100644 --- a/src/test/java/org/codehaus/mojo/versions/api/DefaultVersionsHelperTest.java +++ b/src/test/java/org/codehaus/mojo/versions/api/DefaultVersionsHelperTest.java @@ -182,7 +182,14 @@ public void testMVERSIONS159ExcludedAndNotIncluded() }; // should not throw an IllegalStateException Map result = - helper.getVersionPropertiesMap( project, propertyDefinitions, "foo.version", "bar.version", false ); + helper.getVersionPropertiesMap( VersionsHelper.VersionPropertiesMapRequest.builder() + .withMavenProject( project ) + .withPropertyDefinitions( propertyDefinitions ) + .withIncludeProperties( "foo.version" ) + .withExcludeProperties( "bar.version" ) + .withIncludeParent( false ) + .withAutoLinkItems( false ) + .build() ); assertTrue( result.isEmpty() ); }