Skip to content

Commit

Permalink
[MNG-6415] Project Artifacts Cache does not retain the order of class…
Browse files Browse the repository at this point in the history
…path entries.
  • Loading branch information
rfscholte committed Sep 24, 2018
1 parent 421164e commit 7c1e712
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -86,7 +87,7 @@ public CacheKey( MavenProject project, List<RemoteRepository> repositories,
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() )
Expand Down Expand Up @@ -203,7 +204,7 @@ public CacheRecord put( Key key, Set<Artifact> projectArtifacts )
assertUniqueKey( key );

CacheRecord record =
new CacheRecord( Collections.unmodifiableSet( new HashSet<>( projectArtifacts ) ) );
new CacheRecord( Collections.unmodifiableSet( new LinkedHashSet<>( projectArtifacts ) ) );

cache.put( key, record );

Expand Down
@@ -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] ) );
}
}

0 comments on commit 7c1e712

Please sign in to comment.