001    package org.apache.maven.repository.legacy.resolver.conflict;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.artifact.Artifact;
023    import org.apache.maven.artifact.factory.ArtifactFactory;
024    import org.apache.maven.artifact.resolver.ResolutionNode;
025    import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
026    import org.apache.maven.artifact.versioning.VersionRange;
027    import org.apache.maven.repository.legacy.resolver.conflict.ConflictResolver;
028    import org.codehaus.plexus.PlexusTestCase;
029    
030    /**
031     * Provides a basis for testing conflict resolvers.
032     *
033     * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
034     */
035    public abstract class AbstractConflictResolverTest
036        extends PlexusTestCase
037    {
038        // constants --------------------------------------------------------------
039        
040        private static final String GROUP_ID = "test";
041        
042        // fields -----------------------------------------------------------------
043        
044        protected Artifact a1;
045    
046        protected Artifact a2;
047    
048        protected Artifact b1;
049    
050        private final String roleHint;
051        
052        private ArtifactFactory artifactFactory;
053    
054        private ConflictResolver conflictResolver;
055        
056        // constructors -----------------------------------------------------------
057        
058        public AbstractConflictResolverTest( String roleHint )
059            throws Exception
060        {
061            this.roleHint = roleHint;
062        }
063        
064        // TestCase methods -------------------------------------------------------
065        
066        /*
067         * @see junit.framework.TestCase#setUp()
068         */
069        protected void setUp() throws Exception
070        {
071            super.setUp();
072    
073            artifactFactory = (ArtifactFactory) lookup( ArtifactFactory.ROLE );
074            conflictResolver = (ConflictResolver) lookup( ConflictResolver.ROLE, roleHint );
075            
076            a1 = createArtifact( "a", "1.0" );
077            a2 = createArtifact( "a", "2.0" );
078            b1 = createArtifact( "b", "1.0" );
079        }
080        
081        /*
082         * @see org.codehaus.plexus.PlexusTestCase#tearDown()
083         */
084        protected void tearDown() throws Exception
085        {
086            a1 = null;
087            a2 = null;
088            b1 = null;
089            
090            artifactFactory = null;
091            conflictResolver = null;
092            
093            super.tearDown();
094        }
095        
096        // protected methods ------------------------------------------------------
097        
098        protected ConflictResolver getConflictResolver()
099        {
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    }