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 jdk.jfr.Event;
  29 import jdk.jfr.EventType;
  30 import jdk.jfr.Recording;
  31 import jdk.test.lib.Asserts;
  32 
  33 /**
  34  * @test
  35  * @summary Test Event.isEnabled() with multiple recordings
  36  * @key jfr
  37  *
  38  * @library /lib /
  39  * @run main/othervm jdk.jfr.api.event.TestIsEnabledMultiple
  40  */
  41 
  42 public class TestIsEnabledMultiple {
  43 
  44     enum When {
  45         BeforeStart, DuringRecording
  46     }
  47 
  48     enum RecState {
  49         New, Running
  50     }
  51 
  52     enum EnableState {
  53         Enabled, Disabled
  54     }
  55 
  56     public static void main(String[] args) throws Exception {
  57         for (RecState recStateA : RecState.values()) {
  58             for (RecState recStateB : RecState.values()) {
  59                 for (EnableState enableStateA : EnableState.values()) {
  60                     for (EnableState enableStateB : EnableState.values()) {
  61                         assertEnabled(recStateA, recStateB, enableStateA, enableStateB);
  62                     }
  63                 }
  64             }
  65         }
  66     }
  67 
  68     private static void assertEnabled(RecState recStateA, RecState recStateB, EnableState enableStateA, EnableState enableStateB) {
  69 
  70         Recording a = new Recording();
  71         Recording b = new Recording();
  72         assertEnablement(false); // no recording running
  73 
  74         if (enableStateA == EnableState.Disabled) {
  75             a.disable(MyEvent.class);
  76         }
  77         if (enableStateA == EnableState.Enabled) {
  78             a.enable(MyEvent.class);
  79         }
  80         if (enableStateB == EnableState.Disabled) {
  81             b.disable(MyEvent.class);
  82         }
  83         if (enableStateB == EnableState.Enabled) {
  84             b.enable(MyEvent.class);
  85         }
  86         if ( enableStateA == EnableState.Disabled && recStateA == RecState.Running) {
  87             if ( enableStateB == EnableState.Disabled && recStateB == RecState.Running) {
  88                 System.out.println();
  89             }
  90         }
  91         if (recStateA == RecState.Running) {
  92             a.start();
  93         }
  94         if (recStateB == RecState.Running) {
  95             b.start();
  96         }
  97 
  98         System.out.println("Recording a is " + a.getState() + ". Event is " + enableStateA);
  99         System.out.println("Recording b is " + b.getState() + ". Event is " + enableStateB);
 100         // an event is enabled if at least one recording is running
 101         // and the event is on by default or has been enabled.
 102         boolean aEnabled = recStateA == RecState.Running && enableStateA == EnableState.Enabled;
 103         boolean bEnabled = recStateB == RecState.Running && enableStateB == EnableState.Enabled;
 104         boolean enabled = aEnabled || bEnabled;
 105         System.out.println("Expected enablement: " + enabled);
 106         System.out.println();
 107         assertEnablement(enabled);
 108         if (recStateA == RecState.Running) {
 109             a.stop();
 110         }
 111         if (recStateB == RecState.Running) {
 112             b.stop();
 113         }
 114         assertEnablement(false); // no recording running
 115         a.close();
 116         b.close();
 117     }
 118 
 119     private static void assertEnablement(boolean enabled) {
 120         Asserts.assertEQ(EventType.getEventType(MyEvent.class).isEnabled(), enabled, "Event enablement not as expected");
 121     }
 122 
 123     @SuppressWarnings("unused")
 124     private static class MyEvent extends Event {
 125         public String msg;
 126     }
 127 
 128 }