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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
|
// Copyright (C) 2004, 2005 by Object Refinery Limited
// Copyright (C) 2005 by <zander@kde.org>
// Written by David Gilbert (david.gilbert@object-refinery.com)
// Written by Thomas Zander <zander@kde.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, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301 USA.
package gnu.testlet.runner;
import gnu.testlet.ResourceNotFoundException;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Locale;
/**
* An application that runs a collection of Mauve tests and creates a report
* summarizing the results.
*/
public class Mauve extends TestHarness
{
private String lastCheckPoint;
private int checksSinceLastCheckPoint;
private ClassResult classResult;
private TestResult currentTest;
private CheckResult currentCheck;
private RunResult result;
/**
* runs tests
*
* @param file the text file containing test class names.
* @param prefix the prefix for each test class (usually 'gnu.testlet').
* @param output the name of the directory for writing output.
*/
public synchronized void execute(String file, String prefix, String output)
{
// save the default locale, some tests change the default and we want
// to restore it before generating the HTML report...
Locale savedLocale = Locale.getDefault();
File out = new File(output);
if (out.exists() && !out.isDirectory())
throw new IllegalArgumentException("Output should be a directory");
if (!out.exists())
out.mkdirs();
result = new RunResult("Mauve Test Run");
currentCheck = new CheckResult(0, false);
// initialize
// run tests and collect results
File f = new File(file);
try
{
FileReader testsToRun = new FileReader(f);
LineNumberReader r = new LineNumberReader(testsToRun);
while (r.ready())
{
String line = r.readLine();
if ("".equals(line))
continue;
System.out.println(line);
// check the line is not commented
// load the listed class
try
{
Class c = Class.forName(line);
// strip prefix ('gnu.testlet.') from front of name
String temp = line.substring(prefix.length());
// suffix is the name for the TestResult
String testName = temp.substring(temp.lastIndexOf('.') + 1);
temp = temp.substring(0, temp.lastIndexOf('.'));
String className = temp.substring(temp.lastIndexOf('.') + 1);
if (className.equals("Double") || className.equals("Float")
|| className.equals("Key"))
{
if (!temp.startsWith("java.lang."))
{
temp = temp.substring(0, temp.lastIndexOf('.'));
className = temp.substring(temp.lastIndexOf('.') + 1)
+ '.' + className;
}
}
String packageName = "default package";
int index = temp.lastIndexOf('.');
if (index >= 0)
packageName = temp.substring(0, temp.lastIndexOf('.'));
// remaining suffix is name for ClassResult
// rest of text is name for PackageResult
PackageResult pr = result.getPackageResult(packageName);
if (pr == null)
pr = new PackageResult(packageName);
classResult = pr.getClassResult(className);
if (classResult == null)
classResult = new ClassResult(className);
Testlet testlet;
try
{
testlet = (Testlet) c.newInstance();
}
catch (ClassCastException e)
{
System.err.println("Not a test (does not implement Testlet): "
+ line);
result.addFaultyTest(line, "Does not implement Testlet");
continue; // not a test
}
catch (Throwable t)
{ // instanciation errors etc..
t.printStackTrace(System.out);
result.addFaultyTest(line, t.getMessage());
continue;
}
currentTest = new TestResult(testName);
checksSinceLastCheckPoint = 0;
lastCheckPoint = "-";
try
{
testlet.test(this);
}
catch (Throwable t)
{
t.printStackTrace(System.out);
currentTest.failed(t);
}
classResult.add(currentTest);
if (pr.indexOf(classResult) < 0)
pr.add(classResult);
if (result.indexOf(pr) == -1)
result.add(pr);
}
catch (ClassNotFoundException e)
{
System.err.println("Could not load test: "+ line);
result.addMissingTest(line);
}
}
}
catch (FileNotFoundException e)
{
throw new IllegalArgumentException(e.getMessage());
}
catch (IOException e) {
e.printStackTrace(System.out);
}
// tests are complete so restore the default locale
Locale.setDefault(savedLocale);
// write results to HTML
System.out.println("Creating HTML report...");
try
{
HTMLGenerator.createReport(result, out);
}
catch (IOException e)
{
System.out.println("failed to write HTML due to following error:");
e.printStackTrace(System.out);
}
System.out.println("Creating XML report...");
try
{
new XMLGenerator(result).generate(new File(out, "results.xml"));
}
catch (IOException e)
{
System.out.println("failed to write XML due to following error:");
e.printStackTrace(System.out);
}
System.out.println("DONE!");
}
/**
* Records the result of a boolean check.
*
* @param result the result.
*/
public void check(boolean result)
{
currentCheck.setPassed(result);
checkDone();
}
/**
* 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)
{
currentCheck.setPassed( (result != null) ? result.equals(expected)
: (expected == null));
currentCheck.setActual((result != null) ? result.toString() : "null");
currentCheck.setExpected((expected != null) ? expected.toString() : "null");
checkDone();
}
/**
* 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)
{
currentCheck.setPassed(result == expected);
currentCheck.setActual(String.valueOf(result));
currentCheck.setExpected(String.valueOf(expected));
checkDone();
}
/**
* 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)
{
currentCheck.setPassed(result == expected);
currentCheck.setActual(String.valueOf(result));
currentCheck.setExpected(String.valueOf(expected));
checkDone();
}
/**
* 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)
{
currentCheck.setPassed(result == expected);
currentCheck.setActual(String.valueOf(result));
currentCheck.setExpected(String.valueOf(expected));
checkDone();
}
/**
* 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)
{
currentCheck.setPassed((result == expected
? (result != 0) || (1/result == 1/expected)
: (result != result) && (expected != expected)));
currentCheck.setActual(String.valueOf(result));
currentCheck.setExpected(String.valueOf(expected));
checkDone();
}
/**
* 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 void checkPoint(String name)
{
lastCheckPoint = name;
checksSinceLastCheckPoint = 0;
}
private void checkDone()
{
currentCheck.setNumber(++checksSinceLastCheckPoint);
currentCheck.setCheckPoint(lastCheckPoint);
currentTest.add(currentCheck);
currentCheck = new CheckResult(0, false);
currentCheck.setCheckPoint(lastCheckPoint);
}
/**
* Writes a message to the debug log along with a newline.
*
* @param message the message.
*/
public void debug(String message)
{
debug(message, true);
}
/**
* 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 void debug(String message, boolean newline)
{
currentCheck.appendToLog(message);
if (newline)
currentCheck.appendToLog("\n");
}
/**
* Writes the contents of an array to the log.
*
* @param o the array of objects.
* @param desc the description.
*/
public void debug(Object[] o, String desc)
{
StringBuffer logMessage = new StringBuffer();
logMessage.append("Object array: ");
logMessage.append(desc);
if (o == null)
logMessage.append("null");
else
expand(o, logMessage);
currentCheck.appendToLog(logMessage.toString());
currentCheck.appendToLog("\n");
}
// recursive helper method for debug(Object[], String)
private void expand(Object[] array, StringBuffer buf)
{
for (int i = 0; i < array.length; i++)
{
buf.append("obj["+ i +"]: ");
if (array[i] instanceof Object[])
expand((Object[]) array[i], buf);
else if (array[i] != null)
buf.append(array[i].toString());
else
buf.append("null");
if (i < array.length)
buf.append(", ");
}
}
/**
* Writes a stack trace for the specified exception to the log for the
* current check.
*
* @param ex the exception.
*/
public void debug(Throwable ex)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
PrintWriter w = new PrintWriter(out, false);
ex.printStackTrace(w);
w.close();
try
{
out.close();
debug(out.toString(), true);
}
catch (IOException e)
{
/* this should never happen..*/
}
}
/**
* This will print a message when in verbose mode.
*
* @param message the message.
*/
public void verbose(String message)
{
debug(message, true);
}
public Reader getResourceReader(String name) throws ResourceNotFoundException
{
return new BufferedReader(new InputStreamReader(getResourceStream(name)));
}
public InputStream getResourceStream(String name)
throws ResourceNotFoundException
{
// The following code assumes File.separator is a single character.
if (File.separator.length() > 1)
throw new Error ("File.separator length is greater than 1");
String realName = name.replace('#', File.separator.charAt(0));
try
{
return new FileInputStream(getSourceDirectory() + File.separator
+ realName);
}
catch (FileNotFoundException ex) {
throw new ResourceNotFoundException(ex.getLocalizedMessage() +
": " + getSourceDirectory() + File.separator + realName);
}
}
public String getSourceDirectory ()
{
return null; // TODO
}
public File getResourceFile (String name) throws ResourceNotFoundException
{
// The following code assumes File.separator is a single character.
if (File.separator.length () > 1)
throw new Error ("File.separator length is greater than 1");
String realName = name.replace ('#', File.separator.charAt (0));
File f = new File(getSourceDirectory() + File.separator + realName);
if (!f.exists())
{
throw new ResourceNotFoundException("cannot find mauve resource file" +
": " + getSourceDirectory() + File.separator + realName);
}
return f;
}
/**
* Provide a directory name for writing temporary files.
*
* @return The temporary directory name.
*/
public String getTempDirectory()
{
// TODO
return "/tmp";
}
/**
* Runs the application to generate an HTML report for a collection
* of Mauve tests.
*
* @param args the command line arguments.
*/
public static void main(String[] args)
{
// -prefix <package-prefix>
// -output <root-directory-for-HTML-output>
String file = "tests";
String prefix = "gnu.testlet.";
String output = "results";
for (int i = 0; i < args.length; i++)
{
String a = args[i];
if (a.equals("--prefix") || a.equals("-p"))
{
if (i < args.length)
{
prefix = args[i+1];
i++;
}
else
{
System.err.println("prefix: value missing");
return;
}
}
else if (a.equals("--output") || a.equals("-o"))
{
if (i < args.length)
{
output = args[i+1];
i++;
}
else
{
System.err.println("output: value missing");
return;
}
}
else if (a.equals("--help") || a.equals("-h"))
{
System.out.println("Usage: Mauve [options] [inputfile]");
System.out.println("reads test-class names from inputfile and executes them;");
System.out.println("If no inputfile is passed, then tests.txt will be used");
System.out.println(" options:");
System.out.println(" --help -h this help");
System.out.println(" --output -o the output directory [results]");
System.out.println(" --prefix -p package prefix [gnu.testlet]");
return;
}
else
file = a;
}
try
{
new Mauve().execute(file, prefix, output);
}
catch (IllegalArgumentException e)
{
System.err.println(e.getMessage());
System.err.println("Try --help for more info");
}
System.exit(0);
}
}
|