jdk/test/java/lang/ModuleTests/AddExportsTest.java
changeset 44545 83b611b88ac8
equal deleted inserted replaced
44480:2c33418a6d57 44545:83b611b88ac8
       
     1 /*
       
     2  * Copyright (c) 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  * @modules java.base/jdk.internal.misc
       
    27  *          java.desktop
       
    28  * @run main/othervm --add-exports=java.desktop/sun.awt=java.base AddExportsTest
       
    29  * @run main/othervm --add-exports=java.desktop/sun.awt=ALL-UNNAMED AddExportsTest
       
    30  * @summary Test Module isExported methods with exports changed by -AddExportsTest
       
    31  */
       
    32 
       
    33 import java.util.Optional;
       
    34 import java.util.stream.Stream;
       
    35 
       
    36 import jdk.internal.misc.VM;
       
    37 
       
    38 public class AddExportsTest {
       
    39     /*
       
    40      * jtreg sets -Dtest.modules system property to the internal APIs
       
    41      * specified at @modules tag.  The test will exclude --add-exports set
       
    42      * for @modules.
       
    43      */
       
    44     private static final String TEST_MODULES = System.getProperty("test.modules");
       
    45 
       
    46     public static void main(String[] args) {
       
    47 
       
    48         Optional<String> oaddExports = Stream.of(VM.getRuntimeArguments())
       
    49             .filter(arg -> arg.startsWith("--add-exports="))
       
    50             .filter(arg -> !arg.equals("--add-exports=" + TEST_MODULES + "=ALL-UNNAMED"))
       
    51             .map(arg -> arg.substring("--add-exports=".length(), arg.length()))
       
    52             .findFirst();
       
    53 
       
    54         assertTrue(oaddExports.isPresent());
       
    55 
       
    56         ModuleLayer bootLayer = ModuleLayer.boot();
       
    57 
       
    58         Module unnamedModule = AddExportsTest.class.getModule();
       
    59         assertFalse(unnamedModule.isNamed());
       
    60 
       
    61         for (String expr : oaddExports.get().split(",")) {
       
    62 
       
    63             String[] s = expr.split("=");
       
    64             assertTrue(s.length == 2);
       
    65 
       
    66             // $MODULE/$PACKAGE
       
    67             String[] moduleAndPackage = s[0].split("/");
       
    68             assertTrue(moduleAndPackage.length == 2);
       
    69 
       
    70             String mn = moduleAndPackage[0];
       
    71             String pn = moduleAndPackage[1];
       
    72 
       
    73             // source module
       
    74             Module source;
       
    75             Optional<Module> om = bootLayer.findModule(mn);
       
    76             assertTrue(om.isPresent(), mn + " not in boot layer");
       
    77             source = om.get();
       
    78 
       
    79             // package should not be exported unconditionally
       
    80             assertFalse(source.isExported(pn),
       
    81                         pn + " should not be exported unconditionally");
       
    82 
       
    83             // $TARGET
       
    84             String tn = s[1];
       
    85             if ("ALL-UNNAMED".equals(tn)) {
       
    86 
       
    87                 // package is exported to all unnamed modules
       
    88                 assertTrue(source.isExported(pn, unnamedModule),
       
    89                            pn + " should be exported to all unnamed modules");
       
    90 
       
    91             } else {
       
    92 
       
    93                 om = bootLayer.findModule(tn);
       
    94                 assertTrue(om.isPresent());
       
    95                 Module target = om.get();
       
    96 
       
    97                 // package should be exported to target module
       
    98                 assertTrue(source.isExported(pn, target),
       
    99                            pn + " should be exported to " + target);
       
   100 
       
   101                 // package should not be exported to unnamed modules
       
   102                 assertFalse(source.isExported(pn, unnamedModule),
       
   103                             pn + " should not be exported to unnamed modules");
       
   104 
       
   105             }
       
   106 
       
   107         }
       
   108     }
       
   109 
       
   110     static void assertTrue(boolean cond) {
       
   111         if (!cond) throw new RuntimeException();
       
   112     }
       
   113 
       
   114     static void assertTrue(boolean cond, String msg) {
       
   115         if (!cond) throw new RuntimeException(msg);
       
   116     }
       
   117 
       
   118     static void assertFalse(boolean cond) {
       
   119         if (cond) throw new RuntimeException();
       
   120     }
       
   121 
       
   122     static void assertFalse(boolean cond, String msg) {
       
   123         if (cond) throw new RuntimeException(msg);
       
   124     }
       
   125 
       
   126 }