test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JarBuilder.java
author herrick
Wed, 16 Oct 2019 10:32:08 -0400
branchJDK-8200758-branch
changeset 58648 3bf53ffa9ae7
parent 58416 f09bf58c1f17
child 58696 61c44899b4eb
permissions -rw-r--r--
8232279 : Improve test helpers #2 Submitted-by: asemenyuk Reviewed-by: aherrick, 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
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    24
package jdk.jpackage.test;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    25
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    26
import java.io.File;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    27
import java.nio.file.Files;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    28
import java.nio.file.Path;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    29
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    30
import java.util.ArrayList;
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
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
 * Tool to compile Java sources and pack in a jar file.
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    36
 */
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    37
public final class JarBuilder {
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 JarBuilder() {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    40
        sourceFiles = new ArrayList<>();
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    41
    }
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
    public JarBuilder setOutputJar(File v) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    44
        outputJar = v;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    45
        return this;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    46
    }
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
    public JarBuilder setMainClass(String v) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    49
        mainClass = v;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    50
        return this;
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 JarBuilder addSourceFile(Path v) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    54
        sourceFiles.add(v);
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
58416
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    58
    public JarBuilder setModuleVersion(String v) {
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    59
        moduleVersion = v;
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    60
        return this;
58036
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
58416
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    63
    public void create() {
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    64
        TKit.withTempDirectory("jar-workdir", workDir -> {
58648
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    65
            if (!sourceFiles.isEmpty()) {
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    66
                new Executor()
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    67
                        .setToolProvider(JavaTool.JAVAC)
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    68
                        .addArguments("-d", workDir.toString())
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    69
                        .addPathArguments(sourceFiles)
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    70
                        .execute().assertExitCodeIsZero();
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    71
            }
58036
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    72
            Path tmpJar = workDir.resolve("foo.jar");
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    73
            Executor jarExe = new Executor();
58648
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    74
            jarExe.setToolProvider(JavaTool.JAR).addArguments("-c", "-f", tmpJar.toString());
58416
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    75
            if (moduleVersion != null) {
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    76
                jarExe.addArguments(String.format("--module-version=%s",
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    77
                        moduleVersion));
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    78
            }
58036
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    79
            if (mainClass != null) {
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    80
                jarExe.addArguments("-e", mainClass);
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    81
            }
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    82
            jarExe.addArguments("-C", workDir.toString(), ".");
58648
3bf53ffa9ae7 8232279 : Improve test helpers #2
herrick
parents: 58416
diff changeset
    83
            jarExe.execute().assertExitCodeIsZero();
58036
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    84
            outputJar.getParentFile().mkdirs();
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    85
            Files.copy(tmpJar, outputJar.toPath(), REPLACE_EXISTING);
58416
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    86
        });
58036
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
    private List<Path> sourceFiles;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    89
    private File outputJar;
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    90
    private String mainClass;
58416
f09bf58c1f17 8231605: Improve test helpers
herrick
parents: 58036
diff changeset
    91
    private String moduleVersion;
58036
f7f10023f7c0 8229840 : Add jtreg test for --linux-app-category option
herrick
parents:
diff changeset
    92
}