1 /*
   2  * Copyright (c) 2014, 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 8060206 8067366
  27  * @summary Endorsed standards and override mechanism is removed
  28  */
  29 
  30 import java.io.*;
  31 import java.util.*;
  32 import java.util.concurrent.TimeUnit;
  33 import java.util.stream.Collectors;
  34 
  35 public class EndorsedDirs {
  36     private static String[] VALUES = new String[] {
  37             null,
  38             "",
  39             "\"\""
  40     };
  41     public static void main(String... args) throws Exception {
  42         String value = System.getProperty("java.endorsed.dirs");
  43         System.out.format("java.endorsed.dirs = '%s'%n", value);
  44         if (args.length > 0) {
  45             int index = Integer.valueOf(args[0]);
  46             String expectedValue = VALUES[index];
  47             if (!(expectedValue == value ||
  48                     (value != null && value.isEmpty()) ||
  49                     (expectedValue != null & expectedValue.equals(value)))) {
  50                 throw new RuntimeException("java.endorsed.dirs (" +
  51                         value + ") != " + expectedValue);
  52             }
  53             // launched by subprocess.
  54             return;
  55         }
  56 
  57         if (value != null) {
  58             throw new RuntimeException("java.endorsed.dirs not removed: " + value);
  59         }
  60 
  61         fatalError(0, "-Djava.endorsed.dirs=foo");
  62         start(0);
  63         start(1, "-Djava.endorsed.dirs=");
  64         start(2, "-Djava.endorsed.dirs=\"\"");
  65     }
  66 
  67     static ProcessBuilder newProcessBuilder(int testParam, String... args) throws Exception {
  68         List<String> commands = new ArrayList<>();
  69         String java = System.getProperty("java.home") + "/bin/java";
  70         commands.add(java);
  71         for (String s : args) {
  72             commands.add(s);
  73         }
  74         String cpath = System.getProperty("test.classes", ".");
  75         commands.add("-cp");
  76         commands.add(cpath);
  77         commands.add("EndorsedDirs");
  78         commands.add(String.valueOf(testParam));
  79 
  80         System.out.println("Testing " + commands.stream().collect(Collectors.joining(" ")));
  81         return new ProcessBuilder(commands);
  82     }
  83 
  84     static void start(int testParam, String... args) throws Exception {
  85         start(newProcessBuilder(testParam, args), false);
  86     }
  87 
  88     static void fatalError(int testParam, String... args) throws Exception {
  89         start(newProcessBuilder(testParam, args), true);
  90     }
  91 
  92     static void start(ProcessBuilder pb, boolean fatalError) throws Exception {
  93         final Process process = pb.start();
  94         BufferedReader errorStream = new BufferedReader(
  95                 new InputStreamReader(process.getErrorStream()));
  96         BufferedReader outStream = new BufferedReader(
  97                 new InputStreamReader(process.getInputStream()));
  98         String errorLine;
  99         StringBuilder errors = new StringBuilder();
 100         String outLines;
 101         while ((errorLine = errorStream.readLine()) != null) {
 102             errors.append(errorLine).append("\n");
 103         }
 104         while ((outLines = outStream.readLine()) != null) {
 105             System.out.println(outLines);
 106         }
 107         errorLine = errors.toString();
 108         System.err.println(errorLine);
 109         process.waitFor(1000, TimeUnit.MILLISECONDS);
 110         int exitStatus = process.exitValue();
 111         if (fatalError) {
 112             if (exitStatus == 0) {
 113                 throw new RuntimeException("Expected fatal error");
 114             }
 115             if (!errorLine.contains("Could not create the Java Virtual Machine")) {
 116                 throw new RuntimeException(errorLine);
 117             }
 118         } else if (exitStatus != 0) {
 119             throw new RuntimeException("Failed: " + errorLine);
 120         }
 121     }
 122 }