1 17 package org.apache.avalon.framework.logger.test; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.PrintStream ; 21 import java.util.Properties ; 22 23 import org.apache.avalon.framework.logger.CommonsLogger; 24 import org.apache.avalon.framework.logger.Logger; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.commons.logging.impl.SimpleLog; 28 29 import org.jmock.Mock; 30 import org.jmock.MockObjectTestCase; 31 32 39 public class CommonsLoggerTestCase extends MockObjectTestCase 40 { 41 42 private Mock mockLog; 43 private Exception exception; 44 45 protected void setUp() throws Exception 46 { 47 super.setUp(); 48 mockLog = mock(Log.class); 49 exception = new Exception ("JUnit"); 50 } 51 52 55 public void testDebug() 56 { 57 final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit"); 58 59 mockLog.expects(once()).method("isDebugEnabled").will(returnValue(true)); 60 mockLog.expects(once()).method("debug").with(eq("JUnit")); 61 mockLog.expects(once()).method("debug").with(eq("JUnit"), same(exception)); 62 63 if(logger.isDebugEnabled()) 64 { 65 logger.debug("JUnit"); 66 logger.debug("JUnit", exception); 67 } 68 } 69 70 73 public void testInfo() 74 { 75 final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit"); 76 77 mockLog.expects(once()).method("isInfoEnabled").will(returnValue(true)); 78 mockLog.expects(once()).method("info").with(eq("JUnit")); 79 mockLog.expects(once()).method("info").with(eq("JUnit"), same(exception)); 80 81 if(logger.isInfoEnabled()) 82 { 83 logger.info("JUnit"); 84 logger.info("JUnit", exception); 85 } 86 } 87 88 91 public void testWarn() 92 { 93 final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit"); 94 95 mockLog.expects(once()).method("isWarnEnabled").will(returnValue(true)); 96 mockLog.expects(once()).method("warn").with(eq("JUnit")); 97 mockLog.expects(once()).method("warn").with(eq("JUnit"), same(exception)); 98 99 if(logger.isWarnEnabled()) 100 { 101 logger.warn("JUnit"); 102 logger.warn("JUnit", exception); 103 } 104 } 105 106 109 public void testError() 110 { 111 final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit"); 112 113 mockLog.expects(once()).method("isErrorEnabled").will(returnValue(true)); 114 mockLog.expects(once()).method("error").with(eq("JUnit")); 115 mockLog.expects(once()).method("error").with(eq("JUnit"), same(exception)); 116 117 if(logger.isErrorEnabled()) 118 { 119 logger.error("JUnit"); 120 logger.error("JUnit", exception); 121 } 122 } 123 124 127 public void testFatalError() 128 { 129 final Logger logger = new CommonsLogger((Log)mockLog.proxy(), "JUnit"); 130 131 mockLog.expects(once()).method("isFatalEnabled").will(returnValue(true)); 132 mockLog.expects(once()).method("fatal").with(eq("JUnit")); 133 mockLog.expects(once()).method("fatal").with(eq("JUnit"), same(exception)); 134 135 if(logger.isFatalErrorEnabled()) 136 { 137 logger.fatalError("JUnit"); 138 logger.fatalError("JUnit", exception); 139 } 140 } 141 142 145 public void testChildLogger() 146 { 147 final Properties systemProperties = System.getProperties(); 148 final PrintStream err = System.err; 149 try 150 { 151 final ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 152 final PrintStream stream = new PrintStream (buffer, true); 153 System.setProperty(Log.class.getName(), SimpleLog.class.getName()); 154 LogFactory.releaseAll(); 155 System.setErr(stream); 156 final Logger logger = new CommonsLogger(LogFactory.getLog("JUnit"), "JUnit"); 157 final Logger child = logger.getChildLogger("test"); 158 child.fatalError("foo"); 159 assertEquals("[FATAL] JUnit.test - foo", buffer.toString().trim()); 160 } 161 finally 162 { 163 System.setProperties(systemProperties); 164 System.setErr(err); 165 } 166 167 } 168 } 169 170 | Popular Tags |