Path: blob/master/test/jdk/tools/jpackage/windows/WinScriptTest.java
41152 views
/*1* Copyright (c) 2018, 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 java.io.IOException;24import java.nio.file.Path;25import java.util.List;26import java.util.ArrayList;27import jdk.jpackage.internal.IOUtils;28import jdk.jpackage.test.TKit;29import jdk.jpackage.test.PackageTest;30import jdk.jpackage.test.PackageType;31import jdk.jpackage.test.Annotations.Test;32import jdk.jpackage.test.Annotations.Parameter;33import jdk.jpackage.test.Annotations.Parameters;34import jdk.jpackage.test.JPackageCommand;3536/*37* @test usage of scripts from resource dir38* @summary jpackage with39* @library ../helpers40* @build jdk.jpackage.test.*41* @requires (os.family == "windows")42* @modules jdk.jpackage/jdk.jpackage.internal43* @compile WinScriptTest.java44* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main45* --jpt-run=WinScriptTest46*/4748public class WinScriptTest {4950public WinScriptTest(PackageType type) {51this.packageType = type;5253test = new PackageTest()54.forTypes(type)55.configureHelloApp()56.addInitializer(cmd -> {57cmd.setFakeRuntime().saveConsoleOutput(true);58});59}6061@Parameters62public static List<Object[]> data() {63return List.of(new Object[][]{64{PackageType.WIN_MSI},65{PackageType.WIN_EXE}66});67}6869@Test70@Parameter("0")71@Parameter("10")72public void test(int wsfExitCode) throws IOException {73final ScriptData appImageScriptData;74if (wsfExitCode != 0 && packageType == PackageType.WIN_EXE) {75appImageScriptData = new ScriptData(PackageType.WIN_MSI, 0);76} else {77appImageScriptData = new ScriptData(PackageType.WIN_MSI, wsfExitCode);78}7980final ScriptData msiScriptData = new ScriptData(PackageType.WIN_EXE, wsfExitCode);8182test.setExpectedExitCode(wsfExitCode == 0 ? 0 : 1);8384final Path tempDir = TKit.createTempDirectory("resources");8586test.addInitializer(cmd -> {87cmd.addArguments("--resource-dir", tempDir);8889appImageScriptData.createScript(cmd);90msiScriptData.createScript(cmd);91});9293switch (packageType) {94case WIN_MSI:95test.addBundleVerifier((cmd, result) -> {96appImageScriptData.assertJPackageOutput(result.getOutput());97});98break;99100case WIN_EXE:101test.addBundleVerifier((cmd, result) -> {102appImageScriptData.assertJPackageOutput(result.getOutput());103msiScriptData.assertJPackageOutput(result.getOutput());104});105break;106}107108test.run();109}110111private static class ScriptData {112ScriptData(PackageType scriptType, int wsfExitCode) {113if (scriptType == PackageType.WIN_MSI) {114echoText = "post app image wsf";115envVarName = "JpAppImageDir";116scriptSuffixName = "post-image";117} else {118echoText = "post msi wsf";119envVarName = "JpMsiFile";120scriptSuffixName = "post-msi";121}122this.wsfExitCode = wsfExitCode;123}124125void assertJPackageOutput(List<String> output) {126TKit.assertTextStream(String.format(" jp: %s", echoText))127.predicate(String::equals)128.apply(output.stream());129130String cwdPattern = String.format(" jp: CWD(%s)=", envVarName);131TKit.assertTextStream(cwdPattern)132.predicate(String::startsWith)133.apply(output.stream());134String cwd = output.stream().filter(line -> line.startsWith(135cwdPattern)).findFirst().get().substring(cwdPattern.length());136137String envVarPattern = String.format(" jp: %s=", envVarName);138TKit.assertTextStream(envVarPattern)139.predicate(String::startsWith)140.apply(output.stream());141String envVar = output.stream().filter(line -> line.startsWith(142envVarPattern)).findFirst().get().substring(envVarPattern.length());143144TKit.assertTrue(envVar.startsWith(cwd), String.format(145"Check value of %s environment variable [%s] starts with the current directory [%s] set for %s script",146envVarName, envVar, cwd, echoText));147}148149void createScript(JPackageCommand cmd) throws IOException {150IOUtils.createXml(Path.of(cmd.getArgumentValue("--resource-dir"),151String.format("%s-%s.wsf", cmd.name(), scriptSuffixName)), xml -> {152xml.writeStartElement("job");153xml.writeAttribute("id", "main");154xml.writeStartElement("script");155xml.writeAttribute("language", "JScript");156xml.writeCData(String.join("\n", List.of(157"var shell = new ActiveXObject('WScript.Shell')",158"WScript.Echo('jp: " + envVarName + "=' + shell.ExpandEnvironmentStrings('%" + envVarName + "%'))",159"WScript.Echo('jp: CWD(" + envVarName + ")=' + shell.CurrentDirectory)",160String.format("WScript.Echo('jp: %s')", echoText),161String.format("WScript.Quit(%d)", wsfExitCode)162)));163xml.writeEndElement();164xml.writeEndElement();165});166}167168private final int wsfExitCode;169private final String scriptSuffixName;170private final String echoText;171private final String envVarName;172}173174private final PackageType packageType;175private PackageTest test;176}177178179