Author: bentmann Date: Sat Aug 9 06:09:27 2008 New Revision: 684253 URL: http://svn.apache.org/viewvc?rev=684253&view=rev Log: o Refactored code to ease maintenance
Added: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerProperties.java (with props) maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerPropertiesTest.java (with props) Modified: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerMojoTest.java Modified: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java?rev=684253&r1=684252&r2=684253&view=diff ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java (original) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerMojo.java Sat Aug 9 06:09:27 2008 @@ -743,14 +743,15 @@ private void runBuild( String project, File basedir, File pomFile, List failures ) throws MojoExecutionException { - Properties invokerProperties = getInvokerProperties( basedir ); - if ( getLog().isDebugEnabled() && !invokerProperties.isEmpty() ) + InvokerProperties invokerProperties = getInvokerProperties( basedir ); + if ( getLog().isDebugEnabled() && !invokerProperties.getProperties().isEmpty() ) { + Properties props = invokerProperties.getProperties(); getLog().debug( "Using invoker properties:" ); - for ( Iterator it = new TreeSet( invokerProperties.keySet() ).iterator(); it.hasNext(); ) + for ( Iterator it = new TreeSet( props.keySet() ).iterator(); it.hasNext(); ) { String key = (String) it.next(); - String value = invokerProperties.getProperty( key ); + String value = props.getProperty( key ); getLog().debug( " " + key + " = " + value ); } } @@ -812,8 +813,7 @@ for ( int invocationIndex = 1;; invocationIndex++ ) { - if ( invocationIndex > 1 - && invokerProperties.getProperty( "invoker.goals." + invocationIndex ) == null ) + if ( invocationIndex > 1 && !invokerProperties.isInvocationDefined( invocationIndex ) ) { break; } @@ -824,7 +824,7 @@ request.setMavenOpts( mavenOpts ); - configureInvocation( request, invocationIndex, invokerProperties ); + invokerProperties.configureInvocation( request, invocationIndex ); try { @@ -852,10 +852,6 @@ return; } - final boolean nonZeroExit = - "failure".equalsIgnoreCase( getInvokerProperty( invokerProperties, "invoker.buildResult", - invocationIndex ) ); - if ( result.getExecutionException() != null ) { if ( !suppressSummaries ) @@ -879,7 +875,7 @@ return; } - else if ( ( result.getExitCode() != 0 ) != nonZeroExit ) + else if ( !invokerProperties.isExpectedResult( result.getExitCode(), invocationIndex ) ) { if ( !suppressSummaries ) { @@ -1485,7 +1481,7 @@ * @return The invoker properties, may be empty but never <code>null</code>. * @throws MojoExecutionException If an I/O error occurred during reading the properties. */ - private Properties getInvokerProperties( final File projectDirectory ) + private InvokerProperties getInvokerProperties( final File projectDirectory ) throws MojoExecutionException { Properties props = new Properties(); @@ -1529,74 +1525,7 @@ props.setProperty( key, value ); } } - return props; - } - - /** - * Configures the specified invocation request from the given invoker properties. Settings not present in the - * invoker properties will be left unchanged in the invocation request. - * - * @param request The invocation request to configure, must not be <code>null</code>. - * @param index The one-based index of the invocation to configure, must be positive. - * @param properties The invoker properties used to configure the invocation, must not be <code>null</code>. - */ - private void configureInvocation( InvocationRequest request, int index, Properties properties ) - { - if ( index < 1 ) - { - throw new IllegalArgumentException( "invalid invocation index: " + index ); - } - - String goals = getInvokerProperty( properties, "invoker.goals", index ); - if ( goals != null ) - { - request.setGoals( new ArrayList( Arrays.asList( goals.split( "[,\\s]+" ) ) ) ); - } - - String profiles = getInvokerProperty( properties, "invoker.profiles", index ); - if ( profiles != null ) - { - request.setProfiles( new ArrayList( Arrays.asList( profiles.split( "[,\\s]+" ) ) ) ); - } - - String mvnOpts = getInvokerProperty( properties, "invoker.mavenOpts", index ); - if ( mvnOpts != null ) - { - request.setMavenOpts( mvnOpts ); - } - - String failureBehavior = getInvokerProperty( properties, "invoker.failureBehavior", index ); - if ( failureBehavior != null ) - { - request.setFailureBehavior( failureBehavior ); - } - - String nonRecursive = getInvokerProperty( properties, "invoker.nonRecursive", index ); - if ( nonRecursive != null ) - { - request.setRecursive( !Boolean.valueOf( nonRecursive ).booleanValue() ); - } - } - - /** - * Gets a value from the invoker properties. The invoker properties are intended to describe the invocation settings - * for multiple builds of the same project. For this reason, the properties are indexed. First, a property named - * <code>key.index</code> will be queried. If this property does not exist, the value of the property named - * <code>key</code> will finally be returned. - * - * @param properties The invoker properties from which to lookup the value, must not be <code>null</code>. - * @param key The (base) key for the invoker property to lookup, must not be <code>null</code>. - * @param index The index of the invocation for which to retrieve the value, must not be negative. - * @return The value for the requested invoker property or <code>null</code> if not defined. - */ - static String getInvokerProperty( Properties properties, String key, int index ) - { - String value = properties.getProperty( key + '.' + index ); - if ( value == null ) - { - value = properties.getProperty( key ); - } - return value; + return new InvokerProperties( props ); } } Added: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerProperties.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerProperties.java?rev=684253&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerProperties.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerProperties.java Sat Aug 9 06:09:27 2008 @@ -0,0 +1,153 @@ +package org.apache.maven.plugin.invoker; + +/* + * 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.util.ArrayList; +import java.util.Arrays; +import java.util.Properties; + +import org.apache.maven.shared.invoker.InvocationRequest; + +/** + * Provides a convenient facade around the <code>invoker.properties</code>. + * + * @author Benjamin Bentmann + * @version $Id$ + */ +class InvokerProperties +{ + + /** + * The invoker properties being wrapped, never <code>null</code>. + */ + private final Properties properties; + + /** + * Creates a new facade for the specified invoker properties. The properties will not be copied, so any changes to + * them will be reflected by the facade. + * + * @param properties The invoker properties to wrap, may be <code>null</code> if none. + */ + public InvokerProperties( Properties properties ) + { + this.properties = ( properties != null ) ? properties : new Properties(); + } + + /** + * Gets the invoker properties being wrapped. + * + * @return The invoker properties being wrapped, never <code>null</code>. + */ + public Properties getProperties() + { + return this.properties; + } + + /** + * Determines whether these invoker properties contain a build definition for the specified invocation index. + * + * @param index The one-based index of the invocation to check for, must not be negative. + * @return <code>true</code> if the invocation with the specified index is defined, <code>false</code> otherwise. + */ + public boolean isInvocationDefined( int index ) + { + return properties.getProperty( "invoker.goals." + index ) != null; + } + + /** + * Configures the specified invocation request from these invoker properties. Settings not present in the invoker + * properties will be left unchanged in the invocation request. + * + * @param request The invocation request to configure, must not be <code>null</code>. + * @param index The one-based index of the invocation to configure, must not be negative. + */ + public void configureInvocation( InvocationRequest request, int index ) + { + String goals = get( "invoker.goals", index ); + if ( goals != null ) + { + request.setGoals( new ArrayList( Arrays.asList( goals.trim().split( "[,\\s]+" ) ) ) ); + } + + String profiles = get( "invoker.profiles", index ); + if ( profiles != null ) + { + request.setProfiles( new ArrayList( Arrays.asList( profiles.trim().split( "[,\\s]+" ) ) ) ); + } + + String mvnOpts = get( "invoker.mavenOpts", index ); + if ( mvnOpts != null ) + { + request.setMavenOpts( mvnOpts ); + } + + String failureBehavior = get( "invoker.failureBehavior", index ); + if ( failureBehavior != null ) + { + request.setFailureBehavior( failureBehavior ); + } + + String nonRecursive = get( "invoker.nonRecursive", index ); + if ( nonRecursive != null ) + { + request.setRecursive( !Boolean.valueOf( nonRecursive ).booleanValue() ); + } + } + + /** + * Checks whether the specified exit code matches the one expected for the given invocation. + * + * @param exitCode The exit code of the Maven invocation to check. + * @param index The index of the invocation for which to check the exit code, must not be negative. + * @return <code>true</code> if the exit code is zero and a success was expected or if the exit code is non-zero and + * a failue was expected, <code>false</code> otherwise. + */ + public boolean isExpectedResult( int exitCode, int index ) + { + boolean nonZeroExit = "failure".equalsIgnoreCase( get( "invoker.buildResult", index ) ); + return ( exitCode != 0 ) == nonZeroExit; + } + + /** + * Gets a value from the invoker properties. The invoker properties are intended to describe the invocation settings + * for multiple builds of the same project. For this reason, the properties are indexed. First, a property named + * <code>key.index</code> will be queried. If this property does not exist, the value of the property named + * <code>key</code> will finally be returned. + * + * @param key The (base) key for the invoker property to lookup, must not be <code>null</code>. + * @param index The index of the invocation for which to retrieve the value, must not be negative. + * @return The value for the requested invoker property or <code>null</code> if not defined. + */ + String get( String key, int index ) + { + if ( index < 0 ) + { + throw new IllegalArgumentException( "invalid invocation index: " + index ); + } + + String value = properties.getProperty( key + '.' + index ); + if ( value == null ) + { + value = properties.getProperty( key ); + } + return value; + } + +} Propchange: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerProperties.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-invoker-plugin/src/main/java/org/apache/maven/plugin/invoker/InvokerProperties.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Modified: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerMojoTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerMojoTest.java?rev=684253&r1=684252&r2=684253&view=diff ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerMojoTest.java (original) +++ maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerMojoTest.java Sat Aug 9 06:09:27 2008 @@ -24,7 +24,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Properties; import org.apache.maven.plugin.testing.AbstractMojoTestCase; import org.apache.maven.plugin.testing.stubs.MavenProjectStub; @@ -32,7 +31,6 @@ import org.apache.maven.shared.invoker.Invoker; import org.codehaus.plexus.util.FileUtils; - /** * @author <a href="mailto:[EMAIL PROTECTED]">olamy</a> * @since 18 nov. 07 @@ -206,18 +204,4 @@ assertTrue( new File( cloneProjectsTo, "no-pom/build.log" ).isFile() ); } - public void testGetInvokerProperty() - { - Properties props = new Properties(); - - assertNull( InvokerMojo.getInvokerProperty( props, "undefined-key", 0 ) ); - - props.setProperty( "key", "value" ); - assertEquals( "value", InvokerMojo.getInvokerProperty( props, "key", 1 ) ); - - props.setProperty( "key.1", "another-value" ); - assertEquals( "another-value", InvokerMojo.getInvokerProperty( props, "key", 1 ) ); - assertEquals( "value", InvokerMojo.getInvokerProperty( props, "key", 2 ) ); - } - } Added: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerPropertiesTest.java URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerPropertiesTest.java?rev=684253&view=auto ============================================================================== --- maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerPropertiesTest.java (added) +++ maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerPropertiesTest.java Sat Aug 9 06:09:27 2008 @@ -0,0 +1,182 @@ +package org.apache.maven.plugin.invoker; + +/* + * 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.util.Arrays; +import java.util.Collections; +import java.util.Properties; + +import org.apache.maven.shared.invoker.DefaultInvocationRequest; +import org.apache.maven.shared.invoker.InvocationRequest; + +import junit.framework.TestCase; + +/** + * Tests the invoker properties facade. + * + * @author Benjamin Bentmann + * @version $Id$ + */ +public class InvokerPropertiesTest + extends TestCase +{ + + public void testConstructorNullSafe() + throws Exception + { + InvokerProperties facade = new InvokerProperties( null ); + assertNotNull( facade.getProperties() ); + } + + public void testGetInvokerProperty() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + assertNull( facade.get( "undefined-key", 0 ) ); + + props.setProperty( "key", "value" ); + assertEquals( "value", facade.get( "key", 1 ) ); + + props.setProperty( "key.1", "another-value" ); + assertEquals( "another-value", facade.get( "key", 1 ) ); + assertEquals( "value", facade.get( "key", 2 ) ); + } + + public void testIsExpectedResult() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + assertTrue( facade.isExpectedResult( 0, 0 ) ); + assertFalse( facade.isExpectedResult( 1, 0 ) ); + + props.setProperty( "invoker.buildResult", "success" ); + assertTrue( facade.isExpectedResult( 0, 0 ) ); + assertFalse( facade.isExpectedResult( 1, 0 ) ); + + props.setProperty( "invoker.buildResult", "failure" ); + assertFalse( facade.isExpectedResult( 0, 0 ) ); + assertTrue( facade.isExpectedResult( 1, 0 ) ); + } + + public void testConfigureRequestGoals() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setGoals( Collections.singletonList( "test" ) ); + facade.configureInvocation( request, 0 ); + assertEquals( Collections.singletonList( "test" ), request.getGoals() ); + + props.setProperty( "invoker.goals", "verify" ); + facade.configureInvocation( request, 0 ); + assertEquals( Collections.singletonList( "verify" ), request.getGoals() ); + + props.setProperty( "invoker.goals", " clean , test verify " ); + facade.configureInvocation( request, 0 ); + assertEquals( Arrays.asList( new String[] { "clean", "test", "verify" } ), request.getGoals() ); + } + + public void testConfigureRequestProfiles() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setProfiles( Collections.singletonList( "test" ) ); + facade.configureInvocation( request, 0 ); + assertEquals( Collections.singletonList( "test" ), request.getProfiles() ); + + props.setProperty( "invoker.profiles", "verify" ); + facade.configureInvocation( request, 0 ); + assertEquals( Collections.singletonList( "verify" ), request.getProfiles() ); + + props.setProperty( "invoker.profiles", " clean , test verify " ); + facade.configureInvocation( request, 0 ); + assertEquals( Arrays.asList( new String[] { "clean", "test", "verify" } ), request.getProfiles() ); + } + + public void testConfigureRequestMavenOpts() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setMavenOpts( "default" ); + facade.configureInvocation( request, 0 ); + assertEquals( "default", request.getMavenOpts() ); + + props.setProperty( "invoker.mavenOpts", "-Xmx512m" ); + facade.configureInvocation( request, 0 ); + assertEquals( "-Xmx512m", request.getMavenOpts() ); + } + + public void testConfigureRequestFailureBehavior() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setFailureBehavior( "fail-at-end" ); + facade.configureInvocation( request, 0 ); + assertEquals( "fail-at-end", request.getFailureBehavior() ); + + props.setProperty( "invoker.failureBehavior", "fail-never" ); + facade.configureInvocation( request, 0 ); + assertEquals( "fail-never", request.getFailureBehavior() ); + } + + public void testConfigureRequestRecursion() + throws Exception + { + Properties props = new Properties(); + InvokerProperties facade = new InvokerProperties( props ); + + InvocationRequest request = new DefaultInvocationRequest(); + + request.setRecursive( true ); + facade.configureInvocation( request, 0 ); + assertTrue( request.isRecursive() ); + request.setRecursive( false ); + facade.configureInvocation( request, 0 ); + assertFalse( request.isRecursive() ); + + props.setProperty( "invoker.nonRecursive", "true" ); + facade.configureInvocation( request, 0 ); + assertFalse( request.isRecursive() ); + + props.setProperty( "invoker.nonRecursive", "false" ); + facade.configureInvocation( request, 0 ); + assertTrue( request.isRecursive() ); + } + +} Propchange: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerPropertiesTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: maven/plugins/trunk/maven-invoker-plugin/src/test/java/org/apache/maven/plugin/invoker/InvokerPropertiesTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision