1 /*
   2  * Copyright (c) 2016, 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 package jdk.jfr.api.recording.misc;
  26 
  27 import jdk.jfr.Recording;
  28 import jdk.jfr.RecordingState;
  29 import jdk.jfr.consumer.RecordedEvent;
  30 import jdk.test.lib.Asserts;
  31 import jdk.test.lib.jfr.Events;
  32 import jdk.test.lib.jfr.SimpleEvent;
  33 
  34 import java.util.List;
  35 
  36 /**
  37  * @test
  38  * @summary A simple test for Recording.copy()
  39  * @key jfr
  40  *
  41  * @library /lib /
  42  * @run main/othervm jdk.jfr.api.recording.misc.TestRecordingCopy
  43  */
  44 public class TestRecordingCopy {
  45 
  46     private static final int EVENT_ID = 1001;
  47 
  48     public static void main(String[] args) throws Exception {
  49 
  50         Recording original = new Recording();
  51         original.enable(SimpleEvent.class);
  52 
  53         Recording newCopy = original.copy(false);
  54         Asserts.assertEquals(newCopy.getState(), RecordingState.NEW);
  55 
  56         Recording newStoppedCopy = original.copy(true);
  57         Asserts.assertEquals(newStoppedCopy.getState(), RecordingState.NEW);
  58 
  59         original.start();
  60 
  61         SimpleEvent ev = new SimpleEvent();
  62         ev.id = EVENT_ID;
  63         ev.commit();
  64 
  65         // Verify a stopped copy
  66         Recording stoppedCopy = original.copy(true);
  67         Asserts.assertEquals(stoppedCopy.getState(), RecordingState.STOPPED);
  68         assertCopy(stoppedCopy, original);
  69 
  70         // Verify a running copy
  71         Recording runningCopy = original.copy(false);
  72         Asserts.assertEquals(runningCopy.getState(), RecordingState.RUNNING);
  73         assertCopy(runningCopy, original);
  74 
  75         original.stop();
  76 
  77         // Verify a stopped copy of a stopped
  78         stoppedCopy = original.copy(true);
  79         Asserts.assertEquals(stoppedCopy.getState(), RecordingState.STOPPED);
  80         assertCopy(stoppedCopy, original);
  81 
  82         // Clean-up
  83         original.close();
  84         runningCopy.stop();
  85         runningCopy.close();
  86         stoppedCopy.close();
  87     }
  88 
  89     /**
  90      * Verifies the basic assertions about a copied record
  91      */
  92     private static void assertCopy(Recording recording, Recording original) throws Exception {
  93 
  94         Asserts.assertFalse(recording.getId() == original.getId(), "id of a copied record should differ from that of the original");
  95         Asserts.assertFalse(recording.getName().equals(original.getName()), "name of a copied record should differ from that of the original");
  96 
  97         Asserts.assertEquals(recording.getSettings(), original.getSettings());
  98         Asserts.assertEquals(recording.getStartTime(), original.getStartTime());
  99         Asserts.assertEquals(recording.getMaxSize(), original.getMaxSize());
 100         Asserts.assertEquals(recording.getMaxAge(), original.getMaxAge());
 101         Asserts.assertEquals(recording.isToDisk(), original.isToDisk());
 102         Asserts.assertEquals(recording.getDumpOnExit(), original.getDumpOnExit());
 103 
 104         List<RecordedEvent> recordedEvents = Events.fromRecording(recording);
 105         Events.hasEvents(recordedEvents);
 106         Asserts.assertEquals(1, recordedEvents.size(), "Expected exactly one event");
 107 
 108         RecordedEvent re = recordedEvents.get(0);
 109         Asserts.assertEquals(EVENT_ID, re.getValue("id"));
 110     }
 111 
 112 }