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:
025: public class TestRunner {
026:
027: private TestSuiteBuilder _suiteBuilder;
028: private boolean _reportToFile = true;
029:
030: public TestRunner(TestSuite suite) {
031: this (suite, true);
032: }
033:
034: public TestRunner(TestSuite suite, boolean reportToFile) {
035: if (null == suite)
036: throw new IllegalArgumentException("suite");
037: _suiteBuilder = new NullTestSuiteBuilder(suite);
038: _reportToFile = reportToFile;
039: }
040:
041: public TestRunner(TestSuiteBuilder builder) {
042: if (null == builder)
043: throw new IllegalArgumentException("suite");
044: _suiteBuilder = builder;
045: }
046:
047: public TestRunner(Class clazz) {
048: this (new ReflectionTestSuiteBuilder(clazz));
049: }
050:
051: public int run() {
052: return run(true);
053: }
054:
055: private int run(boolean printLabels) {
056: TestSuite suite = buildTestSuite();
057: if (null == suite)
058: return 1;
059:
060: TestResult result = new TestResult(printLabels);
061: result.runStarted();
062: suite.run(result);
063: result.runFinished();
064: report(result);
065: return result.failures().size();
066: }
067:
068: private TestSuite buildTestSuite() {
069: try {
070: return _suiteBuilder.build();
071: } catch (Exception x) {
072: report(x);
073: }
074: return null;
075: }
076:
077: private void report(Exception x) {
078: TestPlatform.printStackTrace(TestPlatform.getStdErr(), x);
079: }
080:
081: private void report(TestResult result) {
082: if (_reportToFile) {
083: reportToTextFile(result);
084: }
085: reportToStdErr(result);
086: }
087:
088: private void reportToTextFile(TestResult result) {
089: try {
090: java.io.Writer writer = TestPlatform
091: .openTextFile("db4ounit.log");
092: try {
093: report(writer, result);
094: } finally {
095: writer.close();
096: }
097: } catch (IOException e) {
098: report(e);
099: }
100: }
101:
102: private void reportToStdErr(TestResult result) {
103: report(TestPlatform.getStdErr(), result);
104: }
105:
106: private void report(java.io.Writer writer, TestResult result) {
107: try {
108: result.print(writer);
109: writer.flush();
110: } catch (IOException e) {
111: report(e);
112: }
113: }
114: }
|