001: /*
002: This source file is part of Smyle, a database library.
003: For up-to-date information, see http://www.drjava.de/smyle
004: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: For full license text, see doc/license/lgpl.txt in this distribution
021: */
022:
023: package drjava.tf;
024:
025: import java.util.*;
026: import java.io.*;
027: import drjava.util.Timer;
028: import drjava.util.RealTimer;
029:
030: public class TestResult {
031: private String name, description;
032: private ArrayList<TestEvent> events = new ArrayList<TestEvent>();
033: private ArrayList<String> errors = new ArrayList<String>();
034: private ArrayList<TestResult> subtests = new ArrayList<TestResult>();
035: private int totalNumberOfFailures = 0;
036: Timer timer = new RealTimer();
037: double runtime;
038:
039: PrintStream oldOut, oldErr;
040: private ByteArrayOutputStream baosOut = new ByteArrayOutputStream(),
041: baosErr = new ByteArrayOutputStream();
042:
043: TestResult(String name) {
044: this .name = name;
045: }
046:
047: public List<String> getErrors() {
048: return errors;
049: }
050:
051: public List<TestEvent> getEvents() {
052: flush();
053: return events;
054: }
055:
056: public void error(String msg, String lineText) {
057: flush();
058: errors.add(msg);
059: TestEvent e = new TestEvent(TestEvent.ERROR, msg);
060: e.setLineText(lineText);
061: events.add(e);
062: ++totalNumberOfFailures;
063: }
064:
065: private void flush() {
066: String s = baosOut.toString();
067: if (s.length() != 0) {
068: baosOut.reset();
069: events.add(new TestEvent(TestEvent.SYSTEMOUT, s));
070: }
071:
072: s = baosErr.toString();
073: if (s.length() != 0) {
074: baosErr.reset();
075: events.add(new TestEvent(TestEvent.SYSTEMERR, s));
076: }
077: }
078:
079: void redirectSystemStreams() {
080: oldOut = System.out;
081: oldErr = System.err;
082: System.setOut(new PrintStream(baosOut));
083: System.setErr(new PrintStream(baosErr));
084: }
085:
086: void restoreSystemStreams() {
087: System.setOut(oldOut);
088: System.setErr(oldErr);
089: oldOut = oldErr = null;
090: }
091:
092: public String getTestName() {
093: return name;
094: }
095:
096: public String getDescription() {
097: return description;
098: }
099:
100: public List<TestResult> getSubtests() {
101: return subtests;
102: }
103:
104: void addSubtest(TestResult test) {
105: subtests.add(test);
106: totalNumberOfFailures += test.totalNumberOfFailures;
107: }
108:
109: void addException(Throwable e) {
110: flush();
111: events.add(new TestEvent(TestEvent.EXCEPTION, e.toString()));
112: ++totalNumberOfFailures;
113: }
114:
115: public int getTotalNumberOfFailures() {
116: return totalNumberOfFailures;
117: }
118:
119: public static boolean equals(Object a, Object b) {
120: return a == null ? b == null : a.equals(b);
121: }
122:
123: public void assertEquals(String desc, Object expected,
124: Object actual, String lineText) {
125: if (!equals(expected, actual))
126: error(desc + (desc.length() != 0 ? " " : "") + "expected:<"
127: + expected + "> but was:<" + actual + ">", lineText);
128: }
129:
130: public <A> void checkIterator(Iterator<A> it, A[] data,
131: String lineText) {
132: for (int i = 0; i < data.length; i++) {
133: if (!it.hasNext())
134: error("Iterator " + it + " ends at " + i + " of "
135: + data.length, lineText);
136: assertEquals("Element " + i + " of " + data.length,
137: data[i], it.next(), lineText);
138: }
139: if (it.hasNext())
140: error("Superfluous element " + it.next() + " after "
141: + data.length, lineText);
142: }
143:
144: public List<TestEvent> getAllEvents() {
145: ArrayList<TestEvent> list = new ArrayList<TestEvent>();
146: collectEvents(list);
147: return list;
148: }
149:
150: protected void collectEvents(List<TestEvent> list) {
151: list.addAll(events);
152: for (int i = 0; i < subtests.size(); i++)
153: subtests.get(i).collectEvents(list);
154: }
155:
156: /** time this test took to run in ms */
157: public double getRuntime() {
158: return runtime;
159: }
160:
161: public void useTimer(Timer timer) {
162: this .timer = timer;
163: }
164:
165: public void description(String d) {
166: description = d;
167: }
168: }
|