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