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