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 org.apache.maven.artifact.handler.ArtifactHandlerMock;
22 import org.apache.maven.artifact.versioning.VersionRange;
23 import org.junit.jupiter.api.BeforeEach;
24
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27
28 public class DefaultArtifactTest {
29
30 private DefaultArtifact artifact;
31
32 private DefaultArtifact snapshotArtifact;
33
34 private String groupId = "groupid",
35 artifactId = "artifactId",
36 version = "1.0",
37 scope = "artifactScope",
38 type = "type",
39 classifier = "classifier";
40
41 private String snapshotSpecVersion = "1.0-SNAPSHOT";
42 private String snapshotResolvedVersion = "1.0-20070606.010101-1";
43
44 private VersionRange versionRange;
45 private VersionRange snapshotVersionRange;
46
47 private ArtifactHandlerMock artifactHandler;
48
49 @BeforeEach
50 protected void setUp() throws Exception {
51 artifactHandler = new ArtifactHandlerMock();
52 versionRange = VersionRange.createFromVersion(version);
53 artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, classifier, artifactHandler);
54
55 snapshotVersionRange = VersionRange.createFromVersion(snapshotResolvedVersion);
56 snapshotArtifact = new DefaultArtifact(
57 groupId, artifactId, snapshotVersionRange, scope, type, classifier, artifactHandler);
58 }
59
60 public void testGetVersionReturnsResolvedVersionOnSnapshot() {
61 assertEquals(snapshotResolvedVersion, snapshotArtifact.getVersion());
62
63
64
65
66 assertEquals(snapshotSpecVersion, snapshotArtifact.getBaseVersion());
67 }
68
69 public void testGetDependencyConflictId() {
70 assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
71 }
72
73 public void testGetDependencyConflictIdNullGroupId() {
74 artifact.setGroupId(null);
75 assertEquals(null + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
76 }
77
78 public void testGetDependencyConflictIdNullClassifier() {
79 artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, null, artifactHandler);
80 assertEquals(groupId + ":" + artifactId + ":" + type, artifact.getDependencyConflictId());
81 }
82
83 public void testGetDependencyConflictIdNullScope() {
84 artifact.setScope(null);
85 assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
86 }
87
88 public void testToString() {
89 assertEquals(
90 groupId + ":" + artifactId + ":" + type + ":" + classifier + ":" + version + ":" + scope,
91 artifact.toString());
92 }
93
94 public void testToStringNullGroupId() {
95 artifact.setGroupId(null);
96 assertEquals(artifactId + ":" + type + ":" + classifier + ":" + version + ":" + scope, artifact.toString());
97 }
98
99 public void testToStringNullClassifier() {
100 artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, null, artifactHandler);
101 assertEquals(groupId + ":" + artifactId + ":" + type + ":" + version + ":" + scope, artifact.toString());
102 }
103
104 public void testToStringNullScope() {
105 artifact.setScope(null);
106 assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier + ":" + version, artifact.toString());
107 }
108
109 public void testComparisonByVersion() {
110 Artifact artifact1 = new DefaultArtifact(
111 groupId, artifactId, VersionRange.createFromVersion("5.0"), scope, type, classifier, artifactHandler);
112 Artifact artifact2 = new DefaultArtifact(
113 groupId, artifactId, VersionRange.createFromVersion("12.0"), scope, type, classifier, artifactHandler);
114
115 assertTrue(artifact1.compareTo(artifact2) < 0);
116 assertTrue(artifact2.compareTo(artifact1) > 0);
117
118 Artifact artifact = new DefaultArtifact(
119 groupId, artifactId, VersionRange.createFromVersion("5.0"), scope, type, classifier, artifactHandler);
120 assertTrue(artifact.compareTo(artifact1) == 0);
121 assertTrue(artifact1.compareTo(artifact) == 0);
122 }
123
124 public void testNonResolvedVersionRangeConsistentlyYieldsNullVersions() throws Exception {
125 VersionRange vr = VersionRange.createFromVersionSpec("[1.0,2.0)");
126 artifact = new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
127 assertEquals(null, artifact.getVersion());
128 assertEquals(null, artifact.getBaseVersion());
129 }
130 }