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 java.util.ArrayList;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.maven.artifact.versioning.VersionRange;
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  import static org.junit.jupiter.api.Assertions.assertFalse;
30  import static org.junit.jupiter.api.Assertions.assertNotNull;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  /**
34   * Tests {@link ArtifactUtils}.
35   *
36   */
37  class ArtifactUtilsTest {
38  
39      private Artifact newArtifact(String aid) {
40          return new DefaultArtifact("group", aid, VersionRange.createFromVersion("1.0"), "test", "jar", "tests", null);
41      }
42  
43      @Test
44      void testIsSnapshot() {
45          assertFalse(ArtifactUtils.isSnapshot(null));
46          assertFalse(ArtifactUtils.isSnapshot(""));
47          assertFalse(ArtifactUtils.isSnapshot("1.2.3"));
48          assertTrue(ArtifactUtils.isSnapshot("1.2.3-SNAPSHOT"));
49          assertTrue(ArtifactUtils.isSnapshot("1.2.3-snapshot"));
50          assertTrue(ArtifactUtils.isSnapshot("1.2.3-20090413.094722-2"));
51          assertFalse(ArtifactUtils.isSnapshot("1.2.3-20090413X094722-2"));
52      }
53  
54      @Test
55      void testToSnapshotVersion() {
56          assertEquals("1.2.3", ArtifactUtils.toSnapshotVersion("1.2.3"));
57          assertEquals("1.2.3-SNAPSHOT", ArtifactUtils.toSnapshotVersion("1.2.3-SNAPSHOT"));
58          assertEquals("1.2.3-SNAPSHOT", ArtifactUtils.toSnapshotVersion("1.2.3-20090413.094722-2"));
59          assertEquals("1.2.3-20090413X094722-2", ArtifactUtils.toSnapshotVersion("1.2.3-20090413X094722-2"));
60      }
61  
62      /**
63       * Tests that the ordering of the map resembles the ordering of the input collection of artifacts.
64       */
65      @Test
66      void testArtifactMapByVersionlessIdOrdering() throws Exception {
67          List<Artifact> list = new ArrayList<>();
68          list.add(newArtifact("b"));
69          list.add(newArtifact("a"));
70          list.add(newArtifact("c"));
71          list.add(newArtifact("e"));
72          list.add(newArtifact("d"));
73  
74          Map<String, Artifact> map = ArtifactUtils.artifactMapByVersionlessId(list);
75          assertNotNull(map);
76          assertEquals(list, new ArrayList<>(map.values()));
77      }
78  }