001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package db4ounit;
022:
023: import java.io.IOException;
024: import java.io.Writer;
025:
026: import db4ounit.util.StopWatch;
027:
028: public class TestResult extends Printable {
029:
030: private TestFailureCollection _failures = new TestFailureCollection();
031:
032: private int _testCount = 0;
033:
034: private final StopWatch _watch = new StopWatch();
035:
036: private final Writer _stdout;
037:
038: public TestResult(boolean printLabels) {
039: _stdout = printLabels ? TestPlatform.getStdErr() : null;
040: }
041:
042: public TestResult() {
043: this (false);
044: }
045:
046: public void testStarted(Test test) {
047: ++_testCount;
048: print(test.getLabel());
049: }
050:
051: public void testFailed(Test test, Throwable failure) {
052: printFailure(failure);
053: _failures.add(new TestFailure(test, failure));
054: }
055:
056: private void printFailure(Throwable failure) {
057: if (failure == null) {
058: print("\t!");
059: } else {
060: print("\t! " + failure);
061: }
062: }
063:
064: public boolean green() {
065: return _failures.size() == 0;
066: }
067:
068: public TestFailureCollection failures() {
069: return _failures;
070: }
071:
072: public void print(Writer writer) throws IOException {
073: if (green()) {
074: writer.write("GREEN (" + _testCount + " tests) - "
075: + elapsedString() + TestPlatform.NEWLINE);
076: return;
077: }
078: writer.write("RED (" + _failures.size() + " out of "
079: + _testCount + " tests failed) - " + elapsedString()
080: + TestPlatform.NEWLINE);
081: _failures.print(writer);
082: }
083:
084: private String elapsedString() {
085: return _watch.toString();
086: }
087:
088: public int assertions() {
089: return 0;
090: }
091:
092: public void runStarted() {
093: _watch.start();
094: }
095:
096: public void runFinished() {
097: _watch.stop();
098: }
099:
100: private void print(String message) {
101: if (null != _stdout) {
102: try {
103: _stdout.write(message + TestPlatform.NEWLINE);
104: _stdout.flush();
105: } catch (IOException x) {
106: TestPlatform.printStackTrace(x);
107: }
108: }
109: }
110: }
|