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