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 java.util.Locale;
023
024import junit.framework.TestCase;
025
026/**
027 * Test ComparableVersion.
028 *
029 * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
030 */
031@SuppressWarnings( "unchecked" )
032public class ComparableVersionTest
033    extends TestCase
034{
035    private Comparable newComparable( String version )
036    {
037        ComparableVersion ret = new ComparableVersion( version );
038        String canonical = ret.getCanonical();
039        String parsedCanonical = new ComparableVersion( canonical ).getCanonical();
040
041        System.out.println( "canonical( " + version + " ) = " + canonical );
042        assertEquals( "canonical( " + version + " ) = " + canonical + " -> canonical: " + parsedCanonical, canonical,
043                      parsedCanonical );
044
045        return ret;
046    }
047
048    private static final String[] VERSIONS_QUALIFIER =
049        { "1-alpha2snapshot", "1-alpha2", "1-alpha-123", "1-beta-2", "1-beta123", "1-m2", "1-m11", "1-rc", "1-cr2",
050            "1-rc123", "1-SNAPSHOT", "1", "1-sp", "1-sp2", "1-sp123", "1-abc", "1-def", "1-pom-1", "1-1-snapshot",
051            "1-1", "1-2", "1-123" };
052
053    private static final String[] VERSIONS_NUMBER =
054        { "2.0", "2-1", "2.0.a", "2.0.0.a", "2.0.2", "2.0.123", "2.1.0", "2.1-a", "2.1b", "2.1-c", "2.1-1", "2.1.0.1",
055            "2.2", "2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m" };
056
057    private void checkVersionsOrder( String[] versions )
058    {
059        Comparable[] c = new Comparable[versions.length];
060        for ( int i = 0; i < versions.length; i++ )
061        {
062            c[i] = newComparable( versions[i] );
063        }
064
065        for ( int i = 1; i < versions.length; i++ )
066        {
067            Comparable low = c[i - 1];
068            for ( int j = i; j < versions.length; j++ )
069            {
070                Comparable high = c[j];
071                assertTrue( "expected " + low + " < " + high, low.compareTo( high ) < 0 );
072                assertTrue( "expected " + high + " > " + low, high.compareTo( low ) > 0 );
073            }
074        }
075    }
076
077    private void checkVersionsEqual( String v1, String v2 )
078    {
079        Comparable c1 = newComparable( v1 );
080        Comparable c2 = newComparable( v2 );
081        assertTrue( "expected " + v1 + " == " + v2, c1.compareTo( c2 ) == 0 );
082        assertTrue( "expected " + v2 + " == " + v1, c2.compareTo( c1 ) == 0 );
083        assertTrue( "expected same hashcode for " + v1 + " and " + v2, c1.hashCode() == c2.hashCode() );
084        assertTrue( "expected " + v1 + ".equals( " + v2 + " )", c1.equals( c2 ) );
085        assertTrue( "expected " + v2 + ".equals( " + v1 + " )", c2.equals( c1 ) );
086    }
087
088    private void checkVersionsOrder( String v1, String v2 )
089    {
090        Comparable c1 = newComparable( v1 );
091        Comparable c2 = newComparable( v2 );
092        assertTrue( "expected " + v1 + " < " + v2, c1.compareTo( c2 ) < 0 );
093        assertTrue( "expected " + v2 + " > " + v1, c2.compareTo( c1 ) > 0 );
094    }
095
096    public void testVersionsQualifier()
097    {
098        checkVersionsOrder( VERSIONS_QUALIFIER );
099    }
100
101    public void testVersionsNumber()
102    {
103        checkVersionsOrder( VERSIONS_NUMBER );
104    }
105
106    public void testVersionsEqual()
107    {
108        newComparable( "1.0-alpha" );
109        checkVersionsEqual( "1", "1" );
110        checkVersionsEqual( "1", "1.0" );
111        checkVersionsEqual( "1", "1.0.0" );
112        checkVersionsEqual( "1.0", "1.0.0" );
113        checkVersionsEqual( "1", "1-0" );
114        checkVersionsEqual( "1", "1.0-0" );
115        checkVersionsEqual( "1.0", "1.0-0" );
116        // no separator between number and character
117        checkVersionsEqual( "1a", "1-a" );
118        checkVersionsEqual( "1a", "1.0-a" );
119        checkVersionsEqual( "1a", "1.0.0-a" );
120        checkVersionsEqual( "1.0a", "1-a" );
121        checkVersionsEqual( "1.0.0a", "1-a" );
122        checkVersionsEqual( "1x", "1-x" );
123        checkVersionsEqual( "1x", "1.0-x" );
124        checkVersionsEqual( "1x", "1.0.0-x" );
125        checkVersionsEqual( "1.0x", "1-x" );
126        checkVersionsEqual( "1.0.0x", "1-x" );
127
128        // aliases
129        checkVersionsEqual( "1ga", "1" );
130        checkVersionsEqual( "1final", "1" );
131        checkVersionsEqual( "1cr", "1rc" );
132
133        // special "aliases" a, b and m for alpha, beta and milestone
134        checkVersionsEqual( "1a1", "1-alpha-1" );
135        checkVersionsEqual( "1b2", "1-beta-2" );
136        checkVersionsEqual( "1m3", "1-milestone-3" );
137
138        // case insensitive
139        checkVersionsEqual( "1X", "1x" );
140        checkVersionsEqual( "1A", "1a" );
141        checkVersionsEqual( "1B", "1b" );
142        checkVersionsEqual( "1M", "1m" );
143        checkVersionsEqual( "1Ga", "1" );
144        checkVersionsEqual( "1GA", "1" );
145        checkVersionsEqual( "1Final", "1" );
146        checkVersionsEqual( "1FinaL", "1" );
147        checkVersionsEqual( "1FINAL", "1" );
148        checkVersionsEqual( "1Cr", "1Rc" );
149        checkVersionsEqual( "1cR", "1rC" );
150        checkVersionsEqual( "1m3", "1Milestone3" );
151        checkVersionsEqual( "1m3", "1MileStone3" );
152        checkVersionsEqual( "1m3", "1MILESTONE3" );
153    }
154
155    public void testVersionComparing()
156    {
157        checkVersionsOrder( "1", "2" );
158        checkVersionsOrder( "1.5", "2" );
159        checkVersionsOrder( "1", "2.5" );
160        checkVersionsOrder( "1.0", "1.1" );
161        checkVersionsOrder( "1.1", "1.2" );
162        checkVersionsOrder( "1.0.0", "1.1" );
163        checkVersionsOrder( "1.0.1", "1.1" );
164        checkVersionsOrder( "1.1", "1.2.0" );
165
166        checkVersionsOrder( "1.0-alpha-1", "1.0" );
167        checkVersionsOrder( "1.0-alpha-1", "1.0-alpha-2" );
168        checkVersionsOrder( "1.0-alpha-1", "1.0-beta-1" );
169
170        checkVersionsOrder( "1.0-beta-1", "1.0-SNAPSHOT" );
171        checkVersionsOrder( "1.0-SNAPSHOT", "1.0" );
172        checkVersionsOrder( "1.0-alpha-1-SNAPSHOT", "1.0-alpha-1" );
173
174        checkVersionsOrder( "1.0", "1.0-1" );
175        checkVersionsOrder( "1.0-1", "1.0-2" );
176        checkVersionsOrder( "1.0.0", "1.0-1" );
177
178        checkVersionsOrder( "2.0-1", "2.0.1" );
179        checkVersionsOrder( "2.0.1-klm", "2.0.1-lmn" );
180        checkVersionsOrder( "2.0.1", "2.0.1-xyz" );
181
182        checkVersionsOrder( "2.0.1", "2.0.1-123" );
183        checkVersionsOrder( "2.0.1-xyz", "2.0.1-123" );
184    }
185
186    /**
187     * Test <a href="https://jira.codehaus.org/browse/MNG-5568">MNG-5568</a> edge case
188     * which was showing transitive inconsistency: since A > B and B > C then we should have A > C
189     * otherwise sorting a list of ComparableVersions() will in some cases throw runtime exception;
190     * see Netbeans issues <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=240845">240845</a> and
191     * <a href="https://netbeans.org/bugzilla/show_bug.cgi?id=226100">226100</a>
192     */
193    public void testMng5568()
194    {
195        String a = "6.1.0";
196        String b = "6.1.0rc3";
197        String c = "6.1H.5-beta"; // this is the unusual version string, with 'H' in the middle
198
199        checkVersionsOrder( b, a ); // classical
200        checkVersionsOrder( b, c ); // now b < c, but before MNG-5568, we had b > c
201        checkVersionsOrder( a, c );
202    }
203
204    public void testLocaleIndependent()
205    {
206        Locale orig = Locale.getDefault();
207        Locale[] locales = { Locale.ENGLISH, new Locale( "tr" ), Locale.getDefault() };
208        try
209        {
210            for ( Locale locale : locales )
211            {
212                Locale.setDefault( locale );
213                checkVersionsEqual( "1-abcdefghijklmnopqrstuvwxyz", "1-ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
214            }
215        }
216        finally
217        {
218            Locale.setDefault( orig );
219        }
220    }
221
222    public void testReuse()
223    {
224        ComparableVersion c1 = new ComparableVersion( "1" );
225        c1.parseVersion( "2" );
226
227        Comparable c2 = newComparable( "2" );
228
229        assertEquals( "reused instance should be equivalent to new instance", c1, c2 );
230    }
231}