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