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