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.repository.legacy.resolver.conflict;
20  
21  import javax.inject.Inject;
22  
23  import java.util.Collections;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.factory.ArtifactFactory;
27  import org.apache.maven.artifact.repository.ArtifactRepository;
28  import org.apache.maven.artifact.resolver.ResolutionNode;
29  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
30  import org.apache.maven.artifact.versioning.VersionRange;
31  import org.codehaus.plexus.PlexusContainer;
32  import org.junit.jupiter.api.BeforeEach;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.assertNotNull;
36  
37  /**
38   * Provides a basis for testing conflict resolvers.
39   *
40   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
41   */
42  public abstract class AbstractConflictResolverTest {
43      // constants --------------------------------------------------------------
44  
45      private static final String GROUP_ID = "test";
46  
47      // fields -----------------------------------------------------------------
48  
49      protected Artifact a1;
50  
51      protected Artifact a2;
52  
53      protected Artifact b1;
54  
55      private final String roleHint;
56  
57      @Inject
58      private ArtifactFactory artifactFactory;
59  
60      @Inject
61      private PlexusContainer container;
62  
63      private ConflictResolver conflictResolver;
64  
65      // constructors -----------------------------------------------------------
66  
67      public AbstractConflictResolverTest(String roleHint) throws Exception {
68          this.roleHint = roleHint;
69      }
70  
71      // TestCase methods -------------------------------------------------------
72  
73      /*
74       * @see junit.framework.TestCase#setUp()
75       */
76      @BeforeEach
77      protected void setUp() throws Exception {
78  
79          conflictResolver = (ConflictResolver) container.lookup(ConflictResolver.ROLE, roleHint);
80  
81          a1 = createArtifact("a", "1.0");
82          a2 = createArtifact("a", "2.0");
83          b1 = createArtifact("b", "1.0");
84      }
85  
86      // protected methods ------------------------------------------------------
87  
88      protected ConflictResolver getConflictResolver() {
89          return conflictResolver;
90      }
91  
92      protected void assertResolveConflict(
93              ResolutionNode expectedNode, ResolutionNode actualNode1, ResolutionNode actualNode2) {
94          ResolutionNode resolvedNode = getConflictResolver().resolveConflict(actualNode1, actualNode2);
95  
96          assertNotNull(resolvedNode, "Expected resolvable");
97          assertEquals(expectedNode, resolvedNode, "Resolution node");
98      }
99  
100     protected Artifact createArtifact(String id, String version) throws InvalidVersionSpecificationException {
101         return createArtifact(id, version, Artifact.SCOPE_COMPILE);
102     }
103 
104     protected Artifact createArtifact(String id, String version, String scope)
105             throws InvalidVersionSpecificationException {
106         return createArtifact(id, version, scope, null, false);
107     }
108 
109     protected Artifact createArtifact(String id, String version, String scope, String inheritedScope, boolean optional)
110             throws InvalidVersionSpecificationException {
111         VersionRange versionRange = VersionRange.createFromVersionSpec(version);
112 
113         return artifactFactory.createDependencyArtifact(
114                 GROUP_ID, id, versionRange, "jar", null, scope, inheritedScope, optional);
115     }
116 
117     protected ResolutionNode createResolutionNode(Artifact artifact) {
118         return new ResolutionNode(artifact, Collections.<ArtifactRepository>emptyList());
119     }
120 
121     protected ResolutionNode createResolutionNode(Artifact artifact, ResolutionNode parent) {
122         return new ResolutionNode(artifact, Collections.<ArtifactRepository>emptyList(), parent);
123     }
124 }