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 org.junit.jupiter.api.Test;
22  
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertFalse;
25  import static org.junit.jupiter.api.Assertions.assertNotNull;
26  import static org.junit.jupiter.api.Assertions.assertNull;
27  import static org.junit.jupiter.api.Assertions.assertTrue;
28  import static org.junit.jupiter.api.Assertions.fail;
29  
30  class DefaultVersionInfoTest {
31  
32      @Test
33      void testParse() throws Exception {
34          checkParsing("1.0", "1.0", null, null, null);
35      }
36  
37      @Test
38      void testParseWithBadVersion() throws Exception {
39          try {
40              checkParsing("foo", null, null, null, "foo");
41              fail("version is incorrect, must fail.");
42          } catch (VersionParseException e) {
43          }
44      }
45  
46      @Test
47      void testParseMultiDigit() throws Exception {
48          checkParsing("99.99", "99.99", null, null, null);
49          checkParsing("990.990.990", "990.990.990", null, null, null);
50      }
51  
52      @Test
53      void testParseSnapshotVersion() throws Exception {
54          checkParsing("SNAPSHOT", null, null, null, "SNAPSHOT");
55          checkParsing("1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT");
56          checkParsing("1.0-beta-4_SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT");
57      }
58  
59      @Test
60      void testParseAnnotationVersion() throws Exception {
61          checkParsing("1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT");
62          checkParsing("1.0-beta-4", "1.0", "beta", "4", null);
63          checkParsing("1.2.3-beta-99", "1.2.3", "beta", "99", null);
64          checkParsing("1.2.3-beta99", "1.2.3", "beta", "99", null);
65          checkParsing("1.2.3-beta99-SNAPSHOT", "1.2.3", "beta", "99", "SNAPSHOT");
66          checkParsing("1.2.3-RC4", "1.2.3", "RC", "4", null);
67      }
68  
69      @Test
70      void testParseSeparators() throws Exception {
71          checkParsing("1.2.9-beta-9-SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT");
72          checkParsing("1.2.9beta9SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT");
73          checkParsing("1.2.9beta-9SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT");
74          checkParsing("1.2.9_beta_9_SNAPSHOT", "1.2.9", "beta", "9", "SNAPSHOT");
75      }
76  
77      @Test
78      void testParseAnnotationNoVersionButSnapshot() throws Exception {
79          checkParsing("1.0-beta-SNAPSHOT", "1.0", "beta", null, "SNAPSHOT");
80          checkParsing("1.2.3-beta99", "1.2.3", "beta", "99", null);
81          checkParsing("1.2.3-RC4-SNAPSHOT", "1.2.3", "RC", "4", "SNAPSHOT");
82      }
83  
84      @Test
85      void testParseAnnotationVersionWithRevision() throws Exception {
86          checkParsing("1.0-beta-4-SNAPSHOT", "1.0", "beta", "4", "SNAPSHOT");
87          checkParsing("1.0-beta-4", "1.0", "beta", "4", null);
88          checkParsing("1.2.3-beta-99", "1.2.3", "beta", "99", null);
89          checkParsing("1.2.3-beta99", "1.2.3", "beta", "99", null);
90          checkParsing("1.2.3-RC4", "1.2.3", "RC", "4", null);
91  
92          checkParsing("1.2.9", "1.2.9", null, null, null);
93      }
94  
95      @Test
96      void testParseAnnotationVersionWithoutRevision() throws Exception {
97          checkParsing("1.0-beta", "1.0", "beta", null, null);
98          checkParsing("1.0-beta-SNAPSHOT", "1.0", "beta", null, "SNAPSHOT");
99      }
100 
101     @Test
102     void testParseAnnotationRevisionOnly() throws Exception {
103         checkParsing("1.0-4", "1.0", null, "4", null);
104     }
105 
106     @Test
107     void testParseLeadingZeros() throws Exception {
108         checkParsing("1.01-beta-04-SNAPSHOT", "1.01", "beta", "04", "SNAPSHOT");
109         checkParsing("01.01.001-beta-04-SNAPSHOT", "01.01.001", "beta", "04", "SNAPSHOT");
110     }
111 
112     @Test
113     void testParseBuildNumber() throws Exception {
114         checkParsing("1.0-alpha-2-20051013.095555-2", "1.0", "alpha", "2", "20051013.095555-2");
115     }
116 
117     @Test
118     void testNextVersion() throws Exception {
119         VersionInfo v = new DefaultVersionInfo("SNAPSHOT");
120         assertNull(v.getNextVersion());
121 
122         checkNextVersion("1", "2");
123         checkNextVersion("1.01", "1.02");
124         checkNextVersion("1.9", "1.10");
125         checkNextVersion("1.09", "1.10");
126         checkNextVersion("1.009", "1.010");
127 
128         checkNextVersion("1.99", "1.100");
129 
130         // MRELEASE-623 SNAPSHOT is case-insensitive
131         checkNextVersion("2.2-SNAPshot", "2.3-SNAPshot");
132     }
133 
134     @Test
135     void testNextAnnotationRevision() throws Exception {
136         checkNextVersion("1.01-beta-04", "1.01-beta-05");
137         checkNextVersion("1.01-beta-04-SNAPSHOT", "1.01-beta-05-SNAPSHOT");
138         checkNextVersion("9.99.999-beta-9-SNAPSHOT", "9.99.999-beta-10-SNAPSHOT");
139         checkNextVersion("9.99.999-beta-09-SNAPSHOT", "9.99.999-beta-10-SNAPSHOT");
140         checkNextVersion("9.99.999-beta-009-SNAPSHOT", "9.99.999-beta-010-SNAPSHOT");
141         checkNextVersion("9.99.999-beta9-SNAPSHOT", "9.99.999-beta10-SNAPSHOT");
142     }
143 
144     @Test
145     void testCompareToDigitsOnly() throws Exception {
146         checkVersionLessThanVersion("1.01", "1.02");
147 
148         // M2.2.1
149         // checkVersionLessThanVersion( "1.00009", "1.01" );
150         // M3.0, because prefix 0's are ignored, hence 1 < 9
151         checkVersionLessThanVersion("1.01", "1.00009");
152 
153         checkVersionLessThanVersion("1.01", "1.01.01");
154 
155         // M2.2.1
156         // checkVersionLessThanVersion( "1.01", "1.1" );
157         // M3.0, because prefix 0's are ignored, hence 1 == 1
158         checkVersionEqualVersion("1.01", "1.1");
159 
160         checkVersionEqualVersion("1.01", "1.01");
161 
162         // M2.2.1
163         // checkVersionLessThanVersion( "1.001", "1.01" );
164         // M3.0, because prefix 0's are ignored, hence 1 == 1
165         checkVersionEqualVersion("1.001", "1.01");
166     }
167 
168     @Test
169     void testCompareToAnnotation() throws Exception {
170         checkVersionLessThanVersion("1.01-alpha", "1.01");
171         checkVersionLessThanVersion("1.01-alpha", "1.01-beta");
172         checkVersionLessThanVersion("1.01-beta", "1.01-RC1");
173         checkVersionLessThanVersion("1.01-beta", "1.01-RC");
174         checkVersionLessThanVersion("1.01-alpha-4", "1.01.1-beta-1");
175         checkVersionLessThanVersion("1.01-alpha-4-SNAPSHOT", "1.01-beta");
176         checkVersionLessThanVersion("1.01-alpha-4-SNAPSHOT", "1.01-alpha-4");
177         checkVersionLessThanVersion("1.01-alpha-4", "1.01-alpha-5-SNAPSHOT");
178 
179         // M2.2.1
180         // checkVersionLessThanVersion( "1.01-alpha-004-SNAPSHOT", "1.01-alpha-4-SNAPSHOT" );
181         // M3.0, because prefix 0's are ignored, hence 4 == 4
182         checkVersionEqualVersion("1.01-alpha-004-SNAPSHOT", "1.01-alpha-4-SNAPSHOT");
183     }
184 
185     @Test
186     void testCompareToAnnotationRevision() throws Exception {
187         checkVersionLessThanVersion("1.01-beta-04-SNAPSHOT", "1.01-beta-05-SNAPSHOT");
188         checkVersionLessThanVersion("1.01-beta-0004-SNAPSHOT", "1.01-beta-5-SNAPSHOT");
189         checkVersionLessThanVersion("1.01-beta-4-SNAPSHOT", "1.01.1-beta-4-SNAPSHOT");
190 
191         // M2.2.1
192         // checkVersionLessThanVersion( "1.01-beta-0004-SNAPSHOT", "1.01-beta-4-SNAPSHOT" );
193         // M3.0, because prefix 0's are ignored, hence 4 == 4
194         checkVersionEqualVersion("1.01-beta-0004-SNAPSHOT", "1.01-beta-4-SNAPSHOT");
195     }
196 
197     @Test
198     void testCompareToBuildSpecifier() throws Exception {
199         checkVersionLessThanVersion("1.01-SNAPSHOT", "1.01");
200         checkVersionLessThanVersion("1.01-beta-04-SNAPSHOT", "1.01-beta-04");
201 
202         checkVersionEqualVersion("1.01-beta-04-SNAPSHOT", "1.01-beta-04-SNAPSHOT");
203 
204         checkVersionLessThanVersion("1.01-beta-04-20051112.134500-1", "1.01-beta-04-20051112.134500-2");
205         checkVersionLessThanVersion("1.01-beta-04-20051112.134500-1", "1.01-beta-04-20051113.134500-1");
206     }
207 
208     @Test
209     void testGetReleaseVersion() throws Exception {
210         checkGetReleaseVersion("1-SNAPSHOT", "1");
211         checkGetReleaseVersion("1", "1");
212 
213         checkGetReleaseVersion("1.01", "1.01");
214         checkGetReleaseVersion("1.01-beta", "1.01-beta");
215         checkGetReleaseVersion("1.01-beta-04", "1.01-beta-04");
216 
217         checkGetReleaseVersion("1.01-beta-04-SNAPSHOT", "1.01-beta-04");
218         checkGetReleaseVersion("1.01-beta-04-20051112.134500-1", "1.01-beta-04");
219     }
220 
221     @Test
222     void testGetSnapshotVersion() throws Exception {
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     @Test
237     void testSnapshot() throws VersionParseException {
238         assertFalse(new DefaultVersionInfo("1.01").isSnapshot());
239         assertFalse(new DefaultVersionInfo("1.01-beta").isSnapshot());
240         assertFalse(new DefaultVersionInfo("1.01-beta-04").isSnapshot());
241 
242         assertTrue(new DefaultVersionInfo("1.01-beta-04-SNAPSHOT").isSnapshot());
243         assertTrue(new DefaultVersionInfo("1.01-beta-04-20051112.134500-1").isSnapshot());
244         assertFalse(new DefaultVersionInfo("1.01-beta-04_20051112.134500-1").isSnapshot());
245     }
246 
247     // MRELEASE-623 SNAPSHOT is case-insensitive
248     @Test
249     void testCaseInsensitiveSnapshot() throws VersionParseException {
250         DefaultVersionInfo currentVersionInfo = new DefaultVersionInfo("2.2-SNAPshot");
251         assertTrue(currentVersionInfo.isSnapshot());
252         assertEquals("2.2", currentVersionInfo.getReleaseVersionString());
253         VersionInfo nextVersionInfo = currentVersionInfo.getNextVersion();
254         assertEquals("2.3-SNAPSHOT", nextVersionInfo.getSnapshotVersionString());
255     }
256 
257     private static void checkGetReleaseVersion(String strVersion, String expected) throws Exception {
258         VersionInfo v = new DefaultVersionInfo(strVersion);
259         assertEquals(expected, v.getReleaseVersionString());
260     }
261 
262     private static void checkGetSnapshotVersion(String strVersion, String expected) throws Exception {
263         VersionInfo v = new DefaultVersionInfo(strVersion);
264         assertEquals(expected, v.getSnapshotVersionString());
265     }
266 
267     private static void checkParsing(
268             String strVersion, String digits, String annotation, String annotationRevision, String buildSpecifier)
269             throws Exception {
270         DefaultVersionInfo v = new DefaultVersionInfo(strVersion);
271 
272         assertEquals(strVersion, v.toString());
273         assertEquals(digits, DefaultVersionInfo.joinDigitString(v.getDigits()));
274         assertEquals(annotation, v.getAnnotation());
275         assertEquals(annotationRevision, v.getAnnotationRevision());
276         assertEquals(buildSpecifier, v.getBuildSpecifier());
277     }
278 
279     private static void checkNextVersion(String strVersion, String nextVersion) throws Exception {
280         VersionInfo v = new DefaultVersionInfo(strVersion);
281         VersionInfo nextV = v.getNextVersion();
282 
283         assertNotNull(nextV);
284         assertEquals(nextVersion, nextV.toString());
285     }
286 
287     private static void checkVersionLessThanVersion(String lesserVersion, String greaterVersion)
288             throws VersionParseException {
289         checkCompareTo(lesserVersion, greaterVersion, -1);
290         checkCompareTo(greaterVersion, lesserVersion, +1);
291     }
292 
293     private static void checkVersionEqualVersion(String version1, String version2) throws Exception {
294         checkCompareTo(version1, version2, 0);
295     }
296 
297     private static void checkCompareTo(String lesserVersion, String greaterVersion, int comparison)
298             throws VersionParseException {
299         VersionInfo lesserV = new DefaultVersionInfo(lesserVersion);
300         VersionInfo greaterV = new DefaultVersionInfo(greaterVersion);
301 
302         if (comparison == 0) {
303             assertEquals(0, lesserV.compareTo(greaterV));
304             assertEquals(lesserV, greaterV);
305         } else if (comparison < 0) {
306             assertTrue(lesserV.compareTo(greaterV) < 0, "Expected less but was " + lesserV.compareTo(greaterV));
307         } else if (comparison > 0) {
308             assertTrue(lesserV.compareTo(greaterV) > 0, "Expected more but was " + lesserV.compareTo(greaterV));
309         }
310     }
311 }