View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.utils.translators;
20  
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Set;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.factory.ArtifactFactory;
28  import org.apache.maven.artifact.factory.DefaultArtifactFactory;
29  import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
30  import org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager;
31  import org.apache.maven.artifact.repository.ArtifactRepository;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.plugin.LegacySupport;
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.shared.transfer.artifact.ArtifactCoordinate;
41  
42  /**
43   * @author brianf
44   */
45  public class TestClassifierTypeTranslator extends AbstractDependencyMojoTestCase {
46      Set<Artifact> artifacts = new HashSet<>();
47  
48      ArtifactFactory artifactFactory;
49  
50      ArtifactRepository artifactRepository;
51  
52      Log log = new SilentLog();
53  
54      private ArtifactHandlerManager artifactHandlerManager;
55  
56      @Override
57      protected void setUp() throws Exception {
58          super.setUp("classifiertype-translator", false);
59  
60          artifactHandlerManager = new DefaultArtifactHandlerManager();
61          this.setVariableValueToObject(artifactHandlerManager, "artifactHandlers", new HashMap<>());
62  
63          artifactFactory = new DefaultArtifactFactory();
64          this.setVariableValueToObject(artifactFactory, "artifactHandlerManager", artifactHandlerManager);
65  
66          artifactRepository = new StubArtifactRepository(null);
67  
68          DependencyArtifactStubFactory factory = new DependencyArtifactStubFactory(null, false);
69          artifacts = factory.getMixedArtifacts();
70  
71          LegacySupport legacySupport = lookup(LegacySupport.class);
72          MavenSession session = newMavenSession(new MavenProjectStub());
73          legacySupport.setSession(session);
74  
75          installLocalRepository(legacySupport);
76      }
77  
78      public void testNullClassifier() {
79          doTestNullEmptyClassifier(null);
80      }
81  
82      public void testEmptyClassifier() {
83          doTestNullEmptyClassifier("");
84      }
85  
86      public void doTestNullEmptyClassifier(String classifier) {
87          String type = "zip";
88  
89          ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
90          Set<ArtifactCoordinate> results = at.translate(artifacts, log);
91  
92          for (Artifact artifact : artifacts) {
93              Iterator<ArtifactCoordinate> resultIter = results.iterator();
94              boolean found = false;
95              while (resultIter.hasNext()) {
96                  ArtifactCoordinate translatedArtifact = resultIter.next();
97                  if (artifact.getArtifactId().equals(translatedArtifact.getArtifactId())
98                          && artifact.getGroupId().equals(translatedArtifact.getGroupId())
99                  /* && artifact.getScope().equals(translatedArtifact.getScope()) */ ) {
100                     // classifier is null, should be the same as the artifact
101                     assertEquals(artifact.getClassifier(), translatedArtifact.getClassifier());
102                     assertEquals(type, translatedArtifact.getExtension());
103 
104                     found = true;
105                     break;
106                 }
107             }
108             assertTrue(found);
109         }
110     }
111 
112     public void testNullType() {
113         doTestNullEmptyType(null);
114     }
115 
116     public void testEmptyType() {
117         doTestNullEmptyType("");
118     }
119 
120     public void doTestNullEmptyType(String type) {
121         String classifier = "jdk5";
122 
123         ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
124         Set<ArtifactCoordinate> results = at.translate(artifacts, log);
125 
126         for (Artifact artifact : artifacts) {
127             Iterator<ArtifactCoordinate> resultIter = results.iterator();
128             boolean found = false;
129             while (!found && resultIter.hasNext()) {
130                 ArtifactCoordinate translatedArtifact = resultIter.next();
131                 if (artifact.getArtifactId() == translatedArtifact.getArtifactId()
132                         && artifact.getGroupId() == translatedArtifact.getGroupId()
133                 /* && artifact.getScope() == translatedArtifact.getScope() */ ) {
134                     // classifier is null, should be the same as the artifact
135                     assertEquals(classifier, translatedArtifact.getClassifier());
136                     assertEquals(artifact.getType(), translatedArtifact.getExtension());
137 
138                     found = true;
139                     break;
140                 }
141             }
142             assertTrue(found);
143         }
144     }
145 
146     public void testClassifierAndType() {
147         String classifier = "jdk14";
148         String type = "sources";
149         ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
150         Set<ArtifactCoordinate> results = at.translate(artifacts, log);
151 
152         for (Artifact artifact : artifacts) {
153             Iterator<ArtifactCoordinate> resultIter = results.iterator();
154             boolean found = false;
155             while (!found && resultIter.hasNext()) {
156                 ArtifactCoordinate translatedArtifact = resultIter.next();
157                 if (artifact.getArtifactId() == translatedArtifact.getArtifactId()
158                         && artifact.getGroupId() == translatedArtifact.getGroupId()) {
159                     assertEquals(translatedArtifact.getClassifier(), classifier);
160                     assertEquals(translatedArtifact.getExtension(), type);
161 
162                     found = true;
163                     break;
164                 }
165             }
166             assertTrue(found);
167         }
168     }
169 
170     public void testGetterSetter() {
171         String classifier = "class";
172         String type = "type";
173         ClassifierTypeTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
174 
175         assertEquals(classifier, at.getClassifier());
176         assertEquals(type, at.getType());
177 
178         at.setClassifier(type);
179         at.setType(classifier);
180 
181         assertEquals(type, at.getClassifier());
182         assertEquals(classifier, at.getType());
183     }
184 }