View Javadoc
1   package org.apache.maven.plugins.dependency.utils.translators;
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 java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.factory.ArtifactFactory;
29  import org.apache.maven.artifact.factory.DefaultArtifactFactory;
30  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
31  import org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager;
32  import org.apache.maven.artifact.repository.ArtifactRepository;
33  import org.apache.maven.execution.MavenSession;
34  import org.apache.maven.plugin.logging.Log;
35  import org.apache.maven.plugin.testing.SilentLog;
36  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
37  import org.apache.maven.plugin.testing.stubs.StubArtifactRepository;
38  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
39  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
40  import org.apache.maven.project.ProjectBuildingRequest;
41  import org.apache.maven.shared.artifact.ArtifactCoordinate;
42  import org.apache.maven.shared.repository.RepositoryManager;
43  import org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager;
44  import org.sonatype.aether.util.DefaultRepositorySystemSession;
45  
46  /**
47   * @author brianf
48   */
49  public class TestClassifierTypeTranslator
50      extends AbstractDependencyMojoTestCase
51  {
52      Set<Artifact> artifacts = new HashSet<Artifact>();
53  
54      ArtifactFactory artifactFactory;
55  
56      ArtifactRepository artifactRepository;
57  
58      Log log = new SilentLog();
59  
60      private RepositoryManager repoManager;
61  
62      private ProjectBuildingRequest buildingRequest;
63  
64      private ArtifactHandlerManager artifactHandlerManager;
65  
66      protected void setUp()
67          throws Exception
68      {
69          super.setUp( "classifiertype-translator", false );
70  
71          artifactHandlerManager = new DefaultArtifactHandlerManager();
72          this.setVariableValueToObject( artifactHandlerManager, "artifactHandlers", new HashMap() );
73  
74          artifactFactory = new DefaultArtifactFactory();
75          this.setVariableValueToObject( artifactFactory, "artifactHandlerManager", artifactHandlerManager );
76  
77          artifactRepository = new StubArtifactRepository( null );
78  
79          DependencyArtifactStubFactory factory = new DependencyArtifactStubFactory( null, false );
80          artifacts = factory.getMixedArtifacts();
81  
82          repoManager = lookup( RepositoryManager.class );
83  
84          MavenSession session = newMavenSession( new MavenProjectStub() );
85          buildingRequest = session.getProjectBuildingRequest();
86  
87          DefaultRepositorySystemSession repoSession = (DefaultRepositorySystemSession) session.getRepositorySession();
88          repoSession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( stubFactory.getWorkingDir() ) );
89  
90      }
91  
92      public void testNullClassifier()
93      {
94          doTestNullEmptyClassifier( null );
95      }
96  
97      public void testEmptyClassifier()
98      {
99          doTestNullEmptyClassifier( "" );
100     }
101 
102     public void doTestNullEmptyClassifier( String classifier )
103     {
104         String type = "zip";
105 
106         ArtifactTranslator at = new ClassifierTypeTranslator( artifactHandlerManager, classifier, type );
107         Set<ArtifactCoordinate> results = at.translate( artifacts, log );
108 
109         for ( Artifact artifact : artifacts )
110         {
111             Iterator<ArtifactCoordinate> resultIter = results.iterator();
112             boolean found = false;
113             while ( resultIter.hasNext() )
114             {
115                 ArtifactCoordinate translatedArtifact = resultIter.next();
116                 if ( artifact.getArtifactId().equals( translatedArtifact.getArtifactId() )
117                     && artifact.getGroupId().equals( translatedArtifact.getGroupId() )
118                 /* && artifact.getScope().equals(translatedArtifact.getScope()) */ )
119                 {
120                     // classifier is null, should be the same as the artifact
121                     assertEquals( artifact.getClassifier(), translatedArtifact.getClassifier() );
122                     assertEquals( type, translatedArtifact.getExtension() );
123 
124                     found = true;
125                     break;
126                 }
127             }
128             assertTrue( found );
129         }
130     }
131 
132     public void testNullType()
133     {
134         doTestNullEmptyType( null );
135     }
136 
137     public void testEmptyType()
138     {
139         doTestNullEmptyType( "" );
140     }
141 
142     public void doTestNullEmptyType( String type )
143     {
144         String classifier = "jdk5";
145 
146         ArtifactTranslator at = new ClassifierTypeTranslator( artifactHandlerManager, classifier, type );
147         Set<ArtifactCoordinate> results = at.translate( artifacts, log );
148 
149         for ( Artifact artifact : artifacts )
150         {
151             Iterator<ArtifactCoordinate> resultIter = results.iterator();
152             boolean found = false;
153             while ( !found && resultIter.hasNext() )
154             {
155                 ArtifactCoordinate translatedArtifact = resultIter.next();
156                 if ( artifact.getArtifactId() == translatedArtifact.getArtifactId()
157                     && artifact.getGroupId() == translatedArtifact.getGroupId()
158                 /* && artifact.getScope() == translatedArtifact.getScope() */ )
159                 {
160                     // classifier is null, should be the same as the artifact
161                     assertEquals( classifier, translatedArtifact.getClassifier() );
162                     assertEquals( artifact.getType(), translatedArtifact.getExtension() );
163 
164                     found = true;
165                     break;
166                 }
167             }
168             assertTrue( found );
169         }
170     }
171 
172     public void testClassifierAndType()
173     {
174         String classifier = "jdk14";
175         String type = "sources";
176         ArtifactTranslator at = new ClassifierTypeTranslator( artifactHandlerManager, classifier, type );
177         Set<ArtifactCoordinate> results = at.translate( artifacts, log );
178 
179         for ( Artifact artifact : artifacts )
180         {
181             Iterator<ArtifactCoordinate> resultIter = results.iterator();
182             boolean found = false;
183             while ( !found && resultIter.hasNext() )
184             {
185                 ArtifactCoordinate translatedArtifact = resultIter.next();
186                 if ( artifact.getArtifactId() == translatedArtifact.getArtifactId()
187                     && artifact.getGroupId() == translatedArtifact.getGroupId()
188                 /* && artifact.getScope() == translatedArtifact.getScope() */ )
189                 {
190                     assertEquals( translatedArtifact.getClassifier(), classifier );
191                     assertEquals( translatedArtifact.getExtension(), type );
192 
193                     found = true;
194                     break;
195                 }
196             }
197             assertTrue( found );
198         }
199     }
200 
201     public void testGetterSetter()
202     {
203         String classifier = "class";
204         String type = "type";
205         ClassifierTypeTranslator at = new ClassifierTypeTranslator( artifactHandlerManager, classifier, type );
206 
207         assertEquals( classifier, at.getClassifier() );
208         assertEquals( type, at.getType() );
209 
210         at.setClassifier( type );
211         at.setType( classifier );
212 
213         assertEquals( type, at.getClassifier() );
214         assertEquals( classifier, at.getType() );
215 
216     }
217 }