1 /*
   2  * Copyright (c) 2013, 2019, 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.jmx;
  27 
  28 import java.util.HashMap;
  29 import java.util.LinkedHashMap;
  30 import java.util.Map;
  31 
  32 import jdk.management.jfr.FlightRecorderMXBean;
  33 import jdk.testlibrary.Asserts;
  34 import jdk.testlibrary.jfr.JmxHelper;
  35 
  36 /*
  37  * @test
  38  * @key jfr
  39  * @library /lib/testlibrary
  40  * @run main/othervm jdk.jfr.jmx.TestRecordingOptions
  41  */
  42 public class TestRecordingOptions {
  43     @SuppressWarnings({ "rawtypes", "unchecked" })
  44     public static void main(String[] args) throws Exception {
  45         Map<String, String> options = new HashMap<>();
  46         options.put("name", "myName");
  47         options.put("maxAge", "2 h");
  48         options.put("maxSize", "1234567890");
  49         options.put("dumpOnExit", "false");
  50         options.put("disk", "false");
  51         options.put("duration", "1 h"); // don't want recording to stop
  52 
  53         FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
  54         long recId = bean.newRecording();
  55         Map<String, String> defaults = bean.getRecordingOptions(recId);
  56         bean.setRecordingOptions(recId, options);
  57 
  58         // Verify that all options have been set. We only check the option we
  59         // have set. Unknown options are ignored.
  60         Map<String, String> outOptions = bean.getRecordingOptions(recId);
  61         logMap("set options", options);
  62         logMap("get options", outOptions);
  63         for (String key : options.keySet()) {
  64             Asserts.assertTrue(outOptions.containsKey(key), "Missing key " + key);
  65             Asserts.assertEquals(options.get(key), outOptions.get(key), "Wrong value for key " + key);
  66         }
  67 
  68         // Verify options in RecordingInfo
  69         Asserts.assertEquals(outOptions.get("name"), "myName", "Wrong name");
  70         Asserts.assertEquals(outOptions.get("maxAge"), "2 h", "Wrong maxAge");
  71         Asserts.assertEquals(outOptions.get("maxSize"), "1234567890", "Wrong maxSize");
  72         Asserts.assertEquals(outOptions.get("dumpOnExit"), "false", "Wrong dumpOnExit");
  73         Asserts.assertEquals(outOptions.get("disk"), "false", "Wrong disk");
  74         Asserts.assertEquals(outOptions.get("duration"), "1 h", "Wrong duration");
  75 
  76         // try empty map
  77         bean.setRecordingOptions(recId, new HashMap<>());
  78 
  79         // try map that does not have string keys
  80         Map<Integer, String> invalidKeys = new HashMap<>();
  81         invalidKeys.put(4711, "value");
  82         try {
  83             bean.setRecordingOptions(recId, (Map) invalidKeys);
  84             throw new Error("Expected IllagalStateException for non String key");
  85         } catch (IllegalArgumentException iae) {
  86             // OK, as expected
  87         }
  88         // try map that does not have string values
  89         Map<String, Integer> invalidValues = new HashMap<>();
  90         invalidValues.put("duration", 4711);
  91         try {
  92             bean.setRecordingOptions(recId, (Map) invalidKeys);
  93             throw new Error("Expected IllagalStateException for non String value");
  94         } catch (IllegalArgumentException iae) {
  95             // OK, as expected
  96         }
  97 
  98         // Try one incorrect value, and make sure non
  99         // of the other values are set.
 100         Map<String, String> lastIncorrect = new LinkedHashMap<>();
 101         lastIncorrect.put("duration", "10 h");
 102         lastIncorrect.put("whatever", "4711");
 103         try {
 104             bean.setRecordingOptions(recId, lastIncorrect);
 105             throw new Error("Expected IllagalStateException for incorrect key");
 106         } catch (IllegalArgumentException iae) {
 107             // ok
 108             Asserts.assertEquals("1 h", bean.getRecordingOptions(recId).get("duration"));
 109         }
 110 
 111         // verify that defaults are set back, if we use null
 112         Map<String, String> nullMap = new HashMap<>();
 113         nullMap.put("name", null);
 114         nullMap.put("maxAge", null);
 115         nullMap.put("maxSize", null);
 116         nullMap.put("dumpOnExit", null);
 117         nullMap.put("disk", null);
 118         nullMap.put("duration", null);
 119         bean.setRecordingOptions(recId, nullMap);
 120         Asserts.assertEquals(bean.getRecordingOptions(recId), defaults);
 121 
 122         bean.closeRecording(recId);
 123     }
 124 
 125     private static void logMap(String name, Map<String, String> map) {
 126         for (String key : map.keySet()) {
 127             System.out.printf("%s: %s=%s%n", name, key, map.get(key));
 128         }
 129     }
 130 }