1 package org.grlea.log.test.commons; 2 3 6 18 19 import org.grlea.log.DebugLevel; 20 import org.grlea.log.SimpleLog; 21 import org.grlea.log.adapters.commons.CommonsLoggingAdapter; 22 23 import junit.framework.TestCase; 24 import junit.framework.TestSuite; 25 26 import java.io.BufferedReader ; 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.IOException ; 30 import java.io.InputStreamReader ; 31 import java.io.PrintWriter ; 32 import java.lang.reflect.Field ; 33 import java.lang.reflect.Method ; 34 import java.lang.reflect.InvocationTargetException ; 35 import java.util.Properties ; 36 37 43 public class 44 TestOfCommonsLoggingAdapter 45 extends TestCase 46 { 47 private ByteArrayOutputStream outputStream; 48 49 public 50 TestOfCommonsLoggingAdapter(String name) 51 { 52 super(name); 54 } 55 56 static int instanceNumber = 1; 57 58 protected void 59 setUp() 60 throws Exception 61 { 62 outputStream = new ByteArrayOutputStream (512); 63 64 SimpleLog simpleLog = SimpleLog.defaultInstance(); 65 Properties simpleLogProperties = getLogProperties(simpleLog); 66 simpleLogProperties.clear(); 67 simpleLog.reloadProperties(); 68 simpleLog.setWriter(new PrintWriter (outputStream, true)); 69 70 System.setProperty("org.apache.commons.logging.Log", CommonsLoggingAdapter.class.getName()); 71 } 72 73 private Properties 74 getLogProperties(SimpleLog simpleLog) 75 throws NoSuchFieldException , IllegalAccessException 76 { 77 Field propertiesField = simpleLog.getClass().getDeclaredField("properties"); 78 propertiesField.setAccessible(true); 79 return (Properties ) propertiesField.get(simpleLog); 80 } 81 82 protected void 83 tearDown() 84 { 85 outputStream = null; 86 } 87 88 public void 89 testNormalLogging() 90 throws Exception 91 { 92 new SimpleCommonsLoggingClass().doSomeLogging(); 93 94 String [] expectedOutputLineParts = 95 { 96 " |main|SimpleCommonsLoggingClass|Test of Fatal", 97 " |main|SimpleCommonsLoggingClass|Test of Error", 98 " |main|SimpleCommonsLoggingClass|Test of Warn", 99 " |main|SimpleCommonsLoggingClass|Test of Info", 100 }; 101 102 checkOutput(expectedOutputLineParts, true); 103 } 104 105 public void 106 testFatalLogging() 107 throws Exception 108 { 109 Properties logProperties = getLogProperties(SimpleLog.defaultInstance()); 110 logProperties.setProperty(SimpleCommonsLoggingClass.class.getName(), "Fatal"); 111 reconfigureLoggers(); 112 new SimpleCommonsLoggingClass().doSomeLogging(); 113 114 String [] expectedOutputLineParts = 115 { 116 " |main|SimpleCommonsLoggingClass|Test of Fatal", 117 }; 118 119 checkOutput(expectedOutputLineParts, true); 120 } 121 122 private void 123 reconfigureLoggers() 124 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException 125 { 126 Method reconfigureMethod = SimpleLog.class.getDeclaredMethod("reconfigureAllLoggers", new Class [0]); 127 reconfigureMethod.setAccessible(true); 128 reconfigureMethod.invoke(SimpleLog.defaultInstance(), new Object [0]); 129 } 130 131 public void 132 testTracingWhenVerboseOn() 133 throws Exception 134 { 135 SimpleLog.defaultInstance().setDefaultLevel(DebugLevel.L6_VERBOSE); 136 137 new SimpleCommonsLoggingClass().doSomeTracing(); 138 139 String [] expectedOutputLineParts = 140 { 141 " |main|SimpleCommonsLoggingClass|IN: doSomeTracing()", 142 " |main|SimpleCommonsLoggingClass|OUT: doSomeTracing()", 143 }; 144 145 checkOutput(expectedOutputLineParts, true); 146 } 147 148 public void 149 testTracingWhenVerboseOff() 150 throws Exception 151 { 152 SimpleLog.defaultInstance().setDefaultLevel(DebugLevel.L5_DEBUG); 153 new SimpleCommonsLoggingClass().doSomeTracing(); 154 155 checkOutput(new String [0], true); 156 } 157 158 public void 159 testLogginAnError() 160 throws Exception 161 { 162 SimpleLog.defaultInstance().setDefaultLevel(DebugLevel.L4_INFO); 163 new SimpleCommonsLoggingClass().logAnError(); 164 165 String [] expectedOutputLineParts = 166 { 167 " |main|SimpleCommonsLoggingClass|Test of Exception", 168 "***|main|SimpleCommonsLoggingClass|java.lang.Throwable: Test Message", 169 }; 170 171 checkOutput(expectedOutputLineParts, false); 172 } 173 174 public void 175 testLoggerNameThatIsNotAClassName() 176 throws Exception 177 { 178 String loggerName = "MadeUpLoggerName"; 179 SimpleLog.defaultInstance().setDefaultLevel(DebugLevel.L4_INFO); 180 new SimpleCommonsLoggingClass(loggerName).doSomeLogging(); 181 182 String [] expectedOutputLineParts = 183 { 184 " |main|Log[MadeUpLoggerName]|Test of Fatal", 185 " |main|Log[MadeUpLoggerName]|Test of Error", 186 " |main|Log[MadeUpLoggerName]|Test of Warn", 187 " |main|Log[MadeUpLoggerName]|Test of Info", 188 }; 189 190 checkOutput(expectedOutputLineParts, true); 191 } 192 193 public void 194 testConfiguringLoggerNameThatIsNotAClassName() 195 throws Exception 196 { 197 String loggerName = "MadeUpLoggerName"; 198 getLogProperties(SimpleLog.defaultInstance()) 199 .setProperty("org.apache.commons.logging.Log.MadeUpLoggerName", "Error"); 200 reconfigureLoggers(); 201 new SimpleCommonsLoggingClass(loggerName).doSomeLogging(); 202 203 String [] expectedOutputLineParts = 204 { 205 " |main|Log[MadeUpLoggerName]|Test of Fatal", 206 " |main|Log[MadeUpLoggerName]|Test of Error", 207 }; 208 209 checkOutput(expectedOutputLineParts, true); 210 } 211 212 216 public static TestSuite 217 suite() 218 { 219 return new TestSuite(TestOfCommonsLoggingAdapter.class); 220 } 221 222 223 protected void 224 checkOutput(String [] expectedOutputLineParts, boolean failWhenExtraLinesDetected) 225 throws IOException 226 { 227 System.err.flush(); 228 229 byte[] output = outputStream.toByteArray(); 230 ByteArrayInputStream byteIn = new ByteArrayInputStream (output); 231 InputStreamReader streamReader = new InputStreamReader (byteIn); 232 BufferedReader in = new BufferedReader (streamReader); 233 String outputLine; 234 int lineNumber = 0; 235 while ((outputLine = in.readLine()) != null) 236 { 237 if (lineNumber >= expectedOutputLineParts.length) 238 { 239 if (failWhenExtraLinesDetected) 240 fail("More output lines than expected.\nExtra line: " + outputLine); 241 else 242 break; 243 } 244 245 String expectedOutputLinePart = expectedOutputLineParts[lineNumber]; 246 boolean linePartFound = outputLine.indexOf(expectedOutputLinePart) != -1; 247 assertEquals("'" + expectedOutputLinePart + "' found in '" + outputLine + "'", 248 true, linePartFound); 249 250 lineNumber++; 251 } 252 253 assertEquals("output lines", expectedOutputLineParts.length, lineNumber); 254 } 255 } | Popular Tags |