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.handler.manager.ArtifactHandlerManager;
28  import org.apache.maven.artifact.handler.manager.DefaultArtifactHandlerManager;
29  import org.apache.maven.plugin.logging.Log;
30  import org.apache.maven.plugin.testing.SilentLog;
31  import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
32  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
33  
34  /**
35   * @author brianf
36   */
37  public class TestClassifierTypeTranslator extends AbstractDependencyMojoTestCase {
38      Set<Artifact> artifacts = new HashSet<>();
39  
40      Log log = new SilentLog();
41  
42      private ArtifactHandlerManager artifactHandlerManager;
43  
44      @Override
45      protected String getTestDirectoryName() {
46          return "classifiertype-translator";
47      }
48  
49      @Override
50      protected boolean shouldUseFlattenedPath() {
51          return false;
52      }
53  
54      @Override
55      protected void setUp() throws Exception {
56          super.setUp();
57  
58          artifactHandlerManager = new DefaultArtifactHandlerManager();
59          this.setVariableValueToObject(artifactHandlerManager, "artifactHandlers", new HashMap<>());
60  
61          DependencyArtifactStubFactory factory = new DependencyArtifactStubFactory(null, false);
62          artifacts = factory.getMixedArtifacts();
63      }
64  
65      public void testNullClassifier() {
66          doTestNullEmptyClassifier(null);
67      }
68  
69      public void testEmptyClassifier() {
70          doTestNullEmptyClassifier("");
71      }
72  
73      public void doTestNullEmptyClassifier(String classifier) {
74          String type = "zip";
75  
76          ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
77          Set<org.eclipse.aether.artifact.Artifact> results = at.translate(artifacts, log);
78  
79          for (Artifact artifact : artifacts) {
80              Iterator<org.eclipse.aether.artifact.Artifact> resultIter = results.iterator();
81              boolean found = false;
82              while (resultIter.hasNext()) {
83                  org.eclipse.aether.artifact.Artifact translatedArtifact = resultIter.next();
84                  if (artifact.getArtifactId().equals(translatedArtifact.getArtifactId())
85                          && artifact.getGroupId().equals(translatedArtifact.getGroupId())
86                  /* && artifact.getScope().equals(translatedArtifact.getScope()) */ ) {
87                      // classifier is always empty for Resolver sub artifact
88                      assertEquals("", translatedArtifact.getClassifier());
89                      assertEquals(type, translatedArtifact.getExtension());
90  
91                      found = true;
92                      break;
93                  }
94              }
95              assertTrue(found);
96          }
97      }
98  
99      public void testNullType() {
100         doTestNullEmptyType(null);
101     }
102 
103     public void testEmptyType() {
104         doTestNullEmptyType("");
105     }
106 
107     public void doTestNullEmptyType(String type) {
108         String classifier = "jdk5";
109 
110         ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
111         Set<org.eclipse.aether.artifact.Artifact> results = at.translate(artifacts, log);
112 
113         for (Artifact artifact : artifacts) {
114             Iterator<org.eclipse.aether.artifact.Artifact> resultIter = results.iterator();
115             boolean found = false;
116             while (!found && resultIter.hasNext()) {
117                 org.eclipse.aether.artifact.Artifact translatedArtifact = resultIter.next();
118                 if (artifact.getArtifactId() == translatedArtifact.getArtifactId()
119                         && artifact.getGroupId() == translatedArtifact.getGroupId()
120                 /* && artifact.getScope() == translatedArtifact.getScope() */ ) {
121                     // classifier is null, should be the same as the artifact
122                     assertEquals(classifier, translatedArtifact.getClassifier());
123                     assertEquals(artifact.getType(), translatedArtifact.getExtension());
124 
125                     found = true;
126                     break;
127                 }
128             }
129             assertTrue(found);
130         }
131     }
132 
133     public void testClassifierAndType() {
134         String classifier = "jdk14";
135         String type = "sources";
136         ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
137         Set<org.eclipse.aether.artifact.Artifact> results = at.translate(artifacts, log);
138 
139         for (Artifact artifact : artifacts) {
140             Iterator<org.eclipse.aether.artifact.Artifact> resultIter = results.iterator();
141             boolean found = false;
142             while (!found && resultIter.hasNext()) {
143                 org.eclipse.aether.artifact.Artifact translatedArtifact = resultIter.next();
144                 if (artifact.getArtifactId() == translatedArtifact.getArtifactId()
145                         && artifact.getGroupId() == translatedArtifact.getGroupId()) {
146                     assertEquals(translatedArtifact.getClassifier(), classifier);
147                     assertEquals(translatedArtifact.getExtension(), type);
148 
149                     found = true;
150                     break;
151                 }
152             }
153             assertTrue(found);
154         }
155     }
156 
157     public void testGetterSetter() {
158         String classifier = "class";
159         String type = "type";
160         ClassifierTypeTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
161 
162         assertEquals(classifier, at.getClassifier());
163         assertEquals(type, at.getType());
164 
165         at.setClassifier(type);
166         at.setType(classifier);
167 
168         assertEquals(type, at.getClassifier());
169         assertEquals(classifier, at.getType());
170     }
171 }