001package org.apache.maven.artifact.versioning;
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
024/**
025 * Test DefaultArtifactVersion.
026 *
027 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
028 */
029public class DefaultArtifactVersionTest
030    extends TestCase
031{
032    private ArtifactVersion newArtifactVersion( String version )
033    {
034        return new DefaultArtifactVersion( version );
035    }
036
037    private void checkVersionParsing( String version, int major, int minor, int incremental, int buildnumber,
038                                      String qualifier )
039    {
040        ArtifactVersion artifactVersion = newArtifactVersion( version );
041        String parsed =
042            "'" + version + "' parsed as ('" + artifactVersion.getMajorVersion() + "', '"
043                + artifactVersion.getMinorVersion() + "', '" + artifactVersion.getIncrementalVersion() + "', '"
044                + artifactVersion.getBuildNumber() + "', '" + artifactVersion.getQualifier() + "'), ";
045        assertEquals( parsed + "check major version", major, artifactVersion.getMajorVersion() );
046        assertEquals( parsed + "check minor version", minor, artifactVersion.getMinorVersion() );
047        assertEquals( parsed + "check incremental version", incremental, artifactVersion.getIncrementalVersion() );
048        assertEquals( parsed + "check build number", buildnumber, artifactVersion.getBuildNumber() );
049        assertEquals( parsed + "check qualifier", qualifier, artifactVersion.getQualifier() );
050        assertEquals( "check " + version + " string value", version, artifactVersion.toString() );
051    }
052
053    public void testVersionParsing()
054    {
055        checkVersionParsing( "1", 1, 0, 0, 0, null );
056        checkVersionParsing( "1.2", 1, 2, 0, 0, null );
057        checkVersionParsing( "1.2.3", 1, 2, 3, 0, null );
058        checkVersionParsing( "1.2.3-1", 1, 2, 3, 1, null );
059        checkVersionParsing( "1.2.3-alpha-1", 1, 2, 3, 0, "alpha-1" );
060        checkVersionParsing( "1.2-alpha-1", 1, 2, 0, 0, "alpha-1" );
061        checkVersionParsing( "1.2-alpha-1-20050205.060708-1", 1, 2, 0, 0, "alpha-1-20050205.060708-1" );
062        checkVersionParsing( "RELEASE", 0, 0, 0, 0, "RELEASE" );
063        checkVersionParsing( "2.0-1", 2, 0, 0, 1, null );
064
065        // 0 at the beginning of a number has a special handling
066        checkVersionParsing( "02", 0, 0, 0, 0, "02" );
067        checkVersionParsing( "0.09", 0, 0, 0, 0, "0.09" );
068        checkVersionParsing( "0.2.09", 0, 0, 0, 0, "0.2.09" );
069        checkVersionParsing( "2.0-01", 2, 0, 0, 0, "01" );
070
071        // version schemes not really supported: fully transformed as qualifier
072        checkVersionParsing( "1.0.1b", 0, 0, 0, 0, "1.0.1b" );
073        checkVersionParsing( "1.0M2", 0, 0, 0, 0, "1.0M2" );
074        checkVersionParsing( "1.0RC2", 0, 0, 0, 0, "1.0RC2" );
075        checkVersionParsing( "1.1.2.beta1", 1, 1, 2, 0, "beta1" );
076        checkVersionParsing( "1.7.3.beta1", 1, 7, 3, 0, "beta1" );
077        checkVersionParsing( "1.7.3.0", 0, 0, 0, 0, "1.7.3.0" );
078        checkVersionParsing( "1.7.3.0-1", 0, 0, 0, 0, "1.7.3.0-1" );
079        checkVersionParsing( "PATCH-1193602", 0, 0, 0, 0, "PATCH-1193602" );
080        checkVersionParsing( "5.0.0alpha-2006020117", 0, 0, 0, 0, "5.0.0alpha-2006020117" );
081        checkVersionParsing( "1.0.0.-SNAPSHOT", 0, 0, 0, 0, "1.0.0.-SNAPSHOT" );
082        checkVersionParsing( "1..0-SNAPSHOT", 0, 0, 0, 0, "1..0-SNAPSHOT" );
083        checkVersionParsing( "1.0.-SNAPSHOT", 0, 0, 0, 0, "1.0.-SNAPSHOT" );
084        checkVersionParsing( ".1.0-SNAPSHOT", 0, 0, 0, 0, ".1.0-SNAPSHOT" );
085
086        checkVersionParsing( "1.2.3.200705301630", 0, 0, 0, 0, "1.2.3.200705301630" );
087        checkVersionParsing( "1.2.3-200705301630", 1, 2, 3, 0, "200705301630" );
088    }
089
090    public void testVersionComparing()
091    {
092        assertVersionEqual( "1", "1" );
093        assertVersionOlder( "1", "2" );
094        assertVersionOlder( "1.5", "2" );
095        assertVersionOlder( "1", "2.5" );
096        assertVersionEqual( "1", "1.0" );
097        assertVersionEqual( "1", "1.0.0" );
098        assertVersionOlder( "1.0", "1.1" );
099        assertVersionOlder( "1.1", "1.2" );
100        assertVersionOlder( "1.0.0", "1.1" );
101        assertVersionOlder( "1.1", "1.2.0" );
102
103        assertVersionOlder( "1.1.2.alpha1", "1.1.2" );
104        assertVersionOlder( "1.1.2.alpha1", "1.1.2.beta1" );
105        assertVersionOlder( "1.1.2.beta1", "1.2" );
106
107        assertVersionOlder( "1.0-alpha-1", "1.0" );
108        assertVersionOlder( "1.0-alpha-1", "1.0-alpha-2" );
109        assertVersionOlder( "1.0-alpha-2", "1.0-alpha-15" );
110        assertVersionOlder( "1.0-alpha-1", "1.0-beta-1" );
111
112        assertVersionOlder( "1.0-beta-1", "1.0-SNAPSHOT" );
113        assertVersionOlder( "1.0-SNAPSHOT", "1.0" );
114        assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-alpha-1" );
115
116        assertVersionOlder( "1.0", "1.0-1" );
117        assertVersionOlder( "1.0-1", "1.0-2" );
118        assertVersionEqual( "2.0-0", "2.0" );
119        assertVersionOlder( "2.0", "2.0-1" );
120        assertVersionOlder( "2.0.0", "2.0-1" );
121        assertVersionOlder( "2.0-1", "2.0.1" );
122
123        assertVersionOlder( "2.0.1-klm", "2.0.1-lmn" );
124        assertVersionOlder( "2.0.1", "2.0.1-xyz" );
125        assertVersionOlder( "2.0.1-xyz-1", "2.0.1-1-xyz" );
126
127        assertVersionOlder( "2.0.1", "2.0.1-123" );
128        assertVersionOlder( "2.0.1-xyz", "2.0.1-123" );
129
130        assertVersionOlder( "1.2.3-10000000000", "1.2.3-10000000001" );
131        assertVersionOlder( "1.2.3-1", "1.2.3-10000000001" );
132        assertVersionOlder( "2.3.0-v200706262000", "2.3.0-v200706262130" ); // org.eclipse:emf:2.3.0-v200706262000
133        // org.eclipse.wst.common_core.feature_2.0.0.v200706041905-7C78EK9E_EkMNfNOd2d8qq
134        assertVersionOlder( "2.0.0.v200706041905-7C78EK9E_EkMNfNOd2d8qq", "2.0.0.v200706041906-7C78EK9E_EkMNfNOd2d8qq" );
135    }
136
137    public void testVersionSnapshotComparing()
138    {
139        assertVersionEqual( "1-SNAPSHOT", "1-SNAPSHOT" );
140        assertVersionOlder( "1-SNAPSHOT", "2-SNAPSHOT" );
141        assertVersionOlder( "1.5-SNAPSHOT", "2-SNAPSHOT" );
142        assertVersionOlder( "1-SNAPSHOT", "2.5-SNAPSHOT" );
143        assertVersionEqual( "1-SNAPSHOT", "1.0-SNAPSHOT" );
144        assertVersionEqual( "1-SNAPSHOT", "1.0.0-SNAPSHOT" );
145        assertVersionOlder( "1.0-SNAPSHOT", "1.1-SNAPSHOT" );
146        assertVersionOlder( "1.1-SNAPSHOT", "1.2-SNAPSHOT" );
147        assertVersionOlder( "1.0.0-SNAPSHOT", "1.1-SNAPSHOT" );
148        assertVersionOlder( "1.1-SNAPSHOT", "1.2.0-SNAPSHOT" );
149
150        // assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-SNAPSHOT" );
151        assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-alpha-2-SNAPSHOT" );
152        assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-beta-1-SNAPSHOT" );
153
154        assertVersionOlder( "1.0-beta-1-SNAPSHOT", "1.0-SNAPSHOT-SNAPSHOT" );
155        assertVersionOlder( "1.0-SNAPSHOT-SNAPSHOT", "1.0-SNAPSHOT" );
156        assertVersionOlder( "1.0-alpha-1-SNAPSHOT-SNAPSHOT", "1.0-alpha-1-SNAPSHOT" );
157
158        assertVersionOlder( "1.0-SNAPSHOT", "1.0-1-SNAPSHOT" );
159        assertVersionOlder( "1.0-1-SNAPSHOT", "1.0-2-SNAPSHOT" );
160        // assertVersionEqual( "2.0-0-SNAPSHOT", "2.0-SNAPSHOT" );
161        assertVersionOlder( "2.0-SNAPSHOT", "2.0-1-SNAPSHOT" );
162        assertVersionOlder( "2.0.0-SNAPSHOT", "2.0-1-SNAPSHOT" );
163        assertVersionOlder( "2.0-1-SNAPSHOT", "2.0.1-SNAPSHOT" );
164
165        assertVersionOlder( "2.0.1-klm-SNAPSHOT", "2.0.1-lmn-SNAPSHOT" );
166        // assertVersionOlder( "2.0.1-xyz-SNAPSHOT", "2.0.1-SNAPSHOT" );
167        assertVersionOlder( "2.0.1-SNAPSHOT", "2.0.1-123-SNAPSHOT" );
168        assertVersionOlder( "2.0.1-xyz-SNAPSHOT", "2.0.1-123-SNAPSHOT" );
169    }
170
171    public void testSnapshotVsReleases()
172    {
173        assertVersionOlder( "1.0-RC1", "1.0-SNAPSHOT" );
174        assertVersionOlder( "1.0-rc1", "1.0-SNAPSHOT" );
175        assertVersionOlder( "1.0-rc-1", "1.0-SNAPSHOT" );
176    }
177
178    public void testHashCode()
179    {
180        ArtifactVersion v1 = newArtifactVersion( "1" );
181        ArtifactVersion v2 = newArtifactVersion( "1.0" );
182        assertEquals( true, v1.equals( v2 ) );
183        assertEquals( v1.hashCode(), v2.hashCode() );
184    }
185
186    public void testEqualsNullSafe()
187    {
188        assertFalse( newArtifactVersion( "1" ).equals( null ) );
189    }
190
191    public void testEqualsTypeSafe()
192    {
193        assertFalse( newArtifactVersion( "1" ).equals( "non-an-artifact-version-instance" ) );
194    }
195
196    public void testNonNumericVersionRepresentationReturnsANumberFormatException()
197    {
198        try
199        {
200            new DefaultArtifactVersion( "..." );
201        }
202        catch ( Exception e )
203        {
204            assertTrue( "We expect a NumberFormatException to be thrown.", e instanceof NumberFormatException );
205        }
206    }
207
208    private void assertVersionOlder( String left, String right )
209    {
210        assertTrue( left + " should be older than " + right,
211                    newArtifactVersion( left ).compareTo( newArtifactVersion( right ) ) < 0 );
212        assertTrue( right + " should be newer than " + left,
213                    newArtifactVersion( right ).compareTo( newArtifactVersion( left ) ) > 0 );
214    }
215
216    private void assertVersionEqual( String left, String right )
217    {
218        assertTrue( left + " should be equal to " + right,
219                    newArtifactVersion( left ).compareTo( newArtifactVersion( right ) ) == 0 );
220        assertTrue( right + " should be equal to " + left,
221                    newArtifactVersion( right ).compareTo( newArtifactVersion( left ) ) == 0 );
222    }
223}