1 package org.hanseltest; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.PrintStream ; 5 import java.util.Arrays ; 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 19 public class UtilTest extends TestCase { 20 21 private static final String NEW_LINE = System.getProperty("line.separator"); 22 23 26 private PrintStream oldOut; 27 28 31 private PrintStream oldErr; 32 33 35 private ByteArrayOutputStream bout; 36 38 private ByteArrayOutputStream berr; 39 40 44 public UtilTest(String name) { 45 super(name); 46 } 47 48 51 public void startRedirect() { 52 oldOut = System.out; 53 oldErr = System.err; 54 bout = new ByteArrayOutputStream (); 55 berr = new ByteArrayOutputStream (); 56 System.setOut(new PrintStream (bout)); 57 System.setErr(new PrintStream (berr)); 58 } 59 60 63 public void endRedirect() { 64 System.setOut(oldOut); 65 System.setErr(oldErr); 66 } 67 68 72 public void testConstructor() { 73 try { 74 new Util(); 75 fail(); 76 } catch (UnsupportedOperationException e) { 77 } 79 } 80 81 84 public void testConcat() { 85 String [] a = new String [] { "a", "a"}; 86 String [] b = new String [] { "b", "b"}; 87 88 String [] c = Util.concat(a, b); 89 90 assertTrue(Arrays.equals(new String [] {"a", "a", "b", "b"}, c)); 91 } 92 93 97 public void testRedirection() { 98 startRedirect(); 99 System.out.println("test"); 100 assertEquals("test" + NEW_LINE, bout.toString()); 101 endRedirect(); 102 } 103 104 107 public void testEmptyResult() { 108 startRedirect(); 109 TestResult result = new TestResult(); 110 Util.dumpResult(result); 111 assertEquals("", bout.toString()); 112 endRedirect(); 113 } 114 115 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 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 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 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 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 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 199 public static Test suite() { 200 CoverageDecorator result = new CoverageDecorator(UtilTest.class, 201 new Class [] { 202 Util.class }); 203 204 return result; 205 } 206 207 211 private class MockError extends AssertionFailedError { 212 213 private String msg; 214 215 219 public MockError(String msg) { 220 this.msg = msg; 221 } 222 223 226 public void printStackTrace() { 227 System.err.println(msg); 228 } 229 } 230 } 231 | Popular Tags |