jdk/test/java/lang/reflect/Module/AddExportsTest.java
author shurailine
Wed, 15 Jun 2016 10:25:25 -0700
changeset 38966 308f5cc7d5f6
parent 36511 9d0388c6b336
child 40261 86a49ba76f52
permissions -rw-r--r--
8158855: Fix remaining module dependences in java/lang Reviewed-by: alanb, mchung, amlu
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) 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
38966
308f5cc7d5f6 8158855: Fix remaining module dependences in java/lang
shurailine
parents: 36511
diff changeset
    26
 * @modules java.desktop
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @run main/othervm -XaddExports:java.desktop/sun.awt=java.base AddExportsTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @run main/othervm -XaddExports:java.desktop/sun.awt=ALL-UNNAMED AddExportsTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 * @summary Test Module isExported methods with exports changed by -AddExportsTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.reflect.Layer;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.lang.reflect.Module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.Optional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
public class AddExportsTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
    public static void main(String[] args) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
        String addExports = System.getProperty("jdk.launcher.addexports.0");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
        assertTrue(addExports != null, "Expected to be run with -XaddExports");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        Layer bootLayer = Layer.boot();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
        Module unnamedModule = AddExportsTest.class.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
        assertFalse(unnamedModule.isNamed());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        for (String expr : addExports.split(",")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
            String[] s = expr.split("=");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
            assertTrue(s.length == 2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
            // $MODULE/$PACKAGE
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
            String[] moduleAndPackage = s[0].split("/");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
            assertTrue(moduleAndPackage.length == 2);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            String mn = moduleAndPackage[0];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            String pn = moduleAndPackage[1];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
            // source module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
            Module source;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
            Optional<Module> om = bootLayer.findModule(mn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
            assertTrue(om.isPresent(), mn + " not in boot layer");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
            source = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            // package should not be exported unconditionally
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            assertFalse(source.isExported(pn),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                        pn + " should not be exported unconditionally");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
            // $TARGET
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            String tn = s[1];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            if ("ALL-UNNAMED".equals(tn)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                // package is exported to all unnamed modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                assertTrue(source.isExported(pn, unnamedModule),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                           pn + " should be exported to all unnamed modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
                om = bootLayer.findModule(tn);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                assertTrue(om.isPresent());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                Module target = om.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                // package should be exported to target module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                assertTrue(source.isExported(pn, target),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                           pn + " should be exported to " + target);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                // package should not be exported to unnamed modules
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                assertFalse(source.isExported(pn, unnamedModule),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                            pn + " should not be exported to unnamed modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
    static void assertTrue(boolean cond) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
        if (!cond) throw new RuntimeException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
    static void assertTrue(boolean cond, String msg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
        if (!cond) throw new RuntimeException(msg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
    static void assertFalse(boolean cond) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
        if (cond) throw new RuntimeException();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
    static void assertFalse(boolean cond, String msg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
        if (cond) throw new RuntimeException(msg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
}