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