1 4 5 package org.objectweb.util.monolog; 6 7 import junit.framework.Assert; 8 import junit.framework.TestCase; 9 import junit.textui.TestRunner; 10 11 import org.objectweb.util.monolog.api.Handler; 12 import org.objectweb.util.monolog.api.HandlerFactory; 13 14 20 public class TestHandler extends TestCase { 21 22 26 public static final String [] SETTER_METHODS = {"setHandlerFactoryClassName"}; 27 28 protected HandlerFactory hf = null; 29 30 private Handler hc = null; 31 32 public TestHandler() { 33 super(""); 34 } 35 36 40 public void setHandlerFactoryClassName(String lfcn) { 41 try { 42 hf = (HandlerFactory) Class.forName(lfcn).newInstance(); 43 } 44 catch (ClassCastException e) { 45 throw new UnsupportedOperationException ( 46 "The specified class is not a Handler factory: " + lfcn); 47 } 48 catch (Exception e) { 49 throw new UnsupportedOperationException ( 50 "Handler factory class is not availlable: " + lfcn); 51 } 52 } 53 54 62 public static void main(String args[]) { 63 if (args.length < 1) { 64 System.out.println("Syntax error !"); 65 System.out.println("java TestHandler <handler factory class name>"); 66 System.exit(12); 67 } 68 Object [] params = {args[0]}; 69 try { 70 TestSuite suite = 71 new TestSuite(TestHandler.class, SETTER_METHODS, params); 72 TestRunner.run(suite); 73 } 74 catch (Exception e) { 75 e.printStackTrace(); 76 } 77 } 78 79 public static TestSuite getTestSuite(String lfcn) { 80 Object [] params = {lfcn}; 81 try { 82 return new TestSuite(TestHandler.class, SETTER_METHODS, params); 83 } 84 catch (Exception e) { 85 e.printStackTrace(); 86 return null; 87 } 88 } 89 90 93 97 public void testConsoleHandlerDef() { 98 testHandlerDef("console"); 99 } 100 101 105 public void testFileHandlerDef() { 106 testHandlerDef("file"); 107 } 108 109 113 public void testRollingFileHandlerDef() { 114 testHandlerDef("rollingfile"); 115 } 116 117 122 protected void testHandlerDef(String handlerType) { 123 hc = hf.createHandler("MyHandler", handlerType); 124 hc.setAttribute("toto_key", "toto_value"); 125 hc.setAttribute("titi.key", "titi.value"); 126 128 assertEquals("assigns property", 129 "toto_value", hc.getAttribute("toto_key")); 130 assertEquals("assigns property with dot", 131 "titi.value", hc.getAttribute("titi.key")); 132 assertEquals("assigns a handler name", 133 "MyHandler", hc.getName()); 134 135 136 assertEquals("Handler not correctly registered into MonologConf", 137 hc, hf.getHandler("MyHandler")); 138 assertEquals("Handler not correctly registered into MonologConf (time 2)", 139 hc, hf.getHandler("MyHandler")); 140 hc = hf.getHandler("MyHandler"); 141 142 assertEquals("assigns property after getHandlerConf(String)", 143 "toto_value", hc.getAttribute("toto_key")); 144 assertEquals("assigns property with dot after getHandlerConf(String)", 145 "titi.value", hc.getAttribute("titi.key")); 146 assertEquals("assigns a handler name after getHandlerConf(String)", 147 "MyHandler", hc.getName()); 148 149 assertEquals("getHandler() with one HandlerConf", 150 1, hf.getHandlers().length); 151 152 hf.removeHandler("MyHandler"); 153 154 assertEquals("removeHandler with one HandlerConf", 155 0, hf.getHandlers().length); 156 } 157 158 162 public void testgetConsoleHandlerByName() { 163 testgetHandlerByName("console"); 164 } 165 166 170 public void testgetFileHandlerByName() { 171 testgetHandlerByName("file"); 172 } 173 174 178 public void testgetRollingFileHandlerByName() { 179 testgetHandlerByName("rollingfile"); 180 } 181 182 186 protected void testgetHandlerByName(String handlerType) { 187 int iternumber = 20; 188 for (int i = 0; i < iternumber; i++) { 189 hc = hf.createHandler("MyHandler" + i, handlerType); 190 hc.setAttribute("toto_key" + i, "toto_value" + i); 191 hc.setAttribute("titi.key" + i, "titi.value" + i); 192 } 194 for (int i = 0; i < iternumber; i++) { 195 String name = "MyHandler" + i; 196 hc = hf.getHandler(name); 197 assertNotNull("fetch " + name, hc); 198 assertEquals("name value by fetch " + name, name, hc.getName()); 199 200 assertEquals("assigns property by fetch " + name, 201 "toto_value" + i, hc.getAttribute("toto_key" + i)); 202 assertEquals("assigns property with dot by fetch " + name, 203 "titi.value" + i, hc.getAttribute("titi.key" + i)); 204 } 205 } 206 207 211 public void testGetAllHandlerConsole() { 212 testGetAllHandler("console"); 213 } 214 215 219 public void testGetAllHandlerFile() { 220 testGetAllHandler("file"); 221 } 222 223 228 public void testGetAllHandlerRollingFile() { 229 testGetAllHandler("rollingfile"); 230 } 231 232 236 protected void testGetAllHandler(String handlerType) { 237 int iternumber = 20; 238 for (int i = 0; i < iternumber; i++) { 239 hc = hf.createHandler("MyHandler" + i, handlerType); 240 hc.setAttribute("toto_key" + i, "toto_value" + i); 241 hc.setAttribute("titi.key" + i, "titi.value" + i); 242 } 244 Handler[] hcs = hf.getHandlers(); 245 assertEquals("getHandler with " + iternumber + " HandlerConf", 246 iternumber, hcs.length); 247 248 Handler[] hcs2 = new Handler[hcs.length]; 249 for (int i = 0; i < hcs.length; i++) { 250 hc = hcs[i]; 251 int j = Integer.parseInt( 252 hc.getName().substring(9,hc.getName().length())); 253 assertNull("Duplicate HandlerConf", hcs2[j]); 254 hcs2[j] = hc; 255 String name = "MyHandler" + j; 256 assertNotNull("fetch " + name, hc); 257 assertEquals("name value by fetch " + name, name, hc.getName()); 258 259 assertEquals("assigns property by fetch " + name, 260 "toto_value" + j, hc.getAttribute("toto_key" + j)); 261 assertEquals("assigns property with dot by fetch " + name, 262 "titi.value" + j, hc.getAttribute("titi.key" + j)); 263 } 264 } 265 266 270 public void testRemoveConsoleHandler() { 271 testRemoveHandler("console"); 272 } 273 274 278 public void testRemoveFileHandler() { 279 testRemoveHandler("file"); 280 } 281 282 286 public void testRemoveRollingFileHandler() { 287 testRemoveHandler("rollingfile"); 288 } 289 290 293 public void testRemoveHandler(String handlerType) { 294 int iternumber = 20; 295 for (int i = 0; i < iternumber; i++) { 296 hc = hf.createHandler("MyHandler" + i, handlerType); 297 hc.setAttribute("toto_key" + i, "toto_value" + i); 298 hc.setAttribute("titi.key" + i, "titi.value" + i); 299 } 301 302 Handler[] hcs = hf.getHandlers(); 303 for (int i = 0; i < hcs.length; i++) { 304 String name = "MyHandler" + i; 305 hf.removeHandler(name); 306 Assert.assertNull("Remove " + name, hf.getHandler(name)); 307 } 308 } 309 310 314 protected void testgetHandlerByName() { 315 int iternumber = 20; 316 for (int i = 0; i < iternumber; i++) { 317 if (i<(iternumber/3)) { 318 hc = hf.createHandler("MyHandler" + i, "console"); 319 } 320 else if (i<((2*iternumber)/3)) { 321 hc = hf.createHandler("MyHandler" + i, 322 "file"); 323 } 324 else { 325 hc = hf.createHandler("MyHandler" + i, "rollingfile"); 326 } 327 hc.setAttribute("toto_key" + i, "toto_value" + i); 328 hc.setAttribute("titi.key" + i, "titi.value" + i); 329 } 331 for (int i = 0; i < iternumber; i++) { 332 String name = "MyHandler" + i; 333 hc = hf.getHandler(name); 334 assertNotNull("fetch " + name, hc); 335 assertEquals("name value by fetch " + name, 336 name, hc.getName()); 337 338 assertEquals("assigns property by fetch " + name, 339 "toto_value" + i, hc.getAttribute("toto_key" + i)); 340 assertEquals("assigns property with dot by fetch " + name, 341 "titi.value" + i, hc.getAttribute("titi.key" + i)); 342 } 343 } 344 } 345 | Popular Tags |