001 package 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 022 import java.util.Locale; 023 024 import junit.framework.TestCase; 025 026 /** 027 * Test ComparableVersion. 028 * 029 * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a> 030 */ 031 @SuppressWarnings( "unchecked" ) 032 public class ComparableVersionTest 033 extends TestCase 034 { 035 private Comparable newComparable( String version ) 036 { 037 return new ComparableVersion( version ); 038 } 039 040 private static final String[] VERSIONS_QUALIFIER = 041 { "1-alpha2snapshot", "1-alpha2", "1-alpha-123", "1-beta-2", "1-beta123", "1-m2", "1-m11", "1-rc", "1-cr2", 042 "1-rc123", "1-SNAPSHOT", "1", "1-sp", "1-sp2", "1-sp123", "1-abc", "1-def", "1-pom-1", "1-1-snapshot", 043 "1-1", "1-2", "1-123" }; 044 045 private static final String[] VERSIONS_NUMBER = 046 { "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", 047 "2.2", "2.123", "11.a2", "11.a11", "11.b2", "11.b11", "11.m2", "11.m11", "11", "11.a", "11b", "11c", "11m" }; 048 049 private void checkVersionsOrder( String[] versions ) 050 { 051 Comparable[] c = new Comparable[versions.length]; 052 for ( int i = 0; i < versions.length; i++ ) 053 { 054 c[i] = newComparable( versions[i] ); 055 } 056 057 for ( int i = 1; i < versions.length; i++ ) 058 { 059 Comparable low = c[i - 1]; 060 for ( int j = i; j < versions.length; j++ ) 061 { 062 Comparable high = c[j]; 063 assertTrue( "expected " + low + " < " + high, low.compareTo( high ) < 0 ); 064 assertTrue( "expected " + high + " > " + low, high.compareTo( low ) > 0 ); 065 } 066 } 067 } 068 069 private void checkVersionsEqual( String v1, String v2 ) 070 { 071 Comparable c1 = newComparable( v1 ); 072 Comparable c2 = newComparable( v2 ); 073 assertTrue( "expected " + v1 + " == " + v2, c1.compareTo( c2 ) == 0 ); 074 assertTrue( "expected " + v2 + " == " + v1, c2.compareTo( c1 ) == 0 ); 075 assertTrue( "expected same hashcode for " + v1 + " and " + v2, c1.hashCode() == c2.hashCode() ); 076 assertTrue( "expected " + v1 + ".equals( " + v2 + " )", c1.equals( c2 ) ); 077 assertTrue( "expected " + v2 + ".equals( " + v1 + " )", c2.equals( c1 ) ); 078 } 079 080 private void checkVersionsOrder( String v1, String v2 ) 081 { 082 Comparable c1 = newComparable( v1 ); 083 Comparable c2 = newComparable( v2 ); 084 assertTrue( "expected " + v1 + " < " + v2, c1.compareTo( c2 ) < 0 ); 085 assertTrue( "expected " + v2 + " > " + v1, c2.compareTo( c1 ) > 0 ); 086 } 087 088 public void testVersionsQualifier() 089 { 090 checkVersionsOrder( VERSIONS_QUALIFIER ); 091 } 092 093 public void testVersionsNumber() 094 { 095 checkVersionsOrder( VERSIONS_NUMBER ); 096 } 097 098 public void testVersionsEqual() 099 { 100 checkVersionsEqual( "1", "1" ); 101 checkVersionsEqual( "1", "1.0" ); 102 checkVersionsEqual( "1", "1.0.0" ); 103 checkVersionsEqual( "1.0", "1.0.0" ); 104 checkVersionsEqual( "1", "1-0" ); 105 checkVersionsEqual( "1", "1.0-0" ); 106 checkVersionsEqual( "1.0", "1.0-0" ); 107 // no separator between number and character 108 checkVersionsEqual( "1a", "1.a" ); 109 checkVersionsEqual( "1a", "1-a" ); 110 checkVersionsEqual( "1a", "1.0-a" ); 111 checkVersionsEqual( "1a", "1.0.0-a" ); 112 checkVersionsEqual( "1.0a", "1.0.a" ); 113 checkVersionsEqual( "1.0.0a", "1.0.0.a" ); 114 checkVersionsEqual( "1x", "1.x" ); 115 checkVersionsEqual( "1x", "1-x" ); 116 checkVersionsEqual( "1x", "1.0-x" ); 117 checkVersionsEqual( "1x", "1.0.0-x" ); 118 checkVersionsEqual( "1.0x", "1.0.x" ); 119 checkVersionsEqual( "1.0.0x", "1.0.0.x" ); 120 121 // aliases 122 checkVersionsEqual( "1ga", "1" ); 123 checkVersionsEqual( "1final", "1" ); 124 checkVersionsEqual( "1cr", "1rc" ); 125 126 // special "aliases" a, b and m for alpha, beta and milestone 127 checkVersionsEqual( "1a1", "1alpha1" ); 128 checkVersionsEqual( "1b2", "1beta2" ); 129 checkVersionsEqual( "1m3", "1milestone3" ); 130 131 // case insensitive 132 checkVersionsEqual( "1X", "1x" ); 133 checkVersionsEqual( "1A", "1a" ); 134 checkVersionsEqual( "1B", "1b" ); 135 checkVersionsEqual( "1M", "1m" ); 136 checkVersionsEqual( "1Ga", "1" ); 137 checkVersionsEqual( "1GA", "1" ); 138 checkVersionsEqual( "1Final", "1" ); 139 checkVersionsEqual( "1FinaL", "1" ); 140 checkVersionsEqual( "1FINAL", "1" ); 141 checkVersionsEqual( "1Cr", "1Rc" ); 142 checkVersionsEqual( "1cR", "1rC" ); 143 checkVersionsEqual( "1m3", "1Milestone3" ); 144 checkVersionsEqual( "1m3", "1MileStone3" ); 145 checkVersionsEqual( "1m3", "1MILESTONE3" ); 146 } 147 148 public void testVersionComparing() 149 { 150 checkVersionsOrder( "1", "2" ); 151 checkVersionsOrder( "1.5", "2" ); 152 checkVersionsOrder( "1", "2.5" ); 153 checkVersionsOrder( "1.0", "1.1" ); 154 checkVersionsOrder( "1.1", "1.2" ); 155 checkVersionsOrder( "1.0.0", "1.1" ); 156 checkVersionsOrder( "1.0.1", "1.1" ); 157 checkVersionsOrder( "1.1", "1.2.0" ); 158 159 checkVersionsOrder( "1.0-alpha-1", "1.0" ); 160 checkVersionsOrder( "1.0-alpha-1", "1.0-alpha-2" ); 161 checkVersionsOrder( "1.0-alpha-1", "1.0-beta-1" ); 162 163 checkVersionsOrder( "1.0-beta-1", "1.0-SNAPSHOT" ); 164 checkVersionsOrder( "1.0-SNAPSHOT", "1.0" ); 165 checkVersionsOrder( "1.0-alpha-1-SNAPSHOT", "1.0-alpha-1" ); 166 167 checkVersionsOrder( "1.0", "1.0-1" ); 168 checkVersionsOrder( "1.0-1", "1.0-2" ); 169 checkVersionsOrder( "1.0.0", "1.0-1" ); 170 171 checkVersionsOrder( "2.0-1", "2.0.1" ); 172 checkVersionsOrder( "2.0.1-klm", "2.0.1-lmn" ); 173 checkVersionsOrder( "2.0.1", "2.0.1-xyz" ); 174 175 checkVersionsOrder( "2.0.1", "2.0.1-123" ); 176 checkVersionsOrder( "2.0.1-xyz", "2.0.1-123" ); 177 } 178 179 public void testLocaleIndependent() 180 { 181 Locale orig = Locale.getDefault(); 182 Locale[] locales = { Locale.ENGLISH, new Locale( "tr" ), Locale.getDefault() }; 183 try 184 { 185 for ( Locale locale : locales ) 186 { 187 Locale.setDefault( locale ); 188 checkVersionsEqual( "1-abcdefghijklmnopqrstuvwxyz", "1-ABCDEFGHIJKLMNOPQRSTUVWXYZ" ); 189 } 190 } 191 finally 192 { 193 Locale.setDefault( orig ); 194 } 195 } 196 197 public void testReuse() 198 { 199 ComparableVersion c1 = new ComparableVersion( "1" ); 200 c1.parseVersion( "2" ); 201 202 Comparable c2 = newComparable( "2" ); 203 204 assertEquals( "reused instance should be equivalent to new instance", c1, c2 ); 205 } 206 }