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 /*
  25  * @test TestHeapDumpPath
  26  * @summary Test verifies that -XX:HeapDumpPath= supports directory as a parameter.
  27  * @library /test/lib
  28  * @run driver TestHeapDumpPath
  29  */
  30 
  31 import jdk.test.lib.Asserts;
  32 import jdk.test.lib.hprof.HprofParser;
  33 import jdk.test.lib.process.ProcessTools;
  34 import jdk.test.lib.process.OutputAnalyzer;
  35 
  36 import java.io.File;
  37 
  38 public class TestHeapDumpPath {
  39 
  40     public static void main(String[] args) throws Exception {
  41         if (args.length == 1) {
  42             try {
  43                 Object[] oa = new Object[Integer.MAX_VALUE];
  44                 throw new Error("OOME not triggered");
  45             } catch (OutOfMemoryError err) {
  46                 return;
  47             }
  48         }
  49 
  50         testHeapDumpPath();
  51     }
  52     static void testHeapDumpPath() throws Exception {
  53         String heapdumpPath = "dumps";
  54         File dumpDirectory = new File(heapdumpPath);
  55         dumpDirectory.mkdir();
  56         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-XX:+HeapDumpOnOutOfMemoryError",
  57                 "-Xmx64m", "-XX:HeapDumpPath=" + heapdumpPath, TestHeapDumpPath.class.getName(), "OOME");
  58 
  59         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  60         output.stdoutShouldNotBeEmpty();
  61         output.shouldContain("Dumping heap");
  62 
  63         Asserts.assertFalse(dumpDirectory.listFiles().length == 0,
  64                 "There is no dump files found in " + dumpDirectory );
  65 
  66         Asserts.assertTrue(dumpDirectory.listFiles().length == 1,
  67                 "There are unexpected files in " + dumpDirectory
  68                         + ": " + String.join(",", dumpDirectory.list()) +".");
  69 
  70         File dump = dumpDirectory.listFiles()[0];
  71         Asserts.assertTrue(dump.exists() && dump.isFile(),
  72                 "Could not find dump file " + dump.getAbsolutePath());
  73 
  74         HprofParser.parse(dump);
  75         System.out.println("PASSED");
  76     }
  77 
  78 }