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