diff --git a/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java b/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java index 13acc9414..ca3d6fbb4 100644 --- a/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java +++ b/src/main/java/org/codehaus/mojo/versions/SetPropertyMojo.java @@ -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; @@ -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 update-properties or @@ -79,6 +83,31 @@ public class SetPropertyMojo @Parameter( property = "propertiesVersionsFile" ) private String propertiesVersionsFile; + /** + * {@inheritDoc} + */ + @Override + public void execute() throws MojoExecutionException, MojoFailureException + { + List 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 @@ -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 diff --git a/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java b/src/test/java/org/codehaus/mojo/versions/SetPropertyMojoTest.java new file mode 100644 index 000000000..b3aecf2e2 --- /dev/null +++ b/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" ) ); + } + } +} diff --git a/src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java b/src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java index c9ed9c714..d8a0e04f9 100644 --- a/src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java +++ b/src/test/java/org/codehaus/mojo/versions/utils/BaseMojoTestCase.java @@ -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; @@ -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 createMojo( String goal, String pomFilePath ) throws Exception { File pomFile = new File( pomFilePath ); @@ -109,5 +111,11 @@ public File getBasedir() { return pomFile.getParentFile(); } + + @Override + public Properties getProperties() + { + return getModel().getProperties(); + } } } diff --git a/src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml b/src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml new file mode 100644 index 000000000..94e1f8d09 --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/set-property/null-new-version-pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + default-group + default-artifact + 1.0 + pom + + + 1.0.0 + + + + + localhost + dummy-api + ${dummy-api-version} + + + + + + + versions-maven-plugin + + dummy-api-version + + + + + \ No newline at end of file diff --git a/src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml b/src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml new file mode 100644 index 000000000..b429aa3b7 --- /dev/null +++ b/src/test/resources/org/codehaus/mojo/set-property/null-property-pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + default-group + default-artifact + 1.0 + pom + + + 1.0.0 + + + + + localhost + dummy-api + ${dummy-api-version} + + + + + + + versions-maven-plugin + + 2.0.0 + + + + + \ No newline at end of file