1 /*
   2  * Copyright (c) 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.api.metadata.eventtype;
  27 
  28 import jdk.jfr.Event;
  29 import jdk.jfr.EventType;
  30 import jdk.jfr.FlightRecorder;
  31 import jdk.test.lib.Asserts;
  32 import jdk.test.lib.jfr.Events;
  33 
  34 /**
  35  * @test
  36  * @summary Test getDefaultValues()
  37  * @key jfr
  38  * 
  39  * @library /lib /
  40  * @run main/othervm jdk.jfr.api.metadata.eventtype.TestGetDefaultValues
  41  */
  42 public class TestGetDefaultValues {
  43 
  44     private static class DefaultEvent extends Event {
  45     }
  46 
  47     public static void main(String[] args) throws Throwable {
  48         testDefaultEvent();
  49         testCustomEvent();
  50         testCustomWithPeriod();
  51     }
  52 
  53     private static void testCustomWithPeriod() {
  54         Runnable hook = new Runnable() {
  55             @Override
  56             public void run() {
  57             }
  58         };
  59         EventType customEventType = EventType.getEventType(EventWithCustomSettings.class);
  60         FlightRecorder.addPeriodicEvent(EventWithCustomSettings.class, hook);
  61         Asserts.assertEquals(customEventType.getSettingDescriptors().size(), 4, "Wrong number of settings");
  62         assertDefaultValue(customEventType, "period", "10 s");
  63         assertDefaultValue(customEventType, "enabled", "false");
  64         assertDefaultValue(customEventType, "setting1", "none");
  65         assertDefaultValue(customEventType, "setting2", "none");
  66 
  67         FlightRecorder.removePeriodicEvent(hook);
  68         Asserts.assertEquals(customEventType.getSettingDescriptors().size(), 5, "Wrong number of settings");
  69         assertDefaultValue(customEventType, "threshold", "100 ms");
  70         assertDefaultValue(customEventType, "stackTrace", "true");
  71         assertDefaultValue(customEventType, "enabled", "false");
  72         assertDefaultValue(customEventType, "setting1", "none");
  73         assertDefaultValue(customEventType, "setting2", "none");
  74 
  75     }
  76 
  77     private static void testCustomEvent() {
  78         EventType customizedEventType = EventType.getEventType(EventWithCustomSettings.class);
  79         Asserts.assertEquals(customizedEventType.getSettingDescriptors().size(), 5, "Wrong number of default values");
  80         assertDefaultValue(customizedEventType, "setting1", "none");
  81         assertDefaultValue(customizedEventType, "setting2", "none");
  82         assertDefaultValue(customizedEventType, "threshold", "100 ms");
  83         assertDefaultValue(customizedEventType, "stackTrace", "true");
  84         assertDefaultValue(customizedEventType, "enabled", "false");
  85     }
  86 
  87     private static void testDefaultEvent() {
  88         EventType defaultEventType = EventType.getEventType(DefaultEvent.class);
  89         Asserts.assertEquals(defaultEventType.getSettingDescriptors().size(), 3, "Wrong number of default values");
  90         assertDefaultValue(defaultEventType, "threshold", "0 ns");
  91         assertDefaultValue(defaultEventType, "stackTrace", "true");
  92         assertDefaultValue(defaultEventType, "enabled", "true");
  93     }
  94 
  95     private static void assertDefaultValue(EventType eventType, String name, String expected) {
  96         String value = Events.getSetting(eventType, name).getDefaultValue();
  97         Asserts.assertEquals(value, expected, "Incorrect value for " + name);
  98     }
  99 }