1/*
2 * Copyright (c) 2015, 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 TestOnOutOfMemoryError
26 * @summary Test using single and multiple -XX:OnOutOfMemoryError=<cmd>
27 * @modules java.base/jdk.internal.misc
28 * @library /test/lib
29 * @run main TestOnOutOfMemoryError
30 * @bug 8078470 8177522
31 */
32
33import jdk.test.lib.process.ProcessTools;
34import jdk.test.lib.process.OutputAnalyzer;
35
36public class TestOnOutOfMemoryError {
37
38    public static void main(String[] args) throws Exception {
39        if (args.length == 1) {
40            // This should guarantee to throw:
41            //  java.lang.OutOfMemoryError: Requested array size exceeds VM limit
42            Object[] oa = new Object[Integer.MAX_VALUE];
43            return;
44        }
45
46        // else this is the main test
47        String msg1 = "Test1 Succeeded";
48        String msg2 = "Test2 Succeeded";
49        ProcessBuilder pb_single = ProcessTools.createJavaProcessBuilder(
50           "-XX:OnOutOfMemoryError=echo " + msg1,
51           TestOnOutOfMemoryError.class.getName(),
52           "throwOOME");
53
54        ProcessBuilder pb_multiple = ProcessTools.createJavaProcessBuilder(
55           "-XX:OnOutOfMemoryError=echo " + msg1,
56           "-XX:OnOutOfMemoryError=echo " + msg2,
57           TestOnOutOfMemoryError.class.getName(),
58           "throwOOME");
59
60        OutputAnalyzer output_single = new OutputAnalyzer(pb_single.start());
61
62        OutputAnalyzer output_multiple = new OutputAnalyzer(pb_multiple.start());
63
64        /* Actual output should look like this:
65           #
66           # java.lang.OutOfMemoryError: Requested array size exceeds VM limit
67           # -XX:OnOutOfMemoryError="echo Test Succeeded"
68           #   Executing /bin/sh -c "echo Test Succeeded"...
69           Test Succeeded
70           Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
71           at OOME.main(OOME.java:3)
72
73           So we don't want to match on the "# Executing ..." line, and they
74           both get written to stdout.
75        */
76        output_single.shouldContain("Requested array size exceeds VM limit");
77        output_single.stdoutShouldMatch("^" + msg1); // match start of line only
78
79        output_multiple.shouldContain("Requested array size exceeds VM limit");
80        output_multiple.stdoutShouldMatch("^" + msg1); // match start of line only
81        output_multiple.stdoutShouldMatch("^" + msg2); // match start of line only
82
83        System.out.println("PASSED");
84    }
85}
86