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