View Javadoc
1   package org.apache.archiva.common.utils;
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 junit.framework.TestCase;
23  
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.Collections;
27  import java.util.List;
28  
29  /**
30   * VersionComparatorTest 
31   *
32   *
33   */
34  public class VersionComparatorTest
35      extends TestCase
36  {
37      public void testComparator()
38      {
39          /* Sort order is oldest to newest */
40  
41          assertSort( new String[] { "1.0", "3.0", "2.0" }, new String[] { "1.0", "2.0", "3.0" } );
42          assertSort( new String[] { "1.5", "1.2", "1.0" }, new String[] { "1.0", "1.2", "1.5" } );
43  
44          assertSort( new String[] { "1.5-SNAPSHOT", "1.2", "1.20" }, new String[] { "1.2", "1.5-SNAPSHOT", "1.20" } );
45  
46          assertSort( new String[] { "1.1", "1.0-SNAPSHOT", "1.1-m6", "1.1-rc1" }, new String[] {
47              "1.0-SNAPSHOT",
48              "1.1-rc1",
49              "1.1-m6",
50              "1.1" } );
51  
52          assertSort( new String[] { "1.1-m6", "1.0-SNAPSHOT", "1.1-rc1", "1.1" }, new String[] {
53              "1.0-SNAPSHOT",
54              "1.1-rc1",
55              "1.1-m6",
56              "1.1" } );
57  
58          assertSort( new String[] { "2.0.5", "2.0.4-SNAPSHOT", "2.0", "2.0-rc1" }, new String[] {
59              "2.0-rc1",
60              "2.0",
61              "2.0.4-SNAPSHOT",
62              "2.0.5" } );
63  
64          assertSort( new String[] { "1.0-alpha-1", "1.0-alpha-22", "1.0-alpha-10", "1.0-alpha-9" }, new String[] {
65              "1.0-alpha-1",
66              "1.0-alpha-9",
67              "1.0-alpha-10",
68              "1.0-alpha-22" } );
69  
70          assertSort( new String[] { "1.0-alpha1", "1.0-alpha22", "1.0-alpha10", "1.0-alpha9" }, new String[] {
71              "1.0-alpha1",
72              "1.0-alpha9",
73              "1.0-alpha10",
74              "1.0-alpha22" } );
75          
76          assertSort( new String[] { "1.0-1", "1.0-22", "1.0-10", "1.0-9" }, new String[] {
77              "1.0-1",
78              "1.0-9",
79              "1.0-10",
80              "1.0-22" } );
81          
82          assertSort( new String[] { "alpha-1", "alpha-22", "alpha-10", "alpha-9" }, new String[] {
83              "alpha-1",
84              "alpha-9",
85              "alpha-10",
86              "alpha-22" } );
87          
88          assertSort( new String[] { "1.0.1", "1.0.22", "1.0.10", "1.0.9" }, new String[] {
89              "1.0.1",
90              "1.0.9",
91              "1.0.10",
92              "1.0.22" } );
93          
94          
95          // TODO: write more unit tests.
96      }
97  
98      private void assertSort( String[] rawVersions, String[] expectedSort )
99      {
100         List<String> versions = new ArrayList<>();
101         versions.addAll( Arrays.asList( rawVersions ) );
102 
103         Collections.sort( versions, VersionComparator.getInstance() );
104 
105         assertEquals( "Versions.size()", expectedSort.length, versions.size() );
106         for ( int i = 0; i < expectedSort.length; i++ )
107         {
108             assertEquals( "Sorted Versions[" + i + "]", expectedSort[i], (String) versions.get( i ) );
109         }
110     }
111 
112     public void testToParts()
113     {
114         assertParts( "1.0", new String[] { "1", "0" } );
115         assertParts( "1.0-alpha-1", new String[] { "1", "0", "alpha", "1" } );
116         assertParts( "2.0-rc2", new String[] { "2", "0", "rc", "2" } );
117         assertParts( "1.3-m6", new String[] { "1", "3", "m", "6" } );
118     }
119 
120     private void assertParts( String version, String[] expectedParts )
121     {
122         String actualParts[] = VersionComparator.toParts( version );
123         assertEquals( "Parts.length", expectedParts.length, actualParts.length );
124 
125         for ( int i = 0; i < expectedParts.length; i++ )
126         {
127             assertEquals( "parts[" + i + "]", expectedParts[i], actualParts[i] );
128         }
129     }
130 }