1 /*
   2  * Copyright (c) 2017, 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 /*
  25  * @test
  26  * @bug 8175000
  27  * @summary test jexec
  28  * @modules java.compiler
  29  * @build TestHelper
  30  * @run main Jexec
  31  */
  32 
  33 import java.io.File;
  34 import java.io.IOException;
  35 
  36 public class Jexec extends TestHelper {
  37     private final File testJar;
  38     private final File jexecCmd;
  39     private final String message = "Hello, do you read me ?";
  40 
  41     Jexec() throws IOException {
  42         jexecCmd = new File(JAVA_LIB, "jexec");
  43         if (!jexecCmd.exists() || !jexecCmd.canExecute()) {
  44             throw new Error("jexec: does not exist or not executable");
  45         }
  46 
  47         testJar = new File("test.jar");
  48         StringBuilder tsrc = new StringBuilder();
  49         tsrc.append("public static void main(String... args) {\n");
  50         tsrc.append("   for (String x : args) {\n");
  51         tsrc.append("        System.out.println(x);\n");
  52         tsrc.append("   }\n");
  53         tsrc.append("}\n");
  54         createJar(testJar, tsrc.toString());
  55     }
  56 
  57     public static void main(String... args) throws Exception {
  58         // linux is the only supported platform, give the others a pass
  59         if (!isLinux) {
  60             System.err.println("Warning: unsupported platform test passes vacuously");
  61             return;
  62         }
  63         // ok to run the test now
  64         Jexec t = new Jexec();
  65         t.run(null);
  66     }
  67 
  68     @Test
  69     void jexec() throws Exception {
  70         TestResult tr = doExec(jexecCmd.getAbsolutePath(),
  71                 testJar.getAbsolutePath(), message);
  72         if (!tr.isOK()) {
  73             System.err.println(tr);
  74             throw new Exception("incorrect exit value");
  75         }
  76         if (!tr.contains(message)) {
  77             System.err.println(tr);
  78             throw new Exception("expected message \'" + message + "\' not found");
  79         }
  80     }
  81 }