Skip to content

Commit

Permalink
Resolves #793: Added includeParent to DisplayPropertyUpdates and Prop…
Browse files Browse the repository at this point in the history
…ertyUpdatesReport
  • Loading branch information
jarmoniuk committed Oct 26, 2022
1 parent 1826b64 commit 8e8069c
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ protected void update( ModifiedPomXMLEventReader pom )
if ( updatePropertyVersions )
{
Map<Property, PropertyVersions> versionProperties =
this.getHelper().getVersionPropertiesMap( getProject(), null, null, null, true );
this.getHelper().getVersionPropertiesMap( getProject(), null, null, null,
true, true );
List<String> diff = updatePropertyVersions( pom, versionProperties, remoteDepsMap );
propertyDiffs.addAll( diff );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public class DisplayPropertyUpdatesMojo
@Parameter( property = "allowIncrementalUpdates", defaultValue = "true" )
private boolean allowIncrementalUpdates;

/**
* <p>Whether to include property updates from parent.</p>
*
* @since 2.14.0
*/
@Parameter( property = "includeParent", defaultValue = "true" )
protected boolean includeParent = true;

// -------------------------- STATIC METHODS --------------------------

// -------------------------- OTHER METHODS --------------------------
Expand All @@ -151,7 +159,7 @@ public void execute()

Map<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), properties, includeProperties, excludeProperties,
autoLinkItems );
includeParent, autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Property property = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public class PropertyUpdatesReportMojo extends AbstractVersionsReport<PropertyUp
@Parameter( property = "autoLinkItems", defaultValue = "true" )
private boolean autoLinkItems;

/**
* <p>Whether to include property updates from parent.</p>
*
* @since 2.14.0
*/
@Parameter( property = "includeParent", defaultValue = "true" )
private boolean includeParent = true;

@Inject
protected PropertyUpdatesReportMojo( I18N i18n, RepositorySystem repositorySystem,
ArtifactResolver artifactResolver,
Expand Down Expand Up @@ -121,7 +129,7 @@ protected void doGenerateReport( Locale locale, Sink sink )
try
{
updateSet.putAll( getHelper().getVersionPropertiesMap( getProject(), properties, includeProperties,
excludeProperties, autoLinkItems ) );
excludeProperties, includeParent, autoLinkItems ) );
}
catch ( MojoExecutionException e )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ private void resolvePropertyRanges( ModifiedPomXMLEventReader pom )
{

Map<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), null, includeProperties, excludeProperties, true );
this.getHelper().getVersionPropertiesMap( getProject(), null, includeProperties, excludeProperties,
true, true );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Property property = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private void update( ModifiedPomXMLEventReader pom, Property[] propertiesConfig,
{
Map<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), propertiesConfig, properties, "",
autoLinkItems );
true, autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Property currentProperty = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ protected void update( ModifiedPomXMLEventReader pom )
properties,
includeProperties,
excludeProperties,
true,
autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected void update( ModifiedPomXMLEventReader pom )
propertyConfig.setVersion( newVersion );
Map<Property, PropertyVersions> propertyVersions =
this.getHelper().getVersionPropertiesMap( getProject(), new Property[] {propertyConfig}, property, "",
autoLinkItems );
true, autoLinkItems );
for ( Map.Entry<Property, PropertyVersions> entry : propertyVersions.entrySet() )
{
Property property = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,9 @@ public ExpressionEvaluator getExpressionEvaluator( MavenProject project )
@Override
public Map<Property, PropertyVersions> getVersionPropertiesMap( MavenProject project,
Property[] propertyDefinitions,
String includeProperties, String excludeProperties,
String includeProperties,
String excludeProperties,
boolean includeParent,
boolean autoLinkItems )
throws MojoExecutionException
{
Expand All @@ -803,7 +805,7 @@ public Map<Property, PropertyVersions> getVersionPropertiesMap( MavenProject pro
final PropertyVersionsBuilder[] propertyVersionsBuilders;
try
{
propertyVersionsBuilders = PomHelper.getPropertyVersionsBuilders( this, project );
propertyVersionsBuilders = PomHelper.getPropertyVersionsBuilders( this, project, includeParent );
}
catch ( ExpressionEvaluationException | IOException e )
{
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/org/codehaus/mojo/versions/api/PomHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -1018,16 +1019,20 @@ private static Map<MavenProject, Model> 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<MavenProject, Model> reactorModels = getRawModelWithParents( project );
Map<MavenProject, Model> reactorModels = includeParent
? getRawModelWithParents( project )
: singletonMap( project, getRawModel( project ) );

Map<String, PropertyVersionsBuilder> propertiesMap = new TreeMap<>();

Expand Down Expand Up @@ -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 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,15 @@ PluginUpdatesDetails lookupPluginUpdates( Plugin plugin, boolean allowSnapshots
* @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 includeParent whether to include parent POMs
* @param autoLinkItems whether to automatically infer associations
* @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<Property, PropertyVersions> getVersionPropertiesMap( MavenProject project, Property[] propertyDefinitions,
String includeProperties, String excludeProperties,
boolean autoLinkItems )
boolean includeParent, boolean autoLinkItems )
throws MojoExecutionException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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.*" ) ) );
}
}
Original file line number Diff line number Diff line change
@@ -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}" ) ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ public void testMVERSIONS159ExcludedAndNotIncluded()
};
// should not throw an IllegalStateException
Map<Property, PropertyVersions> result =
helper.getVersionPropertiesMap( project, propertyDefinitions, "foo.version", "bar.version", false );
helper.getVersionPropertiesMap( project, propertyDefinitions, "foo.version", "bar.version",
false, false );
assertTrue( result.isEmpty() );
}

Expand Down

0 comments on commit 8e8069c

Please sign in to comment.