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.io.IOException;
  29 
  30 import jdk.jfr.Event;
  31 import jdk.jfr.EventType;
  32 import jdk.jfr.Experimental;
  33 import jdk.jfr.FlightRecorder;
  34 import jdk.jfr.Recording;
  35 import jdk.jfr.consumer.RecordedEvent;
  36 import jdk.jfr.consumer.RecordingFile;
  37 import jdk.test.lib.Asserts;
  38 import jdk.test.lib.jfr.Events;
  39 
  40 /**
  41  * @test
  42  * @summary Tests that abstract events are not part of metadata
  43  * @key jfr
  44  *
  45  * @library /lib /
  46  * @run main/othervm jdk.jfr.api.event.TestAbstractEvent
  47  * @run main/othervm -XX:+AllowParallelDefineClass jdk.jfr.api.event.TestAbstractEvent
  48  */
  49 public class TestAbstractEvent {
  50 
  51     // Should not be included in metadata
  52     @Experimental
  53     static abstract class BaseEvent extends Event {
  54     }
  55 
  56     // Should be included
  57     static class ConcreteEvent extends BaseEvent {
  58     }
  59 
  60     public static void main(String... args) throws IOException {
  61         try {
  62             EventType.getEventType(BaseEvent.class);
  63             Asserts.fail("Should not accept abstract event classes");
  64         } catch (IllegalArgumentException iae) {
  65             // OK, as expected
  66         }
  67 
  68         try {
  69             FlightRecorder.register(BaseEvent.class);
  70             Asserts.fail("Should not accept registering abstract event classes");
  71         } catch (IllegalArgumentException iae) {
  72             // OK, as expected
  73         }
  74 
  75         try {
  76             FlightRecorder.unregister(BaseEvent.class);
  77             Asserts.fail("Should not accept unregistering abstract event classes");
  78         } catch (IllegalArgumentException iae) {
  79             // OK, as expected
  80         }
  81 
  82 
  83         Recording r = new Recording();
  84         try {
  85             r.enable(BaseEvent.class);
  86             Asserts.fail("Should not accept abstract event classes");
  87         } catch (IllegalArgumentException iae) {
  88             // OK, as expected
  89         }
  90         r.start();
  91 
  92         ConcreteEvent event = new ConcreteEvent();
  93         event.commit();
  94         r.stop();
  95         RecordingFile rf = Events.copyTo(r);
  96         RecordedEvent re = rf.readEvent();
  97         if (!re.getEventType().getName().equals(ConcreteEvent.class.getName())) {
  98             Asserts.fail("Expected " + ConcreteEvent.class.getName() + " event to be part of recording. Found " + re.getEventType().getName());
  99         }
 100         if (rf.hasMoreEvents()) {
 101             Asserts.fail("Expected only one event");
 102         }
 103         rf.close();
 104         EventType concreteEventType = null;
 105         for (EventType type : FlightRecorder.getFlightRecorder().getEventTypes()) {
 106             if (type.getName().equals(BaseEvent.class.getName())) {
 107                 Asserts.fail("Abstract events should not be part of metadata");
 108             }
 109             if (type.getName().equals(ConcreteEvent.class.getName())) {
 110                 concreteEventType = type;
 111             }
 112         }
 113         Asserts.assertTrue(concreteEventType != null, "Could not find " + ConcreteEvent.class.getName() + " in metadata");
 114         Experimental exp = concreteEventType.getAnnotation(Experimental.class);
 115         Asserts.assertTrue(exp != null, "Could not find inherited annotation" + Experimental.class.getName() + " from abstract event class");
 116     }
 117 }