1 package org.apache.maven.shared.artifact.filter.resolve.transform;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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 }