| 1 package org.grlea.log.test; 2 3 6 18 import org.grlea.log.SimpleLog; 19 20 import junit.framework.TestCase; 21 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.InputStreamReader ; 27 import java.io.PrintStream ; 28 import java.util.Properties ; 29 30 36 public abstract class 37 AbstractLoggingTest 38 extends TestCase 39 { 40 private ByteArrayOutputStream outputStream; 41 protected SimpleLog log; 42 protected Properties properties; 43 44 public 45 AbstractLoggingTest(String name) 46 { 47 super(name); 48 } 49 50 protected void 51 setUp() 52 { 53 outputStream = new ByteArrayOutputStream (512); 54 System.setErr(new PrintStream (outputStream)); 55 56 properties = new Properties (); 57 log = new SimpleLog(properties); 58 } 59 60 protected void 61 tearDown() 62 { 63 log = null; 64 properties = null; 65 outputStream = null; 66 } 67 68 protected void 69 checkOutput(String [] expectedOutputLineParts) 70 throws IOException  71 { 72 byte[] output = outputStream.toByteArray(); 73 ByteArrayInputStream byteIn = new ByteArrayInputStream (output); 74 InputStreamReader streamReader = new InputStreamReader (byteIn); 75 BufferedReader in = new BufferedReader (streamReader); 76 String outputLine; 77 int lineNumber = 0; 78 while ((outputLine = in.readLine()) != null) 79 { 80 if (lineNumber >= expectedOutputLineParts.length) 81 { 82 fail("More output lines than expected.\nExtra line: " + outputLine); 83 } 84 85 String expectedOutputLinePart = expectedOutputLineParts[lineNumber]; 86 boolean linePartFound = outputLine.indexOf(expectedOutputLinePart) != -1; 87 assertEquals("'" + expectedOutputLinePart + "' found in '" + outputLine + "'", 88 true, linePartFound); 89 90 lineNumber++; 91 } 92 93 assertEquals("output lines", expectedOutputLineParts.length, lineNumber); 94 } 95 } | Popular Tags |