o Extended plugin to allow more thorough/easier testing of effective plugin configuration

git-svn-id: https://svn.apache.org/repos/asf/maven/core-integration-testing/trunk@712530 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benjamin Bentmann 2008-11-09 18:42:27 +00:00
parent 7036642c05
commit aef1cc35f5
4 changed files with 420 additions and 168 deletions

View File

@ -44,16 +44,6 @@ under the License.
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>

View File

@ -0,0 +1,43 @@
package org.apache.maven.plugin.coreit;
/*
* 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.
*/
/**
* A bean to receive mojo configuration.
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class Bean
{
String fieldParam;
String setterParam;
boolean setterCalled;
public void setSetterParam( String value )
{
setterParam = value;
setterCalled = true;
}
}

View File

@ -0,0 +1,377 @@
package org.apache.maven.plugin.coreit;
/*
* 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.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.configuration.PlexusConfiguration;
/**
* Dumps this mojo's configuration into a properties file.
*
* @goal config
* @phase validate
*
* @author Benjamin Bentmann
* @version $Id$
*/
public class ConfigMojo
extends AbstractMojo
{
/**
* The current project's base directory, used for path alignment.
*
* @parameter default-value="${basedir}"
* @readonly
*/
private File basedir;
/**
* The path to the properties file into which to save the mojo configuration. Unlike all the other parameters, this
* parameter features both a default value and an alias.
*
* @parameter alias="outputFile"
* expression="${config.propertiesFile} default-value="${project.build.directory}/config.properties"
*/
private File propertiesFile;
/**
* A simple parameter of type {@link java.lang.Boolean}.
*
* @parameter expression="${config.booleanParam}"
*/
private Boolean booleanParam;
/**
* A simple parameter of type {@link java.lang.Byte}.
*
* @parameter expression="${config.byteParam}"
*/
private Byte byteParam;
/**
* A simple parameter of type {@link java.lang.Short}.
*
* @parameter expression="${config.shortParam}"
*/
private Short shortParam;
/**
* A simple parameter of type {@link java.lang.Integer}.
*
* @parameter expression="${config.intergerParam}"
*/
private Integer integerParam;
/**
* A simple parameter of type {@link java.lang.Long}.
*
* @parameter expression="${config.longParam}"
*/
private Long longParam;
/**
* A simple parameter of type {@link java.lang.Float}.
*
* @parameter expression="${config.floatParam}"
*/
private Float floatParam;
/**
* A simple parameter of type {@link java.lang.Double}.
*
* @parameter expression="${config.doubleParam}"
*/
private Double doubleParam;
/**
* A simple parameter of type {@link java.lang.Character}.
*
* @parameter expression="${config.characterParam}"
*/
private Character characterParam;
/**
* A simple parameter of type {@link java.lang.String}.
*
* @parameter expression="${config.stringParam}"
*/
private String stringParam;
/**
* A simple parameter of type {@link java.io.File}.
*
* @parameter expression="${config.fileParam}"
*/
private File fileParam;
/**
* A simple parameter of type {@link java.util.Date}.
*
* @parameter expression="${config.dateParam}"
*/
private Date dateParam;
/**
* A simple parameter of type {@link java.net.URL}.
*
* @parameter expression="${config.urlParam}"
*/
private URL urlParam;
/**
* A simple parameter of type {@link java.net.URI} (requires Maven 3.x).
*
* @parameter
*/
private URI uriParam;
/**
* An array parameter of component type {@link java.lang.String}.
*
* @parameter
*/
private String[] stringParams;
/**
* A collection parameter of type {@link java.util.List}.
*
* @parameter
*/
private List listParam;
/**
* A collection parameter of type {@link java.util.Set}.
*
* @parameter
*/
private Set setParam;
/**
* A collection parameter of type {@link java.util.Map}.
*
* @parameter
*/
private Map mapParam;
/**
* A collection parameter of type {@link java.util.Properties}.
*
* @parameter
*/
private Properties propertiesParam;
/**
* A complex parameter of type {@link org.apache.maven.plugin.coreit.Bean}.
*
* @parameter
*/
private Bean beanParam;
/**
* A raw DOM snippet.
*
* @parameter
*/
private PlexusConfiguration domParam;
/**
* Runs this mojo.
*
* @throws MojoExecutionException If the output file could not be created.
*/
public void execute()
throws MojoExecutionException
{
getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + propertiesFile );
if ( propertiesFile == null )
{
throw new MojoExecutionException( "Path name for output file has not been specified" );
}
if ( !propertiesFile.isAbsolute() )
{
propertiesFile = new File( basedir, propertiesFile.getPath() ).getAbsoluteFile();
}
Properties configProps = new Properties();
dumpConfiguration( configProps );
getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + propertiesFile );
OutputStream out = null;
try
{
propertiesFile.getParentFile().mkdirs();
out = new FileOutputStream( propertiesFile );
configProps.store( out, "MAVEN-CORE-IT-LOG" );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Output file could not be created: " + propertiesFile, e );
}
finally
{
if ( out != null )
{
try
{
out.close();
}
catch ( IOException e )
{
// just ignore
}
}
}
getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + propertiesFile );
}
/**
* Dumps the mojo configuration into the specified properties.
*
* @param props The properties to dump the configuration into, must not be <code>null</code>.
*/
private void dumpConfiguration( Properties props )
{
dumpValue( props, "propertiesFile", propertiesFile );
dumpValue( props, "booleanParam", booleanParam );
dumpValue( props, "byteParam", byteParam );
dumpValue( props, "shortParam", shortParam );
dumpValue( props, "integerParam", integerParam );
dumpValue( props, "longParam", longParam );
dumpValue( props, "floatParam", floatParam );
dumpValue( props, "doubleParam", doubleParam );
dumpValue( props, "characterParam", characterParam );
dumpValue( props, "stringParam", stringParam );
dumpValue( props, "fileParam", fileParam );
dumpValue( props, "dateParam", dateParam );
dumpValue( props, "urlParam", urlParam );
dumpValue( props, "uriParam", uriParam );
dumpValue( props, "stringParams", stringParams );
dumpValue( props, "listParam", listParam );
dumpValue( props, "setParam", setParam );
dumpValue( props, "mapParam", mapParam );
dumpValue( props, "propertiesParam", propertiesParam );
dumpValue( props, "domParam", domParam );
if ( beanParam != null )
{
dumpValue( props, "beanParam.fieldParam", beanParam.fieldParam );
dumpValue( props, "beanParam.setterParam", beanParam.setterParam );
dumpValue( props, "beanParam.setterCalled", Boolean.valueOf( beanParam.setterCalled ) );
}
}
private void dumpValue( Properties props, String key, Object value )
{
if ( value != null && value.getClass().isArray() )
{
props.setProperty( key, Integer.toString( Array.getLength( value ) ) );
for ( int i = Array.getLength( value ) - 1; i >= 0; i-- )
{
dumpValue( props, key + "." + i, Array.get( value, i ) );
}
}
else if ( value instanceof Collection )
{
Collection collection = (Collection) value;
props.setProperty( key, Integer.toString( collection.size() ) );
int i = 0;
for ( Iterator it = collection.iterator(); it.hasNext(); i++ )
{
dumpValue( props, key + "." + i, it.next() );
}
}
else if ( value instanceof Map )
{
Map map = (Map) value;
props.setProperty( key, Integer.toString( map.size() ) );
int i = 0;
for ( Iterator it = map.keySet().iterator(); it.hasNext(); i++ )
{
Object k = it.next();
Object v = map.get( k );
dumpValue( props, key + "." + k, v );
}
}
else if ( value instanceof PlexusConfiguration )
{
PlexusConfiguration config = (PlexusConfiguration) value;
String val = config.getValue( null );
if ( val != null )
{
props.setProperty( key + ".value", val );
}
String[] attributes = config.getAttributeNames();
props.setProperty( key + ".attributes", Integer.toString( attributes.length ) );
for ( int i = attributes.length - 1; i >= 0; i-- )
{
props.setProperty( key + ".attributes." + attributes[i], config.getAttribute( attributes[i], "" ) );
}
PlexusConfiguration children[] = config.getChildren();
props.setProperty( key + ".children", Integer.toString( children.length ) );
Map indices = new HashMap();
for ( int i = children.length - 1; i >= 0; i-- )
{
PlexusConfiguration child = children[i];
String name = child.getName();
Integer index = (Integer) indices.get( name );
if ( index == null )
{
index = new Integer( 0 );
}
dumpValue( props, key + ".children." + name + "." + index, child );
indices.put( name, new Integer( index.intValue() + 1 ) );
}
}
else if ( value instanceof Date )
{
props.setProperty( key, new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ).format( (Date) value ) );
}
else if ( value != null )
{
props.setProperty( key, value.toString() );
}
}
}

