View Javadoc
1   package org.nuiton.version;
2   
3   import org.junit.Assert;
4   import org.junit.Test;
5   
6   /*
7    * #%L
8    * Nuiton Version
9    * %%
10   * Copyright (C) 2016 CodeLutin, Chatellier Éric
11   * %%
12   * This program is free software: you can redistribute it and/or modify
13   * it under the terms of the GNU Lesser General Public License as
14   * published by the Free Software Foundation, either version 3 of the
15   * License, or (at your option) any later version.
16   * 
17   * This program is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Lesser Public License for more details.
21   * 
22   * You should have received a copy of the GNU General Lesser Public
23   * License along with this program.  If not, see
24   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
25   * #L%
26   */
27  
28  /**
29   * Test about {@link Versions} utility class.
30   * 
31   * @author Eric Chatellier
32   */
33  public class VersionsTest {
34  
35      /**
36       * Test to create a new version by extracting a single component.
37       */
38      @Test
39      public void testExtractSingleComponent() {
40          Version initVersion = Versions.valueOf("10.20.30.40");
41          Version subVersion = Versions.extractVersion(initVersion, 2);
42          Assert.assertEquals(Versions.valueOf("30"), subVersion);
43      }
44      
45      /**
46       * Test to create a new version by extracting a components sub set.
47       */
48      @Test
49      public void testExtractTwoComponents() {
50          Version initVersion = Versions.valueOf("10.20.30.40");
51          Version subVersion = Versions.extractVersion(initVersion, 2, 3);
52          Assert.assertEquals(Versions.valueOf("30.40"), subVersion);
53      }
54      
55      /**
56       * Test to create a new version by extracting with illegal parameters.
57       */
58      @Test(expected=IllegalArgumentException.class)
59      public void testExtractIllegalParams() {
60          Version initVersion = Versions.valueOf("10.20.30.40");
61          Version subVersion = Versions.extractVersion(initVersion, 3, 2);
62      }
63  
64      @Test
65      public void testIncrements() {
66  
67          Version[] versions = new Version[]{
68                  VersionBuilder.create().setSnapshot(true).build(),
69                  VersionBuilder.create().build(),
70                  Versions.valueOf("1"),
71                  Versions.valueOf("1.1-alpha-1"),
72                  Versions.valueOf("1.1-alpha-2"),
73                  Versions.valueOf("1.1-beta-1"),
74                  Versions.valueOf("1.1-rc-1-SNAPSHOT"),
75                  Versions.valueOf("1.1-rc-1"),
76                  Versions.valueOf("1.1-rc2"),
77                  Versions.valueOf("1.1"),
78                  Versions.valueOf("1.1.1"),
79                  Versions.valueOf("1.1.1-blablah"),
80                  Versions.valueOf("1.1.1-blablah1-SNAPSHOT"),
81                  Versions.valueOf("1.1.1-blablah1"),
82                  Versions.valueOf("1.1.1-blablah1a"),
83                  Versions.valueOf("1.1.1-blablah1b")
84          };
85  
86          Version[] versionsIncrements = new Version[]{
87                  Versions.valueOf("1-SNAPSHOT"),
88                  Versions.valueOf("1"),
89                  Versions.valueOf("2"),
90                  Versions.valueOf("1.1-alpha-2"),
91                  Versions.valueOf("1.1-alpha-3"),
92                  Versions.valueOf("1.1-beta-2"),
93                  Versions.valueOf("1.1-rc-2-SNAPSHOT"),
94                  Versions.valueOf("1.1-rc-2"),
95                  Versions.valueOf("1.1-rc3"),
96                  Versions.valueOf("1.2"),
97                  Versions.valueOf("1.1.2"),
98                  Versions.valueOf("1.1.1-blablah.1"),
99                  Versions.valueOf("1.1.1-blablah2-SNAPSHOT"),
100                 Versions.valueOf("1.1.1-blablah2"),
101                 Versions.valueOf("1.1.1-blablah1a.1"),
102                 Versions.valueOf("1.1.1-blablah1b.1")
103         };
104 
105         for (int i = 0, l = versions.length; i < l; i++) {
106 
107             Version v = versions[i];
108             Version vIncrements = versionsIncrements[i];
109             Version increments = Versions.increments(v);
110             Assert.assertEquals(v + " + 1 = " + increments + " should be " + vIncrements, increments, vIncrements);
111             Assert.assertEquals(v + " + 1 = " + increments + " should be " + vIncrements, increments.getVersion(), vIncrements.getVersion());
112 
113         }
114 
115     }
116 }