OPEN - issue MNG-2591: Plugins are merged incorrectly

http://jira.codehaus.org/browse/MNG-2591

Added tests to verify the XML attribute that switches merge-mode from replace/merge to append for children...then, fixed append semantics to force the dominant children (those given by the child POM, in this case) to be appended to those of the recessive/parent-POM configuration.

git-svn-id: https://svn.apache.org/repos/asf/maven/components/trunk@545315 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
John Dennis Casey 2007-06-07 21:11:29 +00:00
parent 7db88ddfaf
commit 8ec3ac8a97
1 changed files with 108 additions and 49 deletions

View File

@ -26,7 +26,11 @@ import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginContainer; import org.apache.maven.model.PluginContainer;
import org.apache.maven.model.PluginExecution; import org.apache.maven.model.PluginExecution;
import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import java.io.IOException;
import java.io.StringReader;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -35,108 +39,108 @@ import java.util.Map;
public class ModelUtilsTest public class ModelUtilsTest
extends TestCase extends TestCase
{ {
public void testShouldNotInheritPluginWithInheritanceSetToFalse() public void testShouldNotInheritPluginWithInheritanceSetToFalse()
{ {
PluginContainer parent = new PluginContainer(); PluginContainer parent = new PluginContainer();
Plugin parentPlugin = createPlugin( "group", "artifact", "1.0", Collections.EMPTY_MAP ); Plugin parentPlugin = createPlugin( "group", "artifact", "1.0", Collections.EMPTY_MAP );
parentPlugin.setInherited( "false" ); parentPlugin.setInherited( "false" );
parent.addPlugin( parentPlugin ); parent.addPlugin( parentPlugin );
PluginContainer child = new PluginContainer(); PluginContainer child = new PluginContainer();
child.addPlugin( createPlugin( "group3", "artifact3", "1.0", Collections.EMPTY_MAP ) ); child.addPlugin( createPlugin( "group3", "artifact3", "1.0", Collections.EMPTY_MAP ) );
ModelUtils.mergePluginLists( child, parent, true ); ModelUtils.mergePluginLists( child, parent, true );
List results = child.getPlugins(); List results = child.getPlugins();
assertEquals( 1, results.size() ); assertEquals( 1, results.size() );
Plugin result1 = (Plugin) results.get( 0 ); Plugin result1 = (Plugin) results.get( 0 );
assertEquals( "group3", result1.getGroupId() ); assertEquals( "group3", result1.getGroupId() );
assertEquals( "artifact3", result1.getArtifactId() ); assertEquals( "artifact3", result1.getArtifactId() );
} }
/** /**
* Test that this is the resulting ordering of plugins after merging: * Test that this is the resulting ordering of plugins after merging:
* *
* Given: * Given:
* *
* parent: X -> A -> B -> D -> E * parent: X -> A -> B -> D -> E
* child: Y -> A -> C -> D -> F * child: Y -> A -> C -> D -> F
* *
* Result: * Result:
* *
* X -> Y -> A -> B -> C -> D -> E -> F * X -> Y -> A -> B -> C -> D -> E -> F
*/ */
public void testShouldPreserveChildOrderingOfPluginsAfterParentMerge() public void testShouldPreserveChildOrderingOfPluginsAfterParentMerge()
{ {
PluginContainer parent = new PluginContainer(); PluginContainer parent = new PluginContainer();
parent.addPlugin( createPlugin( "group", "artifact", "1.0", Collections.EMPTY_MAP ) ); parent.addPlugin( createPlugin( "group", "artifact", "1.0", Collections.EMPTY_MAP ) );
parent.addPlugin( createPlugin( "group2", "artifact2", "1.0", Collections.singletonMap( "key", "value" ) ) ); parent.addPlugin( createPlugin( "group2", "artifact2", "1.0", Collections.singletonMap( "key", "value" ) ) );
PluginContainer child = new PluginContainer(); PluginContainer child = new PluginContainer();
child.addPlugin( createPlugin( "group3", "artifact3", "1.0", Collections.EMPTY_MAP ) ); child.addPlugin( createPlugin( "group3", "artifact3", "1.0", Collections.EMPTY_MAP ) );
child.addPlugin( createPlugin( "group2", "artifact2", "1.0", Collections.singletonMap( "key2", "value2" ) ) ); child.addPlugin( createPlugin( "group2", "artifact2", "1.0", Collections.singletonMap( "key2", "value2" ) ) );
ModelUtils.mergePluginLists( child, parent, true ); ModelUtils.mergePluginLists( child, parent, true );
List results = child.getPlugins(); List results = child.getPlugins();
assertEquals( 3, results.size() ); assertEquals( 3, results.size() );
Plugin result1 = (Plugin) results.get( 0 ); Plugin result1 = (Plugin) results.get( 0 );
assertEquals( "group", result1.getGroupId() ); assertEquals( "group", result1.getGroupId() );
assertEquals( "artifact", result1.getArtifactId() ); assertEquals( "artifact", result1.getArtifactId() );
Plugin result2 = (Plugin) results.get( 1 ); Plugin result2 = (Plugin) results.get( 1 );
assertEquals( "group3", result2.getGroupId() ); assertEquals( "group3", result2.getGroupId() );
assertEquals( "artifact3", result2.getArtifactId() ); assertEquals( "artifact3", result2.getArtifactId() );
Plugin result3 = (Plugin) results.get( 2 ); Plugin result3 = (Plugin) results.get( 2 );
assertEquals( "group2", result3.getGroupId() ); assertEquals( "group2", result3.getGroupId() );
assertEquals( "artifact2", result3.getArtifactId() ); assertEquals( "artifact2", result3.getArtifactId() );
Xpp3Dom result3Config = (Xpp3Dom) result3.getConfiguration(); Xpp3Dom result3Config = (Xpp3Dom) result3.getConfiguration();
assertNotNull( result3Config ); assertNotNull( result3Config );
assertEquals( "value", result3Config.getChild( "key" ).getValue() ); assertEquals( "value", result3Config.getChild( "key" ).getValue() );
assertEquals( "value2", result3Config.getChild( "key2" ).getValue() ); assertEquals( "value2", result3Config.getChild( "key2" ).getValue() );
} }
private Plugin createPlugin( String groupId, String artifactId, String version, Map configuration ) private Plugin createPlugin( String groupId, String artifactId, String version, Map configuration )
{ {
Plugin plugin = new Plugin(); Plugin plugin = new Plugin();
plugin.setGroupId( groupId ); plugin.setGroupId( groupId );
plugin.setArtifactId( artifactId ); plugin.setArtifactId( artifactId );
plugin.setVersion( version ); plugin.setVersion( version );
Xpp3Dom config = new Xpp3Dom( "configuration" ); Xpp3Dom config = new Xpp3Dom( "configuration" );
if( configuration != null ) if( configuration != null )
{ {
for ( Iterator it = configuration.entrySet().iterator(); it.hasNext(); ) for ( Iterator it = configuration.entrySet().iterator(); it.hasNext(); )
{ {
Map.Entry entry = (Map.Entry) it.next(); Map.Entry entry = (Map.Entry) it.next();
Xpp3Dom param = new Xpp3Dom( String.valueOf( entry.getKey() ) ); Xpp3Dom param = new Xpp3Dom( String.valueOf( entry.getKey() ) );
param.setValue( String.valueOf( entry.getValue() ) ); param.setValue( String.valueOf( entry.getValue() ) );
config.addChild( param ); config.addChild( param );
} }
} }
plugin.setConfiguration( config ); plugin.setConfiguration( config );
return plugin; return plugin;
} }
@ -200,7 +204,7 @@ public class ModelUtilsTest
parentExecution.setId( "testExecution" ); parentExecution.setId( "testExecution" );
parent.addExecution( parentExecution ); parent.addExecution( parentExecution );
Build parentContainer = new Build(); Build parentContainer = new Build();
parentContainer.addPlugin( parent ); parentContainer.addPlugin( parent );
@ -208,18 +212,18 @@ public class ModelUtilsTest
child.setArtifactId( "testArtifact" ); child.setArtifactId( "testArtifact" );
child.setGroupId( "testGroup" ); child.setGroupId( "testGroup" );
child.setVersion( "1.0" ); child.setVersion( "1.0" );
Build childContainer = new Build(); Build childContainer = new Build();
childContainer.addPlugin( child ); childContainer.addPlugin( child );
ModelUtils.mergePluginLists( childContainer, parentContainer, true ); ModelUtils.mergePluginLists( childContainer, parentContainer, true );
List plugins = childContainer.getPlugins(); List plugins = childContainer.getPlugins();
assertEquals( 1, plugins.size() ); assertEquals( 1, plugins.size() );
Plugin plugin = (Plugin) plugins.get( 0 ); Plugin plugin = (Plugin) plugins.get( 0 );
assertEquals( 1, plugin.getExecutions().size() ); assertEquals( 1, plugin.getExecutions().size() );
} }
@ -234,7 +238,7 @@ public class ModelUtilsTest
parentExecution.setId( "testExecution" ); parentExecution.setId( "testExecution" );
parent.addExecution( parentExecution ); parent.addExecution( parentExecution );
Build parentContainer = new Build(); Build parentContainer = new Build();
parentContainer.addPlugin( parent ); parentContainer.addPlugin( parent );
@ -248,18 +252,18 @@ public class ModelUtilsTest
child.addExecution( childExecution ); child.addExecution( childExecution );
Build childContainer = new Build(); Build childContainer = new Build();
childContainer.addPlugin( child ); childContainer.addPlugin( child );
ModelUtils.mergePluginLists( childContainer, parentContainer, true ); ModelUtils.mergePluginLists( childContainer, parentContainer, true );
List plugins = childContainer.getPlugins(); List plugins = childContainer.getPlugins();
assertEquals( 1, plugins.size() ); assertEquals( 1, plugins.size() );
Plugin plugin = (Plugin) plugins.get( 0 ); Plugin plugin = (Plugin) plugins.get( 0 );
assertEquals( 2, plugin.getExecutions().size() ); assertEquals( 2, plugin.getExecutions().size() );
} }
@ -402,4 +406,59 @@ public class ModelUtilsTest
Dependency dep2 = (Dependency) child.getDependencies().get( 0 ); Dependency dep2 = (Dependency) child.getDependencies().get( 0 );
assertEquals( dep.getManagementKey(), dep2.getManagementKey() ); assertEquals( dep.getManagementKey(), dep2.getManagementKey() );
} }
public void testShouldOverwritePluginConfigurationSubItemsByDefault()
throws XmlPullParserException, IOException
{
String parentConfigStr = "<configuration><items><item>one</item><item>two</item></items></configuration>";
Xpp3Dom parentConfig = Xpp3DomBuilder.build( new StringReader( parentConfigStr ) );
Plugin parentPlugin = createPlugin( "group", "artifact", "1", null );
parentPlugin.setConfiguration( parentConfig );
String childConfigStr = "<configuration><items><item>three</item></items></configuration>";
Xpp3Dom childConfig = Xpp3DomBuilder.build( new StringReader( childConfigStr ) );
Plugin childPlugin = createPlugin( "group", "artifact", "1", null );
childPlugin.setConfiguration( childConfig );
ModelUtils.mergePluginDefinitions( childPlugin, parentPlugin, true );
Xpp3Dom result = (Xpp3Dom) childPlugin.getConfiguration();
Xpp3Dom items = result.getChild( "items" );
assertEquals( 1, items.getChildCount() );
Xpp3Dom item = items.getChild( 0 );
assertEquals( "three", item.getValue() );
}
public void testShouldMergePluginConfigurationSubItemsWithMergeAttributeSet()
throws XmlPullParserException, IOException
{
String parentConfigStr = "<configuration><items><item>one</item><item>two</item></items></configuration>";
Xpp3Dom parentConfig = Xpp3DomBuilder.build( new StringReader( parentConfigStr ) );
Plugin parentPlugin = createPlugin( "group", "artifact", "1", null );
parentPlugin.setConfiguration( parentConfig );
String childConfigStr = "<configuration><items combine.children=\"append\"><item>three</item></items></configuration>";
Xpp3Dom childConfig = Xpp3DomBuilder.build( new StringReader( childConfigStr ) );
Plugin childPlugin = createPlugin( "group", "artifact", "1", null );
childPlugin.setConfiguration( childConfig );
ModelUtils.mergePluginDefinitions( childPlugin, parentPlugin, true );
Xpp3Dom result = (Xpp3Dom) childPlugin.getConfiguration();
Xpp3Dom items = result.getChild( "items" );
assertEquals( 3, items.getChildCount() );
Xpp3Dom[] item = items.getChildren();
assertEquals( "one", item[0].getValue() );
assertEquals( "two", item[1].getValue() );
assertEquals( "three", item[2].getValue() );
}
} }