[MNG-6824] ModelMerger is broken

Fix modules
Apply Java 8 Functions
This commit is contained in:
rfscholte 2019-12-21 15:03:04 +01:00
parent aad48b73e2
commit 11b8b2d5d4
10 changed files with 523 additions and 319 deletions

View File

@ -115,7 +115,7 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
for ( Plugin element : tgt )
{
Object key = getPluginKey( element );
Object key = getPluginKey().apply( element );
merged.put( key, element );
}
@ -123,7 +123,7 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
for ( Plugin element : src )
{
Object key = getPluginKey( element );
Object key = getPluginKey().apply( element );
Plugin existing = merged.get( key );
if ( existing != null )
{
@ -143,7 +143,7 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
{
for ( Plugin managedPlugin : pluginMgmt.getPlugins() )
{
Object key = getPluginKey( managedPlugin );
Object key = getPluginKey().apply( managedPlugin );
Plugin addedPlugin = added.get( key );
if ( addedPlugin != null )
{

View File

@ -257,7 +257,7 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
plugin.setGroupId( null );
mergePlugin( plugin, element, sourceDominant, context );
Object key = getPluginKey( element );
Object key = getPluginKey().apply( element );
master.put( key, plugin );
}
@ -267,7 +267,7 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
List<Plugin> pending = new ArrayList<>();
for ( Plugin element : tgt )
{
Object key = getPluginKey( element );
Object key = getPluginKey().apply( element );
Plugin existing = master.get( key );
if ( existing != null )
{
@ -331,7 +331,7 @@ protected void mergeReporting_Plugins( Reporting target, Reporting source, boole
for ( ReportPlugin element : src )
{
Object key = getReportPluginKey( element );
Object key = getReportPluginKey().apply( element );
if ( element.isInherited() )
{
// NOTE: Enforce recursive merge to trigger merging/inheritance logic for executions as well
@ -346,7 +346,7 @@ protected void mergeReporting_Plugins( Reporting target, Reporting source, boole
for ( ReportPlugin element : tgt )
{
Object key = getReportPluginKey( element );
Object key = getReportPluginKey().apply( element );
ReportPlugin existing = merged.get( key );
if ( existing != null )
{

View File

@ -72,13 +72,13 @@ public void mergeManagedDependencies( Model model )
for ( Dependency dependency : model.getDependencies() )
{
Object key = getDependencyKey( dependency );
Object key = getDependencyKey().apply( dependency );
dependencies.put( key, dependency );
}
for ( Dependency managedDependency : dependencyManagement.getDependencies() )
{
Object key = getDependencyKey( managedDependency );
Object key = getDependencyKey().apply( managedDependency );
Dependency dependency = dependencies.get( key );
if ( dependency != null )
{

View File

@ -91,13 +91,13 @@ private void mergePluginContainerPlugins( PluginContainer target, PluginContaine
for ( Plugin element : src )
{
Object key = getPluginKey( element );
Object key = getPluginKey().apply( element );
managedPlugins.put( key, element );
}
for ( Plugin element : tgt )
{
Object key = getPluginKey( element );
Object key = getPluginKey().apply( element );
Plugin managedPlugin = managedPlugins.get( key );
if ( managedPlugin != null )
{
@ -121,13 +121,13 @@ protected void mergePlugin_Executions( Plugin target, Plugin source, boolean sou
for ( PluginExecution element : src )
{
Object key = getPluginExecutionKey( element );
Object key = getPluginExecutionKey().apply( element );
merged.put( key, element.clone() );
}
for ( PluginExecution element : tgt )
{
Object key = getPluginExecutionKey( element );
Object key = getPluginExecutionKey().apply( element );
PluginExecution existing = merged.get( key );
if ( existing != null )
{

View File

@ -294,13 +294,13 @@ protected void mergeModelBase_Repositories( ModelBase target, ModelBase source,
for ( Repository element : dominant )
{
Object key = getRepositoryKey( element );
Object key = getRepositoryKey().apply( element );
merged.put( key, element );
}
for ( Repository element : recessive )
{
Object key = getRepositoryKey( element );
Object key = getRepositoryKey().apply( element );
if ( !merged.containsKey( key ) )
{
merged.put( key, element );
@ -335,13 +335,13 @@ protected void mergeModelBase_PluginRepositories( ModelBase target, ModelBase so
for ( Repository element : dominant )
{
Object key = getRepositoryKey( element );
Object key = getRepositoryKey().apply( element );
merged.put( key, element );
}
for ( Repository element : recessive )
{
Object key = getRepositoryKey( element );
Object key = getRepositoryKey().apply( element );
if ( !merged.containsKey( key ) )
{
merged.put( key, element );
@ -567,14 +567,14 @@ protected void mergePlugin_Executions( Plugin target, Plugin source, boolean sou
if ( sourceDominant
|| ( element.getInherited() != null ? element.isInherited() : source.isInherited() ) )
{
Object key = getPluginExecutionKey( element );
Object key = getPluginExecutionKey().apply( element );
merged.put( key, element );
}
}
for ( PluginExecution element : tgt )
{
Object key = getPluginExecutionKey( element );
Object key = getPluginExecutionKey().apply( element );
PluginExecution existing = merged.get( key );
if ( existing != null )
{
@ -623,14 +623,14 @@ protected void mergeReportPlugin_ReportSets( ReportPlugin target, ReportPlugin s
{
if ( sourceDominant || ( rset.getInherited() != null ? rset.isInherited() : source.isInherited() ) )
{
Object key = getReportSetKey( rset );
Object key = getReportSetKey().apply( rset );
merged.put( key, rset );
}
}
for ( ReportSet element : tgt )
{
Object key = getReportSetKey( element );
Object key = getReportSetKey().apply( element );
ReportSet existing = merged.get( key );
if ( existing != null )
{
@ -644,51 +644,51 @@ protected void mergeReportPlugin_ReportSets( ReportPlugin target, ReportPlugin s
}
@Override
protected Object getDependencyKey( Dependency dependency )
protected KeyComputer<Dependency> getDependencyKey()
{
return dependency.getManagementKey();
return Dependency::getManagementKey;
}
@Override
protected Object getPluginKey( Plugin plugin )
protected KeyComputer<Plugin> getPluginKey()
{
return plugin.getKey();
return Plugin::getKey;
}
@Override
protected Object getPluginExecutionKey( PluginExecution pluginExecution )
protected KeyComputer<PluginExecution> getPluginExecutionKey()
{
return pluginExecution.getId();
return PluginExecution::getId;
}
@Override
protected Object getReportPluginKey( ReportPlugin reportPlugin )
protected KeyComputer<ReportPlugin> getReportPluginKey()
{
return reportPlugin.getKey();
return ReportPlugin::getKey;
}
@Override
protected Object getReportSetKey( ReportSet reportSet )
protected KeyComputer<ReportSet> getReportSetKey()
{
return reportSet.getId();
return ReportSet::getId;
}
@Override
protected Object getRepositoryBaseKey( RepositoryBase repositoryBase )
protected KeyComputer<RepositoryBase> getRepositoryBaseKey()
{
return repositoryBase.getId();
return RepositoryBase::getId;
}
@Override
protected Object getExtensionKey( Extension extension )
protected KeyComputer<Extension> getExtensionKey()
{
return extension.getGroupId() + ':' + extension.getArtifactId();
return e -> e.getGroupId() + ':' + e.getArtifactId();
}
@Override
protected Object getExclusionKey( Exclusion exclusion )
protected KeyComputer<Exclusion> getExclusionKey()
{
return exclusion.getGroupId() + ':' + exclusion.getArtifactId();
return e -> e.getGroupId() + ':' + e.getArtifactId();
}
protected String extrapolateChildUrl( String parentUrl, boolean appendPath, Map<Object, Object> context )

View File

@ -105,7 +105,7 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
for ( Plugin element : tgt )
{
Object key = getPluginKey( element );
Object key = getPluginKey().apply( element );
master.put( key, element );
}
@ -113,7 +113,7 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
List<Plugin> pending = new ArrayList<>();
for ( Plugin element : src )
{
Object key = getPluginKey( element );
Object key = getPluginKey().apply( element );
Plugin existing = master.get( key );
if ( existing != null )
{
@ -160,13 +160,13 @@ protected void mergePlugin_Executions( Plugin target, Plugin source, boolean sou
for ( PluginExecution element : tgt )
{
Object key = getPluginExecutionKey( element );
Object key = getPluginExecutionKey().apply( element );
merged.put( key, element );
}
for ( PluginExecution element : src )
{
Object key = getPluginExecutionKey( element );
Object key = getPluginExecutionKey().apply( element );
PluginExecution existing = merged.get( key );
if ( existing != null )
{
@ -195,13 +195,13 @@ protected void mergeReporting_Plugins( Reporting target, Reporting source, boole
for ( ReportPlugin element : tgt )
{
Object key = getReportPluginKey( element );
Object key = getReportPluginKey().apply( element );
merged.put( key, element );
}
for ( ReportPlugin element : src )
{
Object key = getReportPluginKey( element );
Object key = getReportPluginKey().apply( element );
ReportPlugin existing = merged.get( key );
if ( existing == null )
{
@ -229,13 +229,13 @@ protected void mergeReportPlugin_ReportSets( ReportPlugin target, ReportPlugin s
for ( ReportSet element : tgt )
{
Object key = getReportSetKey( element );
Object key = getReportSetKey().apply( element );
merged.put( key, element );
}
for ( ReportSet element : src )
{
Object key = getReportSetKey( element );
Object key = getReportSetKey().apply( element );
ReportSet existing = merged.get( key );
if ( existing != null )
{

View File

@ -42,6 +42,11 @@ under the License.
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -29,6 +29,9 @@
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.maven.model.Activation;
import org.apache.maven.model.Build;
@ -327,28 +330,28 @@ protected void mergeModel_Licenses( Model target, Model source, boolean sourceDo
Map<Object, Object> context )
{
target.setLicenses( merge( target.getLicenses(), source.getLicenses(),
sourceDominant, new LicenseKeyComputer() ) );
sourceDominant, getLicenseKey() ) );
}
protected void mergeModel_MailingLists( Model target, Model source, boolean sourceDominant,
Map<Object, Object> context )
{
target.setMailingLists( merge( target.getMailingLists(), source.getMailingLists(),
sourceDominant, new MailingListKeyComputer() ) );
sourceDominant, getMailingListKey() ) );
}
protected void mergeModel_Developers( Model target, Model source, boolean sourceDominant,
Map<Object, Object> context )
{
target.setDevelopers( merge( target.getDevelopers(), source.getDevelopers(),
sourceDominant, new DeveloperKeyComputer() ) );
sourceDominant, getDeveloperKey() ) );
}
protected void mergeModel_Contributors( Model target, Model source, boolean sourceDominant,
Map<Object, Object> context )
{
target.setContributors( merge( target.getContributors(), source.getContributors(),
sourceDominant, new ContributorKeyComputer() ) );
sourceDominant, getContributorKey() ) );
}
protected void mergeModel_IssueManagement( Model target, Model source, boolean sourceDominant,
@ -435,7 +438,7 @@ protected void mergeModel_Profiles( Model target, Model source, boolean sourceDo
Map<Object, Object> context )
{
target.setProfiles( merge( target.getProfiles(), source.getProfiles(),
sourceDominant, new ProfileKeyComputer() ) );
sourceDominant, getProfileKey() ) );
}
protected void mergeModelBase( ModelBase target, ModelBase source, boolean sourceDominant,
@ -454,36 +457,28 @@ protected void mergeModelBase( ModelBase target, ModelBase source, boolean sourc
protected void mergeModelBase_Modules( ModelBase target, ModelBase source, boolean sourceDominant,
Map<Object, Object> context )
{
List<String> src = source.getModules();
if ( !src.isEmpty() )
{
List<String> tgt = target.getModules();
List<String> merged = new ArrayList<>( tgt.size() + src.size() );
merged.addAll( tgt );
merged.addAll( src );
target.setModules( merged );
}
target.setModules( merge( target.getModules(), source.getModules(), sourceDominant, e -> e ) );
}
protected void mergeModelBase_Dependencies( ModelBase target, ModelBase source, boolean sourceDominant,
Map<Object, Object> context )
{
target.setDependencies( merge( target.getDependencies(), source.getDependencies(),
sourceDominant, new DependencyKeyComputer() ) );
sourceDominant, getDependencyKey() ) );
}
protected void mergeModelBase_Repositories( ModelBase target, ModelBase source, boolean sourceDominant,
Map<Object, Object> context )
{
target.setRepositories( merge( target.getRepositories(), source.getRepositories(),
sourceDominant, new RepositoryKeyComputer() ) );
sourceDominant, getRepositoryKey() ) );
}
protected void mergeModelBase_PluginRepositories( ModelBase target, ModelBase source, boolean sourceDominant,
Map<Object, Object> context )
{
target.setPluginRepositories( merge( target.getPluginRepositories(), source.getPluginRepositories(),
sourceDominant, new RepositoryKeyComputer() ) );
sourceDominant, getRepositoryKey() ) );
}
protected void mergeModelBase_DistributionManagement( ModelBase target, ModelBase source, boolean sourceDominant,
@ -1072,7 +1067,7 @@ protected void mergeDependency_Exclusions( Dependency target, Dependency source,
Map<Object, Object> context )
{
target.setExclusions( merge( target.getExclusions(), source.getExclusions(),
sourceDominant, new ExclusionKeyComputer() ) );
sourceDominant, getExclusionKey() ) );
}
protected void mergeExclusion( Exclusion target, Exclusion source, boolean sourceDominant,
@ -1150,7 +1145,7 @@ protected void mergeReporting_Plugins( Reporting target, Reporting source, boole
Map<Object, Object> context )
{
target.setPlugins( merge( target.getPlugins(), source.getPlugins(),
sourceDominant, new ReportPluginKeyComputer() ) );
sourceDominant, getReportPluginKey() ) );
}
protected void mergeReportPlugin( ReportPlugin target, ReportPlugin source, boolean sourceDominant,
@ -1209,7 +1204,7 @@ protected void mergeReportPlugin_ReportSets( ReportPlugin target, ReportPlugin s
Map<Object, Object> context )
{
target.setReportSets( merge( target.getReportSets(), source.getReportSets(),
sourceDominant, new ReportSetKeyComputer() ) );
sourceDominant, getReportSetKey() ) );
}
protected void mergeReportSet( ReportSet target, ReportSet source, boolean sourceDominant,
@ -1276,7 +1271,7 @@ protected void mergeDependencyManagement_Dependencies( DependencyManagement targ
boolean sourceDominant, Map<Object, Object> context )
{
target.setDependencies( merge( target.getDependencies(), source.getDependencies(),
sourceDominant, new DependencyKeyComputer() ) );
sourceDominant, getDependencyKey() ) );
}
protected void mergeParent( Parent target, Parent source, boolean sourceDominant, Map<Object, Object> context )
@ -1872,7 +1867,7 @@ protected void mergeCiManagement_Notifiers( CiManagement target, CiManagement so
Map<Object, Object> context )
{
target.setNotifiers( merge( target.getNotifiers(), source.getNotifiers(),
sourceDominant, new NotifierKeyComputer() ) );
sourceDominant, getNotifierKey() ) );
}
protected void mergeNotifier( Notifier target, Notifier source, boolean sourceDominant,
@ -2071,7 +2066,7 @@ protected void mergeBuild_Extensions( Build target, Build source, boolean source
Map<Object, Object> context )
{
target.setExtensions( merge( target.getExtensions(), source.getExtensions(),
sourceDominant, new ExtensionKeyComputer() ) );
sourceDominant, getExtensionKey() ) );
}
protected void mergeExtension( Extension target, Extension source, boolean sourceDominant,
@ -2196,14 +2191,14 @@ protected void mergeBuildBase_Resources( BuildBase target, BuildBase source, boo
Map<Object, Object> context )
{
target.setResources( merge( target.getResources(), source.getResources(),
sourceDominant, new ResourceKeyComputer() ) );
sourceDominant, getResourceKey() ) );
}
protected void mergeBuildBase_TestResources( BuildBase target, BuildBase source, boolean sourceDominant,
Map<Object, Object> context )
{
target.setTestResources( merge( target.getTestResources(), source.getTestResources(),
sourceDominant, new ResourceKeyComputer() ) );
sourceDominant, getResourceKey() ) );
}
protected void mergePluginConfiguration( PluginConfiguration target, PluginConfiguration source,
@ -2239,7 +2234,7 @@ protected void mergePluginContainer_Plugins( PluginContainer target, PluginConta
boolean sourceDominant, Map<Object, Object> context )
{
target.setPlugins( merge( target.getPlugins(), source.getPlugins(),
sourceDominant, new PluginKeyComputer() ) );
sourceDominant, getPluginKey() ) );
}
protected void mergePluginManagement( PluginManagement target, PluginManagement source, boolean sourceDominant,
@ -2319,14 +2314,14 @@ protected void mergePlugin_Dependencies( Plugin target, Plugin source, boolean s
Map<Object, Object> context )
{
target.setDependencies( merge( target.getDependencies(), source.getDependencies(),
sourceDominant, new DependencyKeyComputer() ) );
sourceDominant, getDependencyKey() ) );
}
protected void mergePlugin_Executions( Plugin target, Plugin source, boolean sourceDominant,
Map<Object, Object> context )
{
target.setExecutions( merge( target.getExecutions(), source.getExecutions(),
sourceDominant, new ExecutionKeyComputer() ) );
sourceDominant, getPluginExecutionKey() ) );
}
protected void mergeConfigurationContainer( ConfigurationContainer target, ConfigurationContainer source,
@ -2538,302 +2533,189 @@ protected void mergeActivation( Activation target, Activation source, boolean so
// TODO
}
@Deprecated
protected Object getDependencyKey( Dependency dependency )
{
return dependency;
}
@Deprecated
protected Object getPluginKey( Plugin plugin )
{
return plugin;
}
@Deprecated
protected Object getPluginExecutionKey( PluginExecution pluginExecution )
{
return pluginExecution;
}
@Deprecated
protected Object getReportPluginKey( ReportPlugin reportPlugin )
{
return reportPlugin;
}
@Deprecated
protected Object getReportSetKey( ReportSet reportSet )
{
return reportSet;
}
@Deprecated
protected Object getLicenseKey( License license )
{
return license;
}
@Deprecated
protected Object getMailingListKey( MailingList mailingList )
{
return mailingList;
}
@Deprecated
protected Object getDeveloperKey( Developer developer )
{
return developer;
}
@Deprecated
protected Object getContributorKey( Contributor contributor )
{
return contributor;
}
@Deprecated
protected Object getProfileKey( Profile profile )
{
return profile;
}
@Deprecated
protected Object getRepositoryKey( Repository repository )
{
return getRepositoryBaseKey( repository );
}
@Deprecated
protected Object getRepositoryBaseKey( RepositoryBase repositoryBase )
{
return repositoryBase;
}
@Deprecated
protected Object getNotifierKey( Notifier notifier )
{
return notifier;
}
@Deprecated
protected Object getResourceKey( Resource resource )
{
return resource;
}
@Deprecated
protected Object getExtensionKey( Extension extension )
{
return extension;
}
@Deprecated
protected Object getExclusionKey( Exclusion exclusion )
{
return exclusion;
}
protected KeyComputer<Dependency> getDependencyKey()
{
return d -> d;
}
protected KeyComputer<Plugin> getPluginKey()
{
return p -> p;
}
protected KeyComputer<PluginExecution> getPluginExecutionKey()
{
return e -> e;
}
protected KeyComputer<ReportPlugin> getReportPluginKey()
{
return p -> p;
}
protected KeyComputer<ReportSet> getReportSetKey()
{
return s -> s;
}
protected KeyComputer<License> getLicenseKey()
{
return l -> l;
}
protected KeyComputer<MailingList> getMailingListKey()
{
return l -> l;
}
protected KeyComputer<Developer> getDeveloperKey()
{
return d -> d;
}
protected KeyComputer<Contributor> getContributorKey()
{
return c -> c;
}
protected KeyComputer<Profile> getProfileKey()
{
return p -> p;
}
protected KeyComputer<Repository> getRepositoryKey()
{
return r -> r;
}
protected KeyComputer<RepositoryBase> getRepositoryBaseKey()
{
return r -> r;
}
protected KeyComputer<Notifier> getNotifierKey()
{
return n -> n;
}
protected KeyComputer<Resource> getResourceKey()
{
return r -> r;
}
protected KeyComputer<Extension> getExtensionKey()
{
return e -> e;
}
protected KeyComputer<Exclusion> getExclusionKey()
{
return e -> e;
}
/**
* Use to compute keys for data structures
* @param <T>
*/
private interface KeyComputer<T>
@FunctionalInterface
public interface KeyComputer<T> extends Function<T, Object>
{
Object key( T t );
}
/**
* Remapping function
* @param <T>
*/
private interface Remapping<T>
{
T merge( T u, T v );
}
/**
* KeyComputer for Dependency
*/
private final class DependencyKeyComputer implements KeyComputer<Dependency>
{
@Override
public Object key( Dependency dependency )
{
return getDependencyKey( dependency );
}
}
/**
* KeyComputer for License
*/
private class LicenseKeyComputer implements KeyComputer<License>
{
@Override
public Object key( License license )
{
return getLicenseKey( license );
}
}
/**
* KeyComputer for MailingList
*/
private class MailingListKeyComputer implements KeyComputer<MailingList>
{
@Override
public Object key( MailingList mailingList )
{
return getMailingListKey( mailingList );
}
}
/**
* KeyComputer for Developer
*/
private class DeveloperKeyComputer implements KeyComputer<Developer>
{
@Override
public Object key( Developer developer )
{
return getDeveloperKey( developer );
}
}
/**
* KeyComputer for Contributor
*/
private class ContributorKeyComputer implements KeyComputer<Contributor>
{
@Override
public Object key( Contributor contributor )
{
return getContributorKey( contributor );
}
}
/**
* KeyComputer for Profile
*/
private class ProfileKeyComputer implements KeyComputer<Profile>
{
@Override
public Object key( Profile profile )
{
return getProfileKey( profile );
}
}
/**
* KeyComputer for Repository
*/
private class RepositoryKeyComputer implements KeyComputer<Repository>
{
@Override
public Object key( Repository repository )
{
return getRepositoryKey( repository );
}
}
/**
* KeyComputer for ReportPlugin
*/
private class ReportPluginKeyComputer implements KeyComputer<ReportPlugin>
{
@Override
public Object key( ReportPlugin plugin )
{
return getReportPluginKey( plugin );
}
}
/**
* KeyComputer for Plugin
*/
private class PluginKeyComputer implements KeyComputer<Plugin>
{
@Override
public Object key( Plugin plugin )
{
return getPluginKey( plugin );
}
}
/**
* KeyComputer for ReportSet
*/
private class ReportSetKeyComputer implements KeyComputer<ReportSet>
{
@Override
public Object key( ReportSet reportSet )
{
return getReportSetKey( reportSet );
}
}
/**
* KeyComputer for Notifier
*/
private class NotifierKeyComputer implements KeyComputer<Notifier>
{
@Override
public Object key( Notifier notifier )
{
return getNotifierKey( notifier );
}
}
/**
* KeyComputer for Extension
*/
private class ExtensionKeyComputer implements KeyComputer<Extension>
{
@Override
public Object key( Extension extension )
{
return getExtensionKey( extension );
}
}
/**
* KeyComputer for Resource
*/
private class ResourceKeyComputer implements KeyComputer<Resource>
{
@Override
public Object key( Resource resource )
{
return getResourceKey( resource );
}
}
/**
* KeyComputer for PluginExecution
*/
private class ExecutionKeyComputer implements KeyComputer<PluginExecution>
{
@Override
public Object key( PluginExecution pluginExecution )
{
return getPluginExecutionKey( pluginExecution );
}
}
/**
* KeyComputer for Exclusion
*/
private class ExclusionKeyComputer implements KeyComputer<Exclusion>
{
@Override
public Object key( Exclusion exclusion )
{
return getExclusionKey( exclusion );
}
}
/**
* Return the second value if <code>sourceDominant</code> is true, the first one otherwise.
* @param <T>
*/
private static class SourceDominant<T> implements Remapping<T>
{
private final boolean sourceDominant;
SourceDominant( boolean sourceDominant )
{
this.sourceDominant = sourceDominant;
}
@Override
public T merge( T u, T v )
{
return sourceDominant ? v : u;
}
}
/**
@ -2841,10 +2723,10 @@ public T merge( T u, T v )
*/
private static <T> List<T> merge( List<T> tgt, List<T> src, boolean sourceDominant, KeyComputer<T> computer )
{
return merge( tgt, src, computer, new SourceDominant<T>( sourceDominant ) );
return merge( tgt, src, computer, ( t, s ) -> sourceDominant ? s : t );
}
private static <T> List<T> merge( List<T> tgt, List<T> src, KeyComputer<T> computer, Remapping<T> remapping )
private static <T> List<T> merge( List<T> tgt, List<T> src, KeyComputer<T> computer, BinaryOperator<T> remapping )
{
if ( src.isEmpty() )
{
@ -2859,7 +2741,7 @@ private static <T> List<T> merge( List<T> tgt, List<T> src, KeyComputer<T> compu
else
{
list = new MergingList<>( computer, src.size() + tgt.size() );
list.mergeAll( tgt, new SourceDominant<T>( true ) );
list.mergeAll( tgt, ( t, s ) -> s );
}
list.mergeAll( src, remapping );
@ -2895,51 +2777,34 @@ public Iterator<V> iterator()
}
}
void mergeAll( Collection<V> vs, Remapping<V> remapping )
void mergeAll( Collection<V> vs, BinaryOperator<V> remapping )
{
if ( map == null )
{
map = new LinkedHashMap<>( list.size() + vs.size() );
for ( V v : list )
{
map.put( keyComputer.key( v ), v );
}
map = list.stream().collect( Collectors.toMap( keyComputer,
Function.identity(),
null,
LinkedHashMap::new ) );
list = null;
}
if ( vs instanceof MergingList && ( (MergingList) vs ).map != null )
if ( vs instanceof MergingList && ( (MergingList<V>) vs ).map != null )
{
for ( Map.Entry<Object, V> e : ( (MergingList<V>) vs ).map.entrySet() )
{
Object key = e.getKey();
V oldValue = map.get( key );
// JDK8: this should be a call to map.merge( key, v, remapping )
V newValue = ( oldValue == null ) ? e.getValue() : remapping.merge( oldValue, e.getValue() );
if ( newValue == null )
{
remove( key );
}
else if ( newValue != oldValue )
{
map.put( key, newValue );
}
V v = e.getValue();
map.merge( key, v, remapping );
}
}
else
{
for ( V v : vs )
{
Object key = keyComputer.key( v );
// JDK8: this should be a call to map.merge( key, v, remapping )
V oldValue = map.get( key );
V newValue = ( oldValue == null ) ? v : remapping.merge( oldValue, v );
if ( newValue == null )
{
remove( key );
}
else
{
map.put( key, newValue );
}
Object key = keyComputer.apply( v );
map.merge( key, v, remapping );
}
}
}

View File

@ -0,0 +1,334 @@
package org.apache.maven.model.merge;
/*
* 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 static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import java.util.Arrays;
import org.apache.maven.model.Contributor;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.Developer;
import org.apache.maven.model.MailingList;
import org.apache.maven.model.Model;
import org.apache.maven.model.Profile;
import org.apache.maven.model.Repository;
import org.junit.Test;
/**
* ModelMerger is based on same instances, subclasses should override KeyComputer per type
*
* @author Robert Scholte
*
*/
public class ModelMergerTest
{
private ModelMerger modelMerger = new ModelMerger();
@Test
public void mergeArtifactId()
{
Model target = new Model();
target.setArtifactId( "TARGET" );
Model source = new Model();
source.setArtifactId( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getArtifactId(), is( "SOURCE" ) );
target.setArtifactId( "TARGET" );;
modelMerger.merge( target, source, false, null );
assertThat( target.getArtifactId(), is( "TARGET" ) );
}
@Test
public void mergeSameContributors()
{
Contributor contributor = new Contributor();
contributor.setEmail( "contributor@maven.apache.org" );
Model target = new Model();
target.setContributors( Arrays.asList( contributor ) );
Model source = new Model();
source.setContributors( Arrays.asList( contributor ) );
modelMerger.merge( target, source, true, null );
assertThat( target.getContributors(), contains( contributor ) );
}
@Test
public void mergeSameDependencies()
{
Dependency dependency = new Dependency();
dependency.setGroupId( "groupId" );
dependency.setArtifactId( "artifactId" );
dependency.setType( "type" );
Model target = new Model();
target.setDependencies( Arrays.asList( dependency ) );
Model source = new Model();
source.setDependencies( Arrays.asList( dependency ) );
modelMerger.merge( target, source, true, null );
assertThat( target.getDependencies(), contains( dependency ) );
}
@Test
public void mergeDescription()
{
Model target = new Model();
target.setDescription( "TARGET" );
Model source = new Model();
source.setDescription( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getDescription(), is( "SOURCE" ) );
target.setDescription( "TARGET" );
modelMerger.merge( target, source, false, null );
assertThat( target.getDescription(), is( "TARGET" ) );
}
@Test
public void mergeSameDevelopers()
{
Developer developer = new Developer();
developer.setId( "devid" );
Model target = new Model();
target.setDevelopers( Arrays.asList( developer ) );
Model source = new Model();
source.setDevelopers( Arrays.asList( developer ) );
modelMerger.merge( target, source, true, null );
assertThat( target.getDevelopers(), contains( developer ) );
}
@Test
public void mergeGroupId()
{
Model target = new Model();
target.setGroupId( "TARGET" );
Model source = new Model();
source.setGroupId( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getGroupId(), is( "SOURCE" ) );
target.setGroupId( "TARGET" );
modelMerger.merge( target, source, false, null );
assertThat( target.getGroupId(), is( "TARGET" ) );
}
@Test
public void mergeInceptionYear()
{
Model target = new Model();
target.setInceptionYear( "TARGET" );
Model source = new Model();
source.setInceptionYear( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getInceptionYear(), is( "SOURCE" ) );
target.setInceptionYear( "TARGET" );
modelMerger.merge( target, source, false, null );
assertThat( target.getInceptionYear(), is( "TARGET" ) );
}
@Test
public void mergeSameMailingLists()
{
MailingList mailingList = new MailingList();
mailingList.setName( "name" );
Model target = new Model();
target.setMailingLists( Arrays.asList( mailingList ) );
Model source = new Model();
source.setMailingLists( Arrays.asList( mailingList ) );
modelMerger.merge( target, source, true, null );
assertThat( target.getMailingLists(), contains( mailingList ) );
}
@Test
public void mergeModelVersion()
{
Model target = new Model();
target.setModelVersion( "TARGET" );
Model source = new Model();
source.setModelVersion( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getModelVersion(), is( "SOURCE" ) );
target.setModelVersion( "TARGET" );;
modelMerger.merge( target, source, false, null );
assertThat( target.getModelVersion(), is( "TARGET" ) );
}
@Test
public void mergeSameModules()
{
Model target = new Model();
target.setModules( Arrays.asList( "first", "second", "third" ) );
Model source = new Model();
source.setModules( Arrays.asList( "first", "second", "third" ) );
modelMerger.merge( target, source, true, null );
assertThat( target.getModules(), contains( "first", "second", "third" ) );
}
@Test
public void mergeName()
{
Model target = new Model();
target.setName( "TARGET" );
Model source = new Model();
source.setName( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getName(), is( "SOURCE" ) );
target.setName( "TARGET" );;
modelMerger.merge( target, source, false, null );
assertThat( target.getName(), is( "TARGET" ) );
}
@Test
public void mergePackaging()
{
Model target = new Model();
target.setPackaging( "TARGET" );
Model source = new Model();
source.setPackaging( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getPackaging(), is( "SOURCE" ) );
target.setPackaging( "TARGET" );;
modelMerger.merge( target, source, false, null );
assertThat( target.getPackaging(), is( "TARGET" ) );
}
@Test
public void mergeSamePluginRepositories()
{
Repository repository = new Repository();
repository.setId( "repository" );
Model target = new Model();
target.setPluginRepositories( Arrays.asList( repository ) );
Model source = new Model();
source.setPluginRepositories( Arrays.asList( repository ) );
modelMerger.merge( target, source, true, null );
assertThat( target.getPluginRepositories(), contains( repository ) );
}
@Test
public void mergeSameProfiles()
{
Profile profile = new Profile();
profile.setId( "profile" );
Model target = new Model();
target.setProfiles( Arrays.asList( profile ) );
Model source = new Model();
source.setProfiles( Arrays.asList( profile ) );
modelMerger.merge( target, source, true, null );
assertThat( target.getProfiles(), contains( profile ) );
}
@Test
public void mergeSameRepositories()
{
Repository repository = new Repository();
repository.setId( "repository" );
Model target = new Model();
target.setRepositories( Arrays.asList( repository ) );
Model source = new Model();
source.setRepositories( Arrays.asList( repository ) );
modelMerger.merge( target, source, true, null );
assertThat( target.getRepositories(), contains( repository ) );
}
@Test
public void mergeUrl()
{
Model target = new Model();
target.setUrl( "TARGET" );;
Model source = new Model();
source.setUrl( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getUrl(), is( "SOURCE" ) );
target.setUrl( "TARGET" );;
modelMerger.merge( target, source, false, null );
assertThat( target.getUrl(), is( "TARGET" ) );
}
@Test
public void mergeVersion()
{
Model target = new Model();
target.setVersion( "TARGET" );;
Model source = new Model();
source.setVersion( "SOURCE" );
modelMerger.merge( target, source, true, null );
assertThat( target.getVersion(), is( "SOURCE" ) );
target.setVersion( "TARGET" );;
modelMerger.merge( target, source, false, null );
assertThat( target.getVersion(), is( "TARGET" ) );
}
}

View File

@ -415,13 +415,13 @@ under the License.
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<version>2.2</version>
<scope>test</scope>
</dependency>
</dependencies>