1/*
2 * Copyright (c) 2015, 2016, 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
24package jdk.test.lib.jittester.jtreg;
25
26import jdk.test.lib.Asserts;
27import jdk.test.lib.process.OutputAnalyzer;
28import jdk.test.lib.process.ProcessTools;
29import jdk.test.lib.Utils;
30import java.io.IOException;
31import java.nio.file.Files;
32import java.nio.file.Path;
33import java.nio.file.Paths;
34import java.util.Arrays;
35import java.util.function.Predicate;
36import java.util.regex.Pattern;
37import java.util.stream.Collectors;
38import java.util.stream.Stream;
39
40public 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}
87