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