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 org.junit.Before;
27  import org.junit.Test;
28  
29  import static org.junit.Assert.assertEquals;
30  import static org.junit.Assert.assertFalse;
31  import static org.junit.Assert.assertNotNull;
32  import static org.junit.Assert.assertNull;
33  import static org.junit.Assert.assertTrue;
34  import static org.junit.Assert.fail;
35  
36  public class DefaultVersionInfoTest
37  {
38      private String mavenVersion;
39  
40      @Before
41      public void setUp()
42          throws Exception
43      {
44          Properties pomProperties = new Properties();
45          pomProperties.load( DefaultArtifactVersion.class.getResourceAsStream( "/META-INF/maven/org.apache.maven/maven-artifact/pom.properties" ) );
46          mavenVersion = pomProperties.getProperty( "version" );
47      }
48  
49      @Test
50      public void testParse()
51          throws Exception
52      {
53          checkParsing( "1.0", "1.0", null, null, null );
54      }
55  
56      @Test
57      public void testParseWithBadVersion()
58          throws Exception
59      {
60          try
61          {
62              checkParsing( "foo", null, null, null, "foo" );
63              fail( "version is incorrect, must fail." );
64          }
65          catch ( VersionParseException e )
66          {
67          }
68      }
69  
70      @Test
71      public void testParseMultiDigit()
72          throws Exception
73      {
74          checkParsing( "99.99", "99.99", null, null, null );
75          checkParsing( "990.990.990", "990.990.990", null, null, null );
76      }
77  
78      @Test
79      public void testParseSnapshotVersion()
80          throws Exception
81      {
82          checkParsing( "SNAPSHOT", null, null, null, "SNAPSHOT" );
83          checkParsing( "1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT" );
84          checkParsing( "1.0-beta-4_SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT" );
85      }
86  
87      @Test
88      public void testParseAnnotationVersion()
89          throws Exception
90      {
91          checkParsing( "1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT" );
92          checkParsing( "1.0-beta-4", "1.0", "beta", "4", null );
93          checkParsing( "1.2.3-beta-99", "1.2.3", "beta", "99", null );
94          checkParsing( "1.2.3-beta99", "1.2.3", "beta", "99", null );
95          checkParsing( "1.2.3-beta99-SNAPSHOT", "1.2.3", "beta", "99", "SNAPSHOT" );
96          checkParsing( "1.2.3-RC4", "1.2.3", "RC", "4", null );
97      }
98  
99      @Test
100     public void testParseSeparators()
101         throws Exception
102     {
103         checkParsing( "1.2.9-beta-9-SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT" );
104         checkParsing( "1.2.9beta9SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT" );
105         checkParsing( "1.2.9beta-9SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT" );
106         checkParsing( "1.2.9_beta_9_SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT" );
107     }
108 
109     @Test
110     public void testParseAnnotationNoVersionButSnapshot()
111         throws Exception
112     {
113         checkParsing( "1.0-beta-SNAPSHOT", "1.0", "beta", null, "SNAPSHOT" );
114         checkParsing( "1.2.3-beta99", "1.2.3", "beta", "99", null );
115         checkParsing( "1.2.3-RC4-SNAPSHOT", "1.2.3", "RC", "4", "SNAPSHOT" );
116     }
117 
118     @Test
119     public void testParseAnnotationVersionWithRevision()
120         throws Exception
121     {
122         checkParsing( "1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT" );
123         checkParsing( "1.0-beta-4", "1.0", "beta", "4", null );
124         checkParsing( "1.2.3-beta-99", "1.2.3", "beta", "99", null );
125         checkParsing( "1.2.3-beta99", "1.2.3", "beta", "99", null );
126         checkParsing( "1.2.3-RC4", "1.2.3", "RC", "4", null );
127 
128         checkParsing( "1.2.9", "1.2.9", null, null, null );
129     }
130 
131     @Test
132     public void testParseAnnotationVersionWithoutRevision()
133         throws Exception
134     {
135         checkParsing( "1.0-beta", "1.0", "beta", null, null );
136         checkParsing( "1.0-beta-SNAPSHOT", "1.0", "beta", null, "SNAPSHOT" );
137     }
138 
139     @Test
140     public void testParseAnnotationRevisionOnly()
141         throws Exception
142     {
143         checkParsing( "1.0-4", "1.0", null, "4", null );
144     }
145 
146     @Test
147     public void testParseLeadingZeros()
148         throws Exception
149     {
150         checkParsing( "1.01-beta-04-SNAPSHOT", "1.01", "beta", "04", "SNAPSHOT" );
151         checkParsing( "01.01.001-beta-04-SNAPSHOT", "01.01.001", "beta", "04", "SNAPSHOT" );
152     }
153 
154     @Test
155     public void testParseBuildNumber()
156         throws Exception
157     {
158         checkParsing( "1.0-alpha-2-20051013.095555-2", "1.0", "alpha", "2", "20051013.095555-2" );
159     }
160 
161     @Test
162     public void testNextVersion()
163         throws Exception
164     {
165         VersionInfo v = new DefaultVersionInfo( "SNAPSHOT" );
166         assertNull( v.getNextVersion() );
167 
168         checkNextVersion( "1", "2" );
169         checkNextVersion( "1.01", "1.02" );
170         checkNextVersion( "1.9", "1.10" );
171         checkNextVersion( "1.09", "1.10" );
172         checkNextVersion( "1.009", "1.010" );
173 
174         checkNextVersion( "1.99", "1.100" );
175 
176         //MRELEASE-623 SNAPSHOT is case-insensitive
177         checkNextVersion( "2.2-SNAPshot", "2.3-SNAPshot" );
178     }
179 
180     @Test
181     public void testNextAnnotationRevision()
182         throws Exception
183     {
184         checkNextVersion( "1.01-beta-04", "1.01-beta-05" );
185         checkNextVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-05-SNAPSHOT" );
186         checkNextVersion( "9.99.999-beta-9-SNAPSHOT", "9.99.999-beta-10-SNAPSHOT" );
187         checkNextVersion( "9.99.999-beta-09-SNAPSHOT", "9.99.999-beta-10-SNAPSHOT" );
188         checkNextVersion( "9.99.999-beta-009-SNAPSHOT", "9.99.999-beta-010-SNAPSHOT" );
189         checkNextVersion( "9.99.999-beta9-SNAPSHOT", "9.99.999-beta10-SNAPSHOT" );
190     }
191 
192     @Test
193     public void testCompareToDigitsOnly()
194         throws Exception
195     {
196         checkVersionLessThanVersion( "1.01", "1.02" );
197 
198         // M2.2.1
199         // checkVersionLessThanVersion( "1.00009", "1.01" );
200         // M3.0, because prefix 0's are ignored, hence 1 < 9
201         checkVersionLessThanVersion( "1.01", "1.00009" );
202 
203         checkVersionLessThanVersion( "1.01", "1.01.01" );
204 
205         // M2.2.1
206         // checkVersionLessThanVersion( "1.01", "1.1" );
207         // M3.0, because prefix 0's are ignored, hence 1 == 1
208         checkVersionEqualVersion( "1.01", "1.1" );
209 
210         checkVersionEqualVersion( "1.01", "1.01" );
211 
212         // M2.2.1
213         // checkVersionLessThanVersion( "1.001", "1.01" );
214         // M3.0, because prefix 0's are ignored, hence 1 == 1
215         checkVersionEqualVersion( "1.001", "1.01" );
216     }
217 
218     @Test
219     public void testCompareToAnnotation()
220         throws Exception
221     {
222         checkVersionLessThanVersion( "1.01-alpha", "1.01" );
223         checkVersionLessThanVersion( "1.01-alpha", "1.01-beta" );
224         checkVersionLessThanVersion( "1.01-beta", "1.01-RC1" );
225         checkVersionLessThanVersion( "1.01-beta", "1.01-RC" );
226         checkVersionLessThanVersion( "1.01-alpha-4", "1.01.1-beta-1" );
227         checkVersionLessThanVersion( "1.01-alpha-4-SNAPSHOT", "1.01-beta" );
228         checkVersionLessThanVersion( "1.01-alpha-4-SNAPSHOT", "1.01-alpha-4" );
229         checkVersionLessThanVersion( "1.01-alpha-4", "1.01-alpha-5-SNAPSHOT" );
230 
231         // M2.2.1
232         // checkVersionLessThanVersion( "1.01-alpha-004-SNAPSHOT", "1.01-alpha-4-SNAPSHOT" );
233         // M3.0, because prefix 0's are ignored, hence 4 == 4
234         checkVersionEqualVersion( "1.01-alpha-004-SNAPSHOT", "1.01-alpha-4-SNAPSHOT" );
235     }
236 
237     @Test
238     public void testCompareToAnnotationRevision()
239         throws Exception
240     {
241         checkVersionLessThanVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-05-SNAPSHOT" );
242         checkVersionLessThanVersion( "1.01-beta-0004-SNAPSHOT", "1.01-beta-5-SNAPSHOT" );
243         checkVersionLessThanVersion( "1.01-beta-4-SNAPSHOT", "1.01.1-beta-4-SNAPSHOT" );
244 
245         // M2.2.1
246         // checkVersionLessThanVersion( "1.01-beta-0004-SNAPSHOT", "1.01-beta-4-SNAPSHOT" );
247         // M3.0, because prefix 0's are ignored, hence 4 == 4
248         checkVersionEqualVersion( "1.01-beta-0004-SNAPSHOT", "1.01-beta-4-SNAPSHOT" );
249     }
250 
251     @Test
252     public void testCompareToBuildSpecifier()
253         throws Exception
254     {
255         checkVersionLessThanVersion( "1.01-SNAPSHOT", "1.01" );
256         checkVersionLessThanVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-04" );
257 
258         checkVersionEqualVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-04-SNAPSHOT" );
259 
260         if ( !"3.0".equals( mavenVersion ) )
261         {
262             // TODO: bug??
263             // checkVersionLessThanVersion( "1.01-beta-04-20051112.134500-2", "1.01-beta-04-SNAPSHOT" );
264         }
265         checkVersionLessThanVersion( "1.01-beta-04-20051112.134500-1", "1.01-beta-04-20051112.134500-2" );
266         checkVersionLessThanVersion( "1.01-beta-04-20051112.134500-1", "1.01-beta-04-20051113.134500-1" );
267     }
268 
269     @Test
270     public void testGetReleaseVersion()
271         throws Exception
272     {
273         checkGetReleaseVersion( "1-SNAPSHOT", "1" );
274         checkGetReleaseVersion( "1", "1" );
275 
276         checkGetReleaseVersion( "1.01", "1.01" );
277         checkGetReleaseVersion( "1.01-beta", "1.01-beta" );
278         checkGetReleaseVersion( "1.01-beta-04", "1.01-beta-04" );
279 
280         checkGetReleaseVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-04" );
281         checkGetReleaseVersion( "1.01-beta-04-20051112.134500-1", "1.01-beta-04" );
282     }
283 
284     @Test
285     public void testGetSnapshotVersion()
286         throws Exception
287     {
288         checkGetSnapshotVersion( "1", "1-SNAPSHOT" );
289         checkGetSnapshotVersion( "1.01", "1.01-SNAPSHOT" );
290         checkGetSnapshotVersion( "1.01-beta", "1.01-beta-SNAPSHOT" );
291         checkGetSnapshotVersion( "1.01-beta-04", "1.01-beta-04-SNAPSHOT" );
292 
293         checkGetSnapshotVersion( "SNAPSHOT", "SNAPSHOT" );
294         // TODO: bug in Artifact pattern
295 //        checkGetSnapshotVersion( "20051112.134500-1", "SNAPSHOT" );
296         checkGetSnapshotVersion( "1.01-beta-04-SNAPSHOT", "1.01-beta-04-SNAPSHOT" );
297         checkGetSnapshotVersion( "1.01-beta-04-20051112.134500-1", "1.01-beta-04-SNAPSHOT" );
298         checkGetSnapshotVersion( "1.01-beta-04_20051112.134500-1", "1.01-beta-04_20051112.134500-1-SNAPSHOT" );
299     }
300 
301     @Test
302     public void testSnapshot()
303         throws VersionParseException
304     {
305         assertFalse( new DefaultVersionInfo( "1.01" ).isSnapshot() );
306         assertFalse( new DefaultVersionInfo( "1.01-beta" ).isSnapshot() );
307         assertFalse( new DefaultVersionInfo( "1.01-beta-04" ).isSnapshot() );
308 
309         assertTrue( new DefaultVersionInfo( "1.01-beta-04-SNAPSHOT" ).isSnapshot() );
310         assertTrue( new DefaultVersionInfo( "1.01-beta-04-20051112.134500-1" ).isSnapshot() );
311         assertFalse( new DefaultVersionInfo( "1.01-beta-04_20051112.134500-1" ).isSnapshot() );
312 
313     }
314 
315     //MRELEASE-623 SNAPSHOT is case-insensitive
316     @Test
317     public void testCaseInsensitiveSnapshot() throws VersionParseException
318     {
319         DefaultVersionInfo currentVersionInfo = new DefaultVersionInfo( "2.2-SNAPshot" );
320         assertTrue( currentVersionInfo.isSnapshot()  );
321         assertEquals( "2.2", currentVersionInfo.getReleaseVersionString() );
322         VersionInfo nextVersionInfo = currentVersionInfo.getNextVersion();
323         assertEquals( "2.3-SNAPSHOT", nextVersionInfo.getSnapshotVersionString() );
324     }
325 
326 //    Ignore, new DefaultVersionInfo( "LATEST") throws VersionParseException
327 //    public void testLatest() throws VersionParseException
328 //    {
329 //        assertTrue( new DefaultVersionInfo( "LATEST") .isSnapshot() );
330 //    }
331 
332     private static void checkGetReleaseVersion( String strVersion, String expected )
333         throws Exception
334     {
335         VersionInfo v = new DefaultVersionInfo( strVersion );
336         assertEquals( expected, v.getReleaseVersionString() );
337     }
338 
339     private static void checkGetSnapshotVersion( String strVersion, String expected )
340         throws Exception
341     {
342         VersionInfo v = new DefaultVersionInfo( strVersion );
343         assertEquals( expected, v.getSnapshotVersionString() );
344     }
345 
346     private static void checkParsing( String strVersion, String digits, String annotation, String annotationRevision,
347                                       String buildSpecifier )
348         throws Exception
349     {
350         DefaultVersionInfo v = new DefaultVersionInfo( strVersion );
351 
352         assertEquals( strVersion, v.toString() );
353         assertEquals( digits, DefaultVersionInfo.joinDigitString( v.getDigits() ) );
354         assertEquals( annotation, v.getAnnotation() );
355         assertEquals( annotationRevision, v.getAnnotationRevision() );
356         assertEquals( buildSpecifier, v.getBuildSpecifier() );
357     }
358 
359     private static void checkNextVersion( String strVersion, String nextVersion )
360         throws Exception
361     {
362         VersionInfo v = new DefaultVersionInfo( strVersion );
363         VersionInfo nextV = v.getNextVersion();
364 
365         assertNotNull( nextV );
366         assertEquals( nextVersion, nextV.toString() );
367     }
368 
369     private static void checkVersionLessThanVersion( String lesserVersion, String greaterVersion )
370         throws VersionParseException
371     {
372         checkCompareTo( lesserVersion, greaterVersion, -1 );
373         checkCompareTo( greaterVersion, lesserVersion, +1 );
374     }
375 
376     private static void checkVersionEqualVersion( String version1, String version2 )
377         throws Exception
378     {
379         checkCompareTo( version1, version2, 0 );
380     }
381 
382     private static void checkCompareTo( String lesserVersion, String greaterVersion, int comparison )
383         throws VersionParseException
384     {
385         VersionInfo lesserV = new DefaultVersionInfo( lesserVersion );
386         VersionInfo greaterV = new DefaultVersionInfo( greaterVersion );
387 
388         if ( comparison == 0 )
389         {
390             assertEquals( 0, lesserV.compareTo( greaterV ) );
391             assertEquals( lesserV, greaterV );
392         }
393         else if ( comparison < 0 )
394         {
395             assertTrue( "Expected less but was " + lesserV.compareTo( greaterV ), lesserV.compareTo( greaterV ) < 0 );
396         }
397         else if ( comparison > 0 )
398         {
399             assertTrue( "Expected more but was " + lesserV.compareTo( greaterV ), lesserV.compareTo( greaterV ) > 0 );
400         }
401     }
402 }