1 /*
   2  * Copyright (c) 2015, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.jittester.jtreg;
  25 
  26 import jdk.test.lib.Asserts;
  27 import jdk.test.lib.OutputAnalyzer;
  28 import jdk.test.lib.ProcessTools;
  29 import jdk.test.lib.Utils;
  30 import java.io.IOException;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 import java.util.Arrays;
  35 import java.util.function.Predicate;
  36 import java.util.regex.Pattern;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.Stream;
  39 
  40 public class JitTesterDriver {
  41 
  42     public static void main(String[] args) {
  43         if (args.length < 1) {
  44             throw new IllegalArgumentException(
  45                     "[TESTBUG]: wrong number of argument : " + args.length
  46                     + ". Expected at least 1 argument -- jit-tester test name.");
  47         }
  48         OutputAnalyzer oa;
  49         try {
  50             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, args);
  51             oa = new OutputAnalyzer(pb.start());
  52         } catch (Exception e) {
  53             throw new Error("Unexpected exception on test jvm start :" + e, e);
  54         }
  55 
  56         String name = args[args.length - 1];
  57         Pattern splitOut = Pattern.compile("\\n"); // tests use \n only in stdout
  58         Pattern splitErr = Pattern.compile("\\r?\\n"); // can handle both \r\n and \n
  59         Path testDir = Paths.get(Utils.TEST_SRC);
  60         String goldOut = formatOutput(streamGoldFile(testDir, name, "out"));
  61         String anlzOut = formatOutput(Arrays.stream(splitOut.split(oa.getStdout())));
  62         Asserts.assertEQ(anlzOut, goldOut, "Actual stdout isn't equal to golden one");
  63         String goldErr = formatOutput(streamGoldFile(testDir, name, "err"));
  64         String anlzErr = formatOutput(Arrays.stream(splitErr.split(oa.getStderr())));
  65         Asserts.assertEQ(anlzErr, goldErr, "Actual stderr isn't equal to golden one");
  66 
  67         int exitValue = Integer.parseInt(streamGoldFile(testDir, name, "exit").findFirst().get());
  68         oa.shouldHaveExitValue(exitValue);
  69     }
  70 
  71     private static String formatOutput(Stream<String> stream) {
  72         String result = stream.collect(Collectors.joining(Utils.NEW_LINE));
  73         if (result.length() > 0) {
  74             result += Utils.NEW_LINE;
  75         }
  76         return result;
  77     }
  78 
  79     private static Stream<String> streamGoldFile(Path dir, String name, String suffix) {
  80         try {
  81             return Files.lines(dir.resolve(name + ".gold." + suffix));
  82         } catch (IOException e) {
  83             throw new Error(String.format("Can't read golden %s for %s : %s", suffix, name, e), e);
  84         }
  85     }
  86 }