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.Map; 30 31 import jdk.jfr.Recording; 32 import jdk.management.jfr.FlightRecorderMXBean; 33 import jdk.management.jfr.RecordingInfo; 34 import jdk.testlibrary.Asserts; 35 import jdk.testlibrary.jfr.EventNames; 36 import jdk.testlibrary.jfr.JmxHelper; 37 38 /* 39 * @test 40 * @key jfr 41 * @summary Verify Exception when setting invalid config. 42 * @library /lib/testlibrary 43 * @run main/othervm jdk.jfr.jmx.TestSetConfigurationInvalid 44 */ 45 public class TestSetConfigurationInvalid { 46 public static void main(String[] args) throws Exception { 47 FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean(); 48 long recId = bean.newRecording(); 49 50 final String correctConfig = 51 "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n" + 52 "<configuration version=\"2.0\" label=\"TestName\" description='TestDesc' provider='TestProvider'>\n" + 53 " <event name=\"" + EventNames.ClassLoad + "\">\r" + 54 " \t <setting name=\"enabled\" control='class-loading-enabled'>false</setting>\r\n" + 55 " <setting name=\"stackTrace\">true</setting>\t\r\n" + 56 " <setting name=\"threshold\">5 ms</setting> \n" + 57 " </event> " + 58 " <control> " + 59 " <flag name=\"class-loading-enabled\" label=\"Class Loading\">false</flag>\n" + 60 " </control>" + 61 "</configuration>"; 62 63 Map<String, String> expectedSetting = new HashMap<>(); 64 expectedSetting.put(EventNames.ClassLoad + "#enabled", "false"); 65 expectedSetting.put(EventNames.ClassLoad + "#stackTrace", "true"); 66 expectedSetting.put(EventNames.ClassLoad + "#threshold", "5 ms"); 67 68 // First set a few invalid configs. Should get Exceptions. 69 try { 70 bean.setConfiguration(recId, null); 71 Asserts.fail("Expected NullPointerException"); 72 } catch (NullPointerException e) { 73 // Expected exception 74 } 75 76 setInvalidConfig(recId, "Dummy text"); 77 setInvalidConfig(recId, correctConfig.replace("/event", "event")); 78 setInvalidConfig(recId, correctConfig.replace("<control>", "")); 79 80 // Verify that we can set a correct setting after the failed attempts. 81 bean.setConfiguration(recId, correctConfig); 82 RecordingInfo jmxRecording = JmxHelper.getJmxRecording(recId); 83 Recording javaRecording = JmxHelper.getJavaRecording(recId); 84 JmxHelper.verifyEquals(jmxRecording, javaRecording); 85 86 Map<String, String> settings = jmxRecording.getSettings(); 87 for (String name : expectedSetting.keySet()) { 88 String value = settings.remove(name); 89 Asserts.assertNotNull(value, "No setting with name " + name); 90 Asserts.assertEquals(value, expectedSetting.get(name), "Wrong setting value"); 91 } 92 Asserts.assertTrue(settings.isEmpty(), "Extra settings found " + settings.keySet()); 93 } 94 95 private static void setInvalidConfig(long recId, String config) { 96 try { 97 JmxHelper.getFlighteRecorderMXBean().setConfiguration(recId, config); 98 System.out.printf("Invalid config:%n%s", config); 99 Asserts.fail("No exception when setting invalid configuration"); 100 } catch (IllegalArgumentException e) { 101 // Expected exception 102 // Simple check if error message is about parse error. 103 String msg = e.getMessage().toLowerCase(); 104 Asserts.assertTrue(msg.contains("parse"), String.format("Missing 'parse' in msg '%s'", msg)); 105 } 106 } 107 108 }