1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
/**
* This class accumulates test results. Test results can be checked with method @{code checkStatus}.
*/
public class TestResult extends TestBase {
private final List<Info> testCasesInfo;
public TestResult() {
testCasesInfo = new ArrayList<>();
}
/**
* Adds new test case info.
*
* @param info the information about test case
*/
public void addTestCase(String info) {
System.err.println("Test case: " + info);
testCasesInfo.add(new Info(info));
}
public boolean checkEquals(Object actual, Object expected, String message) {
echo("Testing : " + message);
if (!Objects.equals(actual, expected)) {
getLastTestCase().addAssert(String.format("%s\n" +
"Expected: %s,\n" +
" Got: %s", message, expected, actual));
return false;
}
return true;
}
public boolean checkNull(Object actual, String message) {
return checkEquals(actual, null, message);
}
public boolean checkNotNull(Object actual, String message) {
echo("Testing : " + message);
if (Objects.isNull(actual)) {
getLastTestCase().addAssert(message + " : Expected not null value");
return false;
}
return true;
}
public boolean checkFalse(boolean actual, String message) {
return checkEquals(actual, false, message);
}
public boolean checkTrue(boolean actual, String message) {
return checkEquals(actual, true, message);
}
public boolean checkContains(Collection<?> found, Collection<?> expected, String message) {
Set<?> copy = new HashSet<>(expected);
copy.removeAll(found);
if (!found.containsAll(expected)) {
return checkTrue(false, message + " FAIL : not found elements : " + copy + "\n" +
"Actual: " + found);
} else {
return checkTrue(true, message + " PASS : all elements found");
}
}
public void addFailure(Throwable th) {
if (testCasesInfo.isEmpty()) {
testCasesInfo.add(new Info("Dummy info"));
}
getLastTestCase().addFailure(th);
}
private Info getLastTestCase() {
if (testCasesInfo.isEmpty()) {
throw new IllegalStateException("Test case should be created");
}
return testCasesInfo.get(testCasesInfo.size() - 1);
}
/**
* Throws {@code TestFailedException} if one of the checks are failed
* or an exception occurs. Prints error message of failed test cases.
*
* @throws TestFailedException if one of the checks are failed
* or an exception occurs
*/
public void checkStatus() throws TestFailedException {
int passed = 0;
int failed = 0;
for (Info testCaseInfo : testCasesInfo) {
if (testCaseInfo.isFailed()) {
String info = testCaseInfo.info().replace("\n", LINE_SEPARATOR);
String errorMessage = testCaseInfo.getMessage().replace("\n", LINE_SEPARATOR);
System.err.printf("Failure in test case:%n%s%n%s%n", info, errorMessage);
++failed;
} else {
++passed;
}
}
System.err.printf("Test cases: passed: %d, failed: %d, total: %d.%n", passed, failed, passed + failed);
if (failed > 0) {
throw new TestFailedException("Test failed");
}
if (passed + failed == 0) {
throw new TestFailedException("Test cases were not found");
}
}
@Override
public void printf(String template, Object... args) {
getLastTestCase().printf(template, args);
}
private static class Info {
private final String info;
private final StringWriter writer;
private boolean isFailed;
private Info(String info) {
this.info = info;
writer = new StringWriter();
}
public String info() {
return info;
}
public boolean isFailed() {
return isFailed;
}
public void printf(String template, Object... args) {
writer.write(String.format(template, args));
}
public void addFailure(Throwable th) {
isFailed = true;
printf("[ERROR] : %s\n", getStackTrace(th));
}
public void addAssert(String e) {
isFailed = true;
printf("[ASSERT] : %s\n", e);
}
public String getMessage() {
return writer.toString();
}
public String getStackTrace(Throwable throwable) {
StringWriter stringWriter = new StringWriter();
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
throwable.printStackTrace(printWriter);
}
return stringWriter.toString();
}
}
public static class TestFailedException extends Exception {
public TestFailedException(String message) {
super(message);
}
}
}
|