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 junit.framework.TestCase; 023 024 /** 025 * Test DefaultArtifactVersion. 026 * 027 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 028 */ 029 public 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.7.3.0" , 0, 0, 0, 0, "1.7.3.0" ); 075 checkVersionParsing( "1.7.3.0-1" , 0, 0, 0, 0, "1.7.3.0-1" ); 076 checkVersionParsing( "PATCH-1193602" , 0, 0, 0, 0, "PATCH-1193602" ); 077 checkVersionParsing( "5.0.0alpha-2006020117" , 0, 0, 0, 0, "5.0.0alpha-2006020117" ); 078 checkVersionParsing( "1.0.0.-SNAPSHOT", 0, 0, 0, 0, "1.0.0.-SNAPSHOT" ); 079 checkVersionParsing( "1..0-SNAPSHOT", 0, 0, 0, 0, "1..0-SNAPSHOT" ); 080 checkVersionParsing( "1.0.-SNAPSHOT", 0, 0, 0, 0, "1.0.-SNAPSHOT" ); 081 checkVersionParsing( ".1.0-SNAPSHOT", 0, 0, 0, 0, ".1.0-SNAPSHOT" ); 082 083 checkVersionParsing( "1.2.3.200705301630" , 0, 0, 0, 0, "1.2.3.200705301630" ); 084 checkVersionParsing( "1.2.3-200705301630" , 1, 2, 3, 0, "200705301630" ); 085 } 086 087 public void testVersionComparing() 088 { 089 assertVersionEqual( "1", "1" ); 090 assertVersionOlder( "1", "2" ); 091 assertVersionOlder( "1.5", "2" ); 092 assertVersionOlder( "1", "2.5" ); 093 assertVersionEqual( "1", "1.0" ); 094 assertVersionEqual( "1", "1.0.0" ); 095 assertVersionOlder( "1.0", "1.1" ); 096 assertVersionOlder( "1.1", "1.2" ); 097 assertVersionOlder( "1.0.0", "1.1" ); 098 assertVersionOlder( "1.1", "1.2.0" ); 099 100 assertVersionOlder( "1.0-alpha-1", "1.0" ); 101 assertVersionOlder( "1.0-alpha-1", "1.0-alpha-2" ); 102 assertVersionOlder( "1.0-alpha-2", "1.0-alpha-15" ); 103 assertVersionOlder( "1.0-alpha-1", "1.0-beta-1" ); 104 105 assertVersionOlder( "1.0-beta-1", "1.0-SNAPSHOT" ); 106 assertVersionOlder( "1.0-SNAPSHOT", "1.0" ); 107 assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-alpha-1" ); 108 109 assertVersionOlder( "1.0", "1.0-1" ); 110 assertVersionOlder( "1.0-1", "1.0-2" ); 111 assertVersionEqual( "2.0-0", "2.0" ); 112 assertVersionOlder( "2.0", "2.0-1" ); 113 assertVersionOlder( "2.0.0", "2.0-1" ); 114 assertVersionOlder( "2.0-1", "2.0.1" ); 115 116 assertVersionOlder( "2.0.1-klm", "2.0.1-lmn" ); 117 assertVersionOlder( "2.0.1", "2.0.1-xyz" ); 118 assertVersionOlder( "2.0.1-xyz-1", "2.0.1-1-xyz" ); 119 120 assertVersionOlder( "2.0.1", "2.0.1-123" ); 121 assertVersionOlder( "2.0.1-xyz", "2.0.1-123" ); 122 123 assertVersionOlder( "1.2.3-10000000000", "1.2.3-10000000001" ); 124 assertVersionOlder( "1.2.3-1", "1.2.3-10000000001" ); 125 assertVersionOlder( "2.3.0-v200706262000", "2.3.0-v200706262130" ); // org.eclipse:emf:2.3.0-v200706262000 126 // org.eclipse.wst.common_core.feature_2.0.0.v200706041905-7C78EK9E_EkMNfNOd2d8qq 127 assertVersionOlder( "2.0.0.v200706041905-7C78EK9E_EkMNfNOd2d8qq", "2.0.0.v200706041906-7C78EK9E_EkMNfNOd2d8qq" ); 128 } 129 130 public void testVersionSnapshotComparing() 131 { 132 assertVersionEqual( "1-SNAPSHOT", "1-SNAPSHOT" ); 133 assertVersionOlder( "1-SNAPSHOT", "2-SNAPSHOT" ); 134 assertVersionOlder( "1.5-SNAPSHOT", "2-SNAPSHOT" ); 135 assertVersionOlder( "1-SNAPSHOT", "2.5-SNAPSHOT" ); 136 assertVersionEqual( "1-SNAPSHOT", "1.0-SNAPSHOT" ); 137 assertVersionEqual( "1-SNAPSHOT", "1.0.0-SNAPSHOT" ); 138 assertVersionOlder( "1.0-SNAPSHOT", "1.1-SNAPSHOT" ); 139 assertVersionOlder( "1.1-SNAPSHOT", "1.2-SNAPSHOT" ); 140 assertVersionOlder( "1.0.0-SNAPSHOT", "1.1-SNAPSHOT" ); 141 assertVersionOlder( "1.1-SNAPSHOT", "1.2.0-SNAPSHOT" ); 142 143 //assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-SNAPSHOT" ); 144 assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-alpha-2-SNAPSHOT" ); 145 assertVersionOlder( "1.0-alpha-1-SNAPSHOT", "1.0-beta-1-SNAPSHOT" ); 146 147 assertVersionOlder( "1.0-beta-1-SNAPSHOT", "1.0-SNAPSHOT-SNAPSHOT" ); 148 assertVersionOlder( "1.0-SNAPSHOT-SNAPSHOT", "1.0-SNAPSHOT" ); 149 assertVersionOlder( "1.0-alpha-1-SNAPSHOT-SNAPSHOT", "1.0-alpha-1-SNAPSHOT" ); 150 151 assertVersionOlder( "1.0-SNAPSHOT", "1.0-1-SNAPSHOT" ); 152 assertVersionOlder( "1.0-1-SNAPSHOT", "1.0-2-SNAPSHOT" ); 153 //assertVersionEqual( "2.0-0-SNAPSHOT", "2.0-SNAPSHOT" ); 154 assertVersionOlder( "2.0-SNAPSHOT", "2.0-1-SNAPSHOT" ); 155 assertVersionOlder( "2.0.0-SNAPSHOT", "2.0-1-SNAPSHOT" ); 156 assertVersionOlder( "2.0-1-SNAPSHOT", "2.0.1-SNAPSHOT" ); 157 158 assertVersionOlder( "2.0.1-klm-SNAPSHOT", "2.0.1-lmn-SNAPSHOT" ); 159 // assertVersionOlder( "2.0.1-xyz-SNAPSHOT", "2.0.1-SNAPSHOT" ); 160 assertVersionOlder( "2.0.1-SNAPSHOT", "2.0.1-123-SNAPSHOT" ); 161 assertVersionOlder( "2.0.1-xyz-SNAPSHOT", "2.0.1-123-SNAPSHOT" ); 162 } 163 164 public void testSnapshotVsReleases() 165 { 166 assertVersionOlder( "1.0-RC1", "1.0-SNAPSHOT" ); 167 assertVersionOlder( "1.0-rc1", "1.0-SNAPSHOT" ); 168 assertVersionOlder( "1.0-rc-1", "1.0-SNAPSHOT" ); 169 } 170 171 public void testHashCode() 172 { 173 ArtifactVersion v1 = newArtifactVersion( "1" ); 174 ArtifactVersion v2 = newArtifactVersion( "1.0" ); 175 assertEquals( true, v1.equals( v2 ) ); 176 assertEquals( v1.hashCode(), v2.hashCode() ); 177 } 178 179 public void testEqualsNullSafe() 180 { 181 assertFalse( newArtifactVersion( "1" ).equals( null ) ); 182 } 183 184 public void testEqualsTypeSafe() 185 { 186 assertFalse( newArtifactVersion( "1" ).equals( "non-an-artifact-version-instance" ) ); 187 } 188 189 private void assertVersionOlder( String left, String right ) 190 { 191 assertTrue( left + " should be older than " + right, 192 newArtifactVersion( left ).compareTo( newArtifactVersion( right ) ) < 0 ); 193 assertTrue( right + " should be newer than " + left, 194 newArtifactVersion( right ).compareTo( newArtifactVersion( left ) ) > 0 ); 195 } 196 197 private void assertVersionEqual( String left, String right ) 198 { 199 assertTrue( left + " should be equal to " + right, 200 newArtifactVersion( left ).compareTo( newArtifactVersion( right ) ) == 0 ); 201 assertTrue( right + " should be equal to " + left, 202 newArtifactVersion( right ).compareTo( newArtifactVersion( left ) ) == 0 ); 203 } 204 205 }