Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing #315: Guarding against newVersion or property being empty if the properties file is not provided #667

Merged
merged 1 commit into from Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java
Expand Up @@ -22,7 +22,9 @@
import javax.xml.stream.XMLStreamException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -35,6 +37,8 @@
import org.codehaus.mojo.versions.rewriting.ModifiedPomXMLEventReader;
import org.codehaus.mojo.versions.utils.PropertiesVersionsFileReader;

import static org.apache.commons.lang3.StringUtils.isBlank;

/**
* Set a property to a given version without any sanity checks. Please be careful this can lead to changes which might
* not build anymore. The sanity checks are done by other goals like <code>update-properties</code> or
Expand Down Expand Up @@ -79,6 +83,31 @@ public class SetPropertyMojo
@Parameter( property = "propertiesVersionsFile" )
private String propertiesVersionsFile;

/**
* {@inheritDoc}
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
List<String> problems = new ArrayList<>();
if ( isBlank( propertiesVersionsFile ) )
{
if ( isBlank( newVersion ) )
{
problems.add( "newVersion must not be empty" );
}
if ( isBlank( property ) )
{
problems.add( "property must not be empty" );
}
}
if ( !problems.isEmpty() )
{
throw new MojoExecutionException( "Invalid execution arguments: " + String.join( ", ", problems ) );
}
super.execute();
}

/**
* @param pom the pom to update.
* @throws MojoExecutionException when things go wrong
Expand Down Expand Up @@ -120,7 +149,7 @@ else if ( !StringUtils.isEmpty( property ) )
propertyConfig.setVersion( newVersion );
return propertyConfig;
} )
.toArray( size -> new Property[size] );
.toArray( Property[]::new );
properties = property;
}
else
Expand Down
73 changes: 73 additions & 0 deletions src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java
@@ -0,0 +1,73 @@
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 org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.mojo.versions.utils.BaseMojoTestCase;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

/**
* Basic tests for {@linkplain SetPropertyMojoTest}.
*
* @author Andrzej Jarmoniuk
*/
public class SetPropertyMojoTest extends BaseMojoTestCase
{
@Test
public void testNullNewVersion()
throws Exception
{
SetPropertyMojo mojo = createMojo( "set-property",
"src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml" );
assertThat( mojo.getProject().getProperties(), is( mojo.getProject().getModel().getProperties() ) );
try
{
mojo.execute();
fail();
}
catch ( MojoExecutionException e )
{
assertThat( e.getMessage(),
containsString( "Invalid execution arguments: newVersion must not be empty" ) );
}
}

@Test
public void testNullProperty()
throws Exception
{
SetPropertyMojo mojo = createMojo( "set-property",
"src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml" );
try
{
mojo.execute();
fail();
}
catch ( MojoExecutionException e )
{
assertThat( e.getMessage(),
containsString( "Invalid execution arguments: property must not be empty" ) );
}
}
}
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Properties;

import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
Expand Down Expand Up @@ -58,6 +59,7 @@ public abstract class BaseMojoTestCase extends AbstractMojoTestCase
* @return a Mojo instance
* @throws Exception thrown if mojo lookup fails
*/
@SuppressWarnings( "unchecked" )
protected <T extends Mojo> T createMojo( String goal, String pomFilePath ) throws Exception
{
File pomFile = new File( pomFilePath );
Expand Down Expand Up @@ -109,5 +111,11 @@ public File getBasedir()
{
return pomFile.getParentFile();
}

@Override
public Properties getProperties()
{
return getModel().getProperties();
}
}
}
@@ -0,0 +1,31 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<properties>
<dummy-api-version>1.0.0</dummy-api-version>
</properties>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>${dummy-api-version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>versions-maven-plugin</artifactId>
<configuration>
<property>dummy-api-version</property>
</configuration>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,31 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>default-group</groupId>
<artifactId>default-artifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>

<properties>
<dummy-api-version>1.0.0</dummy-api-version>
</properties>

<dependencies>
<dependency>
<groupId>localhost</groupId>
<artifactId>dummy-api</artifactId>
<version>${dummy-api-version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>versions-maven-plugin</artifactId>
<configuration>
<newVersion>2.0.0</newVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>