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.util.ArrayList;
  29 import java.util.Iterator;
  30 import java.util.List;
  31 
  32 import jdk.jfr.Event;
  33 import jdk.jfr.EventType;
  34 import jdk.jfr.Recording;
  35 import jdk.jfr.consumer.RecordedEvent;
  36 import jdk.test.lib.Asserts;
  37 import jdk.test.lib.jfr.Events;
  38 
  39 /**
  40  * @test
  41  * @summary Test enable/disable event and verify recording has expected events.
  42  * @key jfr
  43  * @requires vm.hasJFR
  44  * @library /test/lib
  45  * @run main/othervm jdk.jfr.api.event.TestEnableDisable
  46  */
  47 
  48 public class TestEnableDisable {
  49 
  50     public static void main(String[] args) throws Exception {
  51         List<MyEvent> expectedEvents = new ArrayList<>();
  52         Recording r = new Recording();
  53 
  54         r.start();
  55         createEvent(expectedEvents, true); // id=0 Custom event classes are enabled by default.
  56 
  57         r.disable(MyEvent.class);
  58         createEvent(expectedEvents, false); // id=1
  59         r.enable(MyEvent.class);
  60         createEvent(expectedEvents, true);  // id=2
  61 
  62         // enable/disable by event setting name
  63         String eventSettingName = String.valueOf(EventType.getEventType(MyEvent.class).getId());
  64         System.out.println("eventSettingName=" + eventSettingName);
  65 
  66         r.disable(eventSettingName);
  67         createEvent(expectedEvents, false); // id=3
  68         r.enable(eventSettingName);
  69         createEvent(expectedEvents, true);
  70 
  71         r.stop();
  72         createEvent(expectedEvents, false);
  73 
  74         Iterator<MyEvent> expectedIterator = expectedEvents.iterator();
  75         for (RecordedEvent event : Events.fromRecording(r)) {
  76             System.out.println("event.id=" + Events.assertField(event, "id").getValue());
  77             Asserts.assertTrue(expectedIterator.hasNext(), "Found more events than expected");
  78             Events.assertField(event, "id").equal(expectedIterator.next().id);
  79         }
  80         Asserts.assertFalse(expectedIterator.hasNext(), "Did not find all expected events.");
  81 
  82         r.close();
  83     }
  84 
  85     private static int eventId = 0;
  86     private static void createEvent(List<MyEvent> expectedEvents, boolean isExpected) {
  87         MyEvent event = new MyEvent();
  88         event.begin();
  89         event.id = eventId;
  90         event.commit();
  91 
  92         if (isExpected) {
  93             expectedEvents.add(event);
  94         }
  95         eventId++;
  96     }
  97 
  98     private static class MyEvent extends Event {
  99         private int id;
 100     }
 101 
 102 }