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