1 /*
   2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.nio.file.Path;
  26 import jdk.jpackage.test.TKit;
  27 import jdk.jpackage.test.PackageTest;
  28 import jdk.jpackage.test.PackageType;
  29 import jdk.jpackage.test.Annotations.Test;
  30 import jdk.jpackage.test.Annotations.Parameters;
  31 import java.util.List;
  32 
  33 /**
  34  * Test --resource-dir option. The test should set --resource-dir to point to
  35  * a dir with an empty "main.wxs" file.  As a result, jpackage should try to
  36  * use the customized resource and fail.
  37  */
  38 
  39 /*
  40  * @test
  41  * @summary jpackage with --resource-dir
  42  * @library ../helpers
  43  * @build jdk.jpackage.test.*
  44  * @requires (os.family == "windows")
  45  * @modules jdk.incubator.jpackage/jdk.incubator.jpackage.internal
  46  * @compile WinResourceTest.java
  47  * @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
  48  *  --jpt-run=WinResourceTest
  49  */
  50 
  51 public class WinResourceTest {
  52 
  53     public WinResourceTest(String wixSource, String expectedLogMessage) {
  54          this.wixSource = wixSource;
  55          this.expectedLogMessage = expectedLogMessage;
  56     }
  57 
  58     @Parameters
  59     public static List<Object[]> data() {
  60         return List.of(new Object[][]{
  61             {"main.wxs", "Using custom package resource [Main WiX project file]"},
  62             {"overrides.wxi", "Using custom package resource [Overrides WiX project file]"},
  63         });
  64     }
  65 
  66     @Test
  67     public void test() throws IOException {
  68         new PackageTest()
  69         .forTypes(PackageType.WINDOWS)
  70         .configureHelloApp()
  71         .addInitializer(cmd -> {
  72             Path resourceDir = TKit.createTempDirectory("resources");
  73 
  74             // 1. Set fake run time to save time by skipping jlink step of jpackage.
  75             // 2. Instruct test to save jpackage output.
  76             cmd.setFakeRuntime().saveConsoleOutput(true);
  77 
  78             cmd.addArguments("--resource-dir", resourceDir);
  79             // Create invalid WiX source file in a resource dir.
  80             TKit.createTextFile(resourceDir.resolve(wixSource), List.of(
  81                     "any string that is an invalid WiX source file"));
  82         })
  83         .addBundleVerifier((cmd, result) -> {
  84             // Assert jpackage picked custom main.wxs and failed as expected by
  85             // examining its output
  86             TKit.assertTextStream(expectedLogMessage)
  87                     .predicate(String::startsWith)
  88                     .apply(result.getOutput().stream());
  89             TKit.assertTextStream("error CNDL0104 : Not a valid source file")
  90                     .apply(result.getOutput().stream());
  91         })
  92         .setExpectedExitCode(1)
  93         .run();
  94     }
  95 
  96     final String wixSource;
  97     final String expectedLogMessage;
  98 }