1 /*
   2  * Copyright (c) 2017, 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 import java.io.File;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.nio.file.StandardCopyOption;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 import java.util.stream.Stream;
  32 
  33 import jdk.test.lib.JDKToolFinder;
  34 import jdk.test.lib.compiler.CompilerUtils;
  35 
  36 import static jdk.test.lib.process.ProcessTools.executeCommand;
  37 
  38 /*
  39  * Base class for tests.
  40  * The tests focuse on that LoggerFinder works well in jigsaw environment,
  41  * i.e. make sure correct Logger can be retrieved,
  42  * also verify that basic functionality of retrieved Logger's works well.
  43  *
  44  * Note: As the test will take long time, to avoid timeout,
  45  * split it as several tests, this class is the base class for tests.
  46  */
  47 public class Base {
  48     protected static final String JAVA_HOME = System.getProperty("java.home");
  49     protected static final Path JDK_IMAGE = Paths.get(JAVA_HOME);
  50     protected static final Path JMODS = Paths.get(JAVA_HOME, "jmods");
  51 
  52     protected static final String TEST_SRC = System.getProperty("test.src");
  53 
  54     // logger client to get logger from java.base module, it should get a lazy logger
  55     // which wraps the underlying real logger implementation
  56     protected static final Path SRC_PATCHED_USAGE =
  57             Paths.get(TEST_SRC, "patched_usage", "java.base");
  58     protected static final Path DEST_PATCHED_USAGE = Paths.get("patched_usage", "java.base");
  59     protected static final Path SRC_PATCHED_CLIENT = Paths.get(TEST_SRC, "patched_client");
  60     protected static final Path DEST_PATCHED_CLIENT = Paths.get("patched_client");
  61 
  62     // logger client to get logger from bootclasspath/a, it should get a lazy logger
  63     // which wraps the underlying real logger implementation
  64     protected static final Path SRC_BOOT_USAGE = Paths.get(TEST_SRC, "boot_usage");
  65     protected static final Path DEST_BOOT_USAGE = Paths.get("boot_usage");
  66     protected static final Path SRC_BOOT_CLIENT = Paths.get(TEST_SRC, "boot_client");
  67     protected static final Path DEST_BOOT_CLIENT = Paths.get("boot_client");
  68 
  69     // logger provider in named module m.l.a
  70     protected static final Path SRC_NAMED_LOGGER = Paths.get(TEST_SRC, "named_logger");
  71     protected static final Path DEST_NAMED_LOGGER = Paths.get("mods_named_logger");
  72 
  73     // logger provider in unnamed module
  74     protected static final Path SRC_UNNAMED_LOGGER = Paths.get(TEST_SRC, "unnamed_logger");
  75     protected static final Path DEST_UNNAMED_LOGGER = Paths.get("cp_unnamed_logger");
  76     protected static final Path SRC_UNNAMED_LOGGER_SERVICE_FILE =
  77             SRC_UNNAMED_LOGGER.resolve("META-INF/services/java.lang.System$LoggerFinder");
  78     protected static final Path DEST_UNNAMED_LOGGER_SERVICE_DIR =
  79             DEST_UNNAMED_LOGGER.resolve("META-INF/services");
  80     protected static final Path DEST_UNNAMED_LOGGER_SERVICE_FILE =
  81             DEST_UNNAMED_LOGGER.resolve("META-INF/services/java.lang.System$LoggerFinder");
  82 
  83     // logger client in named module m.t.a
  84     protected static final Path SRC_NAMED_CLIENT = Paths.get(TEST_SRC, "named_client");
  85     protected static final Path DEST_NAMED_CLIENT = Paths.get("mods_named_client");
  86 
  87     // logger client in unnamed module
  88     protected static final Path SRC_UNNAMED_CLIENT = Paths.get(TEST_SRC, "unnamed_client");
  89     protected static final Path DEST_UNNAMED_CLIENT = Paths.get("cp_unnamed_client");
  90 
  91     // customized image with only module java.base
  92     protected static final Path IMAGE = Paths.get("image");
  93     // customized image with java.base and logger provider module m.l.a
  94     protected static final Path IMAGE_LOGGER = Paths.get("image_logger");
  95     // customized image with module java.base and logger client module m.t.a
  96     protected static final Path IMAGE_CLIENT = Paths.get("image_client");
  97     // customized image with module java.base, logger provider module m.l.a
  98     // and logger client module m.t.a
  99     protected static final Path IMAGE_CLIENT_LOGGER = Paths.get("image_all");
 100 
 101     // lazy logger class which wraps the underlying real logger implementation
 102     protected static final String LAZY_LOGGER =
 103             "jdk.internal.logger.LazyLoggers$JdkLazyLogger";
 104     // JUL logger class which wraps java.util.logging.Logger
 105     protected static final String JUL_LOGGER =
 106             "sun.util.logging.internal.LoggingProviderImpl$JULWrapper";
 107     // default simple logger class when no logger provider can be found
 108     protected static final String SIMPLE_LOGGER =
 109             "jdk.internal.logger.SimpleConsoleLogger";
 110     // logger class in named module m.l.a
 111     protected static final String LOGGER_A = "pkg.a.l.LoggerA";
 112     // logger class in unnamed module m.l.b
 113     protected static final String LOGGER_B = "pkg.b.l.LoggerB";
 114 
 115     // logger client in named module
 116     protected static final String CLIENT_A = "m.t.a/pkg.a.t.TestA";
 117     // logger client in unnamed module
 118     protected static final String CLIENT_B = "pkg.b.t.TestB";
 119     // logger client which gets logger through boot class BootUsage
 120     protected static final String BOOT_CLIENT = "BootClient";
 121     // logger client which gets logger through patched class
 122     // java.base/java.lang.PatchedUsage
 123     protected static final String PATCHED_CLIENT = "PatchedClient";
 124 
 125     protected void setupAllClient() throws Throwable {
 126         // compiles logger client which will get logger through patched
 127         // class java.base/java.lang.PatchedUsage
 128         compile(SRC_BOOT_USAGE, DEST_BOOT_USAGE);
 129         compile(SRC_BOOT_CLIENT, DEST_BOOT_CLIENT,
 130                 "--class-path", DEST_BOOT_USAGE.toString());
 131 
 132         // compiles logger client which will get logger through boot
 133         // class BootUsage
 134         compile(SRC_PATCHED_USAGE, DEST_PATCHED_USAGE,
 135                 "--patch-module", "java.base=" + SRC_PATCHED_USAGE.toString());
 136         compile(SRC_PATCHED_CLIENT, DEST_PATCHED_CLIENT,
 137                 "--patch-module", "java.base=" + DEST_PATCHED_USAGE.toString());
 138 
 139         // compiles logger client in unnamed module
 140         compile(SRC_UNNAMED_CLIENT, DEST_UNNAMED_CLIENT,
 141                 "--source-path", SRC_UNNAMED_CLIENT.toString());
 142 
 143         // compiles logger client in named module m.t.a
 144         compile(SRC_NAMED_CLIENT, DEST_NAMED_CLIENT,
 145                 "--module-source-path", SRC_NAMED_CLIENT.toString());
 146     }
 147 
 148     protected void setupNamedLogger() throws Throwable {
 149         // compiles logger provider in named module m.l.a
 150         compile(SRC_NAMED_LOGGER, DEST_NAMED_LOGGER,
 151                 "--module-source-path", SRC_NAMED_LOGGER.toString());
 152     }
 153 
 154     protected void setupUnnamedLogger() throws Throwable {
 155         // compiles logger provider in unnamed module
 156         compile(SRC_UNNAMED_LOGGER, DEST_UNNAMED_LOGGER,
 157                 "--source-path", SRC_UNNAMED_LOGGER.toString());
 158         Files.createDirectories(DEST_UNNAMED_LOGGER_SERVICE_DIR);
 159         Files.copy(SRC_UNNAMED_LOGGER_SERVICE_FILE, DEST_UNNAMED_LOGGER_SERVICE_FILE,
 160                    StandardCopyOption.REPLACE_EXISTING);
 161     }
 162 
 163     protected boolean checkJMODS() throws Throwable {
 164         // if $JAVA_HOME/jmods does not exist, skip below steps
 165         // as there is no way to build customized images by jlink
 166         if (Files.notExists(JMODS)) {
 167             System.err.println("Skip tests which require image");
 168             return false;
 169         }
 170         return true;
 171     }
 172 
 173     protected void setupJavaBaseImage() throws Throwable {
 174         if (!checkJMODS()) {
 175             return;
 176         }
 177 
 178         // build image with just java.base module
 179         String mpath = JMODS.toString();
 180         execTool("jlink",
 181                 "--module-path", mpath,
 182                 "--add-modules", "java.base",
 183                 "--output", IMAGE.toString());
 184     }
 185 
 186     protected void setupLoggerImage() throws Throwable {
 187         if (!checkJMODS()) {
 188             return;
 189         }
 190 
 191         // build image with java.base + m.l.a modules
 192         String mpath = DEST_NAMED_LOGGER.toString() + File.pathSeparator + JMODS.toString();
 193         execTool("jlink",
 194                 "--module-path", mpath,
 195                 "--add-modules", "m.l.a",
 196                 "--output", IMAGE_LOGGER.toString());
 197     }
 198 
 199     protected void setupClientImage() throws Throwable {
 200         if (!checkJMODS()) {
 201             return;
 202         }
 203 
 204         // build image with java.base + m.t.a modules
 205         String mpath = DEST_NAMED_CLIENT.toString() + File.pathSeparator + JMODS.toString();
 206         execTool("jlink",
 207                 "--module-path", mpath,
 208                 "--add-modules", "m.t.a",
 209                 "--output", IMAGE_CLIENT.toString());
 210     }
 211 
 212     protected void setupFullImage() throws Throwable {
 213         if (!checkJMODS()) {
 214             return;
 215         }
 216 
 217         // build image with java.base + m.l.a + m.t.a modules
 218         String mpath = DEST_NAMED_LOGGER.toString() + File.pathSeparator
 219                 + DEST_NAMED_CLIENT.toString() + File.pathSeparator + JMODS.toString();
 220         execTool("jlink",
 221                 "--module-path", mpath,
 222                 "--add-modules", "m.l.a,m.t.a",
 223                 "--output", IMAGE_CLIENT_LOGGER.toString());
 224 
 225     }
 226 
 227     protected static void assertTrue(boolean b) {
 228         if (!b) {
 229             throw new RuntimeException("expected true, but get false.");
 230         }
 231     }
 232 
 233     /*
 234      * run test with supplied java image which could be jdk image or customized image
 235      */
 236     protected void runTest(Path image, String... opts) throws Throwable {
 237         String[] options = Stream.concat(Stream.of(getJava(image)), Stream.of(opts))
 238                                  .toArray(String[]::new);
 239 
 240         ProcessBuilder pb = new ProcessBuilder(options);
 241         int exitValue = executeCommand(pb).outputTo(System.out)
 242                                           .errorTo(System.err)
 243                                           .getExitValue();
 244         assertTrue(exitValue == 0);
 245     }
 246 
 247     private void compile(Path src, Path dest, String... params) throws Throwable {
 248         assertTrue(CompilerUtils.compile(src, dest, params));
 249     }
 250 
 251     private String getJava(Path image) {
 252         boolean isWindows = System.getProperty("os.name").startsWith("Windows");
 253         Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java");
 254         if (Files.notExists(java))
 255             throw new RuntimeException(java + " not found");
 256         return java.toAbsolutePath().toString();
 257     }
 258 
 259     private void execTool(String tool, String... args) throws Throwable {
 260         String path = JDKToolFinder.getJDKTool(tool);
 261         List<String> commands = new ArrayList<>();
 262         commands.add(path);
 263         Stream.of(args).forEach(commands::add);
 264         ProcessBuilder pb = new ProcessBuilder(commands);
 265 
 266         int exitValue = executeCommand(pb).outputTo(System.out)
 267                                           .errorTo(System.out)
 268                                           .shouldNotContain("no module is recorded in hash")
 269                                           .getExitValue();
 270         assertTrue(exitValue == 0);
 271     }
 272 }