1   package org.apache.maven.artifact;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.maven.artifact.versioning.VersionRange;
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * Tests {@link ArtifactUtils}.
32   * 
33   * @author Benjamin Bentmann
34   */
35  public class ArtifactUtilsTest
36      extends TestCase
37  {
38  
39      private Artifact newArtifact( String aid )
40      {
41          return new DefaultArtifact( "group", aid, VersionRange.createFromVersion( "1.0" ), "test", "jar", "tests", null );
42      }
43  
44      public void testIsSnapshot()
45      {
46          assertEquals( false, ArtifactUtils.isSnapshot( null ) );
47          assertEquals( false, ArtifactUtils.isSnapshot( "" ) );
48          assertEquals( false, ArtifactUtils.isSnapshot( "1.2.3" ) );
49          assertEquals( true, ArtifactUtils.isSnapshot( "1.2.3-SNAPSHOT" ) );
50          assertEquals( true, ArtifactUtils.isSnapshot( "1.2.3-snapshot" ) );
51          assertEquals( true, ArtifactUtils.isSnapshot( "1.2.3-20090413.094722-2" ) );
52      }
53  
54      public void testToSnapshotVersion()
55      {
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      }
60  
61      /**
62       * Tests that the ordering of the map resembles the ordering of the input collection of artifacts.
63       */
64      public void testArtifactMapByVersionlessIdOrdering()
65          throws Exception
66      {
67          List<Artifact> list = new ArrayList<Artifact>();
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<Artifact>( map.values() ) );
77      }
78  
79  }