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.info;
  27 
  28 
  29 import java.util.HashMap;
  30 import java.util.List;
  31 import java.util.Map;
  32 
  33 import jdk.jfr.Configuration;
  34 import jdk.management.jfr.ConfigurationInfo;
  35 import jdk.testlibrary.Asserts;
  36 import jdk.testlibrary.jfr.JmxHelper;
  37 
  38 /*
  39  * @test
  40  * @key jfr
  41  * @summary Test for ConfigurationInfo. Compare infos from java API and jmx API.
  42  * @library /lib/testlibrary
  43  * @run main/othervm jdk.jfr.jmx.info.TestConfigurationInfo
  44  */
  45 public class TestConfigurationInfo {
  46     public static void main(String[] args) throws Throwable {
  47         List<ConfigurationInfo> configInfos = JmxHelper.getFlighteRecorderMXBean().getConfigurations();
  48         Asserts.assertFalse(configInfos.isEmpty(), "No ConfigurationInfos found");
  49 
  50         Map<String, Configuration> configs = new HashMap<>();
  51         for (Configuration config : Configuration.getConfigurations()) {
  52             configs.put(config.getName(), config);
  53         }
  54         Asserts.assertFalse(configs.isEmpty(), "No Configurations found");
  55 
  56         for (ConfigurationInfo configInfo : configInfos) {
  57             final String key = configInfo.getName();
  58             Configuration config = configs.remove(key);
  59             Asserts.assertNotNull(config, "No Configuration for name " + key);
  60 
  61             System.out.println("getDescription:" + configInfo.getDescription());
  62             System.out.println("getLabel:" + configInfo.getLabel());
  63             System.out.println("getName:" + configInfo.getName());
  64             System.out.println("getProvider:" + configInfo.getProvider());
  65 
  66             Asserts.assertEquals(configInfo.getContents(), config.getContents(), "Wrong contents");
  67             Asserts.assertEquals(configInfo.getDescription(), config.getDescription(), "Wrong description");
  68             Asserts.assertEquals(configInfo.getLabel(), config.getLabel(), "Wrong label");
  69             Asserts.assertEquals(configInfo.getName(), config.getName(), "Wrong name");
  70             Asserts.assertEquals(configInfo.getProvider(), config.getProvider(), "Wrong provider");
  71 
  72             verifySettingsEqual(config, configInfo);
  73         }
  74 
  75         // Verify that all EventTypes have been matched.
  76         if (!configs.isEmpty()) {
  77             for (String name : configs.keySet()) {
  78                 System.out.println("Found extra Configuration with name " + name);
  79             }
  80             Asserts.fail("Found extra Configuration");
  81         }
  82     }
  83 
  84     private static void verifySettingsEqual(Configuration config, ConfigurationInfo configInfo) {
  85         Map<String, String> javaSettings = config.getSettings();
  86         Map<String, String> jmxSettings = configInfo.getSettings();
  87 
  88         Asserts.assertFalse(javaSettings.isEmpty(), "No Settings found in java apa");
  89         Asserts.assertFalse(jmxSettings.isEmpty(), "No Settings found in jmx api");
  90 
  91         for (String name : jmxSettings.keySet().toArray(new String[0])) {
  92             System.out.printf("%s: jmx=%s, java=%s%n", name, jmxSettings.get(name), javaSettings.get(name));
  93             Asserts.assertNotNull(javaSettings.get(name), "No java setting for " + name);
  94             Asserts.assertEquals(jmxSettings.get(name), javaSettings.get(name), "Wrong value for setting");
  95             javaSettings.remove(name);
  96         }
  97 
  98         // Verify that all Settings have been matched.
  99         if (!javaSettings.isEmpty()) {
 100             for (String name : javaSettings.keySet()) {
 101                 System.out.println("Found extra Settings name " + name);
 102             }
 103             Asserts.fail("Found extra Setting in java api");
 104         }
 105     }
 106 
 107 }