View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project.artifact;
20  
21  import java.util.LinkedHashSet;
22  import java.util.Set;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.DefaultArtifact;
26  import org.apache.maven.lifecycle.internal.SetWithResolutionResult;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
31  
32  class DefaultProjectArtifactsCacheTest {
33  
34      private ProjectArtifactsCache cache;
35  
36      @BeforeEach
37      void setUp() throws Exception {
38          cache = new DefaultProjectArtifactsCache();
39      }
40  
41      @Test
42      void testProjectDependencyOrder() throws Exception {
43          ProjectArtifactsCache.Key project1 = new ProjectArtifactsCache.Key() {};
44  
45          Set<Artifact> artifacts = new LinkedHashSet<>(4);
46          artifacts.add(new DefaultArtifact("g", "a1", "v", "compile", "jar", "", null));
47          artifacts.add(new DefaultArtifact("g", "a2", "v", "compile", "jar", "", null));
48          artifacts.add(new DefaultArtifact("g", "a3", "v", "compile", "jar", "", null));
49          artifacts.add(new DefaultArtifact("g", "a4", "v", "compile", "jar", "", null));
50  
51          cache.put(project1, new SetWithResolutionResult(null, artifacts));
52  
53          assertArrayEquals(
54                  artifacts.toArray(new Artifact[0]),
55                  cache.get(project1).getArtifacts().toArray(new Artifact[0]));
56  
57          ProjectArtifactsCache.Key project2 = new ProjectArtifactsCache.Key() {};
58  
59          Set<Artifact> reversedArtifacts = new LinkedHashSet<>(4);
60          artifacts.add(new DefaultArtifact("g", "a4", "v", "compile", "jar", "", null));
61          artifacts.add(new DefaultArtifact("g", "a3", "v", "compile", "jar", "", null));
62          artifacts.add(new DefaultArtifact("g", "a2", "v", "compile", "jar", "", null));
63          artifacts.add(new DefaultArtifact("g", "a1", "v", "compile", "jar", "", null));
64  
65          cache.put(project2, new SetWithResolutionResult(null, reversedArtifacts));
66  
67          assertArrayEquals(
68                  reversedArtifacts.toArray(new Artifact[0]),
69                  cache.get(project2).getArtifacts().toArray(new Artifact[0]));
70      }
71  }