test/jdk/jdk/jfr/jmx/TestEventTypes.java
changeset 50113 caf115bb98ad
child 51214 67736b4846a0
equal deleted inserted replaced
50112:7a2a740815b7 50113:caf115bb98ad
       
     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.util.ArrayList;
       
    29 import java.util.List;
       
    30 
       
    31 import jdk.jfr.Description;
       
    32 import jdk.jfr.Event;
       
    33 import jdk.jfr.EventType;
       
    34 import jdk.jfr.FlightRecorder;
       
    35 import jdk.jfr.Label;
       
    36 import jdk.jfr.Name;
       
    37 import jdk.jfr.Recording;
       
    38 import jdk.jfr.SettingDescriptor;
       
    39 import jdk.management.jfr.EventTypeInfo;
       
    40 import jdk.management.jfr.FlightRecorderMXBean;
       
    41 import jdk.management.jfr.SettingDescriptorInfo;
       
    42 import jdk.test.lib.Asserts;
       
    43 import jdk.test.lib.jfr.Events;
       
    44 
       
    45 /*
       
    46  * @test
       
    47  * @key jfr
       
    48  * @summary Verifies that EventTypes from jmx and FlightRecorder are the same.
       
    49  * @library /test/lib /test/jdk
       
    50  * @run main/othervm jdk.jfr.jmx.TestEventTypes
       
    51  */
       
    52 public class TestEventTypes {
       
    53     public static void main(String[] args) throws Exception {
       
    54         FlightRecorderMXBean bean = JmxHelper.getFlighteRecorderMXBean();
       
    55         FlightRecorder jfr = FlightRecorder.getFlightRecorder();
       
    56 
       
    57         Recording r = new Recording();
       
    58         r.enable(MyEvent.class);
       
    59         new MyEvent(); // triggers <clinit>
       
    60         List<EventTypeInfo> infos = bean.getEventTypes();
       
    61         List<EventType> types = jfr.getEventTypes();
       
    62         Asserts.assertFalse(infos.isEmpty(), "No EventTypeInfos found");
       
    63         verifyMyEventType(infos);
       
    64         assertSame(infos, types);
       
    65         r.close();
       
    66     }
       
    67 
       
    68     @Name("MyEvent.name")
       
    69     @Label("MyEvent.label")
       
    70     @Description("MyEvent.description")
       
    71     private static class MyEvent extends Event {
       
    72         @Label("MyEvent.message")
       
    73         public String message;
       
    74     }
       
    75 
       
    76     private static void verifyMyEventType(List<EventTypeInfo> infos) {
       
    77         for (EventTypeInfo info : infos) {
       
    78             if ("MyEvent.name".equals(info.getName())) {
       
    79                 System.out.println("EventTypeInfo for MyEvent: " + info);
       
    80                 Asserts.assertEquals("MyEvent.label", info.getLabel());
       
    81                 Asserts.assertEquals("MyEvent.description", info.getDescription());
       
    82                 for (SettingDescriptorInfo si : info.getSettingDescriptors()) {
       
    83                     System.out.println("si=" + si);
       
    84                 }
       
    85                 return;
       
    86             }
       
    87         }
       
    88         Asserts.fail("Missing EventTypeInfo for MyEvent");
       
    89     }
       
    90 
       
    91     private static void assertSame(List<EventTypeInfo> infos, List<EventType> types) {
       
    92         List<Long> ids = new ArrayList<>();
       
    93         for (EventTypeInfo info : infos) {
       
    94             long id = info.getId();
       
    95             Asserts.assertFalse(ids.contains(id), "EventTypeInfo.id not unique:" + id);
       
    96             ids.add(id);
       
    97             boolean isFound = false;
       
    98             for (EventType type : types) {
       
    99                 if (type.getId() == id) {
       
   100                     assertSame(info, type);
       
   101                     isFound = true;
       
   102                     break;
       
   103                 }
       
   104             }
       
   105             if (!isFound) {
       
   106                 String msg = "No EventType for EventTypeInfo";
       
   107                 System.out.println(msg + ": " + info);
       
   108                 Asserts.fail(msg);
       
   109             }
       
   110         }
       
   111         Asserts.assertEquals(infos.size(), types.size(), "Number of EventTypeInfos != EventTypes");
       
   112     }
       
   113 
       
   114     private static void assertSame(EventTypeInfo ti, EventType t) {
       
   115         try {
       
   116             Asserts.assertEquals(ti.getId(), t.getId(), "Wrong id");
       
   117             Asserts.assertEquals(ti.getName(), t.getName(), "Wrong name");
       
   118             Asserts.assertEquals(ti.getLabel(), t.getLabel(), "Wrong label");
       
   119             Asserts.assertEquals(ti.getDescription(), t.getDescription(), "Wrong description");
       
   120             Asserts.assertEquals(ti.getCategoryNames(), t.getCategoryNames(), "Wrong category names");
       
   121 
       
   122             for (SettingDescriptorInfo si : ti.getSettingDescriptors()) {
       
   123                 String settingName = si.getName();
       
   124                 boolean isFound = false;
       
   125                 for (SettingDescriptor d : t.getSettingDescriptors()) {
       
   126                     if (settingName.equals(d.getName())) {
       
   127                         assertSame(si, d, t);
       
   128                         isFound = true;
       
   129                         break;
       
   130                     }
       
   131                 }
       
   132                 if (!isFound) {
       
   133                     Asserts.fail("No ValueDescriptor for SettingDescriptorInfo: " + si);
       
   134                 }
       
   135             }
       
   136         } catch (Exception e) {
       
   137             System.out.printf("EventTypeInfo != EventType%nEventTypeInfo=%s%nEventType=%s%n", ti, t);
       
   138             throw e;
       
   139         }
       
   140     }
       
   141 
       
   142     private static void assertSame(SettingDescriptorInfo si, SettingDescriptor d, EventType type) {
       
   143         try {
       
   144             Asserts.assertEquals(si.getName(), d.getName(), "Wrong name");
       
   145             Asserts.assertEquals(si.getLabel(), d.getLabel(), "Wrong label");
       
   146             Asserts.assertEquals(si.getTypeName(), d.getTypeName(), "Wrong typeName");
       
   147             Asserts.assertEquals(si.getDescription(), d.getDescription(), "Wrong description");
       
   148             String typeDefaultValue = Events.getSetting(type, si.getName()).getDefaultValue();
       
   149             Asserts.assertEquals(si.getDefaultValue(), typeDefaultValue, "Wrong defaultValue");
       
   150         } catch (Exception e) {
       
   151             System.out.printf("SettingDescriptorInfo != SettingDescriptor=%s%nValueDescriptor=%s%n", si, d);
       
   152             throw e;
       
   153         }
       
   154     }
       
   155 }