2
package org.crosswire.util;
7
* A platform to help various SelfTest classes.
9
* <table border='1' cellPadding='3' cellSpacing='0' width="100%">
10
* <tr><td bgColor='white'class='TableRowColor'><font size='-7'>
11
* Distribution Licence:<br />
12
* Project B is free software; you can redistribute it
13
* and/or modify it under the terms of the GNU General Public License,
14
* version 2 as published by the Free Software Foundation.<br />
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* General Public License for more details.<br />
19
* The License is available on the internet
20
* <a href='http://www.gnu.org/copyleft/gpl.html'>here</a>, by writing to
21
* <i>Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22
* MA 02111-1307, USA</i>, Or locally at the Licence link below.<br />
23
* The copyright to this program is held by it's authors.
24
* </font></td></tr></table>
25
* @see <a href='http://www.eireneh.com/servlets/Web'>Project B Home</a>
26
* @see <{docs.Licence}>
29
public abstract class TestBase
32
* This should help people that want to run tests based on this class ...
33
* @param out Where to write the results to
34
* @param fatal Stop dead if anything goes wrong?
36
public abstract void test(PrintWriter out, boolean fatal);
39
* So that we can use this in JLists and the like
41
public String toString()
43
return getClass().getName();
47
* When a new package test starts
49
public static void logPackageStart(PrintWriter out, boolean fatal, Class name)
52
TestBase.fatal = fatal;
54
out.println(name.getName());
60
* When a new package test stops
62
public static void logPackageStop()
66
float elapsed = System.currentTimeMillis()-time;
67
out.println("\t"+elapsed/1000+"");
78
* Log something happening
80
public static void log(Object data)
84
float elapsed = System.currentTimeMillis()-time;
85
out.println("\t"+elapsed/1000+"");
91
time = System.currentTimeMillis();
95
* This allows us to do <code>test(foo, bar);</code> type tests.
96
* We only take any action if (foo.equals(bar))
98
public final static void test(Object param1, Object param2)
102
if (param2 == null) return;
106
if (param1.equals(param2)) return;
109
test(new Error("Object test - param1=["+param1+"] param2=["+param2+"]"));
113
* This allows us to do <code>test(1.0, 2.0);</code> type tests.
114
* We only take any action if (param1 != param2)
116
public final static void test(double param1, double param2)
118
if (param1 == param2) return;
119
test(new Error("Float test - param1="+param1+" param2="+param2));
123
* This allows us to do <code>test(1, 2);</code> type tests.
124
* We only take any action if (param1 != param2)
126
public final static void test(long param1, long param2)
128
if (param1 == param2) return;
129
test(new Error("Integer test - param1="+param1+" param2="+param2));
133
* This allows us to do <code>test(foo.equals(bar));</code> type
134
* tests. We only take any action if ok is false.
135
* @param ok The operand to test. False means a test failed.
137
public final static void test(boolean ok)
140
test(new Error("Boolean tests - false"));
144
* Something has definately gone wrong.
146
public final static void fail(Throwable ex)
152
* Something has definately gone wrong.
154
public final static void fail()
156
test(new Error("Failure"));
160
* Something has definately gone wrong.
162
public final static void fail(Object obj)
164
test(new Error("Failure: "+obj));
168
* Something has gone wrong - We report the problem. We insist that
169
* the caller passes us an exception - Maybe this is as a result of
170
* a real problem that something has caught, or more likely it is
171
* generated by calling <code>new Error()</code> when a test fails.
172
* It is used for the stack trace it contains.
173
* @param ex The Exception (Throwable) containing the stack trace
175
public final static void test(Throwable ex)
179
out.println("=================================================");
180
out.println("= Message: "+ex.getMessage());
181
out.print("= After: ");
186
out.println("=================================================");
189
if (fatal) System.exit(1);
196
* Display a stack trace for an execption
198
private final static void trace(Throwable ex)
200
out.println("= Stack trace:");
201
ex.printStackTrace(out);
202
if (ex instanceof LucidException)
204
LucidException lex = (LucidException) ex;
205
Throwable nex = lex.getException();
212
* Take a new timestamp and print (not println) the time in seconds
213
* since the last calling of <code>reportTime()</code>.
215
public final static void reportTime()
217
long now = System.currentTimeMillis();
218
double diff = (now-time) / 1000;
227
protected static long time = 0;
229
/** The place to print stuff */
230
protected static PrintWriter out = null;
232
/** What to do when it all goes wrong */
233
protected static boolean fatal = false;