1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * The contents of this file are subject to the terms of either the Universal Permissive License 7 * v 1.0 as shown at http://oss.oracle.com/licenses/upl 8 * 9 * or the following license: 10 * 11 * Redistribution and use in source and binary forms, with or without modification, are permitted 12 * provided that the following conditions are met: 13 * 14 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions 15 * and the following disclaimer. 16 * 17 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 18 * conditions and the following disclaimer in the documentation and/or other materials provided with 19 * the distribution. 20 * 21 * 3. Neither the name of the copyright holder nor the names of its contributors may be used to 22 * endorse or promote products derived from this software without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 26 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 31 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 package org.openjdk.jmc.rjmx.services.jfr.test; 34 35 import static org.junit.Assert.assertTrue; 36 37 import java.io.IOException; 38 import java.io.InputStream; 39 40 import org.junit.Test; 41 import org.openjdk.jmc.common.io.IOToolkit; 42 import org.openjdk.jmc.common.unit.IConstrainedMap; 43 import org.openjdk.jmc.common.unit.IDescribedMap; 44 import org.openjdk.jmc.flightrecorder.configuration.events.EventOptionID; 45 import org.openjdk.jmc.flightrecorder.configuration.recording.RecordingOptionsBuilder; 46 import org.openjdk.jmc.rjmx.ConnectionDescriptorBuilder; 47 import org.openjdk.jmc.rjmx.IConnectionDescriptor; 48 import org.openjdk.jmc.rjmx.IConnectionHandle; 49 import org.openjdk.jmc.rjmx.IServerHandle; 50 import org.openjdk.jmc.rjmx.services.jfr.IFlightRecorderService; 51 import org.openjdk.jmc.rjmx.services.jfr.IRecordingDescriptor; 52 import org.openjdk.jmc.rjmx.test.RjmxTestCase; 53 54 /** 55 * This test suite is supposed to test the example code that we ship with the documentation for the 56 * org.openjdk.jmc.rjmx.services.jfr bundle. That is, for each code example included in 57 * org.openjdk.jmc.rjmx.services.jfr/src/org/openjdk/jmc/rjmx/services/jfr/package-info.java, there 58 * should be a test method in here with a verbatim copy of that code. 59 */ 60 // NOTE: If you change the verbatim test YOU MUST update the corresponding package-info.java. 61 @SuppressWarnings("nls") 62 public class JfrPackageExampleTest extends RjmxTestCase { 63 64 @Test 65 public void testPackageExample1FunctionalityVerbatim() throws Exception { 66 IConnectionDescriptor descriptor = new ConnectionDescriptorBuilder().hostName("localhost").port(0).build(); 67 IServerHandle serverHandle = IServerHandle.create(descriptor); 68 IConnectionHandle handle = serverHandle.connect("Get JFR recording info"); 69 try { 70 IFlightRecorderService jfr = handle.getServiceOrThrow(IFlightRecorderService.class); 71 for (IRecordingDescriptor desc : jfr.getAvailableRecordings()) { 72 System.out.println(desc.getName()); 73 } 74 } finally { 75 IOToolkit.closeSilently(handle); 76 } 77 } 78 79 @Test 80 public void testPackageExample2FunctionalityVerbatim() throws Exception { 81 IConnectionDescriptor descriptor = new ConnectionDescriptorBuilder().hostName("localhost").port(0).build(); 82 IServerHandle serverHandle = IServerHandle.create(descriptor); 83 IConnectionHandle handle = serverHandle.connect("Start time bound flight recording"); 84 try { 85 IFlightRecorderService jfr = handle.getServiceOrThrow(IFlightRecorderService.class); 86 87 long duration = 5000; 88 IDescribedMap<EventOptionID> defaultEventOptions = jfr.getDefaultEventOptions(); 89 IConstrainedMap<String> recordingOptions = new RecordingOptionsBuilder(jfr).name("MyRecording") 90 .duration(duration).build(); 91 IRecordingDescriptor recording = jfr.start(recordingOptions, defaultEventOptions); 92 Thread.sleep(duration); 93 while (recording.getState() != IRecordingDescriptor.RecordingState.STOPPED) { 94 Thread.sleep(1000); 95 recording = jfr.getUpdatedRecordingDescription(recording); 96 } 97 InputStream is = jfr.openStream(recording, true); 98 writeStreamToFile(is); 99 } finally { 100 IOToolkit.closeSilently(handle); 101 } 102 } 103 104 private void writeStreamToFile(InputStream in) throws IOException { 105 // Just read and count bytes in the test... 106 int count = 0; 107 try { 108 byte[] buf = new byte[1024 * 1024]; 109 int len; 110 while ((len = in.read(buf)) > 0) { 111 count += len; 112 } 113 } finally { 114 in.close(); 115 } 116 assertTrue(count > 0); 117 } 118 }