View Javadoc

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