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