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.flightrecorder.test.util; 34 35 import java.io.File; 36 import java.io.FileOutputStream; 37 import java.io.IOException; 38 import java.io.InputStream; 39 import java.io.OutputStream; 40 41 import org.openjdk.jmc.common.io.IOToolkit; 42 import org.openjdk.jmc.common.item.IItemCollection; 43 import org.openjdk.jmc.common.test.TestToolkit; 44 import org.openjdk.jmc.common.test.io.IOResourceSet; 45 import org.openjdk.jmc.flightrecorder.CouldNotLoadRecordingException; 46 import org.openjdk.jmc.flightrecorder.JfrLoaderToolkit; 47 48 @SuppressWarnings("nls") 49 public class RecordingToolkit { 50 static final String RECORDING_TEXT_FILE_CHARSET = "UTF-8"; 51 private static final String RECORDINGS_DIRECTORY = "recordings"; 52 private static final String RECORDINGS_INDEXFILE = "index.txt"; 53 54 /** 55 * Return the directory where the recording files reside. 56 * 57 * @return the recording file directory 58 * @throws IOException 59 * if the directory could not be found 60 */ 61 public static File getRecordingDirectory() throws IOException { 62 return TestToolkit.getProjectDirectory(RecordingToolkit.class, RECORDINGS_DIRECTORY); 63 } 64 65 public static IOResourceSet getRecordings() throws IOException { 66 return TestToolkit.getResourcesInDirectory(RecordingToolkit.class, RECORDINGS_DIRECTORY, RECORDINGS_INDEXFILE); 67 } 68 69 public static IItemCollection getFlightRecording(IOResourceSet resourceSet) 70 throws IOException, CouldNotLoadRecordingException { 71 File tmpRecording = createResultFile("recordingTest", "tmp_recording", true); 72 InputStream is = resourceSet.getResource(0).open(); 73 OutputStream os = new FileOutputStream(tmpRecording); 74 int read = 0; 75 byte[] tmp = new byte[4096]; 76 while ((read = is.read(tmp)) > 0) { 77 os.write(tmp, 0, read); 78 } 79 IOToolkit.closeSilently(os); 80 IOToolkit.closeSilently(is); 81 return JfrLoaderToolkit.loadEvents(tmpRecording); 82 } 83 84 public static File createResultFile(String prefix, String suffix, boolean deleteTempOnExit) throws IOException { 85 String resultDir = System.getProperty("results.dir"); 86 File resultFile; 87 if (resultDir != null) { 88 resultFile = new File(resultDir, prefix + '.' + System.currentTimeMillis() + '.' + suffix); 89 } else { 90 resultFile = File.createTempFile(prefix, suffix); 91 if (deleteTempOnExit) { 92 resultFile.deleteOnExit(); 93 } 94 } 95 return resultFile; 96 } 97 98 }