View Javadoc
1   package org.nuiton.version;
2   
3   /*
4    * #%L
5    * Nuiton Version
6    * %%
7    * Copyright (C) 2016 CodeLutin
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as 
11   * published by the Free Software Foundation, either version 3 of the 
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Lesser Public 
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * #L%
23   */
24  
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import java.util.Arrays;
30  
31  /**
32   * @author Benjamin Poussin - poussin@codelutin.com
33   */
34  public class SemVerTest {
35  
36      /**
37       * Liste de toutes les versions a tester, de la plus petite a la plus grande
38       */
39      String[] versions = {
40              "0",
41              "0.0.0-SNAPSHOT",
42              "0.0.0",
43              "1.0.0-alpha-SNAPSHOT",
44              "1.0.0-alpha",
45              "1.0.0-alpha.1",
46              "1.0.0-beta.2 ",
47              " 1.0.0-beta.11 ",
48              " 1.0.0-rc.1 ",
49              " 1.0.0-rc.1+build.1-SNAPSHOT ",
50              " 1.0.0-rc.1+build.1 ",
51              " 1.0.0 ",
52              " 1.0.0+0.3.7 ",
53              " 1.0.0.23 ",
54              " 1.3.7+build ",
55              " 1.3.7+build.2.b8f12d7 ",
56              " 1.3.7+build.11.e0f985a",
57              "99.100-20130127+r123",
58      };
59  
60      // on doit avoir ici les memes entrees que versions dans le meme ordre
61      String[][] versionSplits = {
62              {"0", null, null, null},
63              {"0.0.0", null, null, "SNAPSHOT"},
64              {"0.0.0", null, null, null},
65              {"1.0.0", "alpha", null, "SNAPSHOT"},
66              {"1.0.0", "alpha", null, null},
67              {"1.0.0", "alpha.1", null, null},
68              {"1.0.0", "beta.2", null, null},
69              {"1.0.0", "beta.11", null, null},
70              {"1.0.0", "rc.1", null, null},
71              {"1.0.0", "rc.1", "build.1", "SNAPSHOT"},
72              {"1.0.0", "rc.1", "build.1", null},
73              {"1.0.0", null, null, null},
74              {"1.0.0", null, "0.3.7", null},
75              {"1.0.0.23", null, null, null},
76              {"1.3.7", null, "build", null},
77              {"1.3.7", null, "build.2.b8f12d7", null},
78              {"1.3.7", null, "build.11.e0f985a", null},
79              {"99.100", "20130127", "r123", null},
80      };
81  
82      @Test
83      public void testCreation() {
84          for (int i = 0; i < versions.length; i++) {
85              SemVer v = new SemVer(versions[i]);
86              SemVer vs = SemVer.creator(versionSplits[i]).done();
87              Assert.assertEquals(v, vs);
88  
89              Assert.assertEquals(versionSplits[i][0], v.getVersion());
90              Assert.assertEquals(versionSplits[i][1], v.getPrerelease());
91              Assert.assertEquals(versionSplits[i][2], v.getBuild());
92              Assert.assertEquals(versionSplits[i][3], v.getSnapshot());
93          }
94      }
95  
96      @Test
97      public void testCreator() {
98          SemVer v = new SemVer(" 1.0.0-rc.1+build.1-SNAPSHOT ");
99          SemVer n = SemVer.creator(v).setVersion(1, 1, 3).setPrerelease("rc.2").setBuild("r123").setSnapshot(false).done();
100         Assert.assertEquals("1.1.3-rc.2+r123", n.toString());
101     }
102 
103     @Test
104     public void testInc() {
105 
106         String[][] testIncValue = {
107                 {"0", "1"},
108                 {"0.0.0-SNAPSHOT", "1.0.0-SNAPSHOT", "0.1.0-SNAPSHOT", "0.0.1-SNAPSHOT"},
109                 {"0.0.0", "1.0.0", "0.1.0", "0.0.1"},
110                 {"1.0.0-alpha-SNAPSHOT", "2.0.0-alpha-SNAPSHOT", "1.1.0-alpha-SNAPSHOT", "1.0.1-alpha-SNAPSHOT"},
111                 {"1.0.0-alpha", "2.0.0-alpha", "1.1.0-alpha", "1.0.1-alpha"},
112                 {"1.0.0-alpha.1", "2.0.0-alpha.1", "1.1.0-alpha.1", "1.0.1-alpha.1"},
113                 {" 1.0.0.23 ", " 2.0.0.23 ", " 1.1.0.23 ", " 1.0.1.23 ", " 1.0.0.24 "},
114                 {"99.100-20130127+r123", "100.100-20130127+r123", "99.101-20130127+r123"},
115         };
116 
117         for (int i = 1; i < testIncValue.length; i++) {
118             SemVer version = new SemVer(testIncValue[i][0]);
119             Assert.assertEquals(String.format(
120                     "Le nombre de composante doit correspondre au nombre de test a faire %s",
121                     Arrays.toString(testIncValue[i])),
122                     testIncValue[i].length - 1, version.getVersionCount());
123             for (int x = 1; x < testIncValue[i].length; x++) {
124                 SemVer expected = new SemVer(testIncValue[i][x]);
125                 SemVer inc = SemVer.creator(version).incVersion(x - 1, 1).done();
126                 Assert.assertEquals(expected, inc);
127             }
128         }
129     }
130 
131     @Test
132     public void testIncMajorMinorPatch() {
133         // on test seulement sur un exemple, les autres cas sont gere par le testInc
134         SemVer version = new SemVer(" 1.0.0.23 ");
135 
136         SemVer incMajor = SemVer.creator(version).incMajor().done();
137         Assert.assertEquals(new SemVer(" 2.0.0.23 "), incMajor);
138 
139         SemVer incMinor = SemVer.creator(version).incMinor().done();
140         Assert.assertEquals(new SemVer(" 1.1.0.23 "), incMinor);
141 
142         SemVer incPatch = SemVer.creator(version).incPatch().done();
143         Assert.assertEquals(new SemVer(" 1.0.1.23 "), incPatch);
144     }
145 
146     @Test
147     public void testGetComposant() {
148         SemVer v = new SemVer(" 1.0.3-rc.1+build.1-SNAPSHOT ");
149         Assert.assertEquals("1", v.getMajor());
150         Assert.assertEquals("0", v.getMinor());
151         Assert.assertEquals("3", v.getPatch());
152 
153         Assert.assertEquals("rc", v.getPrerelease(0));
154         Assert.assertEquals("1", v.getPrerelease(1));
155 
156         Assert.assertEquals("build", v.getBuild(0));
157         Assert.assertEquals("1", v.getBuild(1));
158 
159         Assert.assertEquals("SNAPSHOT", v.getSnapshot());
160 
161     }
162 
163     @Test
164     public void testToJavaIdentifier() {
165         SemVer v = new SemVer(" 1.0.3-rc.1+build.1-SNAPSHOT ");
166         Assert.assertEquals("1_0_3_rc_1_build_1_SNAPSHOT", v.toJavaIdentifier());
167     }
168 
169     /**
170      * Ce test ne sert pas vraiment en temps normale, mais lorsqu'il y a un
171      * test qui fail dans testAll, il est pratique de remettre le test qui
172      * echoue plus specifiquement ici pour le debug
173      */
174     @Test
175     public void testOne() {
176         SemVer vi = new SemVer(" 1.0.0+0.3.7 ");
177         SemVer vj = new SemVer(" 1.0.0.23 ");
178 
179         int result = normalize(vi.compareTo(vj));
180         int expected = -1;
181 
182         Assert.assertEquals(String.format(
183                 "Bad compare: Compare(%s, %s) = %s, expected %s",
184                 vi, vj, result, expected), expected, result);
185 //        System.out.println(String.format(
186 //                "Good compare: Compare(%s, %s) = %s, expected %s",
187 //                vi, vj, result, expected));
188     }
189 
190     @Test
191     public void testAll() {
192         for (int i = 0; i < versions.length; i++) {
193             for (int j = 0; j < versions.length; j++) {
194                 SemVer vi = new SemVer(versions[i]);
195                 SemVer vj = new SemVer(versions[j]);
196 
197                 int result = normalize(vi.compareTo(vj));
198                 int expected = Integer.valueOf(i).compareTo(j); // AThimel 2013/02/26 Rewritten for JDK6 compatibility. Was: Integer.compare(i, j);
199 
200                 Assert.assertEquals(String.format(
201                         "Bad compare: Compare(%s, %s) = %s, expected %s",
202                         vi, vj, result, expected), expected, result);
203             }
204         }
205     }
206 
207     /**
208      * Si le comparator ne renvoi pas -1, 0, 1 on normalize pour avoir -1, 0, 1
209      *
210      * @param v
211      * @return
212      */
213     protected int normalize(int v) {
214         int result = v;
215         if (v != 0) {
216             result = v / Math.abs(v);
217         }
218         return result;
219     }
220 }