1 /*
   2  * Copyright (c) 2018, 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 /**
  26  * @test
  27  * @requires vm.cds
  28  * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/appcds
  29  * @modules jdk.compiler
  30  *          jdk.jartool/sun.tools.jar
  31  *          jdk.jlink
  32  * @run main ExportModule
  33  * @summary Tests involve exporting a module from the module path to a jar in the -cp.
  34  */
  35 
  36 import java.io.File;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 
  41 import jdk.test.lib.compiler.CompilerUtils;
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 import jdk.testlibrary.ProcessTools;
  44 import jdk.testlibrary.Asserts;
  45 
  46 public class ExportModule {
  47 
  48     private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
  49 
  50     private static final String TEST_SRC = System.getProperty("test.src");
  51 
  52     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  53     private static final Path MODS_DIR = Paths.get("mods");
  54 
  55     // the module name of the test module
  56     private static final String TEST_MODULE1 = "com.greetings";
  57     private static final String TEST_MODULE2 = "org.astro";
  58 
  59     // unnamed module package name
  60     private static final String PKG_NAME = "com.nomodule";
  61 
  62     // the module main class
  63     private static final String MAIN_CLASS = "com.greetings.Main";
  64     private static final String APP_CLASS = "org.astro.World";
  65 
  66     // unnamed module main class
  67     private static final String UNNAMED_MAIN = "com.nomodule.Main";
  68 
  69     private static Path moduleDir = null;
  70     private static Path moduleDir2 = null;
  71     private static Path appJar = null;
  72     private static Path appJar2 = null;
  73 
  74     public static void buildTestModule() throws Exception {
  75 
  76         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  77         JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE2),
  78                                  MODS_DIR.resolve(TEST_MODULE2),
  79                                  null);
  80 
  81         // javac -d mods/$TESTMODULE --module-path MOD_DIR src/$TESTMODULE/**
  82         JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE1),
  83                                  MODS_DIR.resolve(TEST_MODULE1),
  84                                  MODS_DIR.toString());
  85 
  86         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
  87         Path jar = moduleDir.resolve(TEST_MODULE2 + ".jar");
  88         String classes = MODS_DIR.resolve(TEST_MODULE2).toString();
  89         JarBuilder.createModularJar(jar.toString(), classes, null);
  90 
  91         moduleDir2 = Files.createTempDirectory(USER_DIR, "mlib2");
  92         appJar = moduleDir2.resolve(TEST_MODULE1 + ".jar");
  93         classes = MODS_DIR.resolve(TEST_MODULE1).toString();
  94         JarBuilder.createModularJar(appJar.toString(), classes, MAIN_CLASS);
  95 
  96         // build a non-modular jar containing the main class which
  97         // requires the org.astro package
  98         boolean compiled
  99             = CompilerUtils.compile(SRC_DIR.resolve(PKG_NAME),
 100                                     MODS_DIR.resolve(PKG_NAME),
 101                                     "--module-path", MODS_DIR.toString(),
 102                                     "--add-modules", TEST_MODULE2,
 103                                     "--add-exports", "org.astro/org.astro=ALL-UNNAMED");
 104         Asserts.assertTrue(compiled, "test package did not compile");
 105 
 106         appJar2 = moduleDir2.resolve(PKG_NAME + ".jar");
 107         classes = MODS_DIR.resolve(PKG_NAME).toString();
 108         JarBuilder.createModularJar(appJar2.toString(), classes, null);
 109     }
 110 
 111     public static void main(String... args) throws Exception {
 112         // compile the modules and create the modular jar files
 113         buildTestModule();
 114         String appClasses[] = {MAIN_CLASS, APP_CLASS};
 115         // create an archive with the class in the org.astro module built in the
 116         // previous step and the main class from the modular jar in the -cp
 117         // note: the main class is in the modular jar in the -cp which requires
 118         // the module in the --module-path
 119         OutputAnalyzer output = TestCommon.createArchive(
 120                                         appJar.toString(), appClasses,
 121                                         "-Xlog:class+load=trace",
 122                                         "--module-path", moduleDir.toString(),
 123                                         "--add-modules", TEST_MODULE2, MAIN_CLASS);
 124         TestCommon.checkDump(output);
 125 
 126         // run it using the archive
 127         // both the main class and the class from the org.astro module should
 128         // be loaded from the archive
 129         TestCommon.run("-Xlog:class+load=trace",
 130                               "-cp", appJar.toString(),
 131                               "--module-path", moduleDir.toString(),
 132                               "--add-modules", TEST_MODULE2, MAIN_CLASS)
 133             .assertNormalExit(
 134                 "[class,load] org.astro.World source: shared objects file",
 135                 "[class,load] com.greetings.Main source: shared objects file");
 136 
 137         String appClasses2[] = {UNNAMED_MAIN, APP_CLASS};
 138         // create an archive with the main class from a non-modular jar in the
 139         // -cp and the class from the org.astro module
 140         // note: the org.astro package needs to be exported to "ALL-UNNAMED"
 141         // module since the jar in the -cp is a non-modular jar and thus it is
 142         // unnmaed.
 143         output = TestCommon.createArchive(
 144                                         appJar2.toString(), appClasses2,
 145                                         "-Xlog:class+load=trace",
 146                                         "--module-path", moduleDir.toString(),
 147                                         "--add-modules", TEST_MODULE2,
 148                                         "--add-exports", "org.astro/org.astro=ALL-UNNAMED",
 149                                         UNNAMED_MAIN);
 150         TestCommon.checkDump(output);
 151 
 152         // both the main class and the class from the org.astro module should
 153         // be loaded from the archive
 154         TestCommon.run("-Xlog:class+load=trace",
 155                        "-cp", appJar2.toString(),
 156                        "--module-path", moduleDir.toString(),
 157                        "--add-modules", TEST_MODULE2,
 158                        "--add-exports", "org.astro/org.astro=ALL-UNNAMED",
 159                        UNNAMED_MAIN)
 160             .assertNormalExit(
 161                 "[class,load] org.astro.World source: shared objects file",
 162                 "[class,load] com.nomodule.Main source: shared objects file");
 163     }
 164 }