Path: blob/master/test/jdk/tools/jar/multiRelease/MRTestBase.java
41149 views
/*1* Copyright (c) 2017, 2019, 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 jdk.test.lib.JDKToolFinder;24import jdk.test.lib.Utils;25import jdk.test.lib.process.OutputAnalyzer;26import jdk.test.lib.process.ProcessTools;2728import java.io.*;29import java.nio.file.*;30import java.nio.file.attribute.BasicFileAttributes;31import java.util.ArrayList;32import java.util.Arrays;33import java.util.List;34import java.util.Map;35import java.util.jar.JarEntry;36import java.util.jar.JarFile;37import java.util.spi.ToolProvider;38import java.util.stream.Stream;39import java.util.zip.ZipFile;4041import static org.testng.Assert.assertEquals;4243public class MRTestBase {4445public static final int SUCCESS = 0;4647protected final String src = System.getProperty("test.src", ".");48protected final String usr = System.getProperty("user.dir", ".");4950private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")51.orElseThrow(()52-> new RuntimeException("jar tool not found")53);5455private static final ToolProvider JAVAC_TOOL = ToolProvider.findFirst("javac")56.orElseThrow(()57-> new RuntimeException("javac tool not found")58);5960protected void compile(String test) throws Throwable {61Path classes = Paths.get(usr, "classes", "base");62Files.createDirectories(classes);63Path source = Paths.get(src, "data", test, "base", "version");64javac(classes, source.resolve("Main.java"), source.resolve("Version.java"));6566classes = Paths.get(usr, "classes", "v9");67Files.createDirectories(classes);68source = Paths.get(src, "data", test, "v9", "version");69javac(classes, source.resolve("Version.java"));7071classes = Paths.get(usr, "classes", "v10");72Files.createDirectories(classes);73source = Paths.get(src, "data", test, "v10", "version");74javac(classes, source.resolve("Version.java"));75}7677protected void checkMultiRelease(String jarFile,78boolean expected) throws IOException {79try (JarFile jf = new JarFile(new File(jarFile), true,80ZipFile.OPEN_READ, JarFile.runtimeVersion())) {81assertEquals(jf.isMultiRelease(), expected);82}83}8485// compares the bytes found in the jar entries with the bytes found in the86// corresponding data files used to create the entries87protected void compare(String jarfile,88Map<String, String[]> names) throws IOException {89try (JarFile jf = new JarFile(jarfile)) {90for (String name : names.keySet()) {91Path path = Paths.get("classes", names.get(name));92byte[] b1 = Files.readAllBytes(path);93byte[] b2;94JarEntry je = jf.getJarEntry(name);95try (InputStream is = jf.getInputStream(je)) {96b2 = is.readAllBytes();97}98assertEquals(b1, b2);99}100}101}102103void javac(Path dest, Path... sourceFiles) throws Throwable {104105List<String> commands = new ArrayList<>();106String opts = System.getProperty("test.compiler.opts");107if (!opts.isEmpty()) {108commands.addAll(Arrays.asList(opts.split(" +")));109}110commands.addAll(Utils.getForwardVmOptions());111commands.add("-d");112commands.add(dest.toString());113Stream.of(sourceFiles)114.map(Object::toString)115.forEach(x -> commands.add(x));116117StringWriter sw = new StringWriter();118try (PrintWriter pw = new PrintWriter(sw)) {119int rc = JAVAC_TOOL.run(pw, pw, commands.toArray(new String[0]));120if(rc != 0) {121throw new RuntimeException(sw.toString());122}123}124125}126127OutputAnalyzer jarWithStdin(File stdinSource,128String... args) throws Throwable {129130String jar = JDKToolFinder.getJDKTool("jar");131List<String> commands = new ArrayList<>();132commands.add(jar);133commands.addAll(Utils.getForwardVmOptions());134Stream.of(args).forEach(x -> commands.add(x));135ProcessBuilder p = new ProcessBuilder(commands);136if (stdinSource != null)137p.redirectInput(stdinSource);138return ProcessTools.executeCommand(p);139}140141OutputAnalyzer jar(String... args) throws Throwable {142return jarWithStdin(null, args);143}144145OutputAnalyzer jarTool(String... args) {146List<String> commands = new ArrayList<>();147commands.addAll(Utils.getForwardVmOptions());148Stream.of(args).forEach(x -> commands.add(x));149return run(JAR_TOOL, args);150}151152OutputAnalyzer run(ToolProvider tp, String[] commands) {153int rc = 0;154StringWriter sw = new StringWriter();155StringWriter esw = new StringWriter();156157try (PrintWriter pw = new PrintWriter(sw);158PrintWriter epw = new PrintWriter(esw)) {159rc = tp.run(pw, epw, commands);160}161return new OutputAnalyzer(sw.toString(), esw.toString(), rc);162}163}164165