1 /*
2 * Copyright (C) The Spice Group. All rights reserved.
3 *
4 * This software is published under the terms of the Spice
5 * Software License version 1.1, a copy of which has been included
6 * with this distribution in the LICENSE.txt file.
7 */
8 package org.codehaus.spice.loggerstore;
9
10 import java.util.HashMap;
11 import java.util.Properties;
12 import org.apache.avalon.excalibur.logger.SimpleLogKitManager;
13 import org.apache.avalon.framework.configuration.Configuration;
14 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
15 import org.codehaus.spice.loggerstore.factories.ConsoleLoggerStoreFactory;
16 import org.codehaus.spice.loggerstore.factories.DOMLog4JLoggerStoreFactory;
17 import org.codehaus.spice.loggerstore.factories.ExcaliburLogKitLoggerStoreFactory;
18 import org.codehaus.spice.loggerstore.factories.InitialLoggerStoreFactory;
19 import org.codehaus.spice.loggerstore.factories.Jdk14LoggerStoreFactory;
20 import org.codehaus.spice.loggerstore.factories.LogKitLoggerStoreFactory;
21 import org.codehaus.spice.loggerstore.factories.PropertyLog4JLoggerStoreFactory;
22 import org.codehaus.spice.loggerstore.factories.SimpleLogKitLoggerStoreFactory;
23 import org.jcontainer.dna.Logger;
24 import org.jcontainer.dna.impl.ConsoleLogger;
25 import org.jcontainer.dna.impl.ContainerUtil;
26 import org.w3c.dom.Element;
27
28 /***
29 * Test case for LoggerStoreFactory
30 *
31 * @author <a href="mailto:mauro.talevi at aquilonia.org">Mauro Talevi</a>
32 * @author Peter Donald
33 */
34 public class LoggerStoreFactoryTestCase
35 extends AbstractTestCase
36 {
37 public LoggerStoreFactoryTestCase( final String name )
38 {
39 super( name );
40 }
41
42 // InitialLoggerStoreFactory tests
43 public void testInitialLoggerStoreFactoryFromConfigurerClassLoader()
44 throws Exception
45 {
46 Thread.currentThread().setContextClassLoader( null );
47 final HashMap config = new HashMap();
48 runFactoryTest( new InitialLoggerStoreFactory(),
49 ConsoleLogger.LEVEL_DEBUG,
50 config,
51 "log4j-properties" );
52 }
53
54 public void testInitialLoggerStoreFactoryUsingDefaults()
55 throws Exception
56 {
57 final HashMap config = new HashMap();
58 config.put( ClassLoader.class.getName(),
59 ClassLoader.getSystemClassLoader().getParent() );
60 final InitialLoggerStoreFactory factory = new InitialLoggerStoreFactory();
61 ContainerUtil.enableLogging( factory,
62 new ConsoleLogger(
63 ConsoleLogger.LEVEL_DEBUG ) );
64 try
65 {
66 factory.createLoggerStore( config );
67 fail(
68 "Expected to not be able to create LoggerStoreFactory as no type was specified or on ClassPath" );
69 }
70 catch( final Exception e )
71 {
72 }
73 }
74
75 public void testInitialLoggerStoreFactoryUsingSpecifiedType()
76 throws Exception
77 {
78 final HashMap config = new HashMap();
79 config.put( InitialLoggerStoreFactory.INITIAL_FACTORY,
80 ConsoleLoggerStoreFactory.class.getName() );
81 final InitialLoggerStoreFactory factory = new InitialLoggerStoreFactory();
82 ContainerUtil.enableLogging( factory,
83 new ConsoleLogger(
84 ConsoleLogger.LEVEL_DEBUG ) );
85 final LoggerStore store = factory.createLoggerStore( config );
86 performConsoleTest( store, ConsoleLogger.LEVEL_DEBUG );
87 }
88
89 public void testInitialLoggerStoreFactoryWithInvalidType()
90 throws Exception
91 {
92 final HashMap config = new HashMap();
93 config.put( InitialLoggerStoreFactory.INITIAL_FACTORY, "Blah" );
94 final InitialLoggerStoreFactory factory = new InitialLoggerStoreFactory();
95 ContainerUtil.enableLogging( factory,
96 new ConsoleLogger(
97 ConsoleLogger.LEVEL_DEBUG ) );
98 try
99 {
100 factory.createLoggerStore( config );
101 fail( "Expected exception as invalid type specified" );
102 }
103 catch( final Exception e )
104 {
105 }
106 }
107
108 public void testInitialLoggerStoreFactoryFromSpecifiedClassLoader()
109 throws Exception
110 {
111 Thread.currentThread().setContextClassLoader( null );
112 final HashMap config = new HashMap();
113 config.put( ClassLoader.class.getName(),
114 InitialLoggerStoreFactory.class.getClassLoader() );
115 runFactoryTest( new InitialLoggerStoreFactory(),
116 ConsoleLogger.LEVEL_DEBUG,
117 config,
118 "log4j-properties" );
119 }
120
121 public void testInitialLoggerStoreFactoryFromContextClassLoader()
122 throws Exception
123 {
124 Thread.currentThread().setContextClassLoader(
125 InitialLoggerStoreFactory.class.getClassLoader() );
126 final HashMap config = new HashMap();
127 runFactoryTest( new InitialLoggerStoreFactory(),
128 ConsoleLogger.LEVEL_DEBUG,
129 config,
130 "log4j-properties" );
131 }
132
133 // LogKitLoggerStoreFactory tests
134 public void testLogKitLoggerStoreFactoryInvalidInput()
135 throws Exception
136 {
137 runInvalidInputData( new LogKitLoggerStoreFactory() );
138 }
139
140 public void testLogKitLoggerStoreFactoryWithNullContextClassLoader()
141 throws Exception
142 {
143 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
144 final HashMap config = new HashMap();
145 config.put( Configuration.class.getName(),
146 builder.build( getResource( "logkit-excalibur.xml" ) ) );
147 ClassLoader contextClassLoader = Thread.currentThread()
148 .getContextClassLoader();
149 Thread.currentThread().setContextClassLoader( null );
150 final MockLogKitLoggerStoreFactory factory = new MockLogKitLoggerStoreFactory();
151 assertEquals( "LogKit ClassLoader",
152 factory.getClassLoader( config ),
153 LogKitLoggerStoreFactory.class.getClassLoader() );
154 Thread.currentThread().setContextClassLoader( contextClassLoader );
155 }
156
157 public void testLogKitLoggerStoreFactoryWithSpecifiedClassLoader()
158 throws Exception
159 {
160 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
161 final HashMap config = new HashMap();
162 config.put( Configuration.class.getName(),
163 builder.build( getResource( "logkit-excalibur.xml" ) ) );
164 config.put( ClassLoader.class.getName(),
165 LogKitLoggerStoreFactory.class.getClassLoader() );
166 runFactoryTest( new LogKitLoggerStoreFactory(),
167 ConsoleLogger.LEVEL_DEBUG,
168 config,
169 "logkit-excalibur" );
170 }
171
172 public void testLogKitLoggerStoreFactoryWithSpecifiedAvalonLogger()
173 throws Exception
174 {
175 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
176 final HashMap config = new HashMap();
177 config.put( Configuration.class.getName(),
178 builder.build( getResource( "logkit-excalibur.xml" ) ) );
179 config.put( org.apache.avalon.framework.logger.Logger.class.getName(),
180 new org.apache.avalon.framework.logger.ConsoleLogger() );
181
182 runFactoryTest( new LogKitLoggerStoreFactory(),
183 ConsoleLogger.LEVEL_INFO,
184 config,
185 "logkit-excalibur" );
186 }
187
188 public void testLogKitLoggerStoreFactoryWithConfigurationAndDefaultLoggerManager()
189 throws Exception
190 {
191 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
192 final HashMap config = new HashMap();
193 config.put( Configuration.class.getName(),
194 builder.build( getResource( "logkit-excalibur.xml" ) ) );
195
196 runFactoryTest( new LogKitLoggerStoreFactory(),
197 ConsoleLogger.LEVEL_INFO,
198 config,
199 "logkit-excalibur" );
200 }
201
202 public void testLogKitLoggerStoreFactoryWithConfigurationAndLoggerManager()
203 throws Exception
204 {
205 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
206 final HashMap config = new HashMap();
207 config.put( Configuration.class.getName(),
208 builder.build( getResource( "logkit-simple.xml" ) ) );
209 config.put( LogKitLoggerStoreFactory.LOGGER_MANAGER,
210 SimpleLogKitManager.class.getName() );
211
212 runFactoryTest( new LogKitLoggerStoreFactory(),
213 ConsoleLogger.LEVEL_INFO,
214 config,
215 "logkit-simple" );
216 }
217
218 public void testLogKitLoggerStoreFactoryWithStreamsAndDefaultLoggerManager()
219 throws Exception
220 {
221 final HashMap config = new HashMap();
222 runStreamBasedFactoryTest( "logkit-excalibur.xml",
223 new LogKitLoggerStoreFactory(),
224 ConsoleLogger.LEVEL_INFO,
225 "logkit-excalibur",
226 config );
227 }
228
229 public void testLogKitLoggerStoreFactoryWithStreamsAndLoggerManager()
230 throws Exception
231 {
232
233 final HashMap config = new HashMap();
234 config.put( LogKitLoggerStoreFactory.LOGGER_MANAGER,
235 SimpleLogKitManager.class.getName() );
236 runStreamBasedFactoryTest( "logkit-simple.xml",
237 new LogKitLoggerStoreFactory(),
238 ConsoleLogger.LEVEL_INFO,
239 "logkit-simple",
240 config );
241 }
242
243 public void testLogKitLoggerStoreFactoryWithStreamsAndInvalidLoggerManager()
244 throws Exception
245 {
246
247 final HashMap config = new HashMap();
248 config.put( LogKitLoggerStoreFactory.LOGGER_MANAGER,
249 "SomeInvalidLoggerManager" );
250 try
251 {
252 runStreamBasedFactoryTest( "logkit-simple.xml",
253 new LogKitLoggerStoreFactory(),
254 ConsoleLogger.LEVEL_INFO,
255 "logkit-simple",
256 config );
257 fail( "Expected to not be able to create LoggerManager " );
258 }
259 catch( final Exception e )
260 {
261 }
262 }
263
264 public void testExcaliburLogKitLoggerStoreFactoryInvalidInput()
265 throws Exception
266 {
267 runInvalidInputData( new ExcaliburLogKitLoggerStoreFactory() );
268 }
269
270 public void testExcaliburLogKitLoggerStoreFactoryWithConfiguration()
271 throws Exception
272 {
273 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
274 final HashMap config = new HashMap();
275 config.put( Configuration.class.getName(),
276 builder.build( getResource( "logkit-excalibur.xml" ) ) );
277 config.put( Logger.class.getName(), new ConsoleLogger() );
278
279 runFactoryTest( new ExcaliburLogKitLoggerStoreFactory(),
280 ConsoleLogger.LEVEL_INFO,
281 config,
282 "logkit-excalibur" );
283 }
284
285 public void testExcaliburLogKitLoggerStoreFactoryWithStreams()
286 throws Exception
287 {
288 runStreamBasedFactoryTest( "logkit-excalibur.xml",
289 new ExcaliburLogKitLoggerStoreFactory(),
290 ConsoleLogger.LEVEL_INFO,
291 "logkit-excalibur",
292 new HashMap() );
293 }
294
295 public void testSimpleLogKitLoggerStoreFactoryInvalidInput()
296 throws Exception
297 {
298 runInvalidInputData( new SimpleLogKitLoggerStoreFactory() );
299 }
300
301 public void testSimpleLogKitLoggerStoreFactoryWithConfiguration()
302 throws Exception
303 {
304 final DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
305 final HashMap config = new HashMap();
306 config.put( Configuration.class.getName(),
307 builder.build( getResource( "logkit-simple.xml" ) ) );
308 config.put( Logger.class.getName(), new ConsoleLogger() );
309
310 runFactoryTest( new SimpleLogKitLoggerStoreFactory(),
311 ConsoleLogger.LEVEL_INFO,
312 config,
313 "logkit-simple" );
314 }
315
316 public void testSimpleLogKitLoggerStoreFactoryWithStreams()
317 throws Exception
318 {
319 final HashMap config = new HashMap();
320 runStreamBasedFactoryTest( "logkit-simple.xml",
321 new SimpleLogKitLoggerStoreFactory(),
322 ConsoleLogger.LEVEL_INFO,
323 "logkit-simple",
324 config );
325 }
326
327 // Log4JLoggerStoreFactory tests
328 public void testPropertyLog4jLoggerStoreFactoryInvalidInput()
329 throws Exception
330 {
331 runInvalidInputData( new PropertyLog4JLoggerStoreFactory() );
332 }
333
334 public void testDOMLog4jLoggerStoreFactoryInvalidInput()
335 throws Exception
336 {
337 runInvalidInputData( new DOMLog4JLoggerStoreFactory() );
338 }
339
340 public void testLog4jLoggerStoreFactoryWithProperties()
341 throws Exception
342 {
343 final Properties properties = new Properties();
344 properties.load( getResource( "log4j.properties" ) );
345 final HashMap config = new HashMap();
346 config.put( Properties.class.getName(), properties );
347
348 runFactoryTest( new PropertyLog4JLoggerStoreFactory(),
349 ConsoleLogger.LEVEL_DEBUG,
350 config,
351 "log4j-properties" );
352 }
353
354 public void testLog4jLoggerStoreFactoryWithElement()
355 throws Exception
356 {
357 final HashMap config = new HashMap();
358 final Element element =
359 buildElement( getResource( "log4j.xml" ),
360 new org.apache.log4j.xml.Log4jEntityResolver(),
361 null );
362 config.put( Element.class.getName(), element );
363
364 runFactoryTest( new DOMLog4JLoggerStoreFactory(),
365 ConsoleLogger.LEVEL_DEBUG,
366 config,
367 "log4j-xml" );
368 }
369
370 public void testLog4JLoggerStoreFactoryWithStreamsAsXML()
371 throws Exception
372 {
373 runStreamBasedFactoryTest( "log4j.xml",
374 new DOMLog4JLoggerStoreFactory(),
375 ConsoleLogger.LEVEL_DEBUG,
376 "log4j-xml",
377 new HashMap() );
378 }
379
380 public void testLog4JLoggerStoreFactoryWithStreamsAsProperties()
381 throws Exception
382 {
383 final HashMap inputData = new HashMap();
384 runStreamBasedFactoryTest( "log4j.properties",
385 new PropertyLog4JLoggerStoreFactory(),
386 ConsoleLogger.LEVEL_DEBUG,
387 "log4j-properties",
388 inputData );
389 }
390
391 // JDK14LoggerStoreFactory tests
392 public void testJDK14LoggerStoreFactoryInvalidInput()
393 throws Exception
394 {
395 runInvalidInputData( new Jdk14LoggerStoreFactory() );
396 }
397
398 public void testJDK14LoggerStoreFactoryWithProperties()
399 throws Exception
400 {
401 final Properties properties = new Properties();
402 properties.load( getResource( "logging.properties" ) );
403 final HashMap config = new HashMap();
404 config.put( Properties.class.getName(), properties );
405
406 runFactoryTest( new Jdk14LoggerStoreFactory(),
407 ConsoleLogger.LEVEL_DEBUG,
408 config,
409 "jdk14" );
410 }
411
412 public void testJDK14LoggerStoreFactoryWithStreams()
413 throws Exception
414 {
415 runStreamBasedFactoryTest( "logging.properties",
416 new Jdk14LoggerStoreFactory(),
417 ConsoleLogger.LEVEL_DEBUG,
418 "jdk14",
419 new HashMap() );
420 }
421
422 }
This page was automatically generated by Maven