test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java
author herrick
Fri, 06 Sep 2019 17:31:56 -0400
branchJDK-8200758-branch
changeset 58036 f7f10023f7c0
child 58113 885b0543f6e4
permissions -rw-r--r--
8229840 : Add jtreg test for --linux-app-category option 8229841 : Add jtreg test for --linux-app-release option Submitted-by: asemenyuk Reviewed-by: herrick, almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58036
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     1
/*
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     4
 *
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     8
 *
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    13
 * accompanied this code).
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    14
 *
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    18
 *
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    21
 * questions.
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    22
 */
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    23
package jdk.jpackage.test;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    24
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    25
import java.io.File;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    26
import java.nio.file.Files;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    27
import java.nio.file.Path;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    28
import java.util.ArrayList;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    29
import java.util.Arrays;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    30
import java.util.Collections;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    31
import java.util.List;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    32
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    33
public final class Executor extends CommandArguments<Executor> {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    34
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    35
    public Executor() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    36
        saveOutputType = SaveOutputType.NONE;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    37
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    38
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    39
    public Executor setExecutable(String v) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    40
        executable = v;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    41
        return this;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    42
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    43
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    44
    public Executor setDirectory(Path v) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    45
        directory = v;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    46
        return this;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    47
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    48
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    49
    public Executor setExecutable(JavaTool v) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    50
        return setExecutable(v.getPath().getAbsolutePath());
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    51
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    52
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    53
    public Executor saveOutput() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    54
        saveOutputType = SaveOutputType.FULL;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    55
        return this;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    56
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    57
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    58
    public Executor saveFirstLineOfOutput() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    59
        saveOutputType = SaveOutputType.FIRST_LINE;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    60
        return this;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    61
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    62
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    63
    public class Result {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    64
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    65
        Result(int exitCode) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    66
            this.exitCode = exitCode;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    67
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    68
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    69
        public String getFirstLineOfOutput() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    70
            return output.get(0).trim();
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    71
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    72
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    73
        public List<String> getOutput() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    74
            return output;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    75
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    76
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    77
        public String getPrintableCommandLine() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    78
            return Executor.this.getPrintableCommandLine();
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    79
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    80
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    81
        public Result assertExitCodeIs(int expectedExitCode) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    82
            Test.assertEquals(expectedExitCode, exitCode, String.format(
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    83
                    "Check command %s exited with %d code",
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    84
                    getPrintableCommandLine(), expectedExitCode));
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    85
            return this;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    86
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    87
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    88
        public Result assertExitCodeIsZero() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    89
            return assertExitCodeIs(0);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    90
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    91
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    92
        int exitCode;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    93
        List<String> output;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    94
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    95
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    96
    public Result execute() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    97
        try {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    98
            return executeImpl();
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    99
        } catch (RuntimeException e) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   100
            throw e;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   101
        } catch (Exception e) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   102
            throw new RuntimeException(e);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   103
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   104
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   105
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   106
    private Result executeImpl() throws Exception {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   107
        List<String> command = new ArrayList<>();
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   108
        command.add(executable);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   109
        command.addAll(args);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   110
        Path outputFile = null;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   111
        ProcessBuilder builder = new ProcessBuilder(command);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   112
        StringBuilder sb = new StringBuilder(getPrintableCommandLine());
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   113
        if (saveOutputType != SaveOutputType.NONE) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   114
            outputFile = Test.createTempFile(".out");
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   115
            builder.redirectErrorStream(true);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   116
            builder.redirectOutput(outputFile.toFile());
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   117
            sb.append(String.format("; redirect output to [%s]", outputFile));
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   118
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   119
        if (directory != null) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   120
            builder.directory(directory.toFile());
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   121
            sb.append(String.format("; in directory [%s]", directory));
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   122
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   123
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   124
        try {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   125
            Test.trace("Execute " + sb.toString() + "...");
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   126
            Process process = builder.start();
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   127
            Result reply = new Result(process.waitFor());
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   128
            Test.trace("Done. Exit code: " + reply.exitCode);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   129
            if (saveOutputType == SaveOutputType.FIRST_LINE) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   130
                reply.output = Arrays.asList(
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   131
                        Files.readAllLines(outputFile).stream().findFirst().get());
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   132
            } else if (saveOutputType == SaveOutputType.FULL) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   133
                reply.output = Collections.unmodifiableList(Files.readAllLines(
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   134
                        outputFile));
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   135
            }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   136
            return reply;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   137
        } finally {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   138
            if (outputFile != null) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   139
                Files.deleteIfExists(outputFile);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   140
            }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   141
        }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   142
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   143
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   144
    public String getPrintableCommandLine() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   145
        return "[" + executable + "]; args(" + args.size() + ")=" + Arrays.toString(
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   146
                args.toArray());
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   147
    }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   148
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   149
    private String executable;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   150
    private SaveOutputType saveOutputType;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   151
    private Path directory;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   152
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   153
    private static enum SaveOutputType {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   154
        NONE, FULL, FIRST_LINE
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   155
    };
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
   156
}