1/*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/**
25 * @test
26 * @run testng VersionTest
27 * @summary Basic tests for java.lang.module.ModuleDescriptor.Version.
28 */
29
30import java.lang.module.ModuleDescriptor.Version;
31
32import org.testng.annotations.Test;
33import org.testng.annotations.DataProvider;
34import static org.testng.Assert.*;
35
36@Test
37public class VersionTest {
38
39    // valid version strings
40    @DataProvider(name = "validVersions")
41    public Object[][] validVersions() {
42        return new Object[][]{
43
44            { "1.0",            null },
45            { "1.0.0",          null },
46            { "1.0.0.0",        null },
47
48            { "99",             null },
49            { "99.99",          null },
50            { "99.99.99",       null },
51
52            { "1-SNAPSHOT",     null },
53            { "1.0-SNAPSHOT",   null },
54            { "1.0.0-SNAPSHOT", null },
55
56            { "9-ea",           null },
57            { "9-ea+110",       null },
58            { "9.3.2.1+42-8839942", null}
59
60        };
61    }
62
63    // invalid version strings
64    @DataProvider(name = "invalidVersions")
65    public Object[][] invalidVersions() {
66        return new Object[][]{
67
68            { null,            null },
69            { "",              null },
70            { "A1",            null },  // does not start with number
71            { "1.0-",          null },  // empty branch
72
73        };
74    }
75
76    // Test parsing valid version strings
77    @Test(dataProvider = "validVersions")
78    public void testParseValidVersions(String vs, String ignore) {
79        Version v = Version.parse(vs);
80        assertEquals(v.toString(), vs);
81    }
82
83    // Test parsing an invalid version strings
84    @Test(dataProvider = "invalidVersions",
85          expectedExceptions = IllegalArgumentException.class )
86    public void testParseInvalidVersions(String vs, String ignore) {
87        Version.parse(vs);
88    }
89
90    // Test equals and hashCode
91    @Test(dataProvider = "validVersions")
92    public void testEqualsAndHashCode(String vs, String ignore) {
93
94        Version v1 = Version.parse(vs);
95        Version v2 = Version.parse(vs);
96        assertEquals(v1, v2);
97        assertEquals(v2, v1);
98        assertEquals(v1.hashCode(), v2.hashCode());
99
100        Version v3 = Version.parse("1.0-rhubarb");
101        assertNotEquals(v1, v3);
102        assertNotEquals(v2, v3);
103        assertNotEquals(v3, v1);
104        assertNotEquals(v3, v2);
105
106    }
107
108    // ordered version strings
109    @DataProvider(name = "orderedVersions")
110    public Object[][] orderedVersions() {
111        return new Object[][]{
112
113            { "1.0",     "2.0" },
114            { "1.0-SNAPSHOT", "1.0" },
115            { "1.0-SNAPSHOT2", "1.0" },
116            { "1.2.3-ea", "1.2.3" },
117            { "1.2.3-ea+104", "1.2.3-ea+105" },
118            { "1.2.3-ea+104-4084552", "1.2.3-ea+104-4084552+8849330" },
119            { "1+104", "1+105" },
120            { "1.0-alpha1", "1.0-alpha2" }
121
122        };
123    }
124
125    /**
126     * Test compareTo with ordered versions.
127     */
128    @Test(dataProvider = "orderedVersions")
129    public void testCompareOrderedVersions(String vs1, String vs2) {
130
131        Version v1 = Version.parse(vs1);
132        assertTrue(v1.compareTo(v1) == 0);
133
134        Version v2 = Version.parse(vs2);
135        assertTrue(v2.compareTo(v2) == 0);
136
137        // v1 < v2
138        assertTrue(v1.compareTo(v2) < 0);
139        assertTrue(v2.compareTo(v1) > 0);
140
141    }
142
143    // equal version strings
144    @DataProvider(name = "equalVersions")
145    public Object[][] equalVersions() {
146        return new Object[][]{
147
148            { "1",             "1.0.0" },
149            { "1.0",           "1.0.0" },
150            { "1.0-beta",      "1.0.0-beta" },
151
152        };
153    }
154
155    /**
156     * Test compareTo with equal versions.
157     */
158    @Test(dataProvider = "equalVersions")
159    public void testCompareEqualsVersions(String vs1, String vs2) {
160
161        Version v1 = Version.parse(vs1);
162        assertTrue(v1.compareTo(v1) == 0);
163
164        Version v2 = Version.parse(vs2);
165        assertTrue(v2.compareTo(v2) == 0);
166
167        assertTrue(v1.compareTo(v2) == 0);
168        assertTrue(v2.compareTo(v1) == 0);
169        assertEquals(v1, v2);
170        assertEquals(v2, v1);
171
172    }
173
174}
175