001: package org.hanseltest;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.PrintStream;
005: import java.util.Arrays;
006:
007: import junit.framework.AssertionFailedError;
008: import junit.framework.Test;
009: import junit.framework.TestCase;
010: import junit.framework.TestResult;
011:
012: import org.hansel.CoverageDecorator;
013: import org.hansel.Util;
014:
015: /**
016: * Tests for the org.hansel.Util class.
017: * @author Niklas Mehner
018: */
019: public class UtilTest extends TestCase {
020: /** Line separator */
021: private static final String NEW_LINE = System
022: .getProperty("line.separator");
023:
024: /** This holds the old value of System.out,
025: * after startRedirect() has been called.
026: */
027: private PrintStream oldOut;
028:
029: /** This holds the old value of System.out,
030: * after startRedirect() has been called.
031: */
032: private PrintStream oldErr;
033:
034: /** This contains the redirectied stdio,
035: * after startRedirect() has been called. */
036: private ByteArrayOutputStream bout;
037: /** This contains the redirectied stderr,
038: * after startRedirect() has been called. */
039: private ByteArrayOutputStream berr;
040:
041: /**
042: * Create a new Test.
043: * @param name Name of the test.
044: */
045: public UtilTest(String name) {
046: super (name);
047: }
048:
049: /**
050: * Start redirecting stdio/stderr to bytearrays.
051: */
052: public void startRedirect() {
053: oldOut = System.out;
054: oldErr = System.err;
055: bout = new ByteArrayOutputStream();
056: berr = new ByteArrayOutputStream();
057: System.setOut(new PrintStream(bout));
058: System.setErr(new PrintStream(berr));
059: }
060:
061: /**
062: * End redirecting stdio/stderr to bytearrays.
063: */
064: public void endRedirect() {
065: System.setOut(oldOut);
066: System.setErr(oldErr);
067: }
068:
069: /**
070: * Test the constructor. Trying to instantiate
071: * Util should always cause an exception.
072: */
073: public void testConstructor() {
074: try {
075: new Util();
076: fail();
077: } catch (UnsupportedOperationException e) {
078: // This exception is expected to be thrown
079: }
080: }
081:
082: /**
083: * Test the concat method.
084: */
085: public void testConcat() {
086: String[] a = new String[] { "a", "a" };
087: String[] b = new String[] { "b", "b" };
088:
089: String[] c = Util.concat(a, b);
090:
091: assertTrue(Arrays
092: .equals(new String[] { "a", "a", "b", "b" }, c));
093: }
094:
095: /**
096: * Test the redirection fo stido/stderr to strings.
097: * This is needed for the dump() result tests.
098: */
099: public void testRedirection() {
100: startRedirect();
101: System.out.println("test");
102: assertEquals("test" + NEW_LINE, bout.toString());
103: endRedirect();
104: }
105:
106: /**
107: * Test dump() result with a result containing no errors or failures.
108: */
109: public void testEmptyResult() {
110: startRedirect();
111: TestResult result = new TestResult();
112: Util.dumpResult(result);
113: assertEquals("", bout.toString());
114: endRedirect();
115: }
116:
117: /**
118: * Test dumpResult() with a single failure.
119: */
120: public void testSingleFailure() {
121: startRedirect();
122: TestResult result = new TestResult();
123: result.addFailure(this , new MockError("SingleFailure"));
124: Util.dumpResult(result);
125: assertEquals("", bout.toString());
126: assertEquals("SingleFailure" + NEW_LINE, berr.toString());
127: endRedirect();
128: }
129:
130: /**
131: * Test dumpResult() with two failures.
132: */
133: public void testTwoFailures() {
134: startRedirect();
135: TestResult result = new TestResult();
136: result.addFailure(this , new MockError("FirstFailure"));
137: result.addFailure(this , new MockError("SecondFailure"));
138: Util.dumpResult(result);
139: assertEquals("", bout.toString());
140: assertEquals("FirstFailure" + NEW_LINE + "SecondFailure"
141: + NEW_LINE, berr.toString());
142: endRedirect();
143: }
144:
145: /**
146: * Test dumpResult() with a single error.
147: */
148: public void testSingleError() {
149: startRedirect();
150: TestResult result = new TestResult();
151: result.addError(this , new MockError("SingleError"));
152: Util.dumpResult(result);
153: assertEquals("", bout.toString());
154: assertEquals("SingleError" + NEW_LINE, berr.toString());
155: endRedirect();
156: }
157:
158: /**
159: * Test dumpResult(), when two errors are present.
160: */
161: public void testTwoErrors() {
162: startRedirect();
163: TestResult result = new TestResult();
164: result.addError(this , new MockError("FirstError"));
165: result.addError(this , new MockError("SecondError"));
166: Util.dumpResult(result);
167: assertEquals("", bout.toString());
168: assertEquals(
169: "FirstError" + NEW_LINE + "SecondError" + NEW_LINE,
170: berr.toString());
171: endRedirect();
172: }
173:
174: /**
175: * Test dumpResult(), when failureas and errors are present.
176: */
177: public void testMixed() {
178: startRedirect();
179: TestResult result = new TestResult();
180: result.addError(this , new MockError("Error"));
181: result.addFailure(this , new MockError("Failure"));
182: Util.dumpResult(result);
183: assertEquals("", bout.toString());
184: assertEquals("Failure" + NEW_LINE + "Error" + NEW_LINE, berr
185: .toString());
186: endRedirect();
187: }
188:
189: /**
190: * Test the leftFill method.
191: */
192: public void testLeftFill() {
193: assertEquals(" 1", Util.leftFill(5, "1"));
194: assertEquals(" 12", Util.leftFill(5, "12"));
195: assertEquals("12345", Util.leftFill(5, "12345"));
196: assertEquals("1234567", Util.leftFill(5, "1234567"));
197: }
198:
199: /**
200: * Returns a coverage decorator for this test.
201: * @return CoverageTest.
202: */
203: public static Test suite() {
204: CoverageDecorator result = new CoverageDecorator(
205: UtilTest.class, new Class[] { Util.class });
206:
207: return result;
208: }
209:
210: /**
211: * AssertionFailedError, that prints a simple message instead
212: * of a stack trace.
213: */
214: private class MockError extends AssertionFailedError {
215: /** String to print out. */
216: private String msg;
217:
218: /**
219: * Creates a new MockError.
220: * @param msg String to print out.
221: */
222: public MockError(String msg) {
223: this .msg = msg;
224: }
225:
226: /**
227: * Prints a message instead of a stack trace to stderr.
228: */
229: public void printStackTrace() {
230: System.err.println(msg);
231: }
232: }
233: }
|