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