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