1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.basepom.mojo.duplicatefinder.artifact;
15
16 import org.apache.maven.artifact.Artifact;
17 import org.apache.maven.artifact.DefaultArtifact;
18 import org.apache.maven.artifact.handler.DefaultArtifactHandler;
19 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
20 import org.apache.maven.artifact.versioning.VersionRange;
21 import org.apache.maven.model.Dependency;
22 import org.junit.Test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27
28 public class TestMavenCoordinates {
29
30 @Test
31 public void testCreateFromDependencyBasic() throws java.lang.Exception {
32 final Dependency dependency = new Dependency();
33 dependency.setGroupId("test.group");
34 dependency.setArtifactId("test-artifact");
35 dependency.setVersion("1.0");
36
37 final MavenCoordinates coordinates = new MavenCoordinates(dependency);
38 assertEquals("test.group", coordinates.getGroupId());
39 assertEquals("test-artifact", coordinates.getArtifactId());
40 assertEquals("jar", coordinates.getType());
41
42 assertTrue(coordinates.getVersion().isPresent());
43 assertTrue(coordinates.getVersionRange().isPresent());
44 assertEquals(new DefaultArtifactVersion("1.0"), coordinates.getVersion().get());
45
46 assertFalse(coordinates.getClassifier().isPresent());
47 }
48
49 @Test
50 public void testCreateFromDependencyWithClassifier() throws java.lang.Exception {
51 final Dependency dependency = new Dependency();
52 dependency.setGroupId("test.group");
53 dependency.setArtifactId("test-artifact");
54 dependency.setVersion("1.0");
55 dependency.setClassifier("jdk17");
56
57 final MavenCoordinates coordinates = new MavenCoordinates(dependency);
58 assertEquals("test.group", coordinates.getGroupId());
59 assertEquals("test-artifact", coordinates.getArtifactId());
60 assertEquals("jar", coordinates.getType());
61
62 assertTrue(coordinates.getVersion().isPresent());
63 assertTrue(coordinates.getVersionRange().isPresent());
64 assertEquals(new DefaultArtifactVersion("1.0"), coordinates.getVersion().get());
65
66 assertTrue(coordinates.getClassifier().isPresent());
67 assertEquals("jdk17", coordinates.getClassifier().get());
68 }
69
70 @Test
71 public void testCreateFromDependencyWithVersionRange() throws java.lang.Exception {
72 final Dependency dependency = new Dependency();
73 dependency.setGroupId("test.group");
74 dependency.setArtifactId("test-artifact");
75 dependency.setVersion("[1.0, 2.0)");
76
77 final MavenCoordinates coordinates = new MavenCoordinates(dependency);
78 assertEquals("test.group", coordinates.getGroupId());
79 assertEquals("test-artifact", coordinates.getArtifactId());
80 assertEquals("jar", coordinates.getType());
81
82 assertTrue(coordinates.getVersion().isPresent());
83 assertEquals(new DefaultArtifactVersion("[1.0, 2.0)"), coordinates.getVersion().get());
84
85 assertTrue(coordinates.getVersionRange().isPresent());
86 assertTrue(coordinates.getVersionRange().get().containsVersion(new DefaultArtifactVersion("1.1")));
87 assertFalse(coordinates.getVersionRange().get().containsVersion(new DefaultArtifactVersion("2.0")));
88 }
89
90 @Test
91 public void testCreateFromArtifactBasic() throws java.lang.Exception {
92 final Artifact artifact = new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", null, new DefaultArtifactHandler());
93
94 final MavenCoordinates coordinates = new MavenCoordinates(artifact);
95 assertEquals("test.group", coordinates.getGroupId());
96 assertEquals("test-artifact", coordinates.getArtifactId());
97 assertEquals("jar", coordinates.getType());
98
99 assertTrue(coordinates.getVersion().isPresent());
100 assertTrue(coordinates.getVersionRange().isPresent());
101 assertEquals(new DefaultArtifactVersion("1.0"), coordinates.getVersion().get());
102
103 assertFalse(coordinates.getClassifier().isPresent());
104 }
105
106 @Test
107 public void testCreateFromArtifactWithClassifier() throws java.lang.Exception {
108 final Artifact artifact = new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", "jdk17", new DefaultArtifactHandler());
109
110 final MavenCoordinates coordinates = new MavenCoordinates(artifact);
111 assertEquals("test.group", coordinates.getGroupId());
112 assertEquals("test-artifact", coordinates.getArtifactId());
113 assertEquals("jar", coordinates.getType());
114
115 assertTrue(coordinates.getVersion().isPresent());
116 assertTrue(coordinates.getVersionRange().isPresent());
117 assertEquals(new DefaultArtifactVersion("1.0"), coordinates.getVersion().get());
118
119 assertTrue(coordinates.getClassifier().isPresent());
120 assertEquals("jdk17", coordinates.getClassifier().get());
121 }
122
123 @Test
124 public void testCreateFromArtifactWithVersionRange() throws java.lang.Exception {
125 final VersionRange versionRange = VersionRange.createFromVersionSpec("[1.0, 2.0)");
126 final Artifact artifact = new DefaultArtifact("test.group", "test-artifact", versionRange, "test", "jar", "jdk17", new DefaultArtifactHandler());
127
128 final MavenCoordinates coordinates = new MavenCoordinates(artifact);
129 assertEquals("test.group", coordinates.getGroupId());
130 assertEquals("test-artifact", coordinates.getArtifactId());
131 assertEquals("jar", coordinates.getType());
132
133 assertFalse(coordinates.getVersion().isPresent());
134 assertTrue(coordinates.getVersionRange().isPresent());
135 }
136
137 @Test
138 public void testCreateFromArtifactWithVersionRangeRestricted() throws java.lang.Exception {
139 final VersionRange versionRange = VersionRange.createFromVersionSpec("[1.0, 2.0)").restrict(VersionRange.createFromVersion("1.2"));
140 final Artifact artifact = new DefaultArtifact("test.group", "test-artifact", versionRange, "test", "jar", "jdk17", new DefaultArtifactHandler());
141
142 final MavenCoordinates coordinates = new MavenCoordinates(artifact);
143 assertEquals("test.group", coordinates.getGroupId());
144 assertEquals("test-artifact", coordinates.getArtifactId());
145 assertEquals("jar", coordinates.getType());
146
147 assertTrue(coordinates.getVersion().isPresent());
148 assertEquals(new DefaultArtifactVersion("1.2"), coordinates.getVersion().get());
149
150 assertTrue(coordinates.getVersionRange().isPresent());
151 }
152
153 @Test
154 public void testTestJar() throws java.lang.Exception {
155 MavenCoordinates coordinates = new MavenCoordinates(
156 new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "test-jar", null, new DefaultArtifactHandler()));
157 assertEquals("jar", coordinates.getType());
158 assertTrue(coordinates.getClassifier().isPresent());
159 assertEquals("tests", coordinates.getClassifier().get());
160
161 coordinates = new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", "tests", new DefaultArtifactHandler()));
162 assertEquals("jar", coordinates.getType());
163 assertTrue(coordinates.getClassifier().isPresent());
164 assertEquals("tests", coordinates.getClassifier().get());
165
166
167 coordinates = new MavenCoordinates(
168 new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "test-jar", "special", new DefaultArtifactHandler()));
169 assertEquals("jar", coordinates.getType());
170 assertTrue(coordinates.getClassifier().isPresent());
171 assertEquals("special", coordinates.getClassifier().get());
172 }
173
174 @Test
175 public void testBasicMismatches() throws java.lang.Exception {
176 final Dependency dependency = new Dependency();
177 dependency.setGroupId("test.group");
178 dependency.setArtifactId("test-artifact");
179 dependency.setVersion("1.0");
180 final MavenCoordinates dependencyCoordinates = new MavenCoordinates(dependency);
181
182
183 assertFalse(dependencyCoordinates.matches(
184 new MavenCoordinates(new DefaultArtifact("test.group2", "test-artifact", "1.0", "test", "jar", null, new DefaultArtifactHandler()))));
185
186 assertFalse(dependencyCoordinates.matches(
187 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact2", "1.0", "test", "jar", null, new DefaultArtifactHandler()))));
188
189 assertFalse(dependencyCoordinates.matches(
190 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "war", null, new DefaultArtifactHandler()))));
191
192
193 assertTrue(dependencyCoordinates.matches(
194 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", null, new DefaultArtifactHandler()))));
195 assertTrue(dependencyCoordinates.matches(
196 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", "foo", new DefaultArtifactHandler()))));
197 assertTrue(dependencyCoordinates.matches(
198 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", "bar", new DefaultArtifactHandler()))));
199
200 dependency.setClassifier("baz");
201 final MavenCoordinates dependencyCoordinatesWithClassifier = new MavenCoordinates(dependency);
202
203
204 assertTrue(dependencyCoordinatesWithClassifier.matches(
205 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", "baz", new DefaultArtifactHandler()))));
206
207
208 assertFalse(dependencyCoordinatesWithClassifier.matches(
209 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", null, new DefaultArtifactHandler()))));
210 assertFalse(dependencyCoordinatesWithClassifier.matches(
211 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", "bar", new DefaultArtifactHandler()))));
212 }
213
214 @Test
215 public void testVersionMatches() throws java.lang.Exception {
216 final Dependency dependency = new Dependency();
217 dependency.setGroupId("test.group");
218 dependency.setArtifactId("test-artifact");
219 final MavenCoordinates dependencyCoordinatesNoVersionNoVersionRange = new MavenCoordinates(dependency);
220
221
222 assertTrue(dependencyCoordinatesNoVersionNoVersionRange
223 .matches(new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", null, new DefaultArtifactHandler()))));
224 assertTrue(dependencyCoordinatesNoVersionNoVersionRange
225 .matches(new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "2.0", "test", "jar", null, new DefaultArtifactHandler()))));
226
227 dependency.setVersion("1.0");
228 final MavenCoordinates dependencyCoordinatesVersion = new MavenCoordinates(dependency);
229
230
231 assertFalse(dependencyCoordinatesVersion
232 .matches(new MavenCoordinates(
233 new DefaultArtifact("test.group", "test-artifact", VersionRange.createFromVersionSpec("[1.0, 2.0)"), "test", "jar", null,
234 new DefaultArtifactHandler()))));
235
236
237 assertFalse(dependencyCoordinatesVersion.matches(
238 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.1", "test", "jar", null, new DefaultArtifactHandler()))));
239 assertTrue(dependencyCoordinatesVersion.matches(
240 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", null, new DefaultArtifactHandler()))));
241
242 dependency.setVersion("[2.0, 3.0)");
243 final MavenCoordinates dependencyCoordinatesVersionRange = new MavenCoordinates(dependency);
244
245
246 assertFalse(dependencyCoordinatesVersionRange.matches(
247 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "1.0", "test", "jar", null, new DefaultArtifactHandler()))));
248 assertFalse(dependencyCoordinatesVersionRange.matches(
249 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "3.0", "test", "jar", null, new DefaultArtifactHandler()))));
250
251
252 assertTrue(dependencyCoordinatesVersionRange.matches(
253 new MavenCoordinates(new DefaultArtifact("test.group", "test-artifact", "2.2", "test", "jar", null, new DefaultArtifactHandler()))));
254
255 }
256
257 }