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 5103449
  27  * @summary REGRESSION: getResourceAsStream is broken in JDK1.5.0-rc
  28  * @library /test/lib
  29  * @run main/othervm TestDriver
  30  */
  31 
  32 import jdk.test.lib.JDKToolFinder;
  33 import jdk.test.lib.process.ProcessTools;
  34 
  35 import java.io.IOException;
  36 import java.nio.file.Files;
  37 import java.nio.file.Path;
  38 import java.nio.file.Paths;
  39 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  40 
  41 public class TestDriver {
  42     private static final String ARCHIVE_NAME = "test.jar";
  43     private static final String TEST_NAME = "Test";
  44     private static final String POLICY_FILE = "policy";
  45     public static void main(String[] args)
  46             throws Throwable {
  47 
  48         Path userDir = Paths.get(System.getProperty("user.dir"));
  49         String java = JDKToolFinder.getTestJDKTool("java");
  50         String basename = userDir.getFileName().toString();
  51         setup(userDir);
  52         ProcessBuilder[] tests = new ProcessBuilder[]{
  53                 new ProcessBuilder(
  54                         java, TEST_NAME, "./" + ARCHIVE_NAME
  55                 ),
  56                 new ProcessBuilder(
  57                         java, "-cp", ".",
  58                         "-Djava.security.policy=file:./policy",
  59                         "-Djava.security.manager",
  60                         TEST_NAME, "./" + ARCHIVE_NAME
  61                 ),
  62                 new ProcessBuilder(
  63                         java, "-cp", ".",
  64                         "-Djava.security.policy=file:./policy",
  65                         "-Djava.security.manager",
  66                         TEST_NAME, "./" + ARCHIVE_NAME
  67                 ),
  68                 new ProcessBuilder(
  69                         java, "-cp", "..",
  70                         "-Djava.security.policy=file:../policy",
  71                         "-Djava.security.manager",
  72                         TEST_NAME, "../" + ARCHIVE_NAME
  73                 ).directory(userDir.resolve("tmp").toFile()),
  74                 new ProcessBuilder(
  75                         java, "-cp", basename,
  76                         "-Djava.security.policy=file:" + basename + "/policy",
  77                         "-Djava.security.manager",
  78                         TEST_NAME, basename + "/" + ARCHIVE_NAME
  79                 ).directory(userDir.resolve("..").toFile())};
  80         for (ProcessBuilder test : tests) {
  81             runTest(test);
  82         }
  83     }
  84 
  85     private static void setup(Path userDir) throws IOException {
  86         Path src = Paths.get(System.getProperty("test.src"));
  87         Path testJar = src.resolve(ARCHIVE_NAME);
  88         Path policy = src.resolve(POLICY_FILE);
  89         Path testClass = Paths.get(System.getProperty("test.classes"),
  90                                    TEST_NAME + ".class");
  91         Files.copy(testJar, userDir.resolve(ARCHIVE_NAME), REPLACE_EXISTING);
  92         Files.copy(policy, userDir.resolve(POLICY_FILE), REPLACE_EXISTING);
  93         Files.copy(testClass, userDir.resolve(TEST_NAME + ".class"),
  94                    REPLACE_EXISTING);
  95         Files.createDirectories(userDir.resolve("tmp"));
  96     }
  97 
  98     private static void runTest(ProcessBuilder pb) throws Exception {
  99         System.out.println("Testing with command: [" + pb.command() + "]");
 100         ProcessTools.executeProcess(pb)
 101                     .outputTo(System.out)
 102                     .errorTo(System.err)
 103                     .shouldHaveExitValue(0);
 104     }
 105 }