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