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