1 16 17 package org.apache.commons.logging.simple; 18 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.commons.logging.impl.SimpleLog; 32 33 34 41 42 public class DefaultConfigTestCase extends TestCase { 43 44 45 47 48 53 public DefaultConfigTestCase(String name) { 54 super(name); 55 } 56 57 58 60 61 64 protected LogFactory factory = null; 65 66 67 70 protected Log log = null; 71 72 73 75 76 79 public void setUp() throws Exception { 80 setUpFactory(); 81 setUpLog("TestLogger"); 82 } 83 84 85 88 public static Test suite() { 89 return (new TestSuite(DefaultConfigTestCase.class)); 90 } 91 92 95 public void tearDown() { 96 log = null; 97 factory = null; 98 LogFactory.releaseAll(); 99 } 100 101 102 104 105 public void testPristineDecorated() { 107 108 setUpDecorated("DecoratedLogger"); 109 checkDecorated(); 110 111 } 112 113 114 public void testPristineLog() { 116 117 checkStandard(); 118 119 } 120 121 122 public void testPristineFactory() { 124 125 assertNotNull("LogFactory exists", factory); 126 assertEquals("LogFactory class", 127 "org.apache.commons.logging.impl.LogFactoryImpl", 128 factory.getClass().getName()); 129 130 String names[] = factory.getAttributeNames(); 131 assertNotNull("Names exists", names); 132 assertEquals("Names empty", 0, names.length); 133 134 } 135 136 137 public void testSerializable() throws Exception { 139 140 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 142 ObjectOutputStream oos = new ObjectOutputStream (baos); 143 oos.writeObject(log); 144 oos.close(); 145 ByteArrayInputStream bais = 146 new ByteArrayInputStream (baos.toByteArray()); 147 ObjectInputStream ois = new ObjectInputStream (bais); 148 log = (Log) ois.readObject(); 149 ois.close(); 150 151 checkStandard(); 153 154 } 155 156 157 159 160 161 protected void checkDecorated() { 163 164 assertNotNull("Log exists", log); 165 assertEquals("Log class", 166 "org.apache.commons.logging.simple.DecoratedSimpleLog", 167 log.getClass().getName()); 168 169 assertTrue(!log.isDebugEnabled()); 171 assertTrue(log.isErrorEnabled()); 172 assertTrue(log.isFatalEnabled()); 173 assertTrue(log.isInfoEnabled()); 174 assertTrue(!log.isTraceEnabled()); 175 assertTrue(log.isWarnEnabled()); 176 177 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel()); 179 180 assertEquals("yyyy/MM/dd HH:mm:ss:SSS zzz", 182 ((DecoratedSimpleLog) log).getDateTimeFormat()); 183 assertEquals("DecoratedLogger", 184 ((DecoratedSimpleLog) log).getLogName()); 185 assertTrue(!((DecoratedSimpleLog) log).getShowDateTime()); 186 assertTrue(((DecoratedSimpleLog) log).getShowShortName()); 187 188 } 189 190 191 protected void checkStandard() { 193 194 assertNotNull("Log exists", log); 195 assertEquals("Log class", 196 "org.apache.commons.logging.impl.SimpleLog", 197 log.getClass().getName()); 198 199 assertTrue(!log.isDebugEnabled()); 201 assertTrue(log.isErrorEnabled()); 202 assertTrue(log.isFatalEnabled()); 203 assertTrue(log.isInfoEnabled()); 204 assertTrue(!log.isTraceEnabled()); 205 assertTrue(log.isWarnEnabled()); 206 207 assertEquals(SimpleLog.LOG_LEVEL_INFO, ((SimpleLog) log).getLevel()); 209 210 } 211 212 213 protected void setUpDecorated(String name) { 215 log = new DecoratedSimpleLog(name); 216 } 217 218 219 protected void setUpFactory() throws Exception { 221 factory = LogFactory.getFactory(); 222 } 223 224 225 protected void setUpLog(String name) throws Exception { 227 log = LogFactory.getLog(name); 228 } 229 230 231 } 232 | Popular Tags |