1 /*
   2  * Copyright (c) 2015, 2020, 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  * @summary -Xlog:module should emit logging output
  27  * @library /test/lib
  28  * @modules java.base/jdk.internal.misc
  29  *          java.management
  30  * @run driver ModulesTest
  31  */
  32 
  33 import jdk.test.lib.process.OutputAnalyzer;
  34 import jdk.test.lib.process.ProcessTools;
  35 
  36 public class ModulesTest {
  37     // If modules in the system image have been archived in CDS, no Modules will
  38     // be dynamically created at runtime. Disable CDS so all of the expected messages
  39     // are printed.
  40     private static String XSHARE_OFF = "-Xshare:off";
  41 
  42     public static void main(String[] args) throws Exception {
  43         testModuleTrace("-Xlog:module=trace", XSHARE_OFF, "-version");
  44         testModuleLoad("-Xlog:module+load", XSHARE_OFF, "-version");
  45         testModuleUnload("-Xlog:module+unload", XSHARE_OFF, "-version");
  46 
  47         // same as -Xlog:module+load -Xlog:module+unload
  48         testModuleLoad("-verbose:module", XSHARE_OFF, "-version");
  49     }
  50 
  51     static void testModuleTrace(String... args) throws Exception {
  52         OutputAnalyzer output = run(args);
  53         output.shouldContain("define_javabase_module(): Definition of module:");
  54         output.shouldContain("define_javabase_module(): creation of package");
  55         output.shouldContain("define_module(): creation of module");
  56         output.shouldContain("define_module(): creation of package");
  57         output.shouldContain("set_bootloader_unnamed_module(): recording unnamed");
  58         output.shouldContain("add_module_exports(): package");
  59         output.shouldContain("add_reads_module(): Adding read from module");
  60         output.shouldContain("Setting package: class:");
  61         output.shouldHaveExitValue(0);
  62     }
  63 
  64     static void testModuleLoad(String... args) throws Exception {
  65         OutputAnalyzer output = run(args);
  66         output.shouldContain("java.base location:");
  67         output.shouldContain("java.management location:");
  68         output.shouldHaveExitValue(0);
  69     }
  70 
  71     static void testModuleUnload(String... args) throws Exception {
  72         OutputAnalyzer output = run(args);
  73         output.shouldHaveExitValue(0);
  74     }
  75 
  76     static OutputAnalyzer run(String... args) throws Exception {
  77         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
  78         return new OutputAnalyzer(pb.start());
  79     }
  80 }
  81