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