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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.startupargs;
  27 
  28 import jdk.test.lib.Asserts;
  29 import jdk.test.lib.process.OutputAnalyzer;
  30 import jdk.test.lib.process.ProcessTools;
  31 
  32 /*
  33  * @test
  34  * @key jfr
  35  *
  36  * @library /test/lib
  37  *
  38  * @run main jdk.jfr.startupargs.TestMultipleStartupRecordings
  39  */
  40 public class TestMultipleStartupRecordings {
  41 
  42     private static final String START_FLIGHT_RECORDING = "-XX:StartFlightRecording";
  43     private static final String FLIGHT_RECORDER_OPTIONS = "-XX:FlightRecorderOptions";
  44 
  45     static class MainClass {
  46         public static void main(String[] args) {
  47         }
  48     }
  49 
  50     private static void test(ProcessBuilder pb, String... expectedOutputs) throws Exception {
  51         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  52         for (String s : expectedOutputs) {
  53             output.shouldContain(s);
  54         }
  55     }
  56 
  57     private static void launchUnary(String options) throws Exception {
  58         String recording1 = START_FLIGHT_RECORDING + (options != null ? options : "");
  59         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, recording1, MainClass.class.getName());
  60         test(pb, "Started recording 1");
  61     }
  62 
  63     private static void launchBinary(String options1, String options2) throws Exception {
  64         String recording1 = START_FLIGHT_RECORDING + (options1 != null ? options1 : "");
  65         String recording2 = START_FLIGHT_RECORDING + (options2 != null ? options2 : "");
  66         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, recording1, recording2, MainClass.class.getName());
  67         test(pb, "Started recording 1", "Started recording 2");
  68     }
  69 
  70     private static void launchTernary(String options1, String options2, String options3) throws Exception {
  71         String recording1 = START_FLIGHT_RECORDING + (options1 != null ? options1 : "");
  72         String recording2 = START_FLIGHT_RECORDING + (options2 != null ? options2 : "");
  73         String recording3 = START_FLIGHT_RECORDING + (options3 != null ? options3 : "");
  74         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, recording1, recording2, recording3, MainClass.class.getName());
  75         test(pb, "Started recording 1", "Started recording 2", "Started recording 3");
  76     }
  77 
  78     private static void testDefault() throws Exception {
  79         System.out.println("testDefault");
  80         launchUnary(null);
  81         launchBinary(null, null);
  82         launchTernary(null, null, null);
  83     }
  84 
  85     private static void testColonDelimited() throws Exception {
  86         launchBinary(":name=myrecording1,filename=myrecording1.jfr", ":filename=myrecording2.jfr,name=myrecording2");
  87     }
  88 
  89     private static void testMixed() throws Exception {
  90         launchTernary(":maxage=2d,maxsize=5GB", "=dumponexit=true,maxage=10m,", ":name=myrecording,maxage=10m,filename=myrecording.jfr,disk=false");
  91     }
  92 
  93     private static void testWithFlightRecorderOptions() throws Exception {
  94         System.out.println("testWithFlightRecorderOptions");
  95         String flightRecorderOptions = FLIGHT_RECORDER_OPTIONS + "=maxchunksize=8m";
  96         String recording1 = START_FLIGHT_RECORDING + "=filename=recording1.jfr";
  97         String recording2 = START_FLIGHT_RECORDING + "=name=myrecording,filename=recording2.jfr";
  98         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, flightRecorderOptions, recording1, recording2, MainClass.class.getName());
  99         test(pb, "Started recording 1", "Started recording 2");
 100     }
 101 
 102     public static void main(String[] args) throws Exception {
 103         testDefault();
 104         testColonDelimited();
 105         testMixed();
 106         testWithFlightRecorderOptions();
 107     }
 108 }