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