View Javadoc
1   package org.apache.maven.shared.release.versions;
2   
3   import java.util.Properties;
4   
5   import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
6   
7   /*
8    * Licensed to the Apache Software Foundation (ASF) under one
9    * or more contributor license agreements.  See the NOTICE file
10   * distributed with this work for additional information
11   * regarding copyright ownership.  The ASF licenses this file
12   * to you under the Apache License, Version 2.0 (the
13   * "License"); you may not use this file except in compliance
14   * with the License.  You may obtain a copy of the License at
15   *
16   *   http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing,
19   * software distributed under the License is distributed on an
20   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21   * KIND, either express or implied.  See the License for the
22   * specific language governing permissions and limitations
23   * under the License.
24   */
25  
26  import junit.framework.TestCase;
27  
28  public class DefaultVersionInfoTest
29      extends TestCase
30  {
31      private String mavenVersion;
32  
33      @Override
34      protected void setUp()
35          throws Exception
36      {
37          Properties pomProperties = new Properties();
38          pomProperties.load( DefaultArtifactVersion.class.getResourceAsStream( "/META-INF/maven/org.apache.maven/maven-artifact/pom.properties" ) );
39          mavenVersion = pomProperties.getProperty( "version" );
40      }
41  
42      public void testParse()
43          throws Exception
44      {
45          checkParsing( "1.0", "1.0", null, null, null );
46      }
47  
48      public void testParseWithBadVersion()
49          throws Exception
50      {
51          try
52          {
53              checkParsing( "foo", null, null, null, "foo" );
54              fail( "version is incorrect, must fail." );
55          }
56          catch ( VersionParseException e )
57          {
58          }
59      }
60  
61      public void testParseMultiDigit()
62          throws Exception
63      {
64          checkParsing( "99.99", "99.99", null, null, null );
65          checkParsing( "990.990.990", "990.990.990", null, null, null );
66      }
67  
68      public void testParseSnapshotVersion()
69          throws Exception
70      {
71          checkParsing( "SNAPSHOT", null, null, null, "SNAPSHOT" );
72          checkParsing( "1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT" );
73          checkParsing( "1.0-beta-4_SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT" );
74      }
75  
76      public void testParseAnnotationVersion()
77          throws Exception
78      {
79          checkParsing( "1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT" );
80          checkParsing( "1.0-beta-4", "1.0", "beta", "4", null );
81          checkParsing( "1.2.3-beta-99", "1.2.3", "beta", "99", null );
82          checkParsing( "1.2.3-beta99", "1.2.3", "beta", "99", null );
83          checkParsing( "1.2.3-beta99-SNAPSHOT", "1.2.3", "beta", "99", "SNAPSHOT" );
84          checkParsing( "1.2.3-RC4", "1.2.3", "RC", "4", null );
85      }
86  
87      public void testParseSeparators()
88          throws Exception
89      {
90          checkParsing( "1.2.9-beta-9-SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT" );
91          checkParsing( "1.2.9beta9SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT" );
92          checkParsing( "1.2.9beta-9SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT" );
93          checkParsing( "1.2.9_beta_9_SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT" );
94      }
95  
96      public void testParseAnnotationNoVersionButSnapshot()
97          throws Exception
98      {
99          checkParsing( "1.0-beta-SNAPSHOT", "1.0", "beta", null, "SNAPSHOT" );
100         checkParsing( "1.2.3-beta99", "1.2.3", "beta", "99", null );
101         checkParsing( "1.2.3-RC4-SNAPSHOT", "1.2.3", "RC", "4", "SNAPSHOT" );
102     }
103 
104     public void testParseAnnotationVersionWithRevision()
105         throws Exception
106     {
107         checkParsing( "1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT" );
108         checkParsing( "1.0-beta-4", "1.0", "beta", "4", null );
109         checkParsing( "1.2.3-beta-99", "1.2.3", "beta", "99", null );
110         checkParsing( "1.2.3-beta99", "1.2.3", "beta", "99", null );
111         checkParsing( "1.2.3-RC4", "1.2.3", "RC", "4", null );
112 
113         checkParsing( "1.2.9", "1.2.9", null, null, null );
114     }
115 
116     public void testParseAnnotationVersionWithoutRevision()
117         throws Exception
118     {
119         checkParsing( "1.0-beta", "1.0", "beta", null, null );
120         checkParsing( "1.0-beta-SNAPSHOT", "1.0", "beta", null, "SNAPSHOT" );
121     }
122 
123     public void testParseAnnotationRevisionOnly()
124         throws Exception
125     {
126         checkParsing( "1.0-4", "1.0", null, "4", null );
127     }
128 
129     public void testParseLeadingZeros()
130         throws Exception
131     {
132         checkParsing( "1.01-beta-04-SNAPSHOT", "1.01", "beta", "04", "SNAPSHOT" );
133         checkParsing( "01.01.001-beta-04-SNAPSHOT", "01.01.001", "beta", "04", "SNAPSHOT" );
134     }
135 
136     public void testParseBuildNumber()
137         throws Exception
138     {
139         checkParsing( "1.0-alpha-2-20051013.095555-2", "1.0", "alpha", "2", "20051013.095555-2" );
140     }
141 
142     public void testNextVersion()
143         throws Exception
144     {
145         VersionInfo v = new DefaultVersionInfo( "SNAPSHOT" );
146         assertNull( v.getNextVersion() );
147 
148         checkNextVersion( "1", "2" );
149         checkNextVersion( "1.01", "1.02" );
150         checkNextVersion( "1.9", "1.10" );
151         checkNextVersion( "1.09", "1.10" );
152         checkNextVersion( "1.009", "1.010" );
153 
154         checkNextVersion( "1.99", "1.100" );
155 
156         //MRELEASE-623 SNAPSHOT is case-insensitive
157         checkNextVersion( "2.2-SNAPshot", "2.3-SNAPshot" );
158     }
159 
160     public void testNextAnnotationRevision()
161         throws Exception
162     {
163         checkNextVersion( "1.01-beta-04", "1.01-beta-05" );
164         checkNextVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-05-SNAPSHOT" );
165         checkNextVersion( "9.99.999-beta-9-SNAPSHOT", "9.99.999-beta-10-SNAPSHOT" );
166         checkNextVersion( "9.99.999-beta-09-SNAPSHOT", "9.99.999-beta-10-SNAPSHOT" );
167         checkNextVersion( "9.99.999-beta-009-SNAPSHOT", "9.99.999-beta-010-SNAPSHOT" );
168         checkNextVersion( "9.99.999-beta9-SNAPSHOT", "9.99.999-beta10-SNAPSHOT" );
169     }
170 
171     public void testCompareToDigitsOnly()
172         throws Exception
173     {
174         checkVersionLessThanVersion( "1.01", "1.02" );
175 
176         // M2.2.1
177         // checkVersionLessThanVersion( "1.00009", "1.01" );
178         // M3.0, because prefix 0's are ignored, hence 1 < 9
179         checkVersionLessThanVersion( "1.01", "1.00009" );
180 
181         checkVersionLessThanVersion( "1.01", "1.01.01" );
182 
183         // M2.2.1
184         // checkVersionLessThanVersion( "1.01", "1.1" );
185         // M3.0, because prefix 0's are ignored, hence 1 == 1
186         checkVersionEqualVersion( "1.01", "1.1" );
187 
188         checkVersionEqualVersion( "1.01", "1.01" );
189 
190         // M2.2.1
191         // checkVersionLessThanVersion( "1.001", "1.01" );
192         // M3.0, because prefix 0's are ignored, hence 1 == 1
193         checkVersionEqualVersion( "1.001", "1.01" );
194     }
195 
196     public void testCompareToAnnotation()
197         throws Exception
198     {
199         checkVersionLessThanVersion( "1.01-alpha", "1.01" );
200         checkVersionLessThanVersion( "1.01-alpha", "1.01-beta" );
201         checkVersionLessThanVersion( "1.01-beta", "1.01-RC1" );
202         checkVersionLessThanVersion( "1.01-beta", "1.01-RC" );
203         checkVersionLessThanVersion( "1.01-alpha-4", "1.01.1-beta-1" );
204         checkVersionLessThanVersion( "1.01-alpha-4-SNAPSHOT", "1.01-beta" );
205         checkVersionLessThanVersion( "1.01-alpha-4-SNAPSHOT", "1.01-alpha-4" );
206         checkVersionLessThanVersion( "1.01-alpha-4", "1.01-alpha-5-SNAPSHOT" );
207 
208         // M2.2.1
209         // checkVersionLessThanVersion( "1.01-alpha-004-SNAPSHOT", "1.01-alpha-4-SNAPSHOT" );
210         // M3.0, because prefix 0's are ignored, hence 4 == 4
211         checkVersionEqualVersion( "1.01-alpha-004-SNAPSHOT", "1.01-alpha-4-SNAPSHOT" );
212     }
213 
214     public void testCompareToAnnotationRevision()
215         throws Exception
216     {
217         checkVersionLessThanVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-05-SNAPSHOT" );
218         checkVersionLessThanVersion( "1.01-beta-0004-SNAPSHOT", "1.01-beta-5-SNAPSHOT" );
219         checkVersionLessThanVersion( "1.01-beta-4-SNAPSHOT", "1.01.1-beta-4-SNAPSHOT" );
220 
221         // M2.2.1
222         // checkVersionLessThanVersion( "1.01-beta-0004-SNAPSHOT", "1.01-beta-4-SNAPSHOT" );
223         // M3.0, because prefix 0's are ignored, hence 4 == 4
224         checkVersionEqualVersion( "1.01-beta-0004-SNAPSHOT", "1.01-beta-4-SNAPSHOT" );
225     }
226 
227     public void testCompareToBuildSpecifier()
228         throws Exception
229     {
230         checkVersionLessThanVersion( "1.01-SNAPSHOT", "1.01" );
231         checkVersionLessThanVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-04" );
232 
233         checkVersionEqualVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-04-SNAPSHOT" );
234 
235         if ( !"3.0".equals( mavenVersion ) )
236         {
237             // bug??
238             checkVersionLessThanVersion( "1.01-beta-04-20051112.134500-2", "1.01-beta-04-SNAPSHOT" );
239         }
240         checkVersionLessThanVersion( "1.01-beta-04-20051112.134500-1", "1.01-beta-04-20051112.134500-2" );
241         checkVersionLessThanVersion( "1.01-beta-04-20051112.134500-1", "1.01-beta-04-20051113.134500-1" );
242     }
243 
244     public void testGetReleaseVersion()
245         throws Exception
246     {
247         checkGetReleaseVersion( "1-SNAPSHOT", "1" );
248         checkGetReleaseVersion( "1", "1" );
249 
250         checkGetReleaseVersion( "1.01", "1.01" );
251         checkGetReleaseVersion( "1.01-beta", "1.01-beta" );
252         checkGetReleaseVersion( "1.01-beta-04", "1.01-beta-04" );
253 
254         checkGetReleaseVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-04" );
255         checkGetReleaseVersion( "1.01-beta-04-20051112.134500-1", "1.01-beta-04" );
256     }
257 
258     public void testGetSnapshotVersion()
259         throws Exception
260     {
261         checkGetSnapshotVersion( "1", "1-SNAPSHOT" );
262         checkGetSnapshotVersion( "1.01", "1.01-SNAPSHOT" );
263         checkGetSnapshotVersion( "1.01-beta", "1.01-beta-SNAPSHOT" );
264         checkGetSnapshotVersion( "1.01-beta-04", "1.01-beta-04-SNAPSHOT" );
265 
266         checkGetSnapshotVersion( "SNAPSHOT", "SNAPSHOT" );
267         // TODO: bug in Artifact pattern
268 //        checkGetSnapshotVersion( "20051112.134500-1", "SNAPSHOT" );
269         checkGetSnapshotVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-04-SNAPSHOT" );
270         checkGetSnapshotVersion( "1.01-beta-04-20051112.134500-1", "1.01-beta-04-SNAPSHOT" );
271         checkGetSnapshotVersion( "1.01-beta-04_20051112.134500-1", "1.01-beta-04_20051112.134500-1-SNAPSHOT" );
272     }
273 
274     public void testSnapshot()
275         throws VersionParseException
276     {
277         assertFalse( new DefaultVersionInfo( "1.01" ).isSnapshot() );
278         assertFalse( new DefaultVersionInfo( "1.01-beta" ).isSnapshot() );
279         assertFalse( new DefaultVersionInfo( "1.01-beta-04" ).isSnapshot() );
280 
281         assertTrue( new DefaultVersionInfo( "1.01-beta-04-SNAPSHOT" ).isSnapshot() );
282         assertTrue( new DefaultVersionInfo( "1.01-beta-04-20051112.134500-1" ).isSnapshot() );
283         assertFalse( new DefaultVersionInfo( "1.01-beta-04_20051112.134500-1" ).isSnapshot() );
284 
285     }
286 
287     //MRELEASE-623 SNAPSHOT is case-insensitive
288     public void testCaseInsensitiveSnapshot() throws VersionParseException
289     {
290         DefaultVersionInfo currentVersionInfo = new DefaultVersionInfo( "2.2-SNAPshot" );
291         assertTrue( currentVersionInfo.isSnapshot()  );
292         assertEquals( "2.2", currentVersionInfo.getReleaseVersionString() );
293         VersionInfo nextVersionInfo = currentVersionInfo.getNextVersion();
294         assertEquals( "2.3-SNAPSHOT", nextVersionInfo.getSnapshotVersionString() );
295     }
296 
297 //    Ignore, new DefaultVersionInfo( "LATEST") throws VersionParseException
298 //    public void testLatest() throws VersionParseException
299 //    {
300 //        assertTrue( new DefaultVersionInfo( "LATEST") .isSnapshot() );
301 //    }
302 
303     private static void checkGetReleaseVersion( String strVersion, String expected )
304         throws Exception
305     {
306         VersionInfo v = new DefaultVersionInfo( strVersion );
307         assertEquals( expected, v.getReleaseVersionString() );
308     }
309 
310     private static void checkGetSnapshotVersion( String strVersion, String expected )
311         throws Exception
312     {
313         VersionInfo v = new DefaultVersionInfo( strVersion );
314         assertEquals( expected, v.getSnapshotVersionString() );
315     }
316 
317     private static void checkParsing( String strVersion, String digits, String annotation, String annotationRevision,
318                                       String buildSpecifier )
319         throws Exception
320     {
321         DefaultVersionInfo v = new DefaultVersionInfo( strVersion );
322 
323         assertEquals( strVersion, v.toString() );
324         assertEquals( digits, DefaultVersionInfo.joinDigitString( v.getDigits() ) );
325         assertEquals( annotation, v.getAnnotation() );
326         assertEquals( annotationRevision, v.getAnnotationRevision() );
327         assertEquals( buildSpecifier, v.getBuildSpecifier() );
328     }
329 
330     private static void checkNextVersion( String strVersion, String nextVersion )
331         throws Exception
332     {
333         VersionInfo v = new DefaultVersionInfo( strVersion );
334         VersionInfo nextV = v.getNextVersion();
335 
336         assertNotNull( nextV );
337         assertEquals( nextVersion, nextV.toString() );
338     }
339 
340     private static void checkVersionLessThanVersion( String lesserVersion, String greaterVersion )
341         throws VersionParseException
342     {
343         checkCompareTo( lesserVersion, greaterVersion, -1 );
344         checkCompareTo( greaterVersion, lesserVersion, +1 );
345     }
346 
347     private static void checkVersionEqualVersion( String version1, String version2 )
348         throws Exception
349     {
350         checkCompareTo( version1, version2, 0 );
351     }
352 
353     private static void checkCompareTo( String lesserVersion, String greaterVersion, int comparison )
354         throws VersionParseException
355     {
356         VersionInfo lesserV = new DefaultVersionInfo( lesserVersion );
357         VersionInfo greaterV = new DefaultVersionInfo( greaterVersion );
358 
359         if ( comparison == 0 )
360         {
361             assertEquals( 0, lesserV.compareTo( greaterV ) );
362             assertEquals( lesserV, greaterV );
363         }
364         else if ( comparison < 0 )
365         {
366             assertTrue( "Expected less but was " + lesserV.compareTo( greaterV ), lesserV.compareTo( greaterV ) < 0 );
367         }
368         else if ( comparison > 0 )
369         {
370             assertTrue( "Expected more but was " + lesserV.compareTo( greaterV ), lesserV.compareTo( greaterV ) > 0 );
371         }
372     }
373 }