From aef1cc35f5556c37ebb5972117693bbb36b9a0fb Mon Sep 17 00:00:00 2001 From: Benjamin Bentmann Date: Sun, 9 Nov 2008 18:42:27 +0000 Subject: [PATCH] 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 --- .../maven-it-plugin-configuration/pom.xml | 10 - .../org/apache/maven/plugin/coreit/Bean.java | 43 ++ .../maven/plugin/coreit/ConfigMojo.java | 377 ++++++++++++++++++ .../coreit/PluginConfigurationEmitter.java | 158 -------- 4 files changed, 420 insertions(+), 168 deletions(-) create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/Bean.java create mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/ConfigMojo.java delete mode 100644 its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/PluginConfigurationEmitter.java diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/pom.xml b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/pom.xml index 39ca156d75..3220b29b52 100644 --- a/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/pom.xml +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/pom.xml @@ -44,16 +44,6 @@ under the License. maven-plugin-api 2.0 - - org.apache.maven - maven-project - 2.0 - - - org.codehaus.plexus - plexus-utils - 1.4 - org.codehaus.plexus plexus-container-default diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/Bean.java b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/Bean.java new file mode 100644 index 0000000000..be3f2fa0f6 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/Bean.java @@ -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; + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/ConfigMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/ConfigMojo.java new file mode 100644 index 0000000000..ac5880c20a --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/ConfigMojo.java @@ -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 null. + */ + 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() ); + } + } + +} diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/PluginConfigurationEmitter.java b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/PluginConfigurationEmitter.java deleted file mode 100644 index 4b00478418..0000000000 --- a/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/PluginConfigurationEmitter.java +++ /dev/null @@ -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 ); - } - } -}