Path: blob/master/test/jdk/tools/jimage/JImageCliTest.java
41144 views
/*1* Copyright (c) 2016, 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.ByteArrayOutputStream;24import java.io.PrintStream;25import java.io.PrintWriter;26import java.lang.reflect.Method;27import java.nio.file.Files;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.util.Arrays;31import java.util.function.Consumer;32import java.util.regex.Pattern;3334import static java.nio.charset.StandardCharsets.UTF_8;35import static jdk.test.lib.Asserts.assertFalse;36import static jdk.test.lib.Asserts.assertTrue;37import static jdk.test.lib.Asserts.fail;3839/**40* This class is intended to be a base class for classes which are about to test41* command line interface of jimage.42*/43public class JImageCliTest {4445private String bootImagePath;4647public JImageCliTest() {48Path imagePath = Paths.get(System.getProperty("java.home"), "lib", "modules");49if (Files.exists(imagePath)) {50this.bootImagePath = imagePath.toAbsolutePath().toString();51}52}5354public void assertMatches(String regex, String output) {55Pattern pattern = Pattern.compile(regex);56if (!pattern.matcher(output).find()) {57fail(String.format("Expected to find a string match for [%s] in output \n[\n%s\n]\n.",58pattern, output));59}60}6162/**63* Returns a path to a tested image to share it across tests. By default it returns a path to the boot image64* of tested JDK. This behavior can be redefined by descendants.65*/66public String getImagePath() {67return bootImagePath;68}6970/**71* Runs jimage task with the supplied arguments.72*/73protected static JImageResult jimage(String... args) {74ByteArrayOutputStream baos = new ByteArrayOutputStream();75PrintStream ps = new PrintStream(baos);76System.out.println("jimage " + Arrays.asList(args));77int exitCode = jdk.tools.jimage.Main.run(args, new PrintWriter(ps));78return new JImageResult(exitCode, new String(baos.toByteArray(), UTF_8));79}8081protected static class JImageResult {82final int exitCode;83final String output;8485JImageResult(int exitCode, String output) {86this.exitCode = exitCode;87this.output = output;88}8990JImageResult assertSuccess() { assertTrue(exitCode == 0, output); return this; }91JImageResult assertFailure() { assertFalse(exitCode == 0, output); return this; }9293// a helper to ensure the error output doesn't exhibit implementation details94JImageResult assertShowsError() {95assertTrue(output.contains("Error"),96String.format("Output contains error, output=[%s]\n", output));97assertFalse(output.contains("Exception"),98String.format("Output doesn't contain a stacktrace, output=[%s]\n", output));99return this;100}101102JImageResult resultChecker(Consumer<JImageResult> r) { r.accept(this); return this; }103}104105protected final void runTests() throws Throwable {106if (getImagePath() != null) {107for (Method m : getClass().getDeclaredMethods()) {108if (m.getName().startsWith("test")) {109System.out.printf("Invoking %s\n", m.getName());110m.invoke(this);111}112}113} else {114System.out.println("This is not an image build. Skipping.");115}116}117118}119120121122