diff --git a/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultProjectArtifactsCache.java b/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultProjectArtifactsCache.java index 1f6ab2a72f..ae59ada4d7 100644 --- a/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultProjectArtifactsCache.java +++ b/maven-core/src/main/java/org/apache/maven/project/artifact/DefaultProjectArtifactsCache.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Objects; @@ -86,7 +87,7 @@ public class DefaultProjectArtifactsCache artifactId = project.getArtifactId(); version = project.getVersion(); - Set deps = new HashSet<>(); + Set deps = new LinkedHashSet<>(); if ( project.getDependencyArtifacts() != null ) { for ( Artifact dep: project.getDependencyArtifacts() ) @@ -203,7 +204,7 @@ public class DefaultProjectArtifactsCache assertUniqueKey( key ); CacheRecord record = - new CacheRecord( Collections.unmodifiableSet( new HashSet<>( projectArtifacts ) ) ); + new CacheRecord( Collections.unmodifiableSet( new LinkedHashSet<>( projectArtifacts ) ) ); cache.put( key, record ); diff --git a/maven-core/src/test/java/org/apache/maven/project/artifact/DefaultProjectArtifactsCacheTest.java b/maven-core/src/test/java/org/apache/maven/project/artifact/DefaultProjectArtifactsCacheTest.java new file mode 100644 index 0000000000..166c068b81 --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/project/artifact/DefaultProjectArtifactsCacheTest.java @@ -0,0 +1,72 @@ +package org.apache.maven.project.artifact; + +/* + * 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.assertArrayEquals; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DefaultArtifact; +import org.codehaus.plexus.PlexusTestCase; + +public class DefaultProjectArtifactsCacheTest extends PlexusTestCase +{ + + private ProjectArtifactsCache cache; + + @Override + protected void setUp() + throws Exception + { + super.setUp(); + cache = lookup( ProjectArtifactsCache.class ); + } + + public void testProjectDependencyOrder() throws Exception + { + ProjectArtifactsCache.Key project1 = new ProjectArtifactsCache.Key(){}; + + Set artifacts = new LinkedHashSet<>( 4 ); + artifacts.add( new DefaultArtifact( "g", "a1", "v", "compile", "jar", "", null ) ); + artifacts.add( new DefaultArtifact( "g", "a2", "v", "compile", "jar", "", null ) ); + artifacts.add( new DefaultArtifact( "g", "a3", "v", "compile", "jar", "", null ) ); + artifacts.add( new DefaultArtifact( "g", "a4", "v", "compile", "jar", "", null ) ); + + cache.put( project1, artifacts ); + + assertArrayEquals( artifacts.toArray( new Artifact[0] ), + cache.get( project1 ).getArtifacts().toArray( new Artifact[0] ) ); + + ProjectArtifactsCache.Key project2 = new ProjectArtifactsCache.Key(){}; + + Set reversedArtifacts = new LinkedHashSet<>( 4 ); + artifacts.add( new DefaultArtifact( "g", "a4", "v", "compile", "jar", "", null ) ); + artifacts.add( new DefaultArtifact( "g", "a3", "v", "compile", "jar", "", null ) ); + artifacts.add( new DefaultArtifact( "g", "a2", "v", "compile", "jar", "", null ) ); + artifacts.add( new DefaultArtifact( "g", "a1", "v", "compile", "jar", "", null ) ); + + cache.put( project2, reversedArtifacts ); + + assertArrayEquals( reversedArtifacts.toArray( new Artifact[0] ), + cache.get( project2 ).getArtifacts().toArray( new Artifact[0] ) ); + } +}