1 17 package org.apache.log.output.test; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.OutputStreamWriter ; 24 import junit.framework.TestCase; 25 import org.apache.log.Hierarchy; 26 import org.apache.log.LogTarget; 27 import org.apache.log.Logger; 28 import org.apache.log.Priority; 29 import org.apache.log.format.RawFormatter; 30 import org.apache.log.output.AbstractOutputTarget; 31 import org.apache.log.output.MemoryTarget; 32 import org.apache.log.output.io.FileTarget; 33 import org.apache.log.output.io.SafeFileTarget; 34 import org.apache.log.output.io.StreamTarget; 35 import org.apache.log.output.io.WriterTarget; 36 37 43 public final class OutputTargetTestCase 44 extends TestCase 45 { 46 private static String M1 = "meep meep!"; 47 private static String M2 = "marina"; 48 private static String M3 = "spin and then fall down"; 49 50 private static String HEAD = ""; 51 private static String TAIL = ""; 52 53 private static String R1 = M1; 54 private static String R2 = M2; 55 private static String R3 = M3; 56 57 private static String OUTPUT = HEAD + R1 + R2 + R3 + TAIL; 58 59 private static RawFormatter FORMATTER = new RawFormatter(); 60 61 private final File m_logFile; 62 63 public OutputTargetTestCase( final String name ) 64 throws IOException 65 { 66 super( name ); 67 68 m_logFile = ( new File ( "test/log/logfile.txt" ) ).getCanonicalFile(); 69 } 70 71 private String getResult( final ByteArrayOutputStream output ) 72 { 73 final String result = output.toString(); 74 output.reset(); 75 return result; 76 } 77 78 public void testStreamTarget() 79 throws Exception 80 { 81 final ByteArrayOutputStream output = new ByteArrayOutputStream (); 82 final StreamTarget target = new StreamTarget( output, FORMATTER ); 83 doStreamTest( output, target ); 84 91 } 92 93 public void testWriterTarget() 94 throws Exception 95 { 96 final ByteArrayOutputStream output = new ByteArrayOutputStream (); 97 final OutputStreamWriter writer = new OutputStreamWriter ( output ); 98 final WriterTarget target = new WriterTarget( writer, FORMATTER ); 99 doStreamTest( output, target ); 100 } 101 102 public void testFileTarget() 103 throws Exception 104 { 105 final FileTarget target = new FileTarget( m_logFile, false, FORMATTER ); 106 107 final Logger logger = getNewLogger( target ); 108 logger.debug( M1 ); 109 logger.debug( M2 ); 110 logger.debug( M3 ); 111 target.close(); 112 113 final String data = getFileContents( m_logFile ); 114 assertEquals( "Targets file output", OUTPUT, data ); 115 assertTrue( "Deleting logfile", m_logFile.delete() ); 116 117 logger.debug( M1 ); 118 assertTrue( "Write after close()", !m_logFile.exists() ); 119 } 120 121 public void testSafeFileTarget() 122 throws Exception 123 { 124 final SafeFileTarget target = new SafeFileTarget( m_logFile, false, FORMATTER ); 125 126 final Logger logger = getNewLogger( target ); 127 logger.debug( M1 ); 128 129 final String data1 = getFileContents( m_logFile ); 130 assertEquals( "Targets file output", HEAD + R1, data1 ); 131 assertTrue( "Deleting logfile", m_logFile.delete() ); 132 133 logger.debug( M2 ); 134 logger.debug( M3 ); 135 target.close(); 136 137 final String data2 = getFileContents( m_logFile ); 138 assertEquals( "Targets file output", R2 + R3 + TAIL, data2 ); 139 assertTrue( "Deleting logfile", m_logFile.delete() ); 140 141 logger.debug( M1 ); 142 assertTrue( "Write after close()", !m_logFile.exists() ); 143 } 144 145 150 public void testMemoryTarget1() 151 throws Exception 152 { 153 final ByteArrayOutputStream output = new ByteArrayOutputStream (); 154 final StreamTarget other = new StreamTarget( output, FORMATTER ); 155 final MemoryTarget target = new MemoryTarget( other, 1, Priority.FATAL_ERROR ); 156 doStreamOutputTest( output, target ); 157 } 158 159 164 public void testMemoryTarget2() 165 throws Exception 166 { 167 final ByteArrayOutputStream output = new ByteArrayOutputStream (); 168 final StreamTarget other = new StreamTarget( output, FORMATTER ); 169 final MemoryTarget target = new MemoryTarget( other, 10, Priority.DEBUG ); 170 doStreamOutputTest( output, target ); 171 } 172 173 178 public void testMemoryTarget3() 179 throws Exception 180 { 181 final ByteArrayOutputStream output = new ByteArrayOutputStream (); 182 final StreamTarget other = new StreamTarget( output, FORMATTER ); 183 final MemoryTarget target = new MemoryTarget( other, 10, Priority.FATAL_ERROR ); 184 185 final Logger logger = getNewLogger( target ); 186 187 final String head = getResult( output ); 189 assertEquals( "Targets Head output", "", head ); 190 191 logger.debug( M1 ); 193 final String result1 = getResult( output ); 194 assertEquals( "Targets R1 debug output", "", result1 ); 195 196 target.push(); 197 final String resultPP = getResult( output ); 198 assertEquals( "Targets HEAD+R1 debug output", HEAD + R1, resultPP ); 199 200 logger.debug( M2 ); 201 final String result2 = getResult( output ); 202 203 logger.debug( M3 ); 204 final String result3 = getResult( output ); 205 206 logger.fatalError( M3 ); 208 final String result4 = getResult( output ); 209 210 assertEquals( "Targets R2 debug output", "", result2 ); 211 assertEquals( "Targets R3 debug output", "", result3 ); 212 assertEquals( "Targets R3 debug output", R2 + R3 + R3, result4 ); 213 } 214 215 private Logger getNewLogger( final LogTarget target ) 216 { 217 final Hierarchy hierarchy = new Hierarchy(); 218 final Logger logger = hierarchy.getLoggerFor( "myCategory" ); 219 logger.setLogTargets( new LogTarget[]{target} ); 220 return logger; 221 } 222 223 private String getFileContents( final File file ) 224 throws IOException 225 { 226 final FileInputStream input = new FileInputStream ( file ); 227 final StringBuffer sb = new StringBuffer (); 228 229 int ch = input.read(); 230 while( -1 != ch ) 231 { 232 sb.append( (char)ch ); 233 ch = input.read(); 234 } 235 236 input.close(); 237 238 return sb.toString(); 239 } 240 241 private Logger doStreamOutputTest( final ByteArrayOutputStream output, 242 final LogTarget target ) 243 { 244 final Logger logger = getNewLogger( target ); 245 246 final String head = getResult( output ); 247 248 logger.debug( M1 ); 249 final String result1 = getResult( output ); 250 251 logger.debug( M2 ); 252 final String result2 = getResult( output ); 253 254 logger.debug( M3 ); 255 final String result3 = getResult( output ); 256 257 assertEquals( "Targets Head output", HEAD, head ); 258 assertEquals( "Targets R1 debug output", R1, result1 ); 259 assertEquals( "Targets R2 debug output", R2, result2 ); 260 assertEquals( "Targets R3 debug output", R3, result3 ); 261 262 return logger; 263 } 264 265 private void doStreamTest( final ByteArrayOutputStream output, 266 final AbstractOutputTarget target ) 267 { 268 final Logger logger = doStreamOutputTest( output, target ); 269 270 target.close(); 271 final String tail = getResult( output ); 272 assertEquals( "Targets Tail output", TAIL, tail ); 273 274 logger.debug( M1 ); 275 final String noresult = getResult( output ); 276 278 assertEquals( "Write after close()", "", noresult ); 279 } 281 } 282 | Popular Tags |