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