|
1 /* |
|
2 * Copyright (c) 2018, 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 package jdk.security.logging; |
|
25 |
|
26 import java.nio.file.Paths; |
|
27 import java.util.ArrayList; |
|
28 import java.util.Arrays; |
|
29 import java.util.List; |
|
30 |
|
31 import jdk.test.lib.process.OutputAnalyzer; |
|
32 import jdk.test.lib.process.ProcessTools; |
|
33 |
|
34 public final class LogJvm { |
|
35 private final static String LOGGING_ENABLED= "LOGGING_ENABLED"; |
|
36 private final static String LOGGING_DISABLED= "LOGGING_DISABLED"; |
|
37 |
|
38 private final static boolean debug = false; |
|
39 |
|
40 private final List<String> expectedLogMessages = new ArrayList<>(); |
|
41 private final Class<?> clazz; |
|
42 private final boolean loggingEnabled; |
|
43 |
|
44 public LogJvm(Class<?> clazz, String[] args) { |
|
45 this.clazz = clazz; |
|
46 this.loggingEnabled = Arrays.asList(args).contains(LOGGING_ENABLED); |
|
47 ensureLogging(args); |
|
48 } |
|
49 |
|
50 private void ensureLogging(String[] args) { |
|
51 for(String s : args) { |
|
52 if (s.equals(LOGGING_ENABLED) || s.equals(LOGGING_DISABLED)) { |
|
53 return; |
|
54 } |
|
55 } |
|
56 throw new RuntimeException(LogJvm.class.getName() + |
|
57 " requires command line parameter " + LOGGING_ENABLED + |
|
58 " or " + LOGGING_DISABLED); |
|
59 } |
|
60 |
|
61 public void addExpected(String logMsg) { |
|
62 expectedLogMessages.add(logMsg); |
|
63 } |
|
64 |
|
65 public void testExpected() throws Exception { |
|
66 OutputAnalyzer out = launchJVM(); |
|
67 if (debug) { |
|
68 System.out.println("STDOUT DEBUG:\n " + out.getStdout()); |
|
69 System.out.println("\nSTDERR DEBUG:\n " + out.getStderr()); |
|
70 } |
|
71 if (loggingEnabled) { |
|
72 testLoggingEnabled(out); |
|
73 } else { |
|
74 testLoggingDisabled(out); |
|
75 } |
|
76 } |
|
77 |
|
78 public OutputAnalyzer launchJVM() throws Exception { |
|
79 List<String> args = new ArrayList<>(); |
|
80 if (loggingEnabled) { |
|
81 args.add("-Djava.util.logging.config.file=" + |
|
82 Paths.get(System.getProperty("test.src", "."), "logging.properties")); |
|
83 } |
|
84 args.add("--add-exports"); |
|
85 args.add("java.base/jdk.internal.event=ALL-UNNAMED"); |
|
86 args.add(clazz.getName()); |
|
87 System.out.println(args); |
|
88 OutputAnalyzer out = ProcessTools.executeTestJava(args.toArray(new String[0])); |
|
89 out.shouldHaveExitValue(0); |
|
90 return out; |
|
91 } |
|
92 |
|
93 private void testLoggingDisabled(OutputAnalyzer out) { |
|
94 for (String expected : expectedLogMessages) { |
|
95 out.shouldNotContain(expected); |
|
96 } |
|
97 } |
|
98 |
|
99 private void testLoggingEnabled(OutputAnalyzer out) { |
|
100 for (String expected : expectedLogMessages) { |
|
101 out.shouldContain(expected); |
|
102 } |
|
103 } |
|
104 } |