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