KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hanseltest > UtilTest


1 package org.hanseltest;
2
3 import java.io.ByteArrayOutputStream JavaDoc;
4 import java.io.PrintStream JavaDoc;
5 import java.util.Arrays JavaDoc;
6
7 import junit.framework.AssertionFailedError;
8 import junit.framework.Test;
9 import junit.framework.TestCase;
10 import junit.framework.TestResult;
11
12 import org.hansel.CoverageDecorator;
13 import org.hansel.Util;
14
15 /**
16  * Tests for the org.hansel.Util class.
17  * @author Niklas Mehner
18  */

19 public class UtilTest extends TestCase {
20     /** Line separator */
21     private static final String JavaDoc NEW_LINE = System.getProperty("line.separator");
22
23     /** This holds the old value of System.out,
24      * after startRedirect() has been called.
25      */

26     private PrintStream JavaDoc oldOut;
27
28     /** This holds the old value of System.out,
29      * after startRedirect() has been called.
30      */

31     private PrintStream JavaDoc oldErr;
32     
33     /** This contains the redirectied stdio,
34      * after startRedirect() has been called. */

35     private ByteArrayOutputStream JavaDoc bout;
36     /** This contains the redirectied stderr,
37      * after startRedirect() has been called. */

38     private ByteArrayOutputStream JavaDoc berr;
39
40     /**
41      * Create a new Test.
42      * @param name Name of the test.
43      */

44     public UtilTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     /**
49      * Start redirecting stdio/stderr to bytearrays.
50      */

51     public void startRedirect() {
52         oldOut = System.out;
53         oldErr = System.err;
54         bout = new ByteArrayOutputStream JavaDoc();
55         berr = new ByteArrayOutputStream JavaDoc();
56         System.setOut(new PrintStream JavaDoc(bout));
57         System.setErr(new PrintStream JavaDoc(berr));
58     }
59
60     /**
61      * End redirecting stdio/stderr to bytearrays.
62      */

63      public void endRedirect() {
64         System.setOut(oldOut);
65         System.setErr(oldErr);
66      }
67
68     /**
69      * Test the constructor. Trying to instantiate
70      * Util should always cause an exception.
71      */

72     public void testConstructor() {
73         try {
74             new Util();
75             fail();
76         } catch (UnsupportedOperationException JavaDoc e) {
77             // This exception is expected to be thrown
78
}
79     }
80
81     /**
82      * Test the concat method.
83      */

84     public void testConcat() {
85         String JavaDoc[] a = new String JavaDoc[] { "a", "a"};
86         String JavaDoc[] b = new String JavaDoc[] { "b", "b"};
87
88         String JavaDoc[] c = Util.concat(a, b);
89
90         assertTrue(Arrays.equals(new String JavaDoc[] {"a", "a", "b", "b"}, c));
91     }
92
93     /**
94      * Test the redirection fo stido/stderr to strings.
95      * This is needed for the dump() result tests.
96      */

97     public void testRedirection() {
98         startRedirect();
99         System.out.println("test");
100         assertEquals("test" + NEW_LINE, bout.toString());
101         endRedirect();
102     }
103     
104     /**
105      * Test dump() result with a result containing no errors or failures.
106      */

107     public void testEmptyResult() {
108         startRedirect();
109         TestResult result = new TestResult();
110         Util.dumpResult(result);
111         assertEquals("", bout.toString());
112         endRedirect();
113     }
114
115     /**
116      * Test dumpResult() with a single failure.
117      */

118     public void testSingleFailure() {
119         startRedirect();
120         TestResult result = new TestResult();
121         result.addFailure(this, new MockError("SingleFailure"));
122         Util.dumpResult(result);
123         assertEquals("", bout.toString());
124         assertEquals("SingleFailure" + NEW_LINE, berr.toString());
125         endRedirect();
126     }
127
128
129     /**
130      * Test dumpResult() with two failures.
131      */

132     public void testTwoFailures() {
133         startRedirect();
134         TestResult result = new TestResult();
135         result.addFailure(this, new MockError("FirstFailure"));
136         result.addFailure(this, new MockError("SecondFailure"));
137         Util.dumpResult(result);
138         assertEquals("", bout.toString());
139         assertEquals("FirstFailure" + NEW_LINE + "SecondFailure" + NEW_LINE, berr.toString());
140         endRedirect();
141      }
142
143     /**
144      * Test dumpResult() with a single error.
145      */

146     public void testSingleError() {
147         startRedirect();
148         TestResult result = new TestResult();
149         result.addError(this, new MockError("SingleError"));
150         Util.dumpResult(result);
151         assertEquals("", bout.toString());
152         assertEquals("SingleError" + NEW_LINE, berr.toString());
153         endRedirect();
154     }
155
156     /**
157      * Test dumpResult(), when two errors are present.
158      */

159     public void testTwoErrors() {
160         startRedirect();
161         TestResult result = new TestResult();
162         result.addError(this, new MockError("FirstError"));
163         result.addError(this, new MockError("SecondError"));
164         Util.dumpResult(result);
165         assertEquals("", bout.toString());
166         assertEquals("FirstError" + NEW_LINE + "SecondError" + NEW_LINE, berr.toString());
167         endRedirect();
168     }
169
170     /**
171      * Test dumpResult(), when failureas and errors are present.
172      */

173     public void testMixed() {
174         startRedirect();
175         TestResult result = new TestResult();
176         result.addError(this, new MockError("Error"));
177         result.addFailure(this, new MockError("Failure"));
178         Util.dumpResult(result);
179         assertEquals("", bout.toString());
180         assertEquals("Failure" + NEW_LINE + "Error" + NEW_LINE, berr.toString());
181         endRedirect();
182     }
183
184     /**
185      * Test the leftFill method.
186      */

187     public void testLeftFill() {
188         assertEquals(" 1", Util.leftFill(5, "1"));
189         assertEquals(" 12", Util.leftFill(5, "12"));
190         assertEquals("12345", Util.leftFill(5, "12345"));
191         assertEquals("1234567", Util.leftFill(5, "1234567"));
192     }
193
194
195     /**
196      * Returns a coverage decorator for this test.
197      * @return CoverageTest.
198      */

199      public static Test suite() {
200         CoverageDecorator result = new CoverageDecorator(UtilTest.class,
201                                                          new Class JavaDoc[] {
202                                                              Util.class });
203
204         return result;
205      }
206
207     /**
208      * AssertionFailedError, that prints a simple message instead
209      * of a stack trace.
210      */

211     private class MockError extends AssertionFailedError {
212         /** String to print out. */
213         private String JavaDoc msg;
214
215         /**
216          * Creates a new MockError.
217          * @param msg String to print out.
218          */

219         public MockError(String JavaDoc msg) {
220             this.msg = msg;
221         }
222
223         /**
224          * Prints a message instead of a stack trace to stderr.
225          */

226         public void printStackTrace() {
227             System.err.println(msg);
228         }
229     }
230 }
231
Popular Tags