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
24package tests;
25
26import java.nio.file.Path;
27
28public class Result {
29    private final int exitCode;
30    private final String message;
31    private final Path imageFile;
32
33    public Result(int exitCode, String message, Path imageFile) {
34        this.exitCode = exitCode;
35        this.message = message;
36        this.imageFile = imageFile;
37    }
38
39    public int getExitCode() {
40        return exitCode;
41    }
42
43    public String getMessage() {
44        return message;
45    }
46
47    public Path getFile() {
48        return imageFile;
49    }
50
51    public void assertFailure() {
52        assertFailure(null);
53    }
54
55    public void assertFailure(String expected) {
56        if (getExitCode() == 0) {
57            System.err.println(getMessage());
58            throw new AssertionError("Failure expected: " + getFile());
59        }
60        if (getExitCode() != 1 && getExitCode() != 2) {
61            System.err.println(getMessage());
62            throw new AssertionError("Abnormal exit: " + getFile());
63        }
64        if (expected != null) {
65            if (expected.isEmpty()) {
66                throw new AssertionError("Expected error is empty");
67            }
68            if (!getMessage().matches(expected) && !getMessage().contains(expected)) {
69                System.err.println(getMessage());
70                throw new AssertionError("Output does not fit regexp: " + expected);
71            }
72        }
73        System.err.println("Failed as expected. " + (expected != null ? expected : ""));
74        System.err.println(getMessage());
75    }
76
77    public Path assertSuccess() {
78        if (getExitCode() != 0) {
79            System.err.println(getMessage());
80            throw new AssertionError("Unexpected failure: " + getExitCode());
81        }
82        return getFile();
83    }
84}
85