1/*
2 * Copyright (c) 2014, 2017, 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
24import java.io.PrintWriter;
25import java.io.StringWriter;
26import java.util.*;
27
28/**
29 * This class accumulates test results. Test results can be checked with method @{code checkStatus}.
30 */
31public class TestResult extends TestBase {
32
33    private final List<Info> testCasesInfo;
34
35    public TestResult() {
36        testCasesInfo = new ArrayList<>();
37    }
38
39    /**
40     * Adds new test case info.
41     *
42     * @param info the information about test case
43     */
44    public void addTestCase(String info) {
45        System.err.println("Test case: " + info);
46        testCasesInfo.add(new Info(info));
47    }
48
49    public boolean checkEquals(Object actual, Object expected, String message) {
50        echo("Testing : " + message);
51        if (!Objects.equals(actual, expected)) {
52            getLastTestCase().addAssert(String.format("%s\n" +
53                    "Expected: %s,\n" +
54                    "     Got: %s", message, expected, actual));
55            return false;
56        }
57        return true;
58    }
59
60    public boolean checkNull(Object actual, String message) {
61        return checkEquals(actual, null, message);
62    }
63
64    public boolean checkNotNull(Object actual, String message) {
65        echo("Testing : " + message);
66        if (Objects.isNull(actual)) {
67            getLastTestCase().addAssert(message + " : Expected not null value");
68            return false;
69        }
70        return true;
71    }
72
73    public boolean checkFalse(boolean actual, String message) {
74        return checkEquals(actual, false, message);
75    }
76
77    public boolean checkTrue(boolean actual, String message) {
78        return checkEquals(actual, true, message);
79    }
80
81    public boolean checkContains(Collection<?> found, Collection<?> expected, String message) {
82        Set<?> copy = new HashSet<>(expected);
83        copy.removeAll(found);
84        if (!found.containsAll(expected)) {
85            return checkTrue(false, message + " FAIL : not found elements : " + copy + "\n" +
86                    "Actual: " + found);
87        } else {
88            return checkTrue(true, message + " PASS : all elements found");
89        }
90    }
91
92    public void addFailure(Throwable th) {
93        if (testCasesInfo.isEmpty()) {
94            testCasesInfo.add(new Info("Dummy info"));
95        }
96        getLastTestCase().addFailure(th);
97    }
98
99    private Info getLastTestCase() {
100        if (testCasesInfo.isEmpty()) {
101            throw new IllegalStateException("Test case should be created");
102        }
103        return testCasesInfo.get(testCasesInfo.size() - 1);
104    }
105
106    /**
107     * Throws {@code TestFailedException} if one of the checks are failed
108     * or an exception occurs. Prints error message of failed test cases.
109     *
110     * @throws TestFailedException if one of the checks are failed
111     *                             or an exception occurs
112     */
113    public void checkStatus() throws TestFailedException {
114        int passed = 0;
115        int failed = 0;
116        for (Info testCaseInfo : testCasesInfo) {
117            if (testCaseInfo.isFailed()) {
118                String info = testCaseInfo.info().replace("\n", LINE_SEPARATOR);
119                String errorMessage = testCaseInfo.getMessage().replace("\n", LINE_SEPARATOR);
120                System.err.printf("Failure in test case:%n%s%n%s%n", info, errorMessage);
121                ++failed;
122            } else {
123                ++passed;
124            }
125        }
126        System.err.printf("Test cases: passed: %d, failed: %d, total: %d.%n", passed, failed, passed + failed);
127        if (failed > 0) {
128            throw new TestFailedException("Test failed");
129        }
130        if (passed + failed == 0) {
131            throw new TestFailedException("Test cases were not found");
132        }
133    }
134
135    @Override
136    public void printf(String template, Object... args) {
137        getLastTestCase().printf(template, args);
138    }
139
140    private static class Info {
141
142        private final String info;
143        private final StringWriter writer;
144        private boolean isFailed;
145
146        private Info(String info) {
147            this.info = info;
148            writer = new StringWriter();
149        }
150
151        public String info() {
152            return info;
153        }
154
155        public boolean isFailed() {
156            return isFailed;
157        }
158
159        public void printf(String template, Object... args) {
160            writer.write(String.format(template, args));
161        }
162
163        public void addFailure(Throwable th) {
164            isFailed = true;
165            printf("[ERROR] : %s\n", getStackTrace(th));
166        }
167
168        public void addAssert(String e) {
169            isFailed = true;
170            printf("[ASSERT] : %s\n", e);
171        }
172
173        public String getMessage() {
174            return writer.toString();
175        }
176
177        public String getStackTrace(Throwable throwable) {
178            StringWriter stringWriter = new StringWriter();
179            try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
180                throwable.printStackTrace(printWriter);
181            }
182            return stringWriter.toString();
183        }
184    }
185
186    public static class TestFailedException extends Exception {
187        public TestFailedException(String message) {
188            super(message);
189        }
190    }
191}
192