|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +package jdk.jfr.api.consumer; |
| 25 | + |
| 26 | +import java.lang.reflect.Modifier; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import jdk.jfr.Event; |
| 30 | +import jdk.jfr.Recording; |
| 31 | +import jdk.jfr.consumer.RecordedClass; |
| 32 | +import jdk.jfr.consumer.RecordedEvent; |
| 33 | +import jdk.test.lib.jfr.Events; |
| 34 | + |
| 35 | +/** |
| 36 | + * @test |
| 37 | + * @summary Verifies methods of RecordedClass |
| 38 | + * @key jfr |
| 39 | + * @requires vm.hasJFR |
| 40 | + * @library /test/lib |
| 41 | + * @run main/othervm jdk.jfr.api.consumer.TestRecordedClass |
| 42 | + */ |
| 43 | +public class TestRecordedClass { |
| 44 | + |
| 45 | + static class TestEvent extends Event { |
| 46 | + Class<?> typeA; |
| 47 | + Class<?> typeB; |
| 48 | + } |
| 49 | + |
| 50 | + private static class TypeA { |
| 51 | + } |
| 52 | + |
| 53 | + public final static class TypeB { |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) throws Exception { |
| 57 | + try (Recording recording = new Recording()) { |
| 58 | + recording.start(); |
| 59 | + TestEvent event = new TestEvent(); |
| 60 | + event.typeA = TypeA.class; |
| 61 | + event.typeB = TypeB.class; |
| 62 | + event.commit(); |
| 63 | + recording.stop(); |
| 64 | + |
| 65 | + List<RecordedEvent> events = Events.fromRecording(recording); |
| 66 | + Events.hasEvents(events); |
| 67 | + for (RecordedEvent recordedEvent : events) { |
| 68 | + RecordedClass typeA = recordedEvent.getClass("typeA"); |
| 69 | + RecordedClass typeB = recordedEvent.getClass("typeB"); |
| 70 | + assertModifiers(typeA, TypeA.class); |
| 71 | + assertModifiers(typeB, TypeB.class); |
| 72 | + assertName(typeA, TypeA.class); |
| 73 | + assertName(typeB, TypeB.class); |
| 74 | + assertClassLoader(typeA, TypeA.class.getClassLoader()); |
| 75 | + assertClassLoader(typeB, TypeB.class.getClassLoader()); |
| 76 | + assertId(typeA); |
| 77 | + assertId(typeB); |
| 78 | + if (typeA.getId() == typeB.getId()) { |
| 79 | + throw new Exception("Same ID for different classes"); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static void assertId(RecordedClass recordedClass) throws Exception { |
| 86 | + long id = recordedClass.getId(); |
| 87 | + if (id < 1 || id >= 1024 * 1024) { |
| 88 | + throw new Exception("Expected class ID to be above 1 and below 1 M"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static void assertClassLoader(RecordedClass recordedClass, ClassLoader classLoader) throws Exception { |
| 93 | + String expected = classLoader.getClass().getName(); |
| 94 | + String actual = recordedClass.getClassLoader().getType().getName(); |
| 95 | + if (!expected.equals(actual)) { |
| 96 | + throw new Exception("Expected class loader to be " + expected + ", was " + actual); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static void assertName(RecordedClass recordedClass, Class<?> clazz) throws Exception { |
| 101 | + String className = clazz.getClass().getName(); |
| 102 | + if (className.equals(recordedClass.getName())) { |
| 103 | + throw new Exception("Expected class to be named " + className); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static void assertModifiers(RecordedClass recordedClass, Class<?> clazz) throws Exception { |
| 108 | + int modifiers = clazz.getModifiers(); |
| 109 | + if (modifiers != recordedClass.getModifiers()) { |
| 110 | + String expected = Modifier.toString(modifiers); |
| 111 | + String actual = Modifier.toString(recordedClass.getModifiers()); |
| 112 | + throw new Exception("Expected modifier to be '" + expected + "', was '" + actual + "'"); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments