1 17 package org.apache.log.format.test; 18 19 import junit.framework.TestCase; 20 import org.apache.log.ContextMap; 21 import org.apache.log.LogEvent; 22 import org.apache.log.Priority; 23 import org.apache.log.format.Formatter; 24 import org.apache.log.format.PatternFormatter; 25 import org.apache.log.format.RawFormatter; 26 import org.apache.log.format.SyslogFormatter; 27 import org.apache.log.format.XMLFormatter; 28 29 35 public final class FormatterTestCase 36 extends TestCase 37 { 38 private static String EOL = System.getProperty( "line.separator", "\n" ); 39 40 private static String M1 = "Message1"; 41 private static String M2 = "Message2Message2"; 42 private static String M3 = "Message3Message3Message3"; 43 44 private static String C1 = "Category1"; 45 private static String C2 = "Category2Category2"; 46 private static String C3 = "Category3Category3Category3"; 47 48 private static long T1 = 0; 49 private static long T2 = 1; 50 private static long T3 = 2; 51 52 private static Priority P1 = Priority.FATAL_ERROR; 53 private static Priority P2 = Priority.ERROR; 54 private static Priority P3 = Priority.WARN; 55 56 private static boolean mapsConfigured; 57 private static ContextMap CM1 = new ContextMap(); 58 private static ContextMap CM2 = new ContextMap(); 59 60 private static LogEvent E1 = createEvent( C1, M1, null, T1, P1, CM1 ); 61 private static LogEvent E2 = createEvent( C2, M2, null, T2, P2, CM2 ); 62 private static LogEvent E3 = createEvent( C3, M3, null, T3, P3, null ); 63 64 private static String E1_XML = "<log-entry>" + EOL + 65 " <time>" + T1 + "</time>" + EOL + 66 " <priority>" + P1.getName() + "</priority>" + EOL + 67 " <category>" + C1 + "</category>" + EOL + 68 " <message><![CDATA[" + M1 + "]]></message>" + EOL + 69 "</log-entry>" + EOL; 70 71 private static String E2_XML = "<log-entry>" + EOL + 72 " <time>" + T2 + "</time>" + EOL + 73 " <priority>" + P2.getName() + "</priority>" + EOL + 74 " <category>" + C2 + "</category>" + EOL + 75 " <message><![CDATA[" + M2 + "]]></message>" + EOL + 76 "</log-entry>" + EOL; 77 78 private static String E3_XML = "<log-entry>" + EOL + 79 " <time>" + T3 + "</time>" + EOL + 80 " <priority>" + P3.getName() + "</priority>" + EOL + 81 " <category>" + C3 + "</category>" + EOL + 82 " <message><![CDATA[" + M3 + "]]></message>" + EOL + 83 "</log-entry>" + EOL; 84 85 private static int FACILITY_ID = 9 << 3; private static String FACILITY_NAME = "cron"; 88 private static String E1_SYSLOG = "<" + ( 2 | FACILITY_ID ) + "> " + M1; 89 private static String E2_SYSLOG = "<" + ( 3 | FACILITY_ID ) + "> " + M2; 90 private static String E3_SYSLOG = "<" + ( 4 | FACILITY_ID ) + "> " + M3; 91 92 private static String E1_SYSLOG_WB = "<" + ( 2 | FACILITY_ID ) + "> " + FACILITY_NAME + ": " + M1; 93 private static String E2_SYSLOG_WB = "<" + ( 3 | FACILITY_ID ) + "> " + FACILITY_NAME + ": " + M2; 94 private static String E3_SYSLOG_WB = "<" + ( 4 | FACILITY_ID ) + "> " + FACILITY_NAME + ": " + M3; 95 96 private static String PATTERN1 = "[%8.8{category}]: %{message}" + EOL; 97 private static String E1_PATTERN1 = "[Category]: " + M1 + EOL; 98 private static String E2_PATTERN1 = "[Category]: " + M2 + EOL; 99 private static String E3_PATTERN1 = "[Category]: " + M3 + EOL; 100 101 private static String PATTERN2 = "[%10.{category}]: %{message}" + EOL; 102 private static String E1_PATTERN2 = "[" + C1 + " ]: " + M1 + EOL; 103 private static String E2_PATTERN2 = "[" + C2 + "]: " + M2 + EOL; 104 private static String E3_PATTERN2 = "[" + C3 + "]: " + M3 + EOL; 105 106 private static String PATTERN3 = "[%.10{category}]: %{message}" + EOL; 107 private static String E1_PATTERN3 = "[" + C1 + "]: " + M1 + EOL; 108 private static String E2_PATTERN3 = "[Category2C]: " + M2 + EOL; 109 private static String E3_PATTERN3 = "[Category3C]: " + M3 + EOL; 110 111 private static String PATTERN4 = "[%+10.{category}]: %{message}" + EOL; 112 private static String E1_PATTERN4 = "[" + C1 + " ]: " + M1 + EOL; 113 private static String E2_PATTERN4 = "[" + C2 + "]: " + M2 + EOL; 114 private static String E3_PATTERN4 = "[" + C3 + "]: " + M3 + EOL; 115 116 private static String PATTERN5 = "[%-10.{category}]: %{message}" + EOL; 117 private static String E1_PATTERN5 = "[ " + C1 + "]: " + M1 + EOL; 118 private static String E2_PATTERN5 = "[" + C2 + "]: " + M2 + EOL; 119 private static String E3_PATTERN5 = "[" + C3 + "]: " + M3 + EOL; 120 121 private static String PATTERN6 = "[%{context}]: %{message}" + EOL; 122 private static String E1_PATTERN6 = "[]: " + M1 + EOL; 123 private static String E2_PATTERN6 = "[]: " + M2 + EOL; 124 private static String E3_PATTERN6 = "[]: " + M3 + EOL; 125 126 private static String PATTERN7 = "[%{context:stack}]: %{message}" + EOL; 127 private static String E1_PATTERN7 = "[]: " + M1 + EOL; 128 private static String E2_PATTERN7 = "[]: " + M2 + EOL; 129 private static String E3_PATTERN7 = "[]: " + M3 + EOL; 130 131 private static String PATTERN8 = "[%{context:method}]: %{message}" + EOL; 132 private static String E1_PATTERN8 = "[com.biz.MyObject.myMethod(MyObject:53)]: " + M1 + EOL; 133 private static String E2_PATTERN8 = "[]: " + M2 + EOL; 134 private static String E3_PATTERN8 = "[]: " + M3 + EOL; 135 136 private static String CLASS_PREFIX = FormatterTestCase.class.getName() + "."; 137 138 private static String PATTERN9 = "[%{method}]: %{message}" + EOL; 139 private static String E1_PATTERN9 = "[com.biz.MyObject.myMethod(MyObject:53)]: " + M1 + EOL; 140 private static String E2_PATTERN9_START = "[" + CLASS_PREFIX + "testPattern9Formatter("; 141 private static String E2_PATTERN9_END = ")]: " + M2 + EOL; 142 private static String E3_PATTERN9_START = "[" + CLASS_PREFIX + "testPattern9Formatter("; 143 private static String E3_PATTERN9_END = ")]: " + M3 + EOL; 144 145 private static String PATTERN10 = "[%{context:method}]: %{message}" + EOL; 146 private static String E1_PATTERN10 = "[com.biz.MyObject.myMethod(MyObject:53)]: " + M1 + EOL; 147 private static String E2_PATTERN10 = "[]: " + M2 + EOL; 148 private static String E3_PATTERN10 = "[]: " + M3 + EOL; 149 150 private static String PATTERN11 = "[%{context:method}]: %{message}" + EOL; 151 private static String E1_PATTERN11 = "[com.biz.MyObject.myMethod(MyObject:53)]: " + M1 + EOL; 152 private static String E2_PATTERN11 = "[]: " + M2 + EOL; 153 private static String E3_PATTERN11 = "[]: " + M3 + EOL; 154 155 private static LogEvent createEvent( final String category, 156 final String message, 157 final Throwable throwable, 158 final long time, 159 final Priority priority, 160 final ContextMap contextMap ) 161 { 162 setupContext(); 163 164 final LogEvent event = new LogEvent(); 165 event.setCategory( category ); 166 event.setMessage( message ); 167 event.setThrowable( throwable ); 168 event.setTime( time ); 169 event.setPriority( priority ); 170 event.setContextMap( contextMap ); 171 return event; 172 } 173 174 private static void setupContext() 175 { 176 if( !mapsConfigured ) 177 { 178 mapsConfigured = true; 179 CM1.set( "method", "com.biz.MyObject.myMethod(MyObject:53)" ); 180 CM1.set( "hostname", "helm.realityforge.org" ); 181 CM1.set( "interface", "127.0.0.1" ); 182 CM1.set( "user", "barney" ); 183 CM1.makeReadOnly(); 184 185 CM2.set( "hostname", "helm.realityforge.org" ); 186 CM2.set( "interface", "127.0.0.1" ); 187 CM2.set( "user", "barney" ); 188 CM2.makeReadOnly(); 189 } 190 } 191 192 public FormatterTestCase( final String name ) 193 { 194 super( name ); 195 } 196 197 public void testRawFormatter() 198 { 199 final Formatter formatter = new RawFormatter(); 200 201 final String result1 = formatter.format( E1 ); 202 final String result2 = formatter.format( E2 ); 203 final String result3 = formatter.format( E3 ); 204 205 assertEquals( "Raw formatting of E1", E1.getMessage(), result1 ); 206 assertEquals( "Raw formatting of E2", E2.getMessage(), result2 ); 207 assertEquals( "Raw formatting of E3", E3.getMessage(), result3 ); 208 } 209 210 public void testXMLFormatter() 211 { 212 final Formatter formatter = new XMLFormatter(); 213 214 final String result1 = formatter.format( E1 ); 215 final String result2 = formatter.format( E2 ); 216 final String result3 = formatter.format( E3 ); 217 218 assertEquals( "XML formatting of E1", E1_XML, result1 ); 219 assertEquals( "XML formatting of E2", E2_XML, result2 ); 220 assertEquals( "XML formatting of E3", E3_XML, result3 ); 221 } 222 223 public void testSyslogFormatter() 224 { 225 final Formatter formatter = new SyslogFormatter( FACILITY_ID, false ); 226 227 final String result1 = formatter.format( E1 ); 228 final String result2 = formatter.format( E2 ); 229 final String result3 = formatter.format( E3 ); 230 231 assertEquals( "SYSLOG formatting of E1", E1_SYSLOG, result1 ); 232 assertEquals( "SYSLOG formatting of E2", E2_SYSLOG, result2 ); 233 assertEquals( "SYSLOG formatting of E3", E3_SYSLOG, result3 ); 234 } 235 236 public void testSyslogWithBannerFormatter() 237 { 238 final Formatter formatter = new SyslogFormatter( FACILITY_ID, true ); 239 240 final String result1 = formatter.format( E1 ); 241 final String result2 = formatter.format( E2 ); 242 final String result3 = formatter.format( E3 ); 243 244 assertEquals( "SYSLOG with banner formatting of E1", E1_SYSLOG_WB, result1 ); 245 assertEquals( "SYSLOG with banner formatting of E2", E2_SYSLOG_WB, result2 ); 246 assertEquals( "SYSLOG with banner formatting of E3", E3_SYSLOG_WB, result3 ); 247 } 248 249 public void testPattern1Formatter() 250 { 251 final Formatter formatter = new PatternFormatter( PATTERN1 ); 252 253 final String result1 = formatter.format( E1 ); 254 final String result2 = formatter.format( E2 ); 255 final String result3 = formatter.format( E3 ); 256 257 assertEquals( "Pattern1 formatting of E1", E1_PATTERN1, result1 ); 258 assertEquals( "Pattern1 formatting of E2", E2_PATTERN1, result2 ); 259 assertEquals( "Pattern1 formatting of E3", E3_PATTERN1, result3 ); 260 } 261 262 public void testPattern2Formatter() 263 { 264 final Formatter formatter = new PatternFormatter( PATTERN2 ); 265 266 final String result1 = formatter.format( E1 ); 267 final String result2 = formatter.format( E2 ); 268 final String result3 = formatter.format( E3 ); 269 270 assertEquals( "Pattern2 formatting of E1", E1_PATTERN2, result1 ); 271 assertEquals( "Pattern2 formatting of E2", E2_PATTERN2, result2 ); 272 assertEquals( "Pattern2 formatting of E3", E3_PATTERN2, result3 ); 273 } 274 275 public void testPattern3Formatter() 276 { 277 final Formatter formatter = new PatternFormatter( PATTERN3 ); 278 279 final String result1 = formatter.format( E1 ); 280 final String result2 = formatter.format( E2 ); 281 final String result3 = formatter.format( E3 ); 282 283 assertEquals( "Pattern3 formatting of E1", E1_PATTERN3, result1 ); 284 assertEquals( "Pattern3 formatting of E2", E2_PATTERN3, result2 ); 285 assertEquals( "Pattern3 formatting of E3", E3_PATTERN3, result3 ); 286 } 287 288 public void testPattern4Formatter() 289 { 290 final Formatter formatter = new PatternFormatter( PATTERN4 ); 291 292 final String result1 = formatter.format( E1 ); 293 final String result2 = formatter.format( E2 ); 294 final String result3 = formatter.format( E3 ); 295 296 assertEquals( "Pattern4 formatting of E1", E1_PATTERN4, result1 ); 297 assertEquals( "Pattern4 formatting of E2", E2_PATTERN4, result2 ); 298 assertEquals( "Pattern4 formatting of E3", E3_PATTERN4, result3 ); 299 } 300 301 public void testPattern5Formatter() 302 { 303 final Formatter formatter = new PatternFormatter( PATTERN5 ); 304 305 final String result1 = formatter.format( E1 ); 306 final String result2 = formatter.format( E2 ); 307 final String result3 = formatter.format( E3 ); 308 309 assertEquals( "Pattern5 formatting of E1", E1_PATTERN5, result1 ); 310 assertEquals( "Pattern5 formatting of E2", E2_PATTERN5, result2 ); 311 assertEquals( "Pattern5 formatting of E3", E3_PATTERN5, result3 ); 312 } 313 314 public void testPattern6Formatter() 315 { 316 final Formatter formatter = new PatternFormatter( PATTERN6 ); 317 318 final String result1 = formatter.format( E1 ); 319 final String result2 = formatter.format( E2 ); 320 final String result3 = formatter.format( E3 ); 321 322 assertEquals( "Pattern6 formatting of E1", E1_PATTERN6, result1 ); 323 assertEquals( "Pattern6 formatting of E2", E2_PATTERN6, result2 ); 324 assertEquals( "Pattern6 formatting of E3", E3_PATTERN6, result3 ); 325 } 326 327 public void testPattern7Formatter() 328 { 329 final Formatter formatter = new PatternFormatter( PATTERN7 ); 330 331 final String result1 = formatter.format( E1 ); 332 final String result2 = formatter.format( E2 ); 333 final String result3 = formatter.format( E3 ); 334 335 assertEquals( "Pattern7 formatting of E1", E1_PATTERN7, result1 ); 336 assertEquals( "Pattern7 formatting of E2", E2_PATTERN7, result2 ); 337 assertEquals( "Pattern7 formatting of E3", E3_PATTERN7, result3 ); 338 } 339 340 public void testPattern8Formatter() 341 { 342 final Formatter formatter = new PatternFormatter( PATTERN8 ); 343 344 final String result1 = formatter.format( E1 ); 345 final String result2 = formatter.format( E2 ); 346 final String result3 = formatter.format( E3 ); 347 348 assertEquals( "Pattern8 formatting of E1", E1_PATTERN8, result1 ); 349 assertEquals( "Pattern8 formatting of E2", E2_PATTERN8, result2 ); 350 assertEquals( "Pattern8 formatting of E3", E3_PATTERN8, result3 ); 351 } 352 372 public void testPattern10Formatter() 373 { 374 final Formatter formatter = new PatternFormatter( PATTERN10 ); 375 376 final String result1 = formatter.format( E1 ); 377 final String result2 = formatter.format( E2 ); 378 final String result3 = formatter.format( E3 ); 379 380 assertEquals( "Pattern10 formatting of E1", E1_PATTERN10, result1 ); 381 assertEquals( "Pattern10 formatting of E2", E2_PATTERN10, result2 ); 382 assertEquals( "Pattern10 formatting of E3", E3_PATTERN10, result3 ); 383 } 384 385 public void testPattern11Formatter() 386 { 387 final Formatter formatter = new PatternFormatter( PATTERN11 ); 388 389 final String result1 = formatter.format( E1 ); 390 final String result2 = formatter.format( E2 ); 391 final String result3 = formatter.format( E3 ); 392 393 assertEquals( "Pattern11 formatting of E1", E1_PATTERN11, result1 ); 394 assertEquals( "Pattern11 formatting of E2", E2_PATTERN11, result2 ); 395 assertEquals( "Pattern11 formatting of E3", E3_PATTERN11, result3 ); 396 } 397 } 398 | Popular Tags |