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.misc;
  27 
  28 import static jdk.test.lib.Asserts.assertEquals;
  29 import static jdk.test.lib.Asserts.assertFalse;
  30 import static jdk.test.lib.Asserts.assertNotEquals;
  31 import static jdk.test.lib.Asserts.assertNotNull;
  32 import static jdk.test.lib.Asserts.assertNull;
  33 import static jdk.test.lib.Asserts.assertTrue;
  34 
  35 import java.nio.file.Path;
  36 import java.nio.file.Paths;
  37 import java.time.Duration;
  38 import java.util.Map;
  39 
  40 import jdk.jfr.Recording;
  41 import jdk.jfr.RecordingState;
  42 
  43 /*
  44  * @test
  45  * @summary Basic tests for Recording
  46  * @key jfr
  47  * @library /test/lib
  48  * @run main/othervm jdk.jfr.api.recording.misc.TestRecordingBase
  49  */
  50 public class TestRecordingBase {
  51 
  52     public static void main(String[] args) throws Throwable {
  53         testUninitialized();
  54         testUniqueIdentifier();
  55         testSetGetName();
  56         testSetGetDuration();
  57         testSetGetMaxAge();
  58         testSetGetDestination();
  59         testSetGetDumpOnExit();
  60         testSetGetToDisk();
  61         testSetGetToMaxSize();
  62         testGetSettings();
  63     }
  64 
  65     public static void testUninitialized() throws Throwable {
  66        Recording r = new Recording();
  67        assertNull(r.getDuration(), "Wrong uninitialized duration");
  68        assertNull(r.getStartTime(), "Wrong uninitialized startTime");
  69        assertNull(r.getStopTime(), "Wrong uninitialized stopTime");
  70        assertNull(r.getDestination(), "Wrong uninitialized destination");
  71        assertNull(r.getMaxAge(), "Wrong uninitialized maxAge");
  72        assertEquals(0L, r.getMaxSize(), "Wrong uninitialized maxSize"); // TODO: Should this be null? Why Long if never null?
  73        assertEquals(0L, r.getSize(), "Wrong uninitialized size");
  74        assertNotNull(r.getName(), "Uninitialized name should not be null");
  75        assertFalse(r.getName().isEmpty(), "Uninitialized name should not be empty");
  76        assertEquals(r.getState(), RecordingState.NEW, "Wrong uninitialized state");
  77        assertTrue(r.getSettings().isEmpty(), "Uninitialized settings should be empty");
  78        r.close();
  79     }
  80 
  81     public static void testUniqueIdentifier() throws Throwable {
  82         Recording r1 = new Recording();
  83         Recording r2 = new Recording();
  84         assertNotEquals(r1.getId(), r2.getId(), "Same identifier");
  85         r1.close();
  86         r2.close();
  87     }
  88 
  89     public static void testSetGetName() throws Throwable {
  90         Recording r = new Recording();
  91         final String name = "TestName";
  92         r.setName(name);
  93         assertEquals(name, r.getName(), "Wrong set/get name");
  94         r.close();
  95     }
  96 
  97     public static void testSetGetDuration() throws Throwable {
  98         Recording r = new Recording();
  99         final Duration duration = Duration.ofSeconds(60).plusMillis(50);
 100         r.setDuration(duration);
 101         assertEquals(duration, r.getDuration(), "Wrong set/get duration");
 102         r.close();
 103     }
 104 
 105     public static void testSetGetMaxAge() throws Throwable {
 106         Recording r = new Recording();
 107         final Duration maxAge = Duration.ofSeconds(60).plusMillis(50);
 108         r.setMaxAge(maxAge);
 109         assertEquals(maxAge, r.getMaxAge(), "Wrong set/get maxAge");
 110         r.close();
 111     }
 112 
 113     public static void testSetGetDestination() throws Throwable {
 114         Recording r = new Recording();
 115         final Path destination = Paths.get(".", "testSetGetDestination.jfr");
 116         r.setDestination(destination);
 117         assertEquals(destination, r.getDestination(), "Wrong set/get destination");
 118         r.close();
 119     }
 120 
 121     public static void testSetGetDumpOnExit() throws Throwable {
 122         Recording r = new Recording();
 123         r.setDumpOnExit(true);
 124         assertTrue(r.getDumpOnExit(), "Wrong set/get dumpOnExit true");
 125         r.setDumpOnExit(false);
 126         assertFalse(r.getDumpOnExit(), "Wrong set/get dumpOnExit false");
 127         r.close();
 128     }
 129 
 130     public static void testSetGetToDisk() throws Throwable {
 131         Recording r = new Recording();
 132         r.setToDisk(true);
 133         assertTrue(r.isToDisk(), "Wrong set/get isToDisk true");
 134         r.setToDisk(false);
 135         assertFalse(r.isToDisk(), "Wrong set/get isToDisk false");
 136         r.close();
 137     }
 138 
 139     public static void testSetGetToMaxSize() throws Throwable {
 140         Recording r = new Recording();
 141         final long maxSize = 10000000;
 142         r.setMaxSize(maxSize);
 143         assertEquals(maxSize, r.getMaxSize(), "Wrong set/get maxSize");
 144         r.close();
 145     }
 146 
 147     public static void testGetSettings() throws Throwable {
 148         String eventPath = "my/test/enabledPath";
 149         String settingName = "myTestSetting";
 150         String settingValue = "myTestValue";
 151 
 152         Recording r = new Recording();
 153         r.enable(eventPath).with(settingName, settingValue);
 154 
 155         boolean isEnabledPathFound = false;
 156         boolean isSettingFound = false;
 157         Map<String, String> settings = r.getSettings();
 158         for (String name : settings.keySet()) {
 159             System.out.println("name=" + name + ", value=" + settings.get(name));
 160             if (name.contains(eventPath) && name.contains("#enabled")) {
 161                 isEnabledPathFound = true;
 162                 assertEquals("true", settings.get(name), "Wrong value for enabled path: " + name);
 163             }
 164             if  (name.contains(eventPath) && name.contains(settingName)) {
 165                 isSettingFound = true;
 166                 assertEquals(settingValue, settings.get(name), "Wrong value for setting: " + name);
 167             }
 168         }
 169         assertTrue(isEnabledPathFound, "Enabled path not found in settings");
 170         assertTrue(isSettingFound, "Test setting not found in settings");
 171         r.close();
 172     }
 173 
 174 }