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.event; 27 28 import java.util.ArrayList; 29 import java.util.Iterator; 30 import java.util.List; 31 32 import jdk.jfr.Event; 33 import jdk.jfr.EventType; 34 import jdk.jfr.Recording; 35 import jdk.jfr.consumer.RecordedEvent; 36 import jdk.test.lib.Asserts; 37 import jdk.test.lib.jfr.Events; 38 39 /* 40 * @test 41 * @summary Test enable/disable event and verify recording has expected events. 42 * @key jfr 43 * @library /test/lib 44 * @run main/othervm jdk.jfr.api.event.TestEnableDisable 45 */ 46 47 public class TestEnableDisable { 48 49 public static void main(String[] args) throws Exception { 50 List<MyEvent> expectedEvents = new ArrayList<>(); 51 Recording r = new Recording(); 52 53 r.start(); 54 createEvent(expectedEvents, true); // id=0 Custom event classes are enabled by default. 55 56 r.disable(MyEvent.class); 57 createEvent(expectedEvents, false); // id=1 58 r.enable(MyEvent.class); 59 createEvent(expectedEvents, true); // id=2 60 61 // enable/disable by event setting name 62 String eventSettingName = String.valueOf(EventType.getEventType(MyEvent.class).getId()); 63 System.out.println("eventSettingName=" + eventSettingName); 64 65 r.disable(eventSettingName); 66 createEvent(expectedEvents, false); // id=3 67 r.enable(eventSettingName); 68 createEvent(expectedEvents, true); 69 70 r.stop(); 71 createEvent(expectedEvents, false); 72 73 Iterator<MyEvent> expectedIterator = expectedEvents.iterator(); 74 for (RecordedEvent event : Events.fromRecording(r)) { 75 System.out.println("event.id=" + Events.assertField(event, "id").getValue()); 76 Asserts.assertTrue(expectedIterator.hasNext(), "Found more events than expected"); 77 Events.assertField(event, "id").equal(expectedIterator.next().id); 78 } 79 Asserts.assertFalse(expectedIterator.hasNext(), "Did not find all expected events."); 80 81 r.close(); 82 } 83 84 private static int eventId = 0; 85 private static void createEvent(List<MyEvent> expectedEvents, boolean isExpected) { 86 MyEvent event = new MyEvent(); 87 event.begin(); 88 event.id = eventId; 89 event.commit(); 90 91 if (isExpected) { 92 expectedEvents.add(event); 93 } 94 eventId++; 95 } 96 97 private static class MyEvent extends Event { 98 private int id; 99 } 100 101 }