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