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