View Javadoc

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