KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > db4ounit > TestRunner


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 public class TestRunner {
27     
28     private TestSuiteBuilder _suiteBuilder;
29     
30     public TestRunner(TestSuite suite) {
31         if (null == suite) throw new IllegalArgumentException JavaDoc("suite");
32         _suiteBuilder = new NullTestSuiteBuilder(suite);
33     }
34     
35     public TestRunner(TestSuiteBuilder builder) {
36         if (null == builder) throw new IllegalArgumentException JavaDoc("suite");
37         _suiteBuilder = builder;
38     }
39     
40     public TestRunner(Class JavaDoc clazz) {
41         this(new ReflectionTestSuiteBuilder(clazz));
42     }
43
44     public int run() {
45         return run(true);
46     }
47
48     private int run(boolean printLabels) {
49         TestSuite suite = buildTestSuite();
50         if (null == suite) return 1;
51         
52         TestResult result = new TestResult(printLabels);
53         result.runStarted();
54         suite.run(result);
55         result.runFinished();
56         report(result);
57         return result.failures().size();
58     }
59     
60     private TestSuite buildTestSuite() {
61         try {
62             return _suiteBuilder.build();
63         } catch (Exception JavaDoc x) {
64             report(x);
65         }
66         return null;
67     }
68
69     private void report(Exception JavaDoc x) {
70         Writer JavaDoc stdout = TestPlatform.getStdOut();
71         TestPlatform.printStackTrace(stdout, x);
72     }
73
74     private void report(TestResult result) {
75         try {
76             java.io.Writer JavaDoc stdout = TestPlatform.getStdOut();
77             result.print(stdout);
78             stdout.flush();
79         } catch (IOException JavaDoc e) {
80         }
81     }
82 }
83
Popular Tags