1 /*
   2  * Copyright (c) 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 package jdk.jpackage.test;
  24 
  25 import java.io.File;
  26 import java.io.IOException;
  27 import java.nio.file.Files;
  28 import java.nio.file.Path;
  29 import java.util.Collections;
  30 import java.util.Enumeration;
  31 import java.util.List;
  32 import java.util.concurrent.atomic.AtomicInteger;
  33 
  34 
  35 public class HelloApp {
  36     static void addTo(JPackageCommand cmd) {
  37         cmd.addAction(new Runnable() {
  38             @Override
  39             public void run() {
  40                 String mainClass = "Hello";
  41                 Path jar = cmd.inputDir().resolve("hello.jar");
  42                 new JarBuilder()
  43                         .setOutputJar(jar.toFile())
  44                         .setMainClass(mainClass)
  45                         .addSourceFile(Test.TEST_SRC_ROOT.resolve(
  46                                 Path.of("apps", "image", mainClass + ".java")))
  47                         .create();
  48                 cmd.addArguments("--main-jar", jar.getFileName().toString());
  49                 cmd.addArguments("--main-class", mainClass);
  50             }
  51         });
  52         if (PackageType.WINDOWS.contains(cmd.packageType())) {
  53             cmd.addArguments("--win-console");
  54         }
  55     }
  56 
  57     public static void verifyOutputFile(Path outputFile, String ... args) {
  58         Test.assertFileExists(outputFile, true);
  59 
  60         List<String> output = null;
  61         try {
  62             output = Files.readAllLines(outputFile);
  63         } catch (IOException ex) {
  64             throw new RuntimeException(ex);
  65         }
  66 
  67         final int expectedNumberOfLines = 2 + args.length;
  68         Test.assertEquals(expectedNumberOfLines, output.size(), String.format(
  69                 "Check file [%s] contains %d text lines", outputFile,
  70                 expectedNumberOfLines));
  71 
  72         Test.assertEquals("jpackage test application", output.get(0),
  73                 String.format(
  74                         "Check contents of the first text line in [%s] file",
  75                         outputFile));
  76 
  77         Test.assertEquals(String.format("args.length: %d", args.length),
  78                 output.get(1), String.format(
  79                 "Check contents of the second text line in [%s] file",
  80                 outputFile));
  81 
  82         Enumeration<String> argsEnum = Collections.enumeration(List.of(args));
  83         AtomicInteger counter = new AtomicInteger(2);
  84         output.stream().skip(2).sequential().forEach(line -> Test.assertEquals(
  85                 argsEnum.nextElement(), line, String.format(
  86                 "Check contents of %d text line in [%s] file",
  87                 counter.incrementAndGet(), outputFile)));
  88     }
  89 
  90     public static void executeAndVerifyOutput(Path helloAppLauncher,
  91             String... defaultLauncherArgs) {
  92         File outputFile = Test.workDir().resolve(OUTPUT_FILENAME).toFile();
  93         new Executor()
  94                 .setDirectory(outputFile.getParentFile().toPath())
  95                 .setExecutable(helloAppLauncher.toString())
  96                 .execute()
  97                 .assertExitCodeIsZero();
  98 
  99         verifyOutputFile(outputFile.toPath(), defaultLauncherArgs);
 100     }
 101 
 102     public final static String OUTPUT_FILENAME = "appOutput.txt";
 103 }