test/jdk/tools/jimage/JImageCliTest.java
author prr
Wed, 29 Nov 2017 09:47:02 -0800
changeset 47982 44371ff1ee9a
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42167
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     1
/*
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     4
 *
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     7
 * published by the Free Software Foundation.
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     8
 *
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    13
 * accompanied this code).
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    14
 *
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    18
 *
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    21
 * questions.
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    22
 */
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    23
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    25
import java.io.PrintStream;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    26
import java.io.PrintWriter;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    27
import java.lang.reflect.Method;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    28
import java.nio.file.Files;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    29
import java.nio.file.Path;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    30
import java.nio.file.Paths;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    31
import java.util.Arrays;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    32
import java.util.function.Consumer;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    33
import java.util.regex.Pattern;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    34
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    35
import static java.nio.charset.StandardCharsets.UTF_8;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    36
import static jdk.test.lib.Asserts.assertFalse;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    37
import static jdk.test.lib.Asserts.assertTrue;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    38
import static jdk.test.lib.Asserts.fail;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    39
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    40
/**
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    41
 * This class is intended to be a base class for classes which are about to test
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    42
 * command line interface of jimage.
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    43
 */
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    44
public class JImageCliTest {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    45
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    46
    private String bootImagePath;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    47
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    48
    public JImageCliTest() {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    49
        Path imagePath = Paths.get(System.getProperty("java.home"), "lib", "modules");
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    50
        if (Files.exists(imagePath)) {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    51
            this.bootImagePath = imagePath.toAbsolutePath().toString();
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    52
        }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    53
    }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    54
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    55
    public void assertMatches(String regex, String output) {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    56
        Pattern pattern = Pattern.compile(regex);
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    57
        if (!pattern.matcher(output).find()) {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    58
            fail(String.format("Expected to find a string match for [%s] in output \n[\n%s\n]\n.",
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    59
                    pattern, output));
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    60
        }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    61
    }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    62
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    63
    /**
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    64
     * Returns a path to a tested image to share it across tests. By default it returns a path to the boot image
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    65
     * of tested JDK. This behavior can be redefined by descendants.
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    66
     */
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    67
    public String getImagePath() {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    68
        return bootImagePath;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    69
    }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    70
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    71
    /**
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    72
     * Runs jimage task with the supplied arguments.
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    73
     */
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    74
    protected static JImageResult jimage(String... args) {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    75
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    76
        PrintStream ps = new PrintStream(baos);
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    77
        System.out.println("jimage " + Arrays.asList(args));
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    78
        int exitCode = jdk.tools.jimage.Main.run(args, new PrintWriter(ps));
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    79
        return new JImageResult(exitCode, new String(baos.toByteArray(), UTF_8));
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    80
    }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    81
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    82
    protected static class JImageResult {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    83
        final int exitCode;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    84
        final String output;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    85
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    86
        JImageResult(int exitCode, String output) {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    87
            this.exitCode = exitCode;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    88
            this.output = output;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    89
        }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    90
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    91
        JImageResult assertSuccess() { assertTrue(exitCode == 0, output); return this; }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    92
        JImageResult assertFailure() { assertFalse(exitCode == 0, output); return this; }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    93
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    94
        // a helper to ensure the error output doesn't exhibit implementation details
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    95
        JImageResult assertShowsError() {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    96
            assertTrue(output.contains("Error"),
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    97
                    String.format("Output contains error, output=[%s]\n", output));
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    98
            assertFalse(output.contains("Exception"),
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
    99
                    String.format("Output doesn't contain a stacktrace, output=[%s]\n", output));
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   100
            return this;
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   101
        }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   102
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   103
        JImageResult resultChecker(Consumer<JImageResult> r) { r.accept(this); return this; }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   104
    }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   105
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   106
    protected final void runTests() throws Throwable {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   107
        if (getImagePath() != null) {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   108
            for (Method m : getClass().getDeclaredMethods()) {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   109
                if (m.getName().startsWith("test")) {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   110
                    System.out.printf("Invoking %s\n", m.getName());
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   111
                    m.invoke(this);
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   112
                }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   113
            }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   114
        } else {
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   115
            System.out.println("This is not an image build. Skipping.");
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   116
        }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   117
    }
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   118
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   119
}
312b1ca6b1df 8167240: Write new tests to cover functionality of existing 'jimage' options
dkononenko
parents:
diff changeset
   120