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