View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact;
20  
21  import junit.framework.TestCase;
22  import org.apache.maven.artifact.handler.ArtifactHandlerMock;
23  import org.apache.maven.artifact.versioning.VersionRange;
24  
25  public class DefaultArtifactTest extends TestCase {
26  
27      private DefaultArtifact artifact;
28  
29      private DefaultArtifact snapshotArtifact;
30  
31      private String groupId = "groupid",
32              artifactId = "artifactId",
33              version = "1.0",
34              scope = "artifactScope",
35              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() throws Exception {
47          super.setUp();
48          artifactHandler = new ArtifactHandlerMock();
49          versionRange = VersionRange.createFromVersion(version);
50          artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, classifier, artifactHandler);
51  
52          snapshotVersionRange = VersionRange.createFromVersion(snapshotResolvedVersion);
53          snapshotArtifact = new DefaultArtifact(
54                  groupId, artifactId, snapshotVersionRange, scope, type, classifier, artifactHandler);
55      }
56  
57      public void testGetVersionReturnsResolvedVersionOnSnapshot() {
58          assertEquals(snapshotResolvedVersion, snapshotArtifact.getVersion());
59  
60          // this is FOUL!
61          //        snapshotArtifact.isSnapshot();
62  
63          assertEquals(snapshotSpecVersion, snapshotArtifact.getBaseVersion());
64      }
65  
66      public void testGetDependencyConflictId() {
67          assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
68      }
69  
70      public void testGetDependencyConflictIdNullGroupId() {
71          artifact.setGroupId(null);
72          assertEquals(null + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
73      }
74  
75      public void testGetDependencyConflictIdNullClassifier() {
76          artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, null, artifactHandler);
77          assertEquals(groupId + ":" + artifactId + ":" + type, artifact.getDependencyConflictId());
78      }
79  
80      public void testGetDependencyConflictIdNullScope() {
81          artifact.setScope(null);
82          assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId());
83      }
84  
85      public void testToString() {
86          assertEquals(
87                  groupId + ":" + artifactId + ":" + type + ":" + classifier + ":" + version + ":" + scope,
88                  artifact.toString());
89      }
90  
91      public void testToStringNullGroupId() {
92          artifact.setGroupId(null);
93          assertEquals(artifactId + ":" + type + ":" + classifier + ":" + version + ":" + scope, artifact.toString());
94      }
95  
96      public void testToStringNullClassifier() {
97          artifact = new DefaultArtifact(groupId, artifactId, versionRange, scope, type, null, artifactHandler);
98          assertEquals(groupId + ":" + artifactId + ":" + type + ":" + version + ":" + scope, artifact.toString());
99      }
100 
101     public void testToStringNullScope() {
102         artifact.setScope(null);
103         assertEquals(groupId + ":" + artifactId + ":" + type + ":" + classifier + ":" + version, artifact.toString());
104     }
105 
106     public void testComparisonByVersion() {
107         Artifact artifact1 = new DefaultArtifact(
108                 groupId, artifactId, VersionRange.createFromVersion("5.0"), scope, type, classifier, artifactHandler);
109         Artifact artifact2 = new DefaultArtifact(
110                 groupId, artifactId, VersionRange.createFromVersion("12.0"), scope, type, classifier, artifactHandler);
111 
112         assertTrue(artifact1.compareTo(artifact2) < 0);
113         assertTrue(artifact2.compareTo(artifact1) > 0);
114 
115         Artifact artifact = new DefaultArtifact(
116                 groupId, artifactId, VersionRange.createFromVersion("5.0"), scope, type, classifier, artifactHandler);
117         assertTrue(artifact.compareTo(artifact1) == 0);
118         assertTrue(artifact1.compareTo(artifact) == 0);
119     }
120 
121     public void testNonResolvedVersionRangeConsistentlyYieldsNullVersions() throws Exception {
122         VersionRange vr = VersionRange.createFromVersionSpec("[1.0,2.0)");
123         artifact = new DefaultArtifact(groupId, artifactId, vr, scope, type, null, artifactHandler);
124         assertEquals(null, artifact.getVersion());
125         assertEquals(null, artifact.getBaseVersion());
126     }
127 }