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 junit.framework.TestCase;
023
024import org.apache.maven.artifact.handler.ArtifactHandlerMock;
025import org.apache.maven.artifact.versioning.VersionRange;
026
027public class DefaultArtifactTest
028    extends TestCase
029{
030
031    private DefaultArtifact artifact;
032
033    private DefaultArtifact snapshotArtifact;
034
035    private String groupId = "groupid", artifactId = "artifactId", version = "1.0", scope = "artifactScope", type = "type",
036        classifier = "classifier";
037
038    private String snapshotSpecVersion = "1.0-SNAPSHOT";
039    private String snapshotResolvedVersion = "1.0-20070606.010101-1";
040
041    private VersionRange versionRange;
042    private VersionRange snapshotVersionRange;
043
044    private ArtifactHandlerMock artifactHandler;
045
046    protected void setUp()
047        throws Exception
048    {
049        super.setUp();
050        artifactHandler = new ArtifactHandlerMock();
051        versionRange = VersionRange.createFromVersion( version );
052        artifact = new DefaultArtifact( groupId, artifactId, versionRange, scope, type, classifier, artifactHandler );
053
054        snapshotVersionRange = VersionRange.createFromVersion( snapshotResolvedVersion );
055        snapshotArtifact = new DefaultArtifact( groupId, artifactId, snapshotVersionRange, scope, type, classifier, artifactHandler );
056    }
057
058    public void testGetVersionReturnsResolvedVersionOnSnapshot()
059    {
060        assertEquals( snapshotResolvedVersion, snapshotArtifact.getVersion() );
061
062        // this is FOUL!
063//        snapshotArtifact.isSnapshot();
064
065        assertEquals( snapshotSpecVersion, snapshotArtifact.getBaseVersion() );
066    }
067
068    public void testGetDependencyConflictId()
069    {
070        assertEquals( groupId + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId() );
071    }
072
073    public void testGetDependencyConflictIdNullGroupId()
074    {
075        artifact.setGroupId( null );
076        assertEquals( null + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId() );
077    }
078
079    public void testGetDependencyConflictIdNullClassifier()
080    {
081        artifact = new DefaultArtifact( groupId, artifactId, versionRange, scope, type, null, artifactHandler );
082        assertEquals( groupId + ":" + artifactId + ":" + type, artifact.getDependencyConflictId() );
083    }
084
085    public void testGetDependencyConflictIdNullScope()
086    {
087        artifact.setScope( null );
088        assertEquals( groupId + ":" + artifactId + ":" + type + ":" + classifier, artifact.getDependencyConflictId() );
089    }
090
091    public void testToString()
092    {
093        assertEquals( groupId + ":" + artifactId + ":" + type + ":" + classifier + ":" + version + ":" + scope,
094                      artifact.toString() );
095    }
096
097    public void testToStringNullGroupId()
098    {
099        artifact.setGroupId( null );
100        assertEquals( artifactId + ":" + type + ":" + classifier + ":" + version + ":" + scope, artifact.toString() );
101    }
102
103    public void testToStringNullClassifier()
104    {
105        artifact = new DefaultArtifact( groupId, artifactId, versionRange, scope, type, null, artifactHandler );
106        assertEquals( groupId + ":" + artifactId + ":" + type + ":" + version + ":" + scope, artifact.toString() );
107    }
108
109    public void testToStringNullScope()
110    {
111        artifact.setScope( null );
112        assertEquals( groupId + ":" + artifactId + ":" + type + ":" + classifier + ":" + version, artifact.toString() );
113    }
114
115    public void testComparisonByVersion()
116    {
117        Artifact artifact1 = new DefaultArtifact( groupId, artifactId, VersionRange.createFromVersion( "5.0" ), scope,
118                                                  type, classifier, artifactHandler );
119        Artifact artifact2 = new DefaultArtifact( groupId, artifactId, VersionRange.createFromVersion( "12.0" ), scope,
120                                                  type, classifier, artifactHandler );
121
122        assertTrue( artifact1.compareTo( artifact2 ) < 0 );
123        assertTrue( artifact2.compareTo( artifact1 ) > 0 );
124
125        Artifact artifact = new DefaultArtifact( groupId, artifactId, VersionRange.createFromVersion( "5.0" ), scope,
126                                                  type, classifier, artifactHandler );
127        assertTrue( artifact.compareTo( artifact1 ) == 0 );
128        assertTrue( artifact1.compareTo( artifact ) == 0 );
129    }
130
131    public void testNonResolvedVersionRangeConsistentlyYieldsNullVersions()
132        throws Exception
133    {
134        VersionRange vr = VersionRange.createFromVersionSpec( "[1.0,2.0)" );
135        artifact = new DefaultArtifact( groupId, artifactId, vr, scope, type, null, artifactHandler );
136        assertEquals( null, artifact.getVersion() );
137        assertEquals( null, artifact.getBaseVersion() );
138    }
139
140}