View Javadoc
1   package org.apache.maven.repository.legacy.resolver.conflict;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.factory.ArtifactFactory;
24  import org.apache.maven.artifact.repository.ArtifactRepository;
25  import org.apache.maven.artifact.resolver.ResolutionNode;
26  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
27  import org.apache.maven.artifact.versioning.VersionRange;
28  import org.apache.maven.repository.legacy.resolver.conflict.ConflictResolver;
29  import org.codehaus.plexus.PlexusTestCase;
30  
31  import java.util.Collections;
32  
33  /**
34   * Provides a basis for testing conflict resolvers.
35   *
36   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
37   */
38  public abstract class AbstractConflictResolverTest
39      extends PlexusTestCase
40  {
41      // constants --------------------------------------------------------------
42  
43      private static final String GROUP_ID = "test";
44  
45      // fields -----------------------------------------------------------------
46  
47      protected Artifact a1;
48  
49      protected Artifact a2;
50  
51      protected Artifact b1;
52  
53      private final String roleHint;
54  
55      private ArtifactFactory artifactFactory;
56  
57      private ConflictResolver conflictResolver;
58  
59      // constructors -----------------------------------------------------------
60  
61      public AbstractConflictResolverTest( String roleHint )
62          throws Exception
63      {
64          this.roleHint = roleHint;
65      }
66  
67      // TestCase methods -------------------------------------------------------
68  
69      /*
70       * @see junit.framework.TestCase#setUp()
71       */
72      protected void setUp() throws Exception
73      {
74          super.setUp();
75  
76          artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
77          conflictResolver = (ConflictResolver) lookup( ConflictResolver.ROLE, roleHint );
78  
79          a1 = createArtifact( "a", "1.0" );
80          a2 = createArtifact( "a", "2.0" );
81          b1 = createArtifact( "b", "1.0" );
82      }
83  
84      /*
85       * @see org.codehaus.plexus.PlexusTestCase#tearDown()
86       */
87      protected void tearDown() throws Exception
88      {
89          a1 = null;
90          a2 = null;
91          b1 = null;
92  
93          artifactFactory = null;
94          conflictResolver = null;
95  
96          super.tearDown();
97      }
98  
99      // protected methods ------------------------------------------------------
100 
101     protected ConflictResolver getConflictResolver()
102     {
103         return conflictResolver;
104     }
105 
106     protected void assertResolveConflict( ResolutionNode expectedNode, ResolutionNode actualNode1, ResolutionNode actualNode2 )
107     {
108         ResolutionNode resolvedNode = getConflictResolver().resolveConflict( actualNode1, actualNode2 );
109 
110         assertNotNull( "Expected resolvable", resolvedNode );
111         assertEquals( "Resolution node", expectedNode, resolvedNode );
112     }
113 
114     protected Artifact createArtifact( String id, String version ) throws InvalidVersionSpecificationException
115     {
116         return createArtifact( id, version, Artifact.SCOPE_COMPILE );
117     }
118 
119     protected Artifact createArtifact( String id, String version, String scope )
120         throws InvalidVersionSpecificationException
121     {
122         return createArtifact( id, version, scope, null, false );
123     }
124 
125     protected Artifact createArtifact( String id, String version, String scope, String inheritedScope, boolean optional )
126         throws InvalidVersionSpecificationException
127     {
128         VersionRange versionRange = VersionRange.createFromVersionSpec( version );
129 
130         return artifactFactory.createDependencyArtifact( GROUP_ID, id, versionRange, "jar", null, scope,
131                                                          inheritedScope, optional );
132     }
133 
134     protected ResolutionNode createResolutionNode( Artifact Artifact )
135     {
136         return new ResolutionNode( Artifact, Collections.<ArtifactRepository>emptyList() );
137     }
138     protected ResolutionNode createResolutionNode( Artifact Artifact, ResolutionNode parent )
139     {
140         return new ResolutionNode( Artifact, Collections.<ArtifactRepository>emptyList(), parent );
141     }
142 
143 }