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.time.Instant;
  29 import java.util.Iterator;
  30 
  31 import jdk.jfr.Event;
  32 import jdk.jfr.Recording;
  33 import jdk.jfr.consumer.RecordedEvent;
  34 import jdk.test.lib.Asserts;
  35 import jdk.test.lib.jfr.Events;
  36 
  37 /**
  38  * @test
  39  * @summary Use custom event that reuse method names begin, end and commit.
  40  * @key jfr
  41  * 
  42  * @library /lib /
  43  * @run main/othervm jdk.jfr.api.event.TestOwnCommit
  44  */
  45 
  46 public class TestOwnCommit {
  47 
  48     public static void main(String[] args) throws Throwable {
  49        Recording r = new Recording();
  50         r.enable(MyEvent.class);
  51 
  52         r.start();
  53 
  54         MyEvent event = new MyEvent();
  55         event.begin();
  56         event.begin = 10;
  57         event.duration = Instant.now();
  58         MyEvent.startTime = 20;
  59         event.shouldCommit = "shouldCommit";
  60         MyEvent.set = 30;
  61         event.end();
  62         event.commit();
  63 
  64         // Verify that our methods have not been removed by transformation.
  65         int id = 0;
  66         event.begin(++id);
  67         Asserts.assertEquals(id, staticTestValue, "EventWithBegin failed to set value");
  68         event.end(++id);
  69         Asserts.assertEquals(id, staticTestValue, "EventWithEnd failed to set value");
  70         event.commit(++id);
  71         Asserts.assertEquals(id, staticTestValue, "EventWithCommit failed to set value");
  72         event.shouldCommit(++id);
  73         Asserts.assertEquals(id, staticTestValue, "EventWithShouldCommit failed to set value");
  74         event.set(++id);
  75         Asserts.assertEquals(id, staticTestValue, "EventWithSet failed to set value");
  76 
  77         r.stop();
  78 
  79         Iterator<RecordedEvent> it = Events.fromRecording(r).iterator();
  80         Asserts.assertTrue(it.hasNext(), "No events");
  81         RecordedEvent recordedEvent = it.next();
  82         Asserts.assertTrue(Events.isEventType(recordedEvent, MyEvent.class.getName()));
  83         Events.assertField(recordedEvent, "begin").equal(10L);
  84         Events.assertField(recordedEvent, "shouldCommit").equal("shouldCommit");
  85         Events.assertField(recordedEvent, "startTime");
  86         Events.assertField(recordedEvent, "duration");
  87         Asserts.assertNull(recordedEvent.getEventType().getField("set")); // static not supported
  88         Asserts.assertFalse(it.hasNext(), "More than 1 event");
  89 
  90         r.close();
  91     }
  92 
  93     private static int staticTestValue;
  94 
  95     @SuppressWarnings("unused")
  96     static class MyEvent extends Event {
  97         public long begin;
  98         private Instant duration;
  99         private static int startTime;
 100         protected String shouldCommit;
 101         public static int set;
 102 
 103         public void begin(int testValue) {
 104             staticTestValue = testValue;
 105         }
 106 
 107         public void end(int testValue) {
 108             staticTestValue = testValue;
 109         }
 110 
 111         public void commit(int testValue) {
 112             staticTestValue = testValue;
 113         }
 114 
 115         public void shouldCommit(int testValue) {
 116             staticTestValue = testValue;
 117         }
 118 
 119         public void set(int testValue) {
 120             staticTestValue = testValue;
 121         }
 122     }
 123 
 124 }