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.recording.state; 27 28 import java.time.Duration; 29 import java.util.function.Consumer; 30 31 import jdk.jfr.Recording; 32 import jdk.test.lib.Asserts; 33 34 /** 35 * @test 36 * @key jfr 37 * @summary Test options in different states 38 * @requires vm.hasJFR 39 * @library /test/lib 40 * @run main/othervm jdk.jfr.api.recording.state.TestOptionState 41 */ 42 public class TestOptionState { 43 44 // Name Age Size Dur. Dest. Disk 45 static boolean[] NEW = arrayOf(true, true, true, true, true, true); 46 static boolean[] DELAYED = arrayOf(true, true, true, true, true, true); 47 static boolean[] RUNNING = arrayOf(true, true, true, true, true, false); 48 static boolean[] STOPPED = arrayOf(true, true, true, false, false, false); 49 static boolean[] CLOSED = arrayOf(false, false, false, false, false, false); 50 51 public static void main(String[] args) throws Throwable { 52 Recording r = new Recording(); 53 assertRecordingState(r, NEW); 54 r.scheduleStart(Duration.ofHours(2)); 55 assertRecordingState(r, DELAYED); 56 r.start(); 57 assertRecordingState(r, RUNNING); 58 r.stop(); 59 assertRecordingState(r, STOPPED); 60 r.close(); 61 assertRecordingState(r, CLOSED); 62 } 63 64 private static void assertRecordingState(Recording r, boolean[] states) throws Exception { 65 assertOperation("setName", r, s -> s.setName("Test Name"), states[0]); 66 assertOperation("setMaxAge", r, s -> s.setMaxAge(null), states[1]); 67 assertOperation("setMaxSize", r, s -> s.setMaxSize(0), states[2]); 68 assertOperation("setDuration", r, s -> s.setDuration(null), states[3]); 69 assertOperation("setDestination", r, s -> { 70 try { 71 s.setDestination(null); 72 } catch (IllegalStateException e) { 73 throw e; // rethrow for testing 74 } catch(Exception e) { 75 throw new RuntimeException(e); // should not happen 76 }}, states[4]); 77 assertOperation("setTodisk", r, s -> s.setToDisk(true), states[5]); 78 } 79 80 private static void assertOperation(String opernationName, Recording s, Consumer<Recording> c, boolean ok) { 81 try { 82 c.accept(s); 83 Asserts.assertTrue(ok, opernationName + " should throw ISE when recording is in state " + s.getState()); 84 } catch (IllegalStateException ise) { 85 Asserts.assertFalse(ok, opernationName + " should not throw ISE when recording is in state " + s.getState()); 86 } 87 } 88 89 static boolean[] arrayOf(boolean... array) { 90 return array; 91 } 92 }