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 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          // this is FOUL!
64          //        snapshotArtifact.isSnapshot();
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 }