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