1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.artifact;
20
21 import java.util.stream.Stream;
22
23 import org.apache.maven.artifact.handler.ArtifactHandlerMock;
24 import org.apache.maven.artifact.versioning.VersionRange;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.Arguments;
29 import org.junit.jupiter.params.provider.MethodSource;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertNull;
33 import static org.junit.jupiter.api.Assertions.assertThrows;
34 import static org.junit.jupiter.api.Assertions.assertTrue;
35
36 class DefaultArtifactTest {
37
38 private DefaultArtifact artifact;
39
40 private DefaultArtifact snapshotArtifact;
41
42 private String groupId = "groupid",
43 artifactId = "artifactId",
44 version = "1.0",
45 scope = "artifactScope",
46 type = "type",
47 classifier = "classifier";
48
49 private String snapshotSpecVersion = "1.0-SNAPSHOT";
50 private String snapshotResolvedVersion = "1.0-20070606.010101-1";
51
52 private VersionRange versionRange;
53 private VersionRange snapshotVersionRange;
54
55 private ArtifactHandlerMock artifactHandler;
56
57 @BeforeEach
58 void setUp() throws Exception {
59 artifactHandler = new ArtifactHandlerMock();
60 versionRange = VersionRange.createFromVersion(version);
61 artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, classifier, artifactHandler);
62
63 snapshotVersionRange = VersionRange.createFromVersion(snapshotResolvedVersion);
64 snapshotArtifact = new DefaultArtifact(
65 groupId, artifactId, snapshotVersionRange, scope, type, classifier, artifactHandler);
66 }
67
68 @Test
69 void testGetVersionReturnsResolvedVersionOnSnapshot() {
70 assertEquals(snapshotResolvedVersion, snapshotArtifact.getVersion());
71
72
73
74
75 assertEquals(snapshotSpecVersion, snapshotArtifact.getBaseVersion());
76 }
77
78 @Test
79 void testGetDependencyConflictId() {
80 assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
81 }
82
83 @Test
84 void testGetDependencyConflictIdNullGroupId() {
85 artifact.setGroupId(null);
86 assertEquals(null + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
87 }
88
89 @Test
90 void testGetDependencyConflictIdNullClassifier() {
91 artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, null, artifactHandler);
92 assertEquals(groupId + ":" + artifactId + ":" + type, artifact.getDependencyConflictId());
93 }
94
95 @Test
96 void testGetDependencyConflictIdNullScope() {
97 artifact.setScope(null);
98 assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
99 }
100
101 @Test
102 void testToString() {
103 assertEquals(
104 groupId + ":" + artifactId + ":" + type + ":" + classifier + ":" + version + ":" + scope,
105 artifact.toString());
106 }
107
108 @Test
109 void testToStringNullGroupId() {
110 artifact.setGroupId(null);
111 assertEquals(artifactId + ":" + type + ":" + classifier + ":" + version + ":" + scope, artifact.toString());
112 }
113
114 @Test
115 void testToStringNullClassifier() {
116 artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, null, artifactHandler);
117 assertEquals(groupId + ":" + artifactId + ":" + type + ":" + version + ":" + scope, artifact.toString());
118 }
119
120 @Test
121 void testToStringNullScope() {
122 artifact.setScope(null);
123 assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier + ":" + version, artifact.toString());
124 }
125
126 @Test
127 void testComparisonByVersion() {
128 Artifact artifact1 = new DefaultArtifact(
129 groupId, artifactId, VersionRange.createFromVersion("5.0"), scope, type, classifier, artifactHandler);
130 Artifact artifact2 = new DefaultArtifact(
131 groupId, artifactId, VersionRange.createFromVersion("12.0"), scope, type, classifier, artifactHandler);
132
133 assertTrue(artifact1.compareTo(artifact2) < 0);
134 assertTrue(artifact2.compareTo(artifact1) > 0);
135
136 Artifact artifact = new DefaultArtifact(
137 groupId, artifactId, VersionRange.createFromVersion("5.0"), scope, type, classifier, artifactHandler);
138 assertTrue(artifact.compareTo(artifact1) == 0);
139 assertTrue(artifact1.compareTo(artifact) == 0);
140 }
141
142 @Test
143 void testNonResolvedVersionRangeConsistentlyYieldsNullVersions() throws Exception {
144 VersionRange vr = VersionRange.createFromVersionSpec("[1.0,2.0)");
145 artifact = new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
146 assertNull(artifact.getVersion());
147 assertNull(artifact.getBaseVersion());
148 }
149
150 @Test
151 void testMNG7780() throws Exception {
152 VersionRange vr = VersionRange.createFromVersionSpec("[1.0,2.0)");
153 artifact = new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
154 assertNull(artifact.getVersion());
155 assertNull(artifact.getBaseVersion());
156
157 DefaultArtifact nullVersionArtifact =
158 new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
159 assertEquals(artifact, nullVersionArtifact);
160 }
161
162 @ParameterizedTest
163 @MethodSource("invalidMavenCoordinates")
164 void testIllegalCoordinatesInConstructor(String groupId, String artifactId, String version) {
165 assertThrows(
166 InvalidArtifactRTException.class,
167 () -> new DefaultArtifact(
168 groupId, artifactId, version, scope, type, classifier, artifactHandler, false));
169 if (version == null) {
170 assertThrows(
171 InvalidArtifactRTException.class,
172 () -> new DefaultArtifact(
173 groupId, artifactId, (VersionRange) null, scope, type, classifier, artifactHandler, false));
174 }
175 }
176
177 static Stream<Arguments> invalidMavenCoordinates() {
178 return Stream.of(
179 Arguments.of(null, null, null),
180 Arguments.of("", "", ""),
181 Arguments.of(null, "artifactId", "1.0"),
182 Arguments.of("", "artifactId", "1.0"),
183 Arguments.of("groupId", null, "1.0"),
184 Arguments.of("groupId", "", "1.0"),
185 Arguments.of("groupId", "artifactId", null),
186 Arguments.of("groupId", "artifactId", ""));
187 }
188 }