Path: blob/master/test/jdk/java/lang/System/LoggerFinder/modules/Base.java
41154 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.nio.file.Files;25import java.nio.file.Path;26import java.nio.file.Paths;27import java.nio.file.StandardCopyOption;28import java.util.ArrayList;29import java.util.List;30import java.util.stream.Stream;3132import jdk.test.lib.JDKToolFinder;33import jdk.test.lib.compiler.CompilerUtils;3435import static jdk.test.lib.process.ProcessTools.executeCommand;3637/*38* Base class for tests.39* The tests focuse on that LoggerFinder works well in jigsaw environment,40* i.e. make sure correct Logger can be retrieved,41* also verify that basic functionality of retrieved Logger's works well.42*43* Note: As the test will take long time, to avoid timeout,44* split it as several tests, this class is the base class for tests.45*/46public class Base {47protected static final String JAVA_HOME = System.getProperty("java.home");48protected static final Path JDK_IMAGE = Paths.get(JAVA_HOME);49protected static final Path JMODS = Paths.get(JAVA_HOME, "jmods");5051protected static final String TEST_SRC = System.getProperty("test.src");5253// logger client to get logger from java.base module, it should get a lazy logger54// which wraps the underlying real logger implementation55protected static final Path SRC_PATCHED_USAGE =56Paths.get(TEST_SRC, "patched_usage", "java.base");57protected static final Path DEST_PATCHED_USAGE = Paths.get("patched_usage", "java.base");58protected static final Path SRC_PATCHED_CLIENT = Paths.get(TEST_SRC, "patched_client");59protected static final Path DEST_PATCHED_CLIENT = Paths.get("patched_client");6061// logger client to get logger from bootclasspath/a, it should get a lazy logger62// which wraps the underlying real logger implementation63protected static final Path SRC_BOOT_USAGE = Paths.get(TEST_SRC, "boot_usage");64protected static final Path DEST_BOOT_USAGE = Paths.get("boot_usage");65protected static final Path SRC_BOOT_CLIENT = Paths.get(TEST_SRC, "boot_client");66protected static final Path DEST_BOOT_CLIENT = Paths.get("boot_client");6768// logger provider in named module m.l.a69protected static final Path SRC_NAMED_LOGGER = Paths.get(TEST_SRC, "named_logger");70protected static final Path DEST_NAMED_LOGGER = Paths.get("mods_named_logger");7172// logger provider in unnamed module73protected static final Path SRC_UNNAMED_LOGGER = Paths.get(TEST_SRC, "unnamed_logger");74protected static final Path DEST_UNNAMED_LOGGER = Paths.get("cp_unnamed_logger");75protected static final Path SRC_UNNAMED_LOGGER_SERVICE_FILE =76SRC_UNNAMED_LOGGER.resolve("META-INF/services/java.lang.System$LoggerFinder");77protected static final Path DEST_UNNAMED_LOGGER_SERVICE_DIR =78DEST_UNNAMED_LOGGER.resolve("META-INF/services");79protected static final Path DEST_UNNAMED_LOGGER_SERVICE_FILE =80DEST_UNNAMED_LOGGER.resolve("META-INF/services/java.lang.System$LoggerFinder");8182// logger client in named module m.t.a83protected static final Path SRC_NAMED_CLIENT = Paths.get(TEST_SRC, "named_client");84protected static final Path DEST_NAMED_CLIENT = Paths.get("mods_named_client");8586// logger client in unnamed module87protected static final Path SRC_UNNAMED_CLIENT = Paths.get(TEST_SRC, "unnamed_client");88protected static final Path DEST_UNNAMED_CLIENT = Paths.get("cp_unnamed_client");8990// customized image with only module java.base91protected static final Path IMAGE = Paths.get("image");92// customized image with java.base and logger provider module m.l.a93protected static final Path IMAGE_LOGGER = Paths.get("image_logger");94// customized image with module java.base and logger client module m.t.a95protected static final Path IMAGE_CLIENT = Paths.get("image_client");96// customized image with module java.base, logger provider module m.l.a97// and logger client module m.t.a98protected static final Path IMAGE_CLIENT_LOGGER = Paths.get("image_all");99100// lazy logger class which wraps the underlying real logger implementation101protected static final String LAZY_LOGGER =102"jdk.internal.logger.LazyLoggers$JdkLazyLogger";103// JUL logger class which wraps java.util.logging.Logger104protected static final String JUL_LOGGER =105"sun.util.logging.internal.LoggingProviderImpl$JULWrapper";106// default simple logger class when no logger provider can be found107protected static final String SIMPLE_LOGGER =108"jdk.internal.logger.SimpleConsoleLogger";109// logger class in named module m.l.a110protected static final String LOGGER_A = "pkg.a.l.LoggerA";111// logger class in unnamed module m.l.b112protected static final String LOGGER_B = "pkg.b.l.LoggerB";113114// logger client in named module115protected static final String CLIENT_A = "m.t.a/pkg.a.t.TestA";116// logger client in unnamed module117protected static final String CLIENT_B = "pkg.b.t.TestB";118// logger client which gets logger through boot class BootUsage119protected static final String BOOT_CLIENT = "BootClient";120// logger client which gets logger through patched class121// java.base/java.lang.PatchedUsage122protected static final String PATCHED_CLIENT = "PatchedClient";123124protected void setupAllClient() throws Throwable {125// compiles logger client which will get logger through patched126// class java.base/java.lang.PatchedUsage127compile(SRC_BOOT_USAGE, DEST_BOOT_USAGE);128compile(SRC_BOOT_CLIENT, DEST_BOOT_CLIENT,129"--class-path", DEST_BOOT_USAGE.toString());130131// compiles logger client which will get logger through boot132// class BootUsage133compile(SRC_PATCHED_USAGE, DEST_PATCHED_USAGE,134"--patch-module", "java.base=" + SRC_PATCHED_USAGE.toString());135compile(SRC_PATCHED_CLIENT, DEST_PATCHED_CLIENT,136"--patch-module", "java.base=" + DEST_PATCHED_USAGE.toString());137138// compiles logger client in unnamed module139compile(SRC_UNNAMED_CLIENT, DEST_UNNAMED_CLIENT,140"--source-path", SRC_UNNAMED_CLIENT.toString());141142// compiles logger client in named module m.t.a143compile(SRC_NAMED_CLIENT, DEST_NAMED_CLIENT,144"--module-source-path", SRC_NAMED_CLIENT.toString());145}146147protected void setupNamedLogger() throws Throwable {148// compiles logger provider in named module m.l.a149compile(SRC_NAMED_LOGGER, DEST_NAMED_LOGGER,150"--module-source-path", SRC_NAMED_LOGGER.toString());151}152153protected void setupUnnamedLogger() throws Throwable {154// compiles logger provider in unnamed module155compile(SRC_UNNAMED_LOGGER, DEST_UNNAMED_LOGGER,156"--source-path", SRC_UNNAMED_LOGGER.toString());157Files.createDirectories(DEST_UNNAMED_LOGGER_SERVICE_DIR);158Files.copy(SRC_UNNAMED_LOGGER_SERVICE_FILE, DEST_UNNAMED_LOGGER_SERVICE_FILE,159StandardCopyOption.REPLACE_EXISTING);160}161162protected boolean checkJMODS() throws Throwable {163// if $JAVA_HOME/jmods does not exist, skip below steps164// as there is no way to build customized images by jlink165if (Files.notExists(JMODS)) {166System.err.println("Skip tests which require image");167return false;168}169return true;170}171172protected void setupJavaBaseImage() throws Throwable {173if (!checkJMODS()) {174return;175}176177// build image with just java.base module178String mpath = JMODS.toString();179execTool("jlink",180"--module-path", mpath,181"--add-modules", "java.base",182"--output", IMAGE.toString());183}184185protected void setupLoggerImage() throws Throwable {186if (!checkJMODS()) {187return;188}189190// build image with java.base + m.l.a modules191String mpath = DEST_NAMED_LOGGER.toString() + File.pathSeparator + JMODS.toString();192execTool("jlink",193"--module-path", mpath,194"--add-modules", "m.l.a",195"--output", IMAGE_LOGGER.toString());196}197198protected void setupClientImage() throws Throwable {199if (!checkJMODS()) {200return;201}202203// build image with java.base + m.t.a modules204String mpath = DEST_NAMED_CLIENT.toString() + File.pathSeparator + JMODS.toString();205execTool("jlink",206"--module-path", mpath,207"--add-modules", "m.t.a",208"--output", IMAGE_CLIENT.toString());209}210211protected void setupFullImage() throws Throwable {212if (!checkJMODS()) {213return;214}215216// build image with java.base + m.l.a + m.t.a modules217String mpath = DEST_NAMED_LOGGER.toString() + File.pathSeparator218+ DEST_NAMED_CLIENT.toString() + File.pathSeparator + JMODS.toString();219execTool("jlink",220"--module-path", mpath,221"--add-modules", "m.l.a,m.t.a",222"--output", IMAGE_CLIENT_LOGGER.toString());223224}225226protected static void assertTrue(boolean b) {227if (!b) {228throw new RuntimeException("expected true, but get false.");229}230}231232/*233* run test with supplied java image which could be jdk image or customized image234*/235protected void runTest(Path image, String... opts) throws Throwable {236String[] options = Stream.concat(Stream.of(getJava(image)), Stream.of(opts))237.toArray(String[]::new);238239ProcessBuilder pb = new ProcessBuilder(options);240int exitValue = executeCommand(pb).outputTo(System.out)241.errorTo(System.err)242.getExitValue();243assertTrue(exitValue == 0);244}245246private void compile(Path src, Path dest, String... params) throws Throwable {247assertTrue(CompilerUtils.compile(src, dest, params));248}249250private String getJava(Path image) {251boolean isWindows = System.getProperty("os.name").startsWith("Windows");252Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java");253if (Files.notExists(java))254throw new RuntimeException(java + " not found");255return java.toAbsolutePath().toString();256}257258private void execTool(String tool, String... args) throws Throwable {259String path = JDKToolFinder.getJDKTool(tool);260List<String> commands = new ArrayList<>();261commands.add(path);262Stream.of(args).forEach(commands::add);263ProcessBuilder pb = new ProcessBuilder(commands);264265int exitValue = executeCommand(pb).outputTo(System.out)266.errorTo(System.out)267.shouldNotContain("no module is recorded in hash")268.getExitValue();269assertTrue(exitValue == 0);270}271}272273274