View Javadoc
1   package org.apache.maven.shared.artifact.filter.resolve.transform;
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 static org.junit.Assert.assertEquals;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.apache.maven.model.Dependency;
27  import org.apache.maven.plugin.testing.ArtifactStubFactory;
28  import org.apache.maven.shared.artifact.filter.resolve.Node;
29  import org.eclipse.aether.graph.DependencyNode;
30  import org.junit.Test;
31  
32  public class ArtifactIncludeNodeTest
33  {
34      private ArtifactStubFactory artifactFactory = new ArtifactStubFactory();
35  
36      @Test
37      public void testGAV() throws Exception
38      {
39          Node node = new ArtifactIncludeNode( newArtifact( "g:a:v", null ) );
40  
41          Dependency dependency = node.getDependency();
42  
43          assertEquals( "g", dependency.getGroupId() );
44          assertEquals( "a", dependency.getArtifactId() );
45          assertEquals( "v", dependency.getVersion() );
46          assertEquals( "", dependency.getClassifier() );
47          // This is different compared to AetherNodes. Here it's based on artifact, which in the end always has a type.
48          assertEquals( "jar", dependency.getType() );
49      }
50  
51      @Test
52      public void testClassifier() throws Exception
53      {
54          Node node = new ArtifactIncludeNode( newArtifact( "g:a::c:v", null ) );
55  
56          Dependency dependency = node.getDependency();
57  
58          assertEquals( "g", dependency.getGroupId() );
59          assertEquals( "a", dependency.getArtifactId() );
60          assertEquals( "v", dependency.getVersion() );
61          assertEquals( "c", dependency.getClassifier() );
62          // empty type stays empty type when using ArtifactStubFactory
63          assertEquals( "", dependency.getType() );
64      }
65  
66      @Test
67      public void testType() throws Exception
68      {
69          Node node = new ArtifactIncludeNode( newArtifact( "g:a:pom:v", null ) );
70  
71          Dependency dependency = node.getDependency();
72  
73          assertEquals( "g", dependency.getGroupId() );
74          assertEquals( "a", dependency.getArtifactId() );
75          assertEquals( "v", dependency.getVersion() );
76          assertEquals( null, dependency.getClassifier() );
77          assertEquals( "pom", dependency.getType() );
78      }
79  
80      @Test
81      public void testScope() throws Exception
82      {
83          Node node = new ArtifactIncludeNode( newArtifact( "g:a:v", "s" ) );
84  
85          Dependency dependency = node.getDependency();
86  
87          assertEquals( "g", dependency.getGroupId() );
88          assertEquals( "a", dependency.getArtifactId() );
89          assertEquals( "v", dependency.getVersion() );
90          assertEquals( "", dependency.getClassifier() );
91          assertEquals( "jar", dependency.getType() );
92          assertEquals( "s", dependency.getScope() );
93      }
94  
95      @Test
96      public void testOptional() throws Exception
97      {
98          Node node = new ArtifactIncludeNode( newArtifact( "g:a:pom:v", null, null ) );
99          
100         assertEquals( "false", node.getDependency().getOptional()  );
101         assertEquals( false, node.getDependency().isOptional()  );
102         
103         node = new ArtifactIncludeNode( newArtifact( "g:a:pom:v", null, true ) );
104         assertEquals( "true", node.getDependency().getOptional()  );
105         assertEquals( true, node.getDependency().isOptional()  );
106 
107         node = new ArtifactIncludeNode( newArtifact( "g:a:pom:v", null, false ) );
108         assertEquals( "false", node.getDependency().getOptional()  );
109         assertEquals( false, node.getDependency().isOptional()  );
110     }
111 
112     private Artifact newArtifact( String coor, String scope )
113         throws Exception
114     {
115         return newArtifact( coor, scope, null );
116     }
117 
118     private Artifact newArtifact( String coor, String scope, Boolean optional )
119         throws Exception
120     {
121         String[] gav = coor.split( ":" );
122         String groupId = gav[0];
123         String artifactId = gav[1];
124         String version = null;
125         String classifier = null;
126         String type = null;
127 
128         if ( gav.length == 3 )
129         {
130             version = gav[2];
131         }
132         else if ( gav.length == 4 )
133         {
134             type = gav[2];
135             version = gav[3];
136         }        
137         else if ( gav.length == 5 )
138         {
139             type = gav[2];
140             classifier = gav[3];
141             version = gav[4];
142         }        
143         
144         if( optional != null )
145         {
146             VersionRange versionRange = VersionRange.createFromVersion( version );
147             return artifactFactory.createArtifact( groupId, artifactId, versionRange, scope, type, classifier, optional );
148         }
149         else if ( gav.length == 3 )
150         {
151             return artifactFactory.createArtifact( groupId, artifactId, version, scope );
152         }
153         else if ( gav.length == 4 )
154         {
155             return artifactFactory.createArtifact( groupId, artifactId, version, scope, type, null );
156         }
157         else if ( gav.length == 5 )
158         {
159             return artifactFactory.createArtifact( groupId, artifactId, version, scope, type, classifier );
160         }
161         else
162         {
163             throw new IllegalArgumentException( "Can't translate coor to an Artifact" );
164         }
165     }
166 }