1 /*
   2  * Copyright (c) 2015, 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.jcmd;
  27 
  28 import java.io.File;
  29 
  30 import jdk.jfr.FlightRecorder;
  31 import jdk.jfr.Recording;
  32 import jdk.test.lib.Asserts;
  33 import jdk.test.lib.jfr.EventNames;
  34 import jdk.test.lib.process.OutputAnalyzer;
  35 
  36 
  37 
  38 public class JcmdAsserts {
  39 
  40     private static final String NEW_LINE = System.getProperty("line.separator");
  41 
  42     public static void assertJfrNotUsed(OutputAnalyzer output) {
  43         output.shouldMatch("Flight Recorder has not been used");
  44     }
  45 
  46     public static void assertJfrUsed(OutputAnalyzer output) {
  47         output.shouldMatch("Flight Recorder has been used");
  48     }
  49 
  50     public static void assertRecordingDumpedToFile(OutputAnalyzer output, File recording) {
  51         output.shouldContain("Dumped recording");
  52         output.shouldContain(recording.getAbsolutePath());
  53     }
  54 
  55     public static void assertNotAbleToWriteToFile(OutputAnalyzer output) {
  56         output.shouldContain("Could not start recording, not able to write to file");
  57     }
  58 
  59     public static void assertFileNotFoundException(OutputAnalyzer output, String name) {
  60         output.shouldMatch("Could not write recording \"" + name + "\" to file.*");
  61     }
  62 
  63 //    public static void assertNotAbleToSetFilename(OutputAnalyzer output) {
  64 //        output.shouldContain(
  65 //                "Filename can only be set for a recording with a duration, " +
  66 //                "or if dumponexit=true");
  67 //    }
  68 
  69     public static void assertNotAbleToFindSettingsFile(OutputAnalyzer output) {
  70         output.shouldContain("Could not parse setting");
  71     }
  72 
  73     public static void assertNoRecordingsAvailable(OutputAnalyzer output) {
  74         output.shouldContain("No available recordings");
  75     }
  76 
  77     public static void assertRecordingNotExist(OutputAnalyzer output, String name) {
  78         output.shouldContain("Could not find " + name);
  79     }
  80 
  81     public static void assertRecordingNotRunning(OutputAnalyzer output, String name) {
  82         output.shouldNotMatch(".*" + name + ".*running");
  83     }
  84 
  85     public static void assertRecordingIsRunning(OutputAnalyzer output, String name) {
  86         output.shouldMatch(".*" + name + ".*running");
  87     }
  88 
  89     public static void assertRecordingHasStarted(OutputAnalyzer output) {
  90         output.shouldContain("Started recording");
  91     }
  92 
  93     public static void assertCouldNotStartDefaultRecordingWithName(OutputAnalyzer output) {
  94         output.shouldContain(
  95                 "It's not possible to set custom name for the defaultrecording");
  96     }
  97 
  98     public static void assertCouldNotStartDefaultRecording(OutputAnalyzer output) {
  99         output.shouldContain(
 100                 "The only option that can be combined with defaultrecording is settings");
 101     }
 102 
 103     public static void assertRecordingIsUnstarted(OutputAnalyzer output,
 104             String name, String duration) {
 105         output.stdoutShouldMatch("^Recording \\d+: name=" + name
 106                 + " duration=" + duration + " .*\\W{1}unstarted\\W{1}");
 107     }
 108 
 109     public static void assertRecordingIsStopped(OutputAnalyzer output, String name) {
 110         output.stdoutShouldMatch("^Recording \\d+: name=" + name
 111                 + " .*\\W{1}stopped\\W{1}");
 112     }
 113 
 114     public static void assertRecordingIsStopped(OutputAnalyzer output, String name, String duration) {
 115         output.stdoutShouldMatch("^Recording \\d+: name=" + name
 116                 + " duration=" + duration + " .*\\W{1}stopped\\W{1}");
 117     }
 118 
 119     public static void assertStartTimeGreaterOrEqualThanMBeanValue(String name,
 120             long actualStartTime) throws Exception {
 121         Recording recording = findRecording(name);
 122         Asserts.assertNotNull(recording.getStartTime(), "Start time is not set");
 123         Asserts.assertGreaterThanOrEqual(actualStartTime, recording.getStartTime().toEpochMilli());
 124     }
 125 
 126     public static void assertDelayAtLeast1s(OutputAnalyzer output) {
 127         output.shouldContain("Could not start recording, delay must be at least 1 second.");
 128     }
 129 
 130     public static void assertRecordingIsScheduled(OutputAnalyzer output, String name, String delay) {
 131         output.stdoutShouldMatch(
 132                 "^\\s*Recording\\s+" + name + "\\s+scheduled to start in " + delay);
 133     }
 134 
 135     public static void assertMaxSizeEqualsMBeanValue(String name, long maxSize) throws Exception {
 136         Recording recording = findRecording(name);
 137         Asserts.assertEquals(maxSize, recording.getMaxSize());
 138     }
 139 
 140     private static Recording findRecording(String name) {
 141                 for(Recording r : FlightRecorder.getFlightRecorder().getRecordings()) {
 142                         if (r.getName().equals(name)) {
 143                                 return r;
 144                         }
 145                 }
 146                 throw new AssertionError("Could not find recording named " + name);
 147         }
 148 
 149         public static void assertMaxAgeEqualsMBeanValue(String name, long maxAge)
 150             throws Exception {
 151         Recording recording = findRecording(name);
 152         Asserts.assertNotNull(recording, "No recording found");
 153         Asserts.assertEquals(maxAge, recording.getMaxAge().toMillis());
 154     }
 155 
 156     public static void assertDurationEqualsMBeanValue(String name,
 157             long duration) throws Exception {
 158         Recording recording = findRecording(name);
 159         Asserts.assertNotNull(recording, "No recording found");
 160         Asserts.assertEquals(duration, recording.getDuration().toMillis());
 161     }
 162 
 163     public static void assertDurationAtLeast1s(OutputAnalyzer output) {
 164         output.shouldContain("Could not start recording, duration must be at least 1 second.");
 165     }
 166 
 167     public static void assertStoppedRecording(OutputAnalyzer output, String name) {
 168         output.shouldContain("Stopped recording \"" + name + "\"");
 169     }
 170 
 171     public static void assertStoppedAndWrittenTo(OutputAnalyzer output, String name, File file) {
 172         output.shouldMatch("^Stopped recording \"" + name + "\"" + ".*written to:");
 173         output.shouldContain(file.getAbsolutePath());
 174     }
 175 
 176     public static void assertStoppedDefaultRecording(OutputAnalyzer output) {
 177         output.shouldContain("Stopped recording 0");
 178     }
 179 
 180     public static void assertThreadSleepThresholdIsSet(OutputAnalyzer output) throws Exception {
 181         output.stdoutShouldMatch("\\s+\\W{1}" + EventNames.ThreadSleep + "\\W{1}" +
 182                 NEW_LINE + ".*threshold=1 ms.*");
 183     }
 184 
 185     public static void assertMonitorWaitThresholdIsSet(OutputAnalyzer output) throws Exception {
 186         output.stdoutShouldMatch("\\s+\\W{1}" + EventNames.JavaMonitorWait + "\\W{1}" +
 187                 NEW_LINE + ".*threshold=1 ms.*");
 188     }
 189 
 190 }