You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2018/09/24 17:50:48 UTC

[maven] 01/01: [MNG-6415] Project Artifacts Cache does not retain the order of classpath entries.

This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch MNG-6415
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 7c1e7129b12973769f49cd946236e43f4bd17d51
Author: rfscholte <rf...@apache.org>
AuthorDate: Mon Sep 24 19:50:39 2018 +0200

    [MNG-6415] Project Artifacts Cache does not retain the order of classpath entries.
---
 .../artifact/DefaultProjectArtifactsCache.java     |  5 +-
 .../artifact/DefaultProjectArtifactsCacheTest.java | 72 ++++++++++++++++++++++
 2 files changed, 75 insertions(+), 2 deletions(-)

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 1f6ab2a..ae59ada 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<String> deps = new HashSet<>();
+            Set<String> 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 0000000..166c068
--- /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<Artifact> 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<Artifact> 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] ) );
+    }
+}