View Javadoc
1   package org.nuiton.version;
2   
3   /*
4    * #%L
5    * Nuiton Version
6    * %%
7    * Copyright (C) 2016 CodeLutin, Tony Chemit
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  import com.google.common.collect.Lists;
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  /**
30   * Created on 7/10/14.
31   *
32   * @author Tony Chemit - chemit@codelutin.com
33   * @since 3.0
34   */
35  public class VersionBuilderTest {
36  
37      @Test
38      public void testBuildVersionFromString() {
39  
40          buildVersion(VersionBuilder.create().build(), "0", 1, false, 0);
41          buildVersion(VersionBuilder.create().setSnapshot(true).build(), "0-SNAPSHOT", 1, true, 0);
42          buildVersion(VersionBuilder.create("0").build(), "0", 1, false, 0);
43          buildVersion(VersionBuilder.create("1").build(), "1", 1, false, 1);
44          buildVersion(VersionBuilder.create("12.3").build(), "12.3", 2, false, 12, 3);
45          buildVersion(VersionBuilder.create("12.34-alpha-56").build(), "12.34-alpha-56", 4, false, 12, 34, "alpha", 56);
46          buildVersion(VersionBuilder.create("1.1-beta-1").build(), "1.1-beta-1", 4, false, 1, 1, "beta", 1);
47          buildVersion(VersionBuilder.create("1.1-rc-1").build(), "1.1-rc-1", 4, false, 1, 1, "rc", 1);
48          buildVersion(VersionBuilder.create("1.1-rc-1-SNAPSHOT").build(), "1.1-rc-1-SNAPSHOT", 4, true, 1, 1, "rc", 1);
49          buildVersion(VersionBuilder.create("1.1-rc2").build(), "1.1-rc2", 4, false, 1, 1, "rc", 2);
50          buildVersion(VersionBuilder.create("1.1-rc2a").build(), "1.1-rc2a", 5, false, 1, 1, "rc", 2, "a");
51  
52      }
53  
54      @Test
55      public void testBuildVersionFromComponants() {
56  
57          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(0)).build(), "0", 1, false, 0);
58          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(1)).build(), "1", 1, false, 1);
59          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(12, 3)).build(), "12.3", 2, false, 12, 3);
60          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(12, 34, "alpha", 56)).build(), "12.34.alpha.56", 4, false, 12, 34, "alpha", 56);
61          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(1, 1, "beta", 1)).build(), "1.1.beta.1", 4, false, 1, 1, "beta", 1);
62          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(1, 1, "rc", 1)).build(), "1.1.rc.1", 4, false, 1, 1, "rc", 1);
63          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(1, 1, "rc", 1)).setSnapshot(true).build(), "1.1.rc.1-SNAPSHOT", 4, true, 1, 1, "rc", 1);
64          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(1, 1, "rc", 2)).build(), "1.1.rc.2", 4, false, 1, 1, "rc", 2);
65          buildVersion(VersionBuilder.create(Lists.<Comparable>newArrayList(1, 1, "rc", 2, "a")).build(), "1.1.rc.2.a", 5, false, 1, 1, "rc", 2, "a");
66  
67      }
68  
69  
70      protected void buildVersion(Version version,
71                                  String stringRepresentation,
72                                  int nbComponants,
73                                  boolean snapshot,
74                                  Comparable... componants) {
75  
76          Assert.assertEquals("Bad string represetion", stringRepresentation, version.getVersion());
77          Assert.assertEquals("Bad number of componants", nbComponants, version.getComponantCount());
78          Assert.assertEquals("Bad snapshot value", snapshot, version.isSnapshot());
79          for (int i = 0; i < componants.length; i++) {
80              Comparable componant = componants[i];
81              Assert.assertEquals("Bad componant value at index " + i, componant, version.getComponant(i).getValue());
82          }
83  
84      }
85  
86  }