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.flightrecorder;
  27 
  28 import static jdk.test.lib.Asserts.assertFalse;
  29 import static jdk.test.lib.Asserts.assertTrue;
  30 
  31 import java.util.HashSet;
  32 import java.util.Set;
  33 
  34 import jdk.jfr.Event;
  35 import jdk.jfr.EventType;
  36 import jdk.jfr.FlightRecorder;
  37 import jdk.jfr.Recording;
  38 import jdk.jfr.consumer.RecordedEvent;
  39 import jdk.test.lib.jfr.Events;
  40 
  41 /*
  42  * @test
  43  * @key jfr
  44  * @library /test/lib
  45  * @run main/othervm/timeout=600 jdk.jfr.api.flightrecorder.TestGetEventTypes
  46  */
  47 public class TestGetEventTypes {
  48 
  49     public static void main(String[] args) throws Throwable {
  50         Recording r1 = new Recording();
  51         r1.setToDisk(true);
  52 
  53         MyEvent myEvent = new MyEvent();
  54         EventType t = EventType.getEventType(MyEvent.class);
  55         System.out.println(t.getName());
  56         boolean isMyEventFound = false;
  57         for (EventType eventType : FlightRecorder.getFlightRecorder().getEventTypes()) {
  58             System.out.println(": eventType: " + eventType.getName());
  59             r1.enable(eventType.getName());
  60             if (eventType.getName().equals(MyEvent.class.getName())) {
  61                 isMyEventFound = true;
  62             }
  63         }
  64         assertTrue(isMyEventFound, "EventType for MyEvent not found");
  65 
  66         r1.start();
  67         myEvent.begin();
  68         myEvent.end();
  69         myEvent.commit();
  70         r1.stop();
  71 
  72         Set<String> eventTypeNames = new HashSet<String>();
  73         for (EventType et : FlightRecorder.getFlightRecorder().getEventTypes()) {
  74             assertFalse(eventTypeNames.contains(et.getName()), "EventType returned twice: " + et.getName());
  75             eventTypeNames.add(et.getName());
  76         }
  77 
  78         isMyEventFound = false;
  79         for (RecordedEvent event : Events.fromRecording(r1)) {
  80             final String name = event.getEventType().getName();
  81             System.out.println("event.getEventType: " + name);
  82             assertTrue(eventTypeNames.contains(name), "Missing EventType: " + name);
  83             if (name.equals(MyEvent.class.getName())) {
  84                 isMyEventFound = true;
  85             }
  86         }
  87         r1.close();
  88         assertTrue(isMyEventFound, "Event for MyEvent not found");
  89     }
  90 
  91     private static class MyEvent extends Event {
  92     }
  93 
  94 }