01: package org.bouncycastle.util.test;
02:
03: import java.io.PrintStream;
04:
05: import org.bouncycastle.util.Arrays;
06:
07: public abstract class SimpleTest implements Test {
08: public abstract String getName();
09:
10: private TestResult success() {
11: return SimpleTestResult.successful(this , "Okay");
12: }
13:
14: protected void fail(String message) {
15: throw new TestFailedException(SimpleTestResult.failed(this ,
16: message));
17: }
18:
19: protected void fail(String message, Throwable throwable) {
20: throw new TestFailedException(SimpleTestResult.failed(this ,
21: message, throwable));
22: }
23:
24: protected void fail(String message, Object expected, Object found) {
25: throw new TestFailedException(SimpleTestResult.failed(this ,
26: message, expected, found));
27: }
28:
29: protected boolean areEqual(byte[] a, byte[] b) {
30: return Arrays.areEqual(a, b);
31: }
32:
33: public TestResult perform() {
34: try {
35: performTest();
36:
37: return success();
38: } catch (TestFailedException e) {
39: return e.getResult();
40: } catch (Exception e) {
41: return SimpleTestResult.failed(this , "Exception: " + e, e);
42: }
43: }
44:
45: protected static void runTest(Test test) {
46: runTest(test, System.out);
47: }
48:
49: protected static void runTest(Test test, PrintStream out) {
50: TestResult result = test.perform();
51:
52: out.println(result.toString());
53: if (result.getException() != null) {
54: result.getException().printStackTrace(out);
55: }
56: }
57:
58: public abstract void performTest() throws Exception;
59: }
|