jdk/test/java/lang/ModuleTests/AddExportsTest.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     1
/*
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     4
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     8
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    13
 * accompanied this code).
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    14
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    18
 *
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    21
 * questions.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    22
 */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    23
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    24
/**
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    25
 * @test
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    26
 * @modules java.base/jdk.internal.misc
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    27
 *          java.desktop
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    28
 * @run main/othervm --add-exports=java.desktop/sun.awt=java.base AddExportsTest
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    29
 * @run main/othervm --add-exports=java.desktop/sun.awt=ALL-UNNAMED AddExportsTest
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    30
 * @summary Test Module isExported methods with exports changed by -AddExportsTest
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    31
 */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    32
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    33
import java.util.Optional;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    34
import java.util.stream.Stream;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    35
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    36
import jdk.internal.misc.VM;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    37
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    38
public class AddExportsTest {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    39
    /*
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    40
     * jtreg sets -Dtest.modules system property to the internal APIs
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    41
     * specified at @modules tag.  The test will exclude --add-exports set
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    42
     * for @modules.
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    43
     */
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    44
    private static final String TEST_MODULES = System.getProperty("test.modules");
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    45
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    46
    public static void main(String[] args) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    47
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    48
        Optional<String> oaddExports = Stream.of(VM.getRuntimeArguments())
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    49
            .filter(arg -> arg.startsWith("--add-exports="))
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    50
            .filter(arg -> !arg.equals("--add-exports=" + TEST_MODULES + "=ALL-UNNAMED"))
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    51
            .map(arg -> arg.substring("--add-exports=".length(), arg.length()))
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    52
            .findFirst();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    53
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    54
        assertTrue(oaddExports.isPresent());
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    55
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    56
        ModuleLayer bootLayer = ModuleLayer.boot();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    57
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    58
        Module unnamedModule = AddExportsTest.class.getModule();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    59
        assertFalse(unnamedModule.isNamed());
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    60
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    61
        for (String expr : oaddExports.get().split(",")) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    62
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    63
            String[] s = expr.split("=");
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    64
            assertTrue(s.length == 2);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    65
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    66
            // $MODULE/$PACKAGE
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    67
            String[] moduleAndPackage = s[0].split("/");
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    68
            assertTrue(moduleAndPackage.length == 2);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    69
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    70
            String mn = moduleAndPackage[0];
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    71
            String pn = moduleAndPackage[1];
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    72
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    73
            // source module
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    74
            Module source;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    75
            Optional<Module> om = bootLayer.findModule(mn);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    76
            assertTrue(om.isPresent(), mn + " not in boot layer");
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    77
            source = om.get();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    78
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    79
            // package should not be exported unconditionally
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    80
            assertFalse(source.isExported(pn),
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    81
                        pn + " should not be exported unconditionally");
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    82
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    83
            // $TARGET
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    84
            String tn = s[1];
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    85
            if ("ALL-UNNAMED".equals(tn)) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    86
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    87
                // package is exported to all unnamed modules
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    88
                assertTrue(source.isExported(pn, unnamedModule),
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    89
                           pn + " should be exported to all unnamed modules");
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    90
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    91
            } else {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    92
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    93
                om = bootLayer.findModule(tn);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    94
                assertTrue(om.isPresent());
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    95
                Module target = om.get();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    96
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    97
                // package should be exported to target module
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    98
                assertTrue(source.isExported(pn, target),
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
    99
                           pn + " should be exported to " + target);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   100
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   101
                // package should not be exported to unnamed modules
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   102
                assertFalse(source.isExported(pn, unnamedModule),
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   103
                            pn + " should not be exported to unnamed modules");
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   104
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   105
            }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   106
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   107
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   108
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   109
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   110
    static void assertTrue(boolean cond) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   111
        if (!cond) throw new RuntimeException();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   112
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   113
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   114
    static void assertTrue(boolean cond, String msg) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   115
        if (!cond) throw new RuntimeException(msg);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   116
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   117
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   118
    static void assertFalse(boolean cond) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   119
        if (cond) throw new RuntimeException();
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   120
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   121
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   122
    static void assertFalse(boolean cond, String msg) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   123
        if (cond) throw new RuntimeException(msg);
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   124
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   125
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents:
diff changeset
   126
}