~sword-devel/jsword/trunk

« back to all changes in this revision

Viewing changes to jsword/java/historic/org/crosswire/util/TestBase.java

  • Committer: joe
  • Date: 2002-10-08 21:36:18 UTC
  • Revision ID: svn-v4:a88caf3b-7e0a-0410-8d0d-cecb45342206:trunk:80
big config and comment update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
 
package org.crosswire.util;
3
 
 
4
 
import java.io.*;
5
 
 
6
 
/**
7
 
* A platform to help various SelfTest classes.
8
 
*
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}>
27
 
* @author Joe Walker
28
 
*/
29
 
public abstract class TestBase
30
 
{
31
 
    /**
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?
35
 
    */
36
 
    public abstract void test(PrintWriter out, boolean fatal);
37
 
 
38
 
    /**
39
 
    * So that we can use this in JLists and the like
40
 
    */
41
 
    public String toString()
42
 
    {
43
 
        return getClass().getName();
44
 
    }
45
 
 
46
 
    /**
47
 
    * When a new package test starts
48
 
    */
49
 
    public static void logPackageStart(PrintWriter out, boolean fatal, Class name)
50
 
    {
51
 
        TestBase.out = out;
52
 
        TestBase.fatal = fatal;
53
 
 
54
 
        out.println(name.getName());
55
 
        out.println("{");
56
 
        out.flush();
57
 
    }
58
 
 
59
 
    /**
60
 
    * When a new package test stops
61
 
    */
62
 
    public static void logPackageStop()
63
 
    {
64
 
        if (time != 0)
65
 
        {
66
 
            float elapsed = System.currentTimeMillis()-time;
67
 
            out.println("\t"+elapsed/1000+"");
68
 
            out.flush();
69
 
        }
70
 
 
71
 
        out.println("}\n");
72
 
        out.flush();
73
 
 
74
 
        time = 0;
75
 
    }
76
 
 
77
 
    /**
78
 
    * Log something happening
79
 
    */
80
 
    public static void log(Object data)
81
 
    {
82
 
        if (time != 0)
83
 
        {
84
 
            float elapsed = System.currentTimeMillis()-time;
85
 
            out.println("\t"+elapsed/1000+"");
86
 
            out.flush();
87
 
        }
88
 
 
89
 
        out.print("    "+data);
90
 
 
91
 
        time = System.currentTimeMillis();
92
 
    }
93
 
 
94
 
    /**
95
 
    * This allows us to do <code>test(foo, bar);</code> type tests.
96
 
    * We only take any action if (foo.equals(bar))
97
 
    */
98
 
    public final static void test(Object param1, Object param2)
99
 
    {
100
 
        if (param1 == null)
101
 
        {
102
 
            if (param2 == null) return;
103
 
        }
104
 
        else
105
 
        {
106
 
            if (param1.equals(param2)) return;
107
 
        }
108
 
 
109
 
        test(new Error("Object test - param1=["+param1+"] param2=["+param2+"]"));
110
 
    }
111
 
 
112
 
    /**
113
 
    * This allows us to do <code>test(1.0, 2.0);</code> type tests.
114
 
    * We only take any action if (param1 != param2)
115
 
    */
116
 
    public final static void test(double param1, double param2)
117
 
    {
118
 
        if (param1 == param2) return;
119
 
        test(new Error("Float test - param1="+param1+" param2="+param2));
120
 
    }
121
 
 
122
 
    /**
123
 
    * This allows us to do <code>test(1, 2);</code> type tests.
124
 
    * We only take any action if (param1 != param2)
125
 
    */
126
 
    public final static void test(long param1, long param2)
127
 
    {
128
 
        if (param1 == param2) return;
129
 
        test(new Error("Integer test - param1="+param1+" param2="+param2));
130
 
    }
131
 
 
132
 
    /**
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.
136
 
    */
137
 
    public final static void test(boolean ok)
138
 
    {
139
 
        if (ok) return;
140
 
        test(new Error("Boolean tests - false"));
141
 
    }
142
 
 
143
 
    /**
144
 
    * Something has definately gone wrong.
145
 
    */
146
 
    public final static void fail(Throwable ex)
147
 
    {
148
 
        test(ex);
149
 
    }
150
 
 
151
 
    /**
152
 
    * Something has definately gone wrong.
153
 
    */
154
 
    public final static void fail()
155
 
    {
156
 
        test(new Error("Failure"));
157
 
    }
158
 
 
159
 
    /**
160
 
    * Something has definately gone wrong.
161
 
    */
162
 
    public final static void fail(Object obj)
163
 
    {
164
 
        test(new Error("Failure: "+obj));
165
 
    }
166
 
 
167
 
    /**
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
174
 
    */
175
 
    public final static void test(Throwable ex)
176
 
    {
177
 
        out.println();
178
 
        out.println();
179
 
        out.println("=================================================");
180
 
        out.println("= Message: "+ex.getMessage());
181
 
        out.print("= After: ");
182
 
        reportTime();
183
 
        out.println("s");
184
 
        out.println("=");
185
 
        trace(ex);
186
 
        out.println("=================================================");
187
 
        out.flush();
188
 
 
189
 
        if (fatal) System.exit(1);
190
 
 
191
 
        out.println();
192
 
        out.flush();
193
 
    }
194
 
 
195
 
    /**
196
 
    * Display a stack trace for an execption
197
 
    */
198
 
    private final static void trace(Throwable ex)
199
 
    {
200
 
        out.println("= Stack trace:");
201
 
        ex.printStackTrace(out);
202
 
        if (ex instanceof LucidException)
203
 
        {
204
 
            LucidException lex = (LucidException) ex;
205
 
            Throwable nex = lex.getException();
206
 
            if (nex != null)
207
 
                trace(nex);
208
 
        }
209
 
    }
210
 
 
211
 
    /**
212
 
    * Take a new timestamp and print (not println) the time in seconds
213
 
    * since the last calling of <code>reportTime()</code>.
214
 
    */
215
 
    public final static void reportTime()
216
 
    {
217
 
        long now = System.currentTimeMillis();
218
 
        double diff = (now-time) / 1000;
219
 
 
220
 
        out.print(diff);
221
 
        out.flush();
222
 
 
223
 
        time = now;
224
 
    }
225
 
 
226
 
    /** For timing */
227
 
    protected static long time = 0;
228
 
 
229
 
    /** The place to print stuff */
230
 
    protected static PrintWriter out = null;
231
 
 
232
 
    /** What to do when it all goes wrong */
233
 
    protected static boolean fatal = false;
234
 
}
235