1 16 17 package org.apache.commons.logging.simple; 18 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import junit.framework.Test; 25 import junit.framework.TestSuite; 26 27 import org.apache.commons.logging.impl.SimpleLog; 28 29 30 37 public class CustomConfigTestCase extends DefaultConfigTestCase { 38 39 40 42 43 48 public CustomConfigTestCase(String name) { 49 super(name); 50 } 51 52 53 55 56 59 protected List expected; 60 61 62 65 69 70 71 74 protected String testMessages[] = 75 { "debug", "info", "warn", "error", "fatal" }; 76 77 78 80 81 84 public void setUp() throws Exception { 85 expected = new ArrayList (); 86 setUpFactory(); 87 setUpLog("DecoratedLogger"); 88 } 89 90 91 94 public static Test suite() { 95 return (new TestSuite(CustomConfigTestCase.class)); 96 } 97 98 101 public void tearDown() { 102 super.tearDown(); 103 expected = null; 104 } 105 106 107 109 110 public void testExceptionMessages() throws Exception { 112 113 ((DecoratedSimpleLog) log).clearCache(); 114 logExceptionMessages(); 115 checkExpected(); 116 117 } 118 119 120 public void testPlainMessages() throws Exception { 122 123 ((DecoratedSimpleLog) log).clearCache(); 124 logPlainMessages(); 125 checkExpected(); 126 127 } 128 129 130 public void testSerializable() throws Exception { 132 133 ((DecoratedSimpleLog) log).clearCache(); 134 logPlainMessages(); 135 super.testSerializable(); 136 logExceptionMessages(); 137 checkExpected(); 138 139 } 140 141 142 144 145 protected void checkDecorated() { 147 148 assertNotNull("Log exists", log); 149 assertEquals("Log class", 150 "org.apache.commons.logging.simple.DecoratedSimpleLog", 151 log.getClass().getName()); 152 153 assertTrue(log.isDebugEnabled()); 155 assertTrue(log.isErrorEnabled()); 156 assertTrue(log.isFatalEnabled()); 157 assertTrue(log.isInfoEnabled()); 158 assertTrue(!log.isTraceEnabled()); 159 assertTrue(log.isWarnEnabled()); 160 161 assertEquals(SimpleLog.LOG_LEVEL_DEBUG, ((SimpleLog) log).getLevel()); 163 164 checkDecoratedDateTime(); 166 assertEquals("DecoratedLogger", 167 ((DecoratedSimpleLog) log).getLogName()); 168 checkShowDateTime(); 169 assertTrue(((DecoratedSimpleLog) log).getShowShortName()); 170 171 } 172 173 174 protected void checkShowDateTime() { 175 assertTrue(!((DecoratedSimpleLog) log).getShowDateTime()); 176 } 177 178 179 protected void checkDecoratedDateTime() { 180 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz", 181 ((DecoratedSimpleLog) log).getDateTimeFormat()); 182 } 183 184 185 186 protected void checkExpected() { 188 189 List acts = ((DecoratedSimpleLog) log).getCache(); 190 Iterator exps = expected.iterator(); 191 int n = 0; 192 while (exps.hasNext()) { 193 LogRecord exp = (LogRecord) exps.next(); 194 LogRecord act = (LogRecord) acts.get(n++); 195 assertEquals("Row " + n + " type", exp.type, act.type); 196 assertEquals("Row " + n + " message", exp.message, act.message); 197 assertEquals("Row " + n + " throwable", exp.t, act.t); 198 } 199 200 } 201 202 203 protected void checkStandard() { 205 206 checkDecorated(); 207 208 } 209 210 211 protected void logExceptionMessages() { 213 214 Throwable t = new IndexOutOfBoundsException (); 216 log.trace("trace", t); log.debug("debug", t); 218 log.info("info", t); 219 log.warn("warn", t); 220 log.error("error", t); 221 log.fatal("fatal", t); 222 223 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", t)); 225 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", t)); 226 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", t)); 227 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", t)); 228 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", t)); 229 230 } 231 232 233 protected void logPlainMessages() { 235 236 log.trace("trace"); log.debug("debug"); 239 log.info("info"); 240 log.warn("warn"); 241 log.error("error"); 242 log.fatal("fatal"); 243 244 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_DEBUG, "debug", null)); 246 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_INFO, "info", null)); 247 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_WARN, "warn", null)); 248 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_ERROR, "error", null)); 249 expected.add(new LogRecord(SimpleLog.LOG_LEVEL_FATAL, "fatal", null)); 250 251 } 252 253 254 } 255 | Popular Tags |