001package org.apache.maven.artifact;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.List;
024import java.util.Map;
025
026import org.apache.maven.artifact.versioning.VersionRange;
027
028import junit.framework.TestCase;
029
030/**
031 * Tests {@link ArtifactUtils}.
032 * 
033 * @author Benjamin Bentmann
034 */
035public class ArtifactUtilsTest
036    extends TestCase
037{
038
039    private Artifact newArtifact( String aid )
040    {
041        return new DefaultArtifact( "group", aid, VersionRange.createFromVersion( "1.0" ), "test", "jar", "tests", null );
042    }
043
044    public void testIsSnapshot()
045    {
046        assertEquals( false, ArtifactUtils.isSnapshot( null ) );
047        assertEquals( false, ArtifactUtils.isSnapshot( "" ) );
048        assertEquals( false, ArtifactUtils.isSnapshot( "1.2.3" ) );
049        assertEquals( true, ArtifactUtils.isSnapshot( "1.2.3-SNAPSHOT" ) );
050        assertEquals( true, ArtifactUtils.isSnapshot( "1.2.3-snapshot" ) );
051        assertEquals( true, ArtifactUtils.isSnapshot( "1.2.3-20090413.094722-2" ) );
052    }
053
054    public void testToSnapshotVersion()
055    {
056        assertEquals( "1.2.3", ArtifactUtils.toSnapshotVersion( "1.2.3" ) );
057        assertEquals( "1.2.3-SNAPSHOT", ArtifactUtils.toSnapshotVersion( "1.2.3-SNAPSHOT" ) );
058        assertEquals( "1.2.3-SNAPSHOT", ArtifactUtils.toSnapshotVersion( "1.2.3-20090413.094722-2" ) );
059    }
060
061    /**
062     * Tests that the ordering of the map resembles the ordering of the input collection of artifacts.
063     */
064    public void testArtifactMapByVersionlessIdOrdering()
065        throws Exception
066    {
067        List<Artifact> list = new ArrayList<Artifact>();
068        list.add( newArtifact( "b" ) );
069        list.add( newArtifact( "a" ) );
070        list.add( newArtifact( "c" ) );
071        list.add( newArtifact( "e" ) );
072        list.add( newArtifact( "d" ) );
073
074        Map<String, Artifact> map = ArtifactUtils.artifactMapByVersionlessId( list );
075        assertNotNull( map );
076        assertEquals( list, new ArrayList<Artifact>( map.values() ) );
077    }
078
079}