1 /*
   2  * Copyright (c) 2016, 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.cmd;
  27 
  28 import java.io.IOException;
  29 import java.nio.file.Files;
  30 import java.nio.file.Path;
  31 import java.nio.file.Paths;
  32 import java.text.ParseException;
  33 import java.text.SimpleDateFormat;
  34 import java.util.Date;
  35 
  36 import jdk.jfr.Configuration;
  37 import jdk.jfr.Recording;
  38 import jdk.jfr.consumer.RecordingFile;
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 
  41 /*
  42  * @test
  43  * @summary Test jfr split
  44  * @key jfr
  45  * @library /test/lib /test/jdk
  46  * @run main/othervm jdk.jfr.cmd.TestSplit
  47  */
  48 public class TestSplit {
  49 
  50     public static void main(String[] args) throws Exception {
  51         SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
  52         String dateText = formatter.format(new Date());
  53 
  54         Path recordingFileA = Paths.get("many-chunks-A-" + dateText + ".jfr");
  55         Path recordingFileB = Paths.get("many-chunks-B-" + dateText + ".jfr");
  56         makeRecordingWithChunks(6, recordingFileA);
  57         Files.copy(recordingFileA, recordingFileB);
  58 
  59         String fileAText = recordingFileA.toAbsolutePath().toString();
  60         String fileBText = recordingFileB.toAbsolutePath().toString();
  61 
  62         OutputAnalyzer output = ExecuteHelper.run("split");
  63         output.shouldContain("Missing file");
  64 
  65         output = ExecuteHelper.run("split", "--wrongOption1", "..wrongOption2", "..wrongOption3", fileAText);
  66         output.shouldContain("Too many arguments");
  67 
  68         output = ExecuteHelper.run("split", "--wrongOption", fileAText);
  69         output.shouldContain("Unknown option");
  70 
  71         output = ExecuteHelper.run("split", "--wrongOption", "1", fileAText);
  72         output.shouldContain("Unknown option");
  73 
  74         output = ExecuteHelper.run("split", "--maxchunks", "-3", fileAText);
  75         output.shouldContain("Must be at least one chunk per file");
  76 
  77         output = ExecuteHelper.run("split", "--maxchunks", "1000", fileAText);
  78         output.shouldContain("Number of chunks in recording");
  79         output.shouldContain("doesn't exceed max chunks");
  80         output = ExecuteHelper.run("split", fileAText); // maxchunks is 5 by
  81                                                         // default
  82         System.out.println(output.getOutput());
  83         System.out.println(fileAText);
  84         verifyRecording(fileAText.substring(0, fileAText.length() - 4) + "_1.jfr");
  85         verifyRecording(fileAText.substring(0, fileAText.length() - 4) + "_2.jfr");
  86 
  87         output = ExecuteHelper.run("split", "--maxchunks", "2", fileBText);
  88 
  89         verifyRecording(fileBText.substring(0, fileBText.length() - 4) + "_1.jfr");
  90         verifyRecording(fileBText.substring(0, fileBText.length() - 4) + "_2.jfr");
  91         verifyRecording(fileBText.substring(0, fileBText.length() - 4) + "_3.jfr");
  92 
  93         output = ExecuteHelper.run("split", "--maxchunks", "2", fileBText);
  94         output.shouldContain("file with that name already exist");
  95     }
  96 
  97     private static void verifyRecording(String name) throws IOException {
  98         System.out.println("split name " + name);
  99         try (RecordingFile rf = new RecordingFile(Paths.get(name))) {
 100             rf.readEvent();
 101         }
 102     }
 103 
 104     // Will create at least 2 * count + 1 chunks.
 105     private static void makeRecordingWithChunks(int count, Path file) throws IOException, ParseException {
 106         Recording main = new Recording(Configuration.getConfiguration("default"));
 107         main.setToDisk(true);
 108         main.start();
 109         for (int i = 0; i < count; i++) {
 110             Recording r = new Recording();
 111             r.setToDisk(true);
 112             r.start();
 113             r.stop();
 114             r.close();
 115         }
 116         main.stop();
 117         main.dump(file);
 118         main.close();
 119     }
 120 }