Skip to content

Commit

Permalink
Fixing mojohaus#315: Guarding against newVersion or property being em…
Browse files Browse the repository at this point in the history
…pty if the properties file is not provided
  • Loading branch information
andrzejj0 committed Sep 3, 2022
1 parent b551d90 commit 029d0e8
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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" ) );
}
}
}
Original file line number Diff line number Diff line change
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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>

0 comments on commit 029d0e8

Please sign in to comment.