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