| 1 package org.grlea.log.test.slf4j; 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 TestOfSlf4jAdapter 45 extends TestCase 46 { 47 private ByteArrayOutputStream outputStream; 48 49 public 50 TestOfSlf4jAdapter(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 71 private Properties  72 getLogProperties(SimpleLog simpleLog) 73 throws NoSuchFieldException , IllegalAccessException  74 { 75 Field propertiesField = simpleLog.getClass().getDeclaredField("properties"); 76 propertiesField.setAccessible(true); 77 return (Properties ) propertiesField.get(simpleLog); 78 } 79 80 protected void 81 tearDown() 82 { 83 outputStream = null; 84 } 85 86 public void 87 testNormalLogging() 88 throws Exception  89 { 90 new SimpleSlf4jLoggingClass().doSomeLogging(); 91 92 String [] expectedOutputLineParts = 93 { 94 " |main|SimpleSlf4jLoggingClass|Test of Error", 95 " |main|SimpleSlf4jLoggingClass|Test of Warn", 96 " |main|SimpleSlf4jLoggingClass|Test of Info", 97 }; 98 99 checkOutput(expectedOutputLineParts, true); 100 } 101 102 public void 103 testErrorLogging() 104 throws Exception  105 { 106 Properties logProperties = getLogProperties(SimpleLog.defaultInstance()); 107 logProperties.setProperty(SimpleSlf4jLoggingClass.class.getName(), "Error"); 108 reconfigureLoggers(); 109 new SimpleSlf4jLoggingClass().doSomeLogging(); 110 111 String [] expectedOutputLineParts = 112 { 113 " |main|SimpleSlf4jLoggingClass|Test of Error", 114 }; 115 116 checkOutput(expectedOutputLineParts, true); 117 } 118 119 private void 120 reconfigureLoggers() 121 throws NoSuchMethodException , IllegalAccessException , InvocationTargetException  122 { 123 Method reconfigureMethod = SimpleLog.class.getDeclaredMethod("reconfigureAllLoggers", new Class [0]); 124 reconfigureMethod.setAccessible(true); 125 reconfigureMethod.invoke(SimpleLog.defaultInstance(), new Object [0]); 126 } 127 128 public void 129 testLogginAnError() 130 throws Exception  131 { 132 SimpleLog.defaultInstance().setDefaultLevel(DebugLevel.L4_INFO); 133 new SimpleSlf4jLoggingClass().logAnError(); 134 135 String [] expectedOutputLineParts = 136 { 137 " |main|SimpleSlf4jLoggingClass|Test of Exception", 138 "***|main|SimpleSlf4jLoggingClass|java.lang.Throwable: Test Message", 139 }; 140 141 checkOutput(expectedOutputLineParts, false); 142 } 143 144 public void 145 testLoggerNameThatIsNotAClassName() 146 throws Exception  147 { 148 String loggerName = "MadeUpLoggerName"; 149 SimpleLog.defaultInstance().setDefaultLevel(DebugLevel.L4_INFO); 150 new SimpleSlf4jLoggingClass(loggerName).doSomeLogging(); 151 152 String [] expectedOutputLineParts = 153 { 154 " |main|Logger[MadeUpLoggerName]|Test of Error", 155 " |main|Logger[MadeUpLoggerName]|Test of Warn", 156 " |main|Logger[MadeUpLoggerName]|Test of Info", 157 }; 158 159 checkOutput(expectedOutputLineParts, true); 160 } 161 162 public void 163 testConfiguringLoggerNameThatIsNotAClassName() 164 throws Exception  165 { 166 String loggerName = "MadeUpLoggerName"; 167 getLogProperties(SimpleLog.defaultInstance()) 168 .setProperty("org.slf4j.Logger.MadeUpLoggerName", "Error"); 169 reconfigureLoggers(); 170 new SimpleSlf4jLoggingClass(loggerName).doSomeLogging(); 171 172 String [] expectedOutputLineParts = 173 { 174 " |main|Logger[MadeUpLoggerName]|Test of Error", 175 }; 176 177 checkOutput(expectedOutputLineParts, true); 178 } 179 180 184 public static TestSuite 185 suite() 186 { 187 return new TestSuite(TestOfSlf4jAdapter.class); 188 } 189 190 191 protected void 192 checkOutput(String [] expectedOutputLineParts, boolean failWhenExtraLinesDetected) 193 throws IOException  194 { 195 System.err.flush(); 196 197 byte[] output = outputStream.toByteArray(); 198 ByteArrayInputStream byteIn = new ByteArrayInputStream (output); 199 InputStreamReader streamReader = new InputStreamReader (byteIn); 200 BufferedReader in = new BufferedReader (streamReader); 201 String outputLine; 202 int lineNumber = 0; 203 while ((outputLine = in.readLine()) != null) 204 { 205 if (lineNumber >= expectedOutputLineParts.length) 206 { 207 if (failWhenExtraLinesDetected) 208 fail("More output lines than expected.\nExtra line: " + outputLine); 209 else 210 break; 211 } 212 213 String expectedOutputLinePart = expectedOutputLineParts[lineNumber]; 214 boolean linePartFound = outputLine.indexOf(expectedOutputLinePart) != -1; 215 assertEquals("'" + expectedOutputLinePart + "' not found in '" + outputLine + "'", 216 true, linePartFound); 217 218 lineNumber++; 219 } 220 221 assertEquals("output lines", expectedOutputLineParts.length, lineNumber); 222 } 223 } | Popular Tags |