KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > db4ounit > TestResult


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package db4ounit;
22
23 import java.io.IOException JavaDoc;
24 import java.io.Writer JavaDoc;
25
26 import db4ounit.util.StopWatch;
27
28 public class TestResult extends Printable {
29
30     private TestFailureCollection _failures = new TestFailureCollection();
31     
32     private int _testCount = 0;
33     
34     private final StopWatch _watch = new StopWatch();
35     
36     private final Writer JavaDoc _stdout;
37     
38     public TestResult(boolean printLabels) {
39         _stdout = printLabels ? TestPlatform.getStdOut() : null;
40     }
41     
42     public TestResult() {
43         this(false);
44     }
45
46     public void testStarted(Test test) throws IOException JavaDoc {
47         ++_testCount;
48         printLabel(test.getLabel());
49     }
50
51     private void printLabel(String JavaDoc label) throws IOException JavaDoc {
52         if (null != _stdout) {
53             _stdout.write(label + "\n");
54             _stdout.flush();
55         }
56     }
57     
58     public void testFailed(Test test, Throwable JavaDoc failure) {
59         _failures.add(new TestFailure(test, failure));
60     }
61     
62     public boolean green() {
63         return _failures.size() == 0;
64     }
65
66     public TestFailureCollection failures() {
67         return _failures;
68     }
69     
70     public void print(Writer JavaDoc writer) throws IOException JavaDoc {
71         if (green()) {
72             writer.write("GREEN (" + _testCount + " tests) - " + elapsedString() + "\n");
73             return;
74         }
75         writer.write("RED (" + _failures.size() +" out of " + _testCount + " tests failed) - " + elapsedString() + "\n");
76         _failures.print(writer);
77     }
78     
79     private String JavaDoc elapsedString() {
80         return _watch.toString();
81     }
82
83     public int assertions() {
84         return 0;
85     }
86
87     public void runStarted() {
88         _watch.start();
89     }
90
91     public void runFinished() {
92         _watch.stop();
93     }
94 }
95
Popular Tags