1 16 17 package org.apache.commons.logging.jdk14; 18 19 20 import java.io.InputStream ; 21 import java.util.Iterator ; 22 import java.util.logging.Handler ; 23 import java.util.logging.Level ; 24 import java.util.logging.LogManager ; 25 import java.util.logging.LogRecord ; 26 import java.util.logging.Logger ; 27 28 import junit.framework.Test; 29 import junit.framework.TestSuite; 30 31 32 40 41 public class CustomConfigTestCase extends DefaultConfigTestCase { 42 43 44 46 47 52 public CustomConfigTestCase(String name) { 53 super(name); 54 } 55 56 57 59 60 63 protected TestHandler handler = null; 64 65 66 69 protected Handler handlers[] = null; 70 71 72 75 protected Logger logger = null; 76 77 78 81 protected LogManager manager = null; 82 83 84 87 protected Level testLevels[] = 88 { Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE, Level.SEVERE }; 89 90 91 94 protected String testMessages[] = 95 { "debug", "info", "warn", "error", "fatal" }; 96 97 98 100 101 104 public void setUp() throws Exception { 105 setUpManager 106 ("org/apache/commons/logging/jdk14/CustomConfig.properties"); 107 setUpLogger("TestLogger"); 108 setUpHandlers(); 109 setUpFactory(); 110 setUpLog("TestLogger"); 111 } 112 113 114 117 public static Test suite() { 118 return (new TestSuite(CustomConfigTestCase.class)); 119 } 120 121 124 public void tearDown() { 125 super.tearDown(); 126 handlers = null; 127 logger = null; 128 manager = null; 129 } 130 131 132 134 135 public void testExceptionMessages() throws Exception { 137 138 logExceptionMessages(); 139 checkLogRecords(true); 140 141 } 142 143 144 public void testPlainMessages() throws Exception { 146 147 logPlainMessages(); 148 checkLogRecords(false); 149 150 } 151 152 153 public void testPristineHandlers() { 155 156 assertNotNull(handlers); 157 assertEquals(1, handlers.length); 158 assertTrue(handlers[0] instanceof TestHandler); 159 assertNotNull(handler); 160 161 } 162 163 164 public void testPristineLogger() { 166 167 assertNotNull("Logger exists", logger); 168 assertEquals("Logger name", "TestLogger", logger.getName()); 169 170 assertTrue(logger.isLoggable(Level.SEVERE)); 172 assertTrue(logger.isLoggable(Level.WARNING)); 173 assertTrue(logger.isLoggable(Level.INFO)); 174 assertTrue(logger.isLoggable(Level.CONFIG)); 175 assertTrue(logger.isLoggable(Level.FINE)); 176 assertTrue(!logger.isLoggable(Level.FINER)); 177 assertTrue(!logger.isLoggable(Level.FINEST)); 178 179 } 180 181 182 public void testSerializable() throws Exception { 184 185 super.testSerializable(); 186 testExceptionMessages(); 187 188 } 189 190 191 193 194 protected void checkLog() { 196 197 assertNotNull("Log exists", log); 198 assertEquals("Log class", 199 "org.apache.commons.logging.impl.Jdk14Logger", 200 log.getClass().getName()); 201 202 assertTrue(log.isFatalEnabled()); 204 assertTrue(log.isErrorEnabled()); 205 assertTrue(log.isWarnEnabled()); 206 assertTrue(log.isInfoEnabled()); 207 assertTrue(log.isDebugEnabled()); 208 assertTrue(!log.isTraceEnabled()); 209 210 } 211 212 213 protected void checkLogRecords(boolean thrown) { 215 Iterator records = handler.records(); 216 for (int i = 0; i < testMessages.length; i++) { 217 assertTrue(records.hasNext()); 218 LogRecord record = (LogRecord ) records.next(); 219 assertEquals("LogRecord level", 220 testLevels[i], record.getLevel()); 221 assertEquals("LogRecord message", 222 testMessages[i], record.getMessage()); 223 assertEquals("LogRecord class", 224 this.getClass().getName(), 225 record.getSourceClassName()); 226 if (thrown) { 227 assertEquals("LogRecord method", 228 "logExceptionMessages", 229 record.getSourceMethodName()); 230 } else { 231 assertEquals("LogRecord method", 232 "logPlainMessages", 233 record.getSourceMethodName()); 234 } 235 if (thrown) { 236 assertNotNull("LogRecord thrown", record.getThrown()); 237 assertTrue("LogRecord thrown type", 238 record.getThrown() instanceof IndexOutOfBoundsException ); 239 } else { 240 assertNull("LogRecord thrown", 241 record.getThrown()); 242 } 243 } 244 assertTrue(!records.hasNext()); 245 handler.flush(); 246 } 247 248 249 protected void logExceptionMessages() { 251 Throwable t = new IndexOutOfBoundsException (); 252 log.trace("trace", t); log.debug("debug", t); 254 log.info("info", t); 255 log.warn("warn", t); 256 log.error("error", t); 257 log.fatal("fatal", t); 258 } 259 260 261 protected void logPlainMessages() { 263 log.trace("trace"); log.debug("debug"); 265 log.info("info"); 266 log.warn("warn"); 267 log.error("error"); 268 log.fatal("fatal"); 269 } 270 271 272 protected void setUpHandlers() throws Exception { 274 Logger parent = logger; 275 while (parent.getParent() != null) { 276 parent = parent.getParent(); 277 } 278 handlers = parent.getHandlers(); 279 if ((handlers != null) && (handlers.length == 1) && 280 (handlers[0] instanceof TestHandler)) { 281 handler = (TestHandler) handlers[0]; 282 } 283 } 284 285 286 protected void setUpLogger(String name) throws Exception { 288 logger = Logger.getLogger(name); 289 } 290 291 292 protected void setUpManager(String config) throws Exception { 294 manager = LogManager.getLogManager(); 295 InputStream is = 296 this.getClass().getClassLoader().getResourceAsStream(config); 297 manager.readConfiguration(is); 298 is.close(); 299 } 300 301 302 } 303 | Popular Tags |