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 jdk.jfr.EventType;
  29 import jdk.jfr.Recording;
  30 import jdk.testlibrary.Asserts;
  31 import jdk.testlibrary.jfr.SimpleEvent;
  32 
  33 /*
  34  * @test
  35  * @summary Test Event.isEnabled()
  36  * @key jfr
  37  * @library /lib/testlibrary
  38  * @run main/othervm jdk.jfr.api.event.TestIsEnabled
  39  */
  40 
  41 public class TestIsEnabled {
  42 
  43     public static void main(String[] args) throws Exception {
  44         assertDisabled("Event enabled with no recording");
  45 
  46         Recording r = new Recording();
  47         assertDisabled("Event enabled at new Recording()");
  48 
  49         r.enable(SimpleEvent.class);
  50         assertDisabled("Event enabled before r.start()");
  51 
  52         r.start();
  53 
  54         // enable/disable by class
  55         assertEnabled("Event not enabled after r.start()");
  56         r.disable(SimpleEvent.class);
  57         assertDisabled("Event enabled after r.disable()");
  58         r.enable(SimpleEvent.class);
  59         assertEnabled("Event disabled afer r.enable()");
  60 
  61         // enable/disable by event setting name
  62         String eventSettingName = String.valueOf(EventType.getEventType(SimpleEvent.class).getId());
  63         System.out.println("eventSettingName=" + eventSettingName);
  64 
  65         r.disable(eventSettingName);
  66         assertDisabled("Event enabled after r.disable(name)");
  67         r.enable(eventSettingName);
  68         assertEnabled("Event disabled after r.enable(name)");
  69 
  70         r.stop();
  71         assertDisabled("Event enabled after r.stop()");
  72 
  73         r.close();
  74         assertDisabled("Event enabled after r.close()");
  75     }
  76 
  77     private static void assertEnabled(String msg) {
  78         SimpleEvent event = new SimpleEvent();
  79         Asserts.assertTrue(event.isEnabled(), msg);
  80     }
  81 
  82     private static void assertDisabled(String msg) {
  83         SimpleEvent event = new SimpleEvent();
  84         Asserts.assertFalse(event.isEnabled(), msg);
  85     }
  86 
  87 }