File: TestHarness.java

package info (click to toggle)
mauve 20080616-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,856 kB
  • ctags: 21,952
  • sloc: java: 234,107; sh: 2,834; xml: 208; makefile: 59
file content (334 lines) | stat: -rw-r--r-- 8,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright (c) 1998, 1999, 2001  Cygnus Solutions
// Written by Tom Tromey <tromey@cygnus.com>
// Copyright (c) 2005  Mark J. Wielaard  <mark@klomp.org>

// This file is part of Mauve.

// Mauve is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2, or (at your option)
// any later version.

// Mauve 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 for more details.

// You should have received a copy of the GNU General Public License
// along with Mauve; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.

package gnu.testlet;

import java.awt.AWTException;
import java.awt.Robot;

import java.io.File;
import java.io.Reader;
import java.io.InputStream;

/**
 * This base class defines the API that test cases can report against.  This
 * code has been lifted from the Mauve project (and reformatted and 
 * commented).
 */
public abstract class TestHarness
  implements config
{
  /**
   * Records the result of a boolean check.
   *
   * @param result  the result.
   */
  public abstract void check (boolean result);

  /**
   * Checks the two objects for equality and records the result of
   * the check.
   *
   * @param result  the actual result.
   * @param expected  the expected result.
   */
  public void check(Object result, Object expected)
  {
    boolean ok = (result == null ? expected == null : result.equals(expected));
    check(ok);
    // This debug message may be misleading, depending on whether
    // string conversion produces same results for unequal objects.
    if (! ok)
      debug("got " + result + " but expected " + expected);
  }

  /**
   * Checks two booleans for equality and records the result of the check.
   * 
   * @param result the actual result.
   * @param expected the expected result.
   */
  public void check(boolean result, boolean expected)
  {
    boolean ok = (result == expected);
    check(ok);
    if (! ok)
      debug("got " + result + " but expected " + expected);
  }

  /**
   * Checks two ints for equality and records the result of the check.
   * 
   * @param result the actual result.
   * @param expected the expected result.
   */
  public void check(int result, int expected)
  {
    boolean ok = (result == expected);
    check(ok);
    if (! ok)
      debug("got " + result + " but expected " + expected);
  }

  /**
   * Checks two longs for equality and records the result of the check.
   * 
   * @param result the actual result.
   * @param expected the expected result.
   */
  public void check(long result, long expected)
  {
    boolean ok = (result == expected);
    check(ok);
    if (! ok)
      debug("got " + result + " but expected " + expected);
  }

  /**
   * Checks two doubles for equality and records the result of the check.
   * 
   * @param result the actual result.
   * @param expected the expected result.
   */
  public void check(double result, double expected)
  {
    // This triple check overcomes the fact that == does not
    // compare NaNs, and cannot tell between 0.0 and -0.0;
    // and all without relying on java.lang.Double (which may
    // itself be buggy - else why would we be testing it? ;)
    // For 0, we switch to infinities, and for NaN, we rely
    // on the identity in JLS 15.21.1 that NaN != NaN is true.
    boolean ok = (result == expected ? (result != 0)
                                       || (1 / result == 1 / expected)
                                    : (result != result)
                                      && (expected != expected));
    check(ok);
    if (! ok)
      // If Double.toString() is buggy, this debug statement may
      // accidentally show the same string for two different doubles!
      debug("got " + result + " but expected " + expected);
  }

  /**
   * Checks if <code>result</code> is equal to <code>expected</code> and
   * take a rounding delta into account.
   * 
   * @param result the actual result
   * @param expected the expected value
   * @param delta the rounding delta
   */
  public void check(double result, double expected, double delta)
  {
    boolean ok = true;
    if (Double.isInfinite(expected))
      {
        if (result != expected)
          ok = false;
      }
    else if (! (Math.abs(expected - result) <= delta))
      ok = false;

    check(ok);
    if (! ok)
      // If Double.toString() is buggy, this debug statement may
      // accidentally show the same string for two different doubles!
      debug("got " + result + " but expected " + expected);
  }

  // These methods are like the above, but checkpoint first.
  public void check(boolean result, String name)
  {
    checkPoint(name);
    check(result);
  }

  public void check(Object result, Object expected, String name)
  {
    checkPoint(name);
    check(result, expected);
  }

  public void check(boolean result, boolean expected, String name)
  {
    checkPoint(name);
    check(result, expected);
  }

  public void check(int result, int expected, String name)
  {
    checkPoint(name);
    check(result, expected);
  }

  public void check(long result, long expected, String name)
  {
    checkPoint(name);
    check(result, expected);
  }

  public void check(double result, double expected, String name)
  {
    checkPoint(name);
    check(result, expected);
  }

  public Robot createRobot()
  {
    Robot r = null;

    try
      {
        r = new Robot();
      }
    catch (AWTException e)
      {
        fail("TestHarness: couldn't create Robot: " + e.getMessage());
      }

    return r;
  }

  /**
   * A convenience method that sets a checkpoint with the specified name
   * then records a failed check.
   *
   * @param name  the checkpoint name.
   */
  public void fail(String name)
  {
    checkPoint(name);
    check(false);
  }
  
  // Given a resource name, return a Reader on it. Resource names are
  // just like file names.  They are relative to the top level Mauve
  // directory, but '#' characters are used in place of directory
  // separators.
  public abstract Reader getResourceReader (String name) 
    throws ResourceNotFoundException;

  public abstract InputStream getResourceStream (String name) 
    throws ResourceNotFoundException;

  public abstract File getResourceFile (String name) 
    throws ResourceNotFoundException;

  /**
   * Records a check point.  This can be used to mark a known place in a 
   * testlet.  It is useful if you have a large number of tests -- it makes 
   * it easier to find a failing test in the source code. 
   *
   * @param name  the check point name.
   */
  public abstract void checkPoint (String name);

  /**
   * This will print a message when in verbose mode.
   *
   * @param message  the message.
   */
  public abstract void verbose (String message);

  /**
   * Writes a message to the debug log.
   *
   * @param message  the message.
   */
  public abstract void debug (String message);

  /**
   * Writes a message to the debug log with or without a newline.
   *
   * @param message  the message.
   * @param newline  a flag to control whether or not a newline is added.
   */
  public abstract void debug (String message, boolean newline);
  /**
   * Writes a stack trace for the specified exception to a log.
   *
   * @param ex  the exception.
   */
  public abstract void debug (Throwable ex);

  /**
   * Writes the contents of an array to the log.
   *
   * @param o  the array of objects.
   * @param desc  the description.
   */
  public abstract void debug (Object[] o, String desc);

  // Default config interface methods.
  public String getSourceDirectory ()
  {
    return srcdir;
  }
  
  /**
   * Provide a directory name for writing temporary files.
   *
   * @return The temporary directory name.
   */

  public String getTempDirectory ()
  {
    return tmpdir;
  }
  
  public String getPathSeparator ()
  {
    return pathSeparator;
  }
  
  public String getSeparator ()
  {
    return separator;
  }
  
  public String getMailHost ()
  {
    return mailHost;
  }
  public String getAutoCompile()
  {
    return autoCompile;
  }

  public String getCpInstallDir()
  {
    return cpInstallDir;
  }

  public String getEcjJar()
  {
    return ecjJar;
  }
  
  public String getEmmaString()
  {
    return emmaString;
  }

  public String getTestJava()
  {
    return testJava;
  } 
}