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