2014-07-04 10:52:22 +01:00
|
|
|
/*
|
2015-03-25 10:29:14 +03:00
|
|
|
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
|
2014-07-04 10:52:22 +01:00
|
|
|
* 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;
|
2014-08-12 13:24:40 +04:00
|
|
|
import java.util.*;
|
2014-11-12 15:16:35 +02:00
|
|
|
import java.util.stream.Collectors;
|
2014-07-04 10:52:22 +01:00
|
|
|
|
2014-08-12 13:24:40 +04:00
|
|
|
/**
|
|
|
|
* This class accumulates test results. Test results can be checked with method @{code checkStatus}.
|
|
|
|
*/
|
2014-07-04 10:52:22 +01:00
|
|
|
public class TestResult extends TestBase {
|
|
|
|
|
|
|
|
private final List<Info> testCases;
|
|
|
|
|
|
|
|
public TestResult() {
|
|
|
|
testCases = new ArrayList<>();
|
|
|
|
testCases.add(new Info("Global test info"));
|
|
|
|
}
|
|
|
|
|
2014-08-12 13:24:40 +04:00
|
|
|
/**
|
|
|
|
* Adds new test case info.
|
|
|
|
*
|
|
|
|
* @param info the information about test case
|
|
|
|
*/
|
|
|
|
public void addTestCase(String info) {
|
|
|
|
testCases.add(new Info(info));
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2014-08-12 13:24:40 +04:00
|
|
|
private String errorMessage() {
|
2014-07-04 10:52:22 +01:00
|
|
|
return testCases.stream().filter(Info::isFailed)
|
2014-11-12 15:16:35 +02:00
|
|
|
.map(tc -> String.format("Failure in test case:\n%s\n%s", tc.info(), tc.getMessage()))
|
|
|
|
.collect(Collectors.joining("\n"));
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:16:35 +02:00
|
|
|
public boolean checkEquals(Object actual, Object expected, String message) {
|
|
|
|
echo("Testing : " + message);
|
|
|
|
if (!Objects.equals(actual, expected)) {
|
2015-03-25 10:29:14 +03:00
|
|
|
getLastTestCase().addAssert(String.format("%s\n" +
|
|
|
|
"Expected: %s,\n" +
|
|
|
|
" Got: %s", message, expected, actual));
|
2014-11-12 15:16:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:16:35 +02:00
|
|
|
public boolean checkNull(Object actual, String message) {
|
|
|
|
return checkEquals(actual, null, message);
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:16:35 +02:00
|
|
|
public boolean checkNotNull(Object actual, String message) {
|
|
|
|
echo("Testing : " + message);
|
|
|
|
if (Objects.isNull(actual)) {
|
2015-03-25 10:29:14 +03:00
|
|
|
getLastTestCase().addAssert(message + " : Expected not null value");
|
2014-11-12 15:16:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:16:35 +02:00
|
|
|
public boolean checkFalse(boolean actual, String message) {
|
|
|
|
return checkEquals(actual, false, message);
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:16:35 +02:00
|
|
|
public boolean checkTrue(boolean actual, String message) {
|
|
|
|
return checkEquals(actual, true, message);
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 15:16:35 +02:00
|
|
|
public boolean checkContains(Set<?> found, Set<?> expected, String message) {
|
2014-08-12 13:24:40 +04:00
|
|
|
Set<?> copy = new HashSet<>(expected);
|
|
|
|
copy.removeAll(found);
|
2015-03-25 10:29:14 +03:00
|
|
|
if (!found.containsAll(expected)) {
|
|
|
|
return checkTrue(false, message + " FAIL : not found elements : " + copy);
|
|
|
|
} else {
|
|
|
|
return checkTrue(true, message + " PASS : all elements found");
|
|
|
|
}
|
2014-08-12 13:24:40 +04:00
|
|
|
}
|
|
|
|
|
2014-07-04 10:52:22 +01:00
|
|
|
public void addFailure(Throwable th) {
|
2014-08-12 13:24:40 +04:00
|
|
|
testCases.get(testCases.size() - 1).addFailure(th);
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private Info getLastTestCase() {
|
|
|
|
if (testCases.size() == 1) {
|
|
|
|
throw new IllegalStateException("Test case should be created");
|
|
|
|
}
|
|
|
|
return testCases.get(testCases.size() - 1);
|
|
|
|
}
|
|
|
|
|
2014-08-12 13:24:40 +04:00
|
|
|
/**
|
2014-11-12 15:16:35 +02:00
|
|
|
* Throws {@code TestFailedException} if one of the checks are failed
|
2014-08-12 13:24:40 +04:00
|
|
|
* or an exception occurs. Prints error message of failed test cases.
|
|
|
|
*
|
2014-11-12 15:16:35 +02:00
|
|
|
* @throws TestFailedException if one of the checks are failed
|
2014-08-12 13:24:40 +04:00
|
|
|
* or an exception occurs
|
|
|
|
*/
|
2014-07-04 10:52:22 +01:00
|
|
|
public void checkStatus() throws TestFailedException {
|
|
|
|
if (testCases.stream().anyMatch(Info::isFailed)) {
|
|
|
|
echo(errorMessage());
|
|
|
|
throw new TestFailedException("Test failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class Info {
|
|
|
|
|
|
|
|
private final String info;
|
2015-03-25 10:29:14 +03:00
|
|
|
private final List<String> asserts;
|
2014-07-04 10:52:22 +01:00
|
|
|
private final List<Throwable> errors;
|
|
|
|
|
|
|
|
private Info(String info) {
|
|
|
|
this.info = info;
|
|
|
|
asserts = new ArrayList<>();
|
|
|
|
errors = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String info() {
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isFailed() {
|
|
|
|
return !asserts.isEmpty() || !errors.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addFailure(Throwable th) {
|
|
|
|
errors.add(th);
|
|
|
|
printf("[ERROR] : %s\n", getStackTrace(th));
|
|
|
|
}
|
|
|
|
|
2015-03-25 10:29:14 +03:00
|
|
|
public void addAssert(String e) {
|
2014-11-12 15:16:35 +02:00
|
|
|
asserts.add(e);
|
2015-03-25 10:29:14 +03:00
|
|
|
printf("[ASSERT] : %s\n", e);
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2014-08-12 13:24:40 +04:00
|
|
|
public String getMessage() {
|
2015-03-25 10:29:14 +03:00
|
|
|
return (asserts.size() > 0 ? getAssertMessages(asserts) + "\n" : "")
|
|
|
|
+ getErrorMessages(errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAssertMessages(List<String> list) {
|
|
|
|
return list.stream()
|
|
|
|
.map(message -> String.format("[ASSERT] : %s", message))
|
|
|
|
.collect(Collectors.joining("\n"));
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 10:29:14 +03:00
|
|
|
public String getErrorMessages(List<? extends Throwable> list) {
|
2014-11-12 15:16:35 +02:00
|
|
|
return list.stream()
|
2015-03-25 10:29:14 +03:00
|
|
|
.map(throwable -> String.format("[ERROR] : %s", getStackTrace(throwable)))
|
2014-11-12 15:16:35 +02:00
|
|
|
.collect(Collectors.joining("\n"));
|
2014-07-04 10:52:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|