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.test.lib.jfr; 27 28 import java.time.Duration; 29 import java.util.ArrayList; 30 import java.util.HashSet; 31 import java.util.List; 32 import java.util.Set; 33 34 import jdk.jfr.Recording; 35 import jdk.jfr.consumer.RecordedEvent; 36 import jdk.test.lib.Asserts; 37 38 public class SimpleEventHelper { 39 40 public static void enable(Recording r, boolean isEnabled) { 41 if (isEnabled) { 42 r.enable(SimpleEvent.class).withThreshold(Duration.ofMillis(0)).withoutStackTrace(); 43 } else { 44 r.disable(SimpleEvent.class); 45 } 46 } 47 48 public static SimpleEvent createEvent(int id) { 49 SimpleEvent event = new SimpleEvent(); 50 event.begin(); 51 event.id = id; 52 event.end(); 53 event.commit(); 54 return event; 55 } 56 57 public static void verifyEvents(Recording r, int ... ids) throws Exception { 58 List<Integer> eventIds = new ArrayList<>(); 59 for (RecordedEvent event : Events.fromRecording(r)) { 60 if (Events.isEventType(event, SimpleEvent.class.getName())) { 61 int id = Events.assertField(event, "id").getValue(); 62 System.out.printf("recording %s: event.id=%d%n", r.getName(), id); 63 eventIds.add(id); 64 } 65 } 66 Asserts.assertEquals(eventIds.size(), ids.length, "Wrong number of events"); 67 for (int i = 0; i < ids.length; ++i) { 68 Asserts.assertEquals(eventIds.get(i).intValue(), ids[i], "Wrong id in event"); 69 } 70 } 71 72 public static void verifyContains(List<RecordedEvent> events, int ... ids) throws Exception { 73 Set<Integer> missingIds = new HashSet<>(); 74 for (int id : ids) { 75 missingIds.add(id); 76 } 77 for (RecordedEvent event : getSimpleEvents(events)) { 78 int id = Events.assertField(event, "id").getValue(); 79 System.out.printf("event.id=%d%n", id); 80 missingIds.remove(new Integer(id)); 81 } 82 if (!missingIds.isEmpty()) { 83 missingIds.forEach(id -> System.out.println("Missing MyEvent with id " + id)); 84 Asserts.fail("Missing some MyEvent events"); 85 } 86 } 87 88 public static void verifyNotContains(List<RecordedEvent> events, int ... ids) throws Exception { 89 for (RecordedEvent event : getSimpleEvents(events)) { 90 int eventId = Events.assertField(event, "id").getValue(); 91 System.out.printf("event.id=%d%n", eventId); 92 for (int id : ids) { 93 Events.assertField(event, "id").notEqual(id); 94 } 95 } 96 } 97 98 public static List<RecordedEvent> getSimpleEvents(List<RecordedEvent> events) { 99 List<RecordedEvent> myEvents = new ArrayList<>(); 100 for (RecordedEvent event : events) { 101 if (Events.isEventType(event, SimpleEvent.class.getName())) { 102 myEvents.add(event); 103 } 104 } 105 return myEvents; 106 } 107 }