View File

@ -1,158 +0,0 @@
package org.apache.maven.plugin.coreit;
/*
* 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.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.configuration.PlexusConfiguration;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Properties;
//MAPI: This is a canidate for the internal state dump (ISD). This is probably similar to what is in the help plugin.
// Compare:
// MavenProject
// PluginExpressionEvaluator
// Raw DOM
//
// Currently we don't have the means to easily test this inside the core. So this will be a model to drive
// making the core more testable.
/**
* @goal config
* @phase generate-resources
* @description Goal produces a raw string with contains full interpolated plugin configurations.
*/
public class PluginConfigurationEmitter
extends AbstractMojo
{
/**
* The MavenProject we will use for comparision.
*
* @parameter expression="${project}"
*/
private MavenProject project;
// How to enumerate all the possible expressions that can be used.
/**
* This is the raw interpolated DOM will be used for comparison.
*
* @parameter expression="${dom}"
*/
private PlexusConfiguration dom;
/** @parameter expression="${directory}" default-value="${project.build.directory}" */
private File directory;
/**
* Where to place the serialized version of the DOM for analysis.
*
* @parameter expression="${fileName}" default-value="interpolated-plugin-configuration.xml"
*/
private String fileName;
public void execute()
throws MojoExecutionException
{
if ( !directory.exists() )
{
directory.mkdirs();
}
emitMavenProjectValues();
emitExpressionEvaluatorValues();
emitRawDomValues();
}
private void emitMavenProjectValues()
throws MojoExecutionException
{
try
{
Properties p = new Properties();
p.setProperty( "project.build.directory", directory.getAbsolutePath() );
File file = new File( directory, "maven-project-output.txt" );
OutputStream os = new FileOutputStream( file );
p.store( os, "expression evaluator values" );
os.close();
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error writing out plugin configuration.", e );
}
}
private void emitExpressionEvaluatorValues()
throws MojoExecutionException
{
try
{
Properties p = new Properties();
p.setProperty( "project.build.directory", directory.getAbsolutePath() );
File file = new File( directory, "expression-evaluator-output.txt" );
OutputStream os = new FileOutputStream( file );
p.store( os, "expression evaluator values" );
os.close();
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error writing out plugin configuration.", e );
}
}
private void emitRawDomValues()
throws MojoExecutionException
{
try
{
File file = new File( directory, fileName );
Writer writer = new FileWriter( file );
writer.write( dom.toString() );
writer.close();
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error writing out plugin configuration.", e );
}
}
}