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