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