1 /* 2 * Copyright (C) The JContainer Group. All rights reserved. 3 * 4 * This software is published under the terms of the JContainer 5 * Software License version 1.1, a copy of which has been included 6 * with this distribution in the LICENSE.txt file. 7 */ 8 package org.jcontainer.dna.impl; 9 10 import junit.framework.TestCase; 11 import org.apache.log.Hierarchy; 12 import org.apache.log.LogTarget; 13 import org.apache.log.Logger; 14 import org.apache.log.Priority; 15 16 public class LogkitLoggerTestCase 17 extends TestCase 18 { 19 public void testLogkitLoggerEmptyCtor() 20 throws Exception 21 { 22 try 23 { 24 new LogkitLogger( null ); 25 } 26 catch( NullPointerException npe ) 27 { 28 assertEquals( "npe.getMessage()", "logger", npe.getMessage() ); 29 } 30 } 31 32 public void testLogkitLoggerGetChildLogger() 33 throws Exception 34 { 35 final MockLogTarget target = new MockLogTarget(); 36 final LogkitLogger logger = createLogger( target, Priority.DEBUG ); 37 38 assertNotSame( "logger.getChildLogger == logger", 39 logger, 40 logger.getChildLogger( "whatever" ) ); 41 } 42 43 public void testLogkitLoggerTraceEnabled() 44 throws Exception 45 { 46 final Priority level = Priority.DEBUG; 47 final Priority type = Priority.DEBUG; 48 final String message = "Meep!"; 49 final Throwable throwable = null; 50 final boolean output = true; 51 52 final MockLogTarget target = new MockLogTarget(); 53 final LogkitLogger logger = createLogger( target, level ); 54 logger.trace( message ); 55 checkLogger( target, output, message, throwable, type ); 56 } 57 58 public void testLogkitLoggerTraceDisabled() 59 throws Exception 60 { 61 final Priority level = Priority.ERROR; 62 final String message = "Meep!"; 63 64 final MockLogTarget target = new MockLogTarget(); 65 final LogkitLogger logger = createLogger( target, level ); 66 logger.trace( message ); 67 checkLogger( target, false, null, null, null ); 68 } 69 70 public void testLogkitLoggerTraceWithExceptionEnabled() 71 throws Exception 72 { 73 final Priority level = Priority.DEBUG; 74 final Priority type = Priority.DEBUG; 75 final String message = "Meep!"; 76 final Throwable throwable = new Throwable(); 77 final boolean output = true; 78 79 final MockLogTarget target = new MockLogTarget(); 80 final LogkitLogger logger = createLogger( target, level ); 81 82 logger.trace( message, throwable ); 83 checkLogger( target, output, message, throwable, type ); 84 } 85 86 public void testLogkitLoggerTraceWithExceptionDisabled() 87 throws Exception 88 { 89 final Priority level = Priority.ERROR; 90 final String message = "Meep!"; 91 final Throwable throwable = new Throwable(); 92 93 final MockLogTarget target = new MockLogTarget(); 94 final LogkitLogger logger = createLogger( target, level ); 95 96 logger.trace( message, throwable ); 97 checkLogger( target, false, null, null, null ); 98 } 99 100 public void testLogkitLoggerDebugEnabled() 101 throws Exception 102 { 103 final Priority level = Priority.DEBUG; 104 final Priority type = Priority.DEBUG; 105 final String message = "Meep!"; 106 final Throwable throwable = null; 107 final boolean output = true; 108 109 final MockLogTarget target = new MockLogTarget(); 110 final LogkitLogger logger = createLogger( target, level ); 111 logger.debug( message ); 112 checkLogger( target, output, message, throwable, type ); 113 } 114 115 public void testLogkitLoggerDebugDisabled() 116 throws Exception 117 { 118 final Priority level = Priority.ERROR; 119 final String message = "Meep!"; 120 121 final MockLogTarget target = new MockLogTarget(); 122 final LogkitLogger logger = createLogger( target, level ); 123 logger.debug( message ); 124 checkLogger( target, false, null, null, null ); 125 } 126 127 public void testLogkitLoggerDebugWithExceptionEnabled() 128 throws Exception 129 { 130 final Priority level = Priority.DEBUG; 131 final Priority type = Priority.DEBUG; 132 final String message = "Meep!"; 133 final Throwable throwable = new Throwable(); 134 final boolean output = true; 135 136 final MockLogTarget target = new MockLogTarget(); 137 final LogkitLogger logger = createLogger( target, level ); 138 logger.debug( message, throwable ); 139 checkLogger( target, output, message, throwable, type ); 140 } 141 142 public void testLogkitLoggerDebugWithExceptionDisabled() 143 throws Exception 144 { 145 final Priority level = Priority.ERROR; 146 final String message = "Meep!"; 147 final Throwable throwable = new Throwable(); 148 149 final MockLogTarget target = new MockLogTarget(); 150 final LogkitLogger logger = createLogger( target, level ); 151 logger.debug( message, throwable ); 152 checkLogger( target, false, null, null, null ); 153 } 154 155 public void testLogkitLoggerInfoEnabled() 156 throws Exception 157 { 158 final Priority level = Priority.DEBUG; 159 final Priority type = Priority.INFO; 160 final String message = "Meep!"; 161 final Throwable throwable = null; 162 final boolean output = true; 163 164 final MockLogTarget target = new MockLogTarget(); 165 final LogkitLogger logger = createLogger( target, level ); 166 logger.info( message ); 167 checkLogger( target, output, message, throwable, type ); 168 } 169 170 public void testLogkitLoggerInfoDisabled() 171 throws Exception 172 { 173 final Priority level = Priority.ERROR; 174 final String message = "Meep!"; 175 176 final MockLogTarget target = new MockLogTarget(); 177 final LogkitLogger logger = createLogger( target, level ); 178 logger.info( message ); 179 checkLogger( target, false, null, null, null ); 180 } 181 182 public void testLogkitLoggerInfoWithExceptionEnabled() 183 throws Exception 184 { 185 final Priority level = Priority.DEBUG; 186 final Priority type = Priority.INFO; 187 final String message = "Meep!"; 188 final Throwable throwable = new Throwable(); 189 final boolean output = true; 190 191 final MockLogTarget target = new MockLogTarget(); 192 final LogkitLogger logger = createLogger( target, level ); 193 logger.info( message, throwable ); 194 checkLogger( target, output, message, throwable, type ); 195 } 196 197 public void testLogkitLoggerInfoWithExceptionDisabled() 198 throws Exception 199 { 200 final Priority level = Priority.ERROR; 201 final String message = "Meep!"; 202 final Throwable throwable = new Throwable(); 203 204 final MockLogTarget target = new MockLogTarget(); 205 final LogkitLogger logger = createLogger( target, level ); 206 logger.info( message, throwable ); 207 checkLogger( target, false, null, null, null ); 208 } 209 210 public void testLogkitLoggerWarnEnabled() 211 throws Exception 212 { 213 final Priority level = Priority.DEBUG; 214 final Priority type = Priority.WARN; 215 final String message = "Meep!"; 216 final Throwable throwable = null; 217 final boolean output = true; 218 219 final MockLogTarget target = new MockLogTarget(); 220 final LogkitLogger logger = createLogger( target, level ); 221 logger.warn( message ); 222 checkLogger( target, output, message, throwable, type ); 223 } 224 225 public void testLogkitLoggerWarnDisabled() 226 throws Exception 227 { 228 final Priority level = Priority.ERROR; 229 final String message = "Meep!"; 230 231 final MockLogTarget target = new MockLogTarget(); 232 final LogkitLogger logger = createLogger( target, level ); 233 logger.warn( message ); 234 checkLogger( target, false, null, null, null ); 235 } 236 237 public void testLogkitLoggerWarnWithExceptionEnabled() 238 throws Exception 239 { 240 final Priority level = Priority.DEBUG; 241 final Priority type = Priority.WARN; 242 final String message = "Meep!"; 243 final Throwable throwable = new Throwable(); 244 final boolean output = true; 245 246 final MockLogTarget target = new MockLogTarget(); 247 final LogkitLogger logger = createLogger( target, level ); 248 logger.warn( message, throwable ); 249 checkLogger( target, output, message, throwable, type ); 250 } 251 252 public void testLogkitLoggerWarnWithExceptionDisabled() 253 throws Exception 254 { 255 final Priority level = Priority.ERROR; 256 final String message = "Meep!"; 257 final Throwable throwable = new Throwable(); 258 259 final MockLogTarget target = new MockLogTarget(); 260 final LogkitLogger logger = createLogger( target, level ); 261 logger.warn( message, throwable ); 262 checkLogger( target, false, null, null, null ); 263 } 264 265 public void testLogkitLoggerErrorEnabled() 266 throws Exception 267 { 268 final Priority level = Priority.DEBUG; 269 final Priority type = Priority.ERROR; 270 final String message = "Meep!"; 271 final Throwable throwable = null; 272 final boolean output = true; 273 274 final MockLogTarget target = new MockLogTarget(); 275 final LogkitLogger logger = createLogger( target, level ); 276 logger.error( message ); 277 checkLogger( target, output, message, throwable, type ); 278 } 279 280 public void testLogkitLoggerErrorWithExceptionEnabled() 281 throws Exception 282 { 283 final Priority level = Priority.DEBUG; 284 final Priority type = Priority.ERROR; 285 final String message = "Meep!"; 286 final Throwable throwable = new Throwable(); 287 final boolean output = true; 288 289 final MockLogTarget target = new MockLogTarget(); 290 final LogkitLogger logger = createLogger( target, level ); 291 logger.error( message, throwable ); 292 checkLogger( target, output, message, throwable, type ); 293 } 294 295 public void testConsoleLevelComparisonWithDebugEnabled() 296 throws Exception 297 { 298 final MockLogTarget target = new MockLogTarget(); 299 final LogkitLogger logger = createLogger( target, Priority.DEBUG ); 300 301 assertEquals( "logger.isTraceEnabled()", true, logger.isTraceEnabled() ); 302 assertEquals( "logger.isDebugEnabled()", true, logger.isDebugEnabled() ); 303 assertEquals( "logger.isInfoEnabled()", true, logger.isInfoEnabled() ); 304 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() ); 305 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() ); 306 } 307 308 public void testConsoleLevelComparisonWithInfoEnabled() 309 throws Exception 310 { 311 final MockLogTarget target = new MockLogTarget(); 312 final LogkitLogger logger = createLogger( target, Priority.INFO ); 313 314 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() ); 315 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() ); 316 assertEquals( "logger.isInfoEnabled()", true, logger.isInfoEnabled() ); 317 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() ); 318 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() ); 319 } 320 321 public void testConsoleLevelComparisonWithWarnEnabled() 322 throws Exception 323 { 324 final MockLogTarget target = new MockLogTarget(); 325 final LogkitLogger logger = createLogger( target, Priority.WARN ); 326 327 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() ); 328 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() ); 329 assertEquals( "logger.isInfoEnabled()", false, logger.isInfoEnabled() ); 330 assertEquals( "logger.isWarnEnabled()", true, logger.isWarnEnabled() ); 331 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() ); 332 } 333 334 public void testConsoleLevelComparisonWithErrorEnabled() 335 throws Exception 336 { 337 final MockLogTarget target = new MockLogTarget(); 338 final LogkitLogger logger = createLogger( target, Priority.ERROR ); 339 340 assertEquals( "logger.isTraceEnabled()", false, logger.isTraceEnabled() ); 341 assertEquals( "logger.isDebugEnabled()", false, logger.isDebugEnabled() ); 342 assertEquals( "logger.isInfoEnabled()", false, logger.isInfoEnabled() ); 343 assertEquals( "logger.isWarnEnabled()", false, logger.isWarnEnabled() ); 344 assertEquals( "logger.isErrorEnabled()", true, logger.isErrorEnabled() ); 345 } 346 347 private LogkitLogger createLogger( final MockLogTarget target, 348 final Priority priority ) 349 { 350 final Hierarchy hierarchy = new Hierarchy(); 351 final Logger logkitLogger = hierarchy.getLoggerFor( "test" ); 352 logkitLogger.setLogTargets( new LogTarget[]{target} ); 353 logkitLogger.setPriority( priority ); 354 final LogkitLogger logger = new LogkitLogger( logkitLogger ); 355 return logger; 356 } 357 358 private void checkLogger( final MockLogTarget target, 359 final boolean output, 360 final String message, 361 final Throwable throwable, 362 final Priority priority ) 363 { 364 assertEquals( "logger.m_message == message", message, target.m_message ); 365 assertEquals( "logger.m_output == output", output, target.m_output ); 366 assertEquals( "logger.m_throwable == null", throwable, target.m_throwable ); 367 assertEquals( "logger.m_priority == null", priority, target.m_priority ); 368 } 369 }

This page was automatically generated by Maven