1 package org.apache.archiva.common.utils;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collections;
25 import java.util.List;
27 import junit.framework.TestCase;
30 * VersionComparatorTest
34 public class VersionComparatorTest
37 public void testComparator()
39 /* Sort order is oldest to newest */
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" } );
44 assertSort( new String[] { "1.5-SNAPSHOT", "1.2", "1.20" }, new String[] { "1.2", "1.5-SNAPSHOT", "1.20" } );
46 assertSort( new String[] { "1.1", "1.0-SNAPSHOT", "1.1-m6", "1.1-rc1" }, new String[] {
52 assertSort( new String[] { "1.1-m6", "1.0-SNAPSHOT", "1.1-rc1", "1.1" }, new String[] {
58 assertSort( new String[] { "2.0.5", "2.0.4-SNAPSHOT", "2.0", "2.0-rc1" }, new String[] {
64 assertSort( new String[] { "1.0-alpha-1", "1.0-alpha-22", "1.0-alpha-10", "1.0-alpha-9" }, new String[] {
70 assertSort( new String[] { "1.0-alpha1", "1.0-alpha22", "1.0-alpha10", "1.0-alpha9" }, new String[] {
76 assertSort( new String[] { "1.0-1", "1.0-22", "1.0-10", "1.0-9" }, new String[] {
82 assertSort( new String[] { "alpha-1", "alpha-22", "alpha-10", "alpha-9" }, new String[] {
88 assertSort( new String[] { "1.0.1", "1.0.22", "1.0.10", "1.0.9" }, new String[] {
95 // TODO: write more unit tests.
98 private void assertSort( String[] rawVersions, String[] expectedSort )
100 List<String> versions = new ArrayList<String>();
101 versions.addAll( Arrays.asList( rawVersions ) );
103 Collections.sort( versions, VersionComparator.getInstance() );
105 assertEquals( "Versions.size()", expectedSort.length, versions.size() );
106 for ( int i = 0; i < expectedSort.length; i++ )
108 assertEquals( "Sorted Versions[" + i + "]", expectedSort[i], (String) versions.get( i ) );
112 public void testToParts()
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" } );
120 private void assertParts( String version, String[] expectedParts )
122 String actualParts[] = VersionComparator.toParts( version );
123 assertEquals( "Parts.length", expectedParts.length, actualParts.length );
125 for ( int i = 0; i < expectedParts.length; i++ )
127 assertEquals( "parts[" + i + "]", expectedParts[i], actualParts[i] );