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