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  * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/appcds
  28  * @modules jdk.compiler
  29  *          jdk.jartool/sun.tools.jar
  30  *          jdk.jlink
  31  * @build jdk.test.lib.compiler.CompilerUtils jdk.testlibrary.*
  32  * @run main AddReads
  33  * @summary sanity test the --add-reads option 
  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.process.OutputAnalyzer;
  42 import jdk.testlibrary.ProcessTools;
  43 import jdk.testlibrary.Asserts;
  44 
  45 public class AddReads {
  46 
  47     private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
  48 
  49     private static final String TEST_SRC = System.getProperty("test.src");
  50 
  51     private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
  52     private static final Path MODS_DIR = Paths.get("mods");
  53 
  54     // the module name of the test module
  55     private static final String MAIN_MODULE = "com.norequires";
  56     private static final String SUB_MODULE = "org.astro";
  57 
  58     // the module main class
  59     private static final String MAIN_CLASS = "com.norequires.Main";
  60     private static final String APP_CLASS = "org.astro.World";
  61 
  62     private static Path moduleDir = null;
  63     private static Path subJar = null;
  64     private static Path mainJar = null;
  65 
  66     public static void buildTestModule() throws Exception {
  67 
  68         // javac -d mods/$TESTMODULE src/$TESTMODULE/**
  69         JarBuilder.compileModule(SRC_DIR.resolve(SUB_MODULE),
  70                                        MODS_DIR.resolve(SUB_MODULE),
  71                                        null);
  72 
  73         Asserts.assertTrue(CompilerUtils
  74             .compile(SRC_DIR.resolve(MAIN_MODULE),
  75                      MODS_DIR.resolve(MAIN_MODULE),
  76                      "-cp", MODS_DIR.resolve(SUB_MODULE).toString(),
  77                      "--add-reads", "com.norequires=ALL-UNNAMED"));
  78 
  79         moduleDir = Files.createTempDirectory(USER_DIR, "mlib");
  80         subJar = moduleDir.resolve(SUB_MODULE + ".jar");
  81         String classes = MODS_DIR.resolve(SUB_MODULE).toString();
  82         JarBuilder.createModularJar(subJar.toString(), classes, null);
  83 
  84         mainJar = moduleDir.resolve(MAIN_MODULE + ".jar");
  85         classes = MODS_DIR.resolve(MAIN_MODULE).toString();
  86         JarBuilder.createModularJar(mainJar.toString(), classes, MAIN_CLASS);
  87     }
  88 
  89     public static void main(String... args) throws Exception {
  90         // compile the modules and create the modular jar files
  91         buildTestModule();
  92         String appClasses[] = {MAIN_CLASS, APP_CLASS};
  93         // create an archive with the classes in the modules built in the
  94         // previous step
  95         OutputAnalyzer output = TestCommon.createArchive(
  96                                         null, appClasses,
  97                                         "--module-path", moduleDir.toString(),
  98                                         "--add-modules", SUB_MODULE,
  99                                         "--add-reads", "com.norequires=org.astro",
 100                                         "-m", MAIN_MODULE);
 101         TestCommon.checkDump(output);
 102         String prefix[] = {"-cp", "\"\"", "-Xlog:class+load=trace",
 103                            "--add-modules", SUB_MODULE,
 104                            "--add-reads", "com.norequires=org.astro"};
 105 
 106         // run the com.norequires module with the archive with the same args 
 107         // used during dump time.
 108         // The classes should be loaded from the archive.
 109         TestCommon.runWithModules(prefix,
 110                                   null, // --upgrade-module-path
 111                                   moduleDir.toString(), // --module-path
 112                                   MAIN_MODULE) // -m
 113             .assertNormalExit(out -> {
 114                 out.shouldContain("[class,load] com.norequires.Main source: shared objects file")
 115                    .shouldContain("[class,load] org.astro.World source: shared objects file");
 116             }); 
 117 
 118         // create an archive with -cp pointing to the jar file containing the
 119         // org.astro module and --module-path pointing to the main module
 120         output = TestCommon.createArchive(
 121                                         subJar.toString(), appClasses,
 122                                         "--module-path", moduleDir.toString(),
 123                                         "--add-modules", SUB_MODULE,
 124                                         "--add-reads", "com.norequires=org.astro",
 125                                         "-m", MAIN_MODULE);
 126         TestCommon.checkDump(output);
 127         // run the com.norequires module with the archive with the sub-module
 128         // in the -cp and with -add-reads=com.norequires=ALL-UNNAMED
 129         // The main class should be loaded from the archive.
 130         // The org.astro.World should be loaded from the jar.
 131         String prefix2[] = {"-cp", subJar.toString(), "-Xlog:class+load=trace",
 132                            "--add-reads", "com.norequires=ALL-UNNAMED"};
 133         TestCommon.runWithModules(prefix2,
 134                                   null, // --upgrade-module-path
 135                                   moduleDir.toString(), // --module-path
 136                                   MAIN_MODULE) // -m
 137             .assertNormalExit(out -> {
 138                 out.shouldContain("[class,load] com.norequires.Main source: shared objects file")
 139                    .shouldMatch(".class.load. org.astro.World source:.*org.astro.jar");
 140             });
 141 
 142     }
 143 }