TestResult.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2014, 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 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); 86 } else { 87 return checkTrue(true, message + " PASS : all elements found"); 88 } 89 } 90 91 public void addFailure(Throwable th) { 92 if (testCasesInfo.isEmpty()) { 93 testCasesInfo.add(new Info("Dummy info")); 94 } 95 getLastTestCase().addFailure(th); 96 } 97 98 private Info getLastTestCase() { 99 if (testCasesInfo.isEmpty()) { 100 throw new IllegalStateException("Test case should be created"); 101 } 102 return testCasesInfo.get(testCasesInfo.size() - 1); 103 } 104 105 /** 106 * Throws {@code TestFailedException} if one of the checks are failed 107 * or an exception occurs. Prints error message of failed test cases. 108 * 109 * @throws TestFailedException if one of the checks are failed 110 * or an exception occurs 111 */ 112 public void checkStatus() throws TestFailedException { 113 int passed = 0; 114 int failed = 0; 115 for (Info testCaseInfo : testCasesInfo) { 116 if (testCaseInfo.isFailed()) { 117 String info = testCaseInfo.info().replace("\n", LINE_SEPARATOR); 118 String errorMessage = testCaseInfo.getMessage().replace("\n", LINE_SEPARATOR); 119 System.err.printf("Failure in test case:%n%s%n%s%n", info, errorMessage); 120 ++failed; 121 } else { 122 ++passed; 123 } 124 } 125 System.err.printf("Test cases: passed: %d, failed: %d, total: %d.%n", passed, failed, passed + failed); 126 if (failed > 0) { 127 throw new TestFailedException("Test failed"); 128 } 129 if (passed + failed == 0) { 130 throw new TestFailedException("Test cases were not found"); 131 } 132 } 133 134 @Override 135 public void printf(String template, Object... args) { 136 getLastTestCase().printf(template, args); 137 } 138 139 private static class Info { 140 141 private final String info; 142 private final StringWriter writer; 143 private boolean isFailed; 144 145 private Info(String info) { 146 this.info = info; 147 writer = new StringWriter(); 148 } 149 150 public String info() { 151 return info; 152 } 153 154 public boolean isFailed() { 155 return isFailed; 156 } 157 158 public void printf(String template, Object... args) { 159 writer.write(String.format(template, args)); 160 } 161 162 public void addFailure(Throwable th) { 163 isFailed = true; 164 printf("[ERROR] : %s\n", getStackTrace(th)); 165 } 166 167 public void addAssert(String e) { 168 isFailed = true; 169 printf("[ASSERT] : %s\n", e); 170 } 171 172 public String getMessage() { 173 return writer.toString(); 174 } 175 176 public String getStackTrace(Throwable throwable) { 177 StringWriter stringWriter = new StringWriter(); 178 try (PrintWriter printWriter = new PrintWriter(stringWriter)) { 179 throwable.printStackTrace(printWriter); 180 } 181 return stringWriter.toString(); 182 } 183 } 184 185 public static class TestFailedException extends Exception { 186 public TestFailedException(String message) { 187 super(message); 188 } 189 } 190} 191