001: /* ====================================================================
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowledgment may appear in the software
024: * itself, if and wherever such third-party acknowledgments
025: * normally appear.
026: *
027: * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
028: * must not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation. For more
052: * information on the Apache Software Foundation, please see
053: * <http://www.apache.org/>.
054: */
055: package org.apache.log.test;
056:
057: import java.io.ByteArrayOutputStream;
058: import junit.framework.TestCase;
059: import org.apache.log.Hierarchy;
060: import org.apache.log.LogTarget;
061: import org.apache.log.Logger;
062: import org.apache.log.Priority;
063: import org.apache.log.format.PatternFormatter;
064: import org.apache.log.output.io.StreamTarget;
065:
066: /**
067: * Test suite for inheritance features of Logger.
068: *
069: * @author <a href="mailto:peter@apache.org">Peter Donald</a>
070: */
071: public final class InheritanceTestCase extends TestCase {
072: private static final String PATTERN = "%{priority}-%{message}";
073: private static final PatternFormatter FORMATTER = new PatternFormatter(
074: PATTERN);
075:
076: private static final String PATTERN2 = "Simon saids %{priority}-%{message}";
077: private static final PatternFormatter FORMATTER2 = new PatternFormatter(
078: PATTERN2);
079:
080: private static final String MSG = "No soup for you!";
081: private static final String RMSG = "DEBUG-" + MSG;
082: private static final String R2MSG = "Simon saids DEBUG-" + MSG;
083:
084: public InheritanceTestCase(final String name) {
085: super (name);
086: }
087:
088: private String getResult(final ByteArrayOutputStream output) {
089: final String result = output.toString();
090: output.reset();
091: return result;
092: }
093:
094: public void testPriorityInheritance() throws Exception {
095: final Hierarchy hierarchy = new Hierarchy();
096: final ByteArrayOutputStream output = new ByteArrayOutputStream();
097: final StreamTarget target = new StreamTarget(output, FORMATTER);
098: hierarchy.setDefaultLogTarget(target);
099:
100: final Logger b = hierarchy.getLoggerFor("b");
101: final Logger bc = hierarchy.getLoggerFor("b.c");
102: final Logger bcd = hierarchy.getLoggerFor("b.c.d");
103:
104: b.debug(MSG);
105: assertEquals("Priority debug output", RMSG, getResult(output));
106: bc.debug(MSG);
107: assertEquals("Priority debug output", RMSG, getResult(output));
108: bcd.debug(MSG);
109: assertEquals("Priority debug output", RMSG, getResult(output));
110:
111: b.setPriority(Priority.WARN);
112: b.debug(MSG);
113: assertEquals("Priority debug output", "", getResult(output));
114: bc.debug(MSG);
115: assertEquals("Priority debug output", "", getResult(output));
116: bcd.debug(MSG);
117: assertEquals("Priority debug output", "", getResult(output));
118:
119: bc.setPriority(Priority.DEBUG);
120: b.debug(MSG);
121: assertEquals("Priority debug output", "", getResult(output));
122: bc.debug(MSG);
123: assertEquals("Priority debug output", RMSG, getResult(output));
124: bcd.debug(MSG);
125: assertEquals("Priority debug output", RMSG, getResult(output));
126:
127: bcd.setPriority(Priority.WARN);
128: b.debug(MSG);
129: assertEquals("Priority debug output", "", getResult(output));
130: bc.debug(MSG);
131: assertEquals("Priority debug output", RMSG, getResult(output));
132: bcd.debug(MSG);
133: assertEquals("Priority debug output", "", getResult(output));
134:
135: bcd.unsetPriority();
136: b.debug(MSG);
137: assertEquals("Priority debug output", "", getResult(output));
138: bc.debug(MSG);
139: assertEquals("Priority debug output", RMSG, getResult(output));
140: bcd.debug(MSG);
141: assertEquals("Priority debug output", RMSG, getResult(output));
142:
143: bc.unsetPriority();
144: b.debug(MSG);
145: assertEquals("Priority debug output", "", getResult(output));
146: bc.debug(MSG);
147: assertEquals("Priority debug output", "", getResult(output));
148: bcd.debug(MSG);
149: assertEquals("Priority debug output", "", getResult(output));
150:
151: b.unsetPriority();
152: b.debug(MSG);
153: assertEquals("Priority debug output", RMSG, getResult(output));
154: bc.debug(MSG);
155: assertEquals("Priority debug output", RMSG, getResult(output));
156: bcd.debug(MSG);
157: assertEquals("Priority debug output", RMSG, getResult(output));
158:
159: bc.setPriority(Priority.WARN);
160: b.debug(MSG);
161: assertEquals("Priority debug output", RMSG, getResult(output));
162: bc.debug(MSG);
163: assertEquals("Priority debug output", "", getResult(output));
164: bcd.debug(MSG);
165: assertEquals("Priority debug output", "", getResult(output));
166:
167: b.unsetPriority(true);
168: b.debug(MSG);
169: assertEquals("Priority debug output", RMSG, getResult(output));
170: bc.debug(MSG);
171: assertEquals("Priority debug output", RMSG, getResult(output));
172: bcd.debug(MSG);
173: assertEquals("Priority debug output", RMSG, getResult(output));
174: }
175:
176: public void testLogTargetInheritance() {
177: final ByteArrayOutputStream output1 = new ByteArrayOutputStream();
178: final StreamTarget target1 = new StreamTarget(output1,
179: FORMATTER);
180: final ByteArrayOutputStream output2 = new ByteArrayOutputStream();
181: final StreamTarget target2 = new StreamTarget(output2,
182: FORMATTER2);
183:
184: final LogTarget[] targets1 = new LogTarget[] { target1 };
185: final LogTarget[] targets2 = new LogTarget[] { target2 };
186:
187: final Hierarchy hierarchy = new Hierarchy();
188: hierarchy.setDefaultLogTarget(target1);
189:
190: final Logger b = hierarchy.getLoggerFor("b");
191: final Logger bc = hierarchy.getLoggerFor("b.c");
192: final Logger bcd = hierarchy.getLoggerFor("b.c.d");
193:
194: b.setLogTargets(targets1);
195: b.debug(MSG);
196: assertEquals("LogTarget inherit debug output", RMSG,
197: getResult(output1));
198: bc.debug(MSG);
199: assertEquals("LogTarget inherit debug output", RMSG,
200: getResult(output1));
201: bcd.debug(MSG);
202: assertEquals("LogTarget inherit debug output", RMSG,
203: getResult(output1));
204:
205: b.setLogTargets(targets2);
206: b.debug(MSG);
207: assertEquals("LogTarget inherit debug output", R2MSG,
208: getResult(output2));
209: bc.debug(MSG);
210: assertEquals("LogTarget inherit debug output", R2MSG,
211: getResult(output2));
212: bcd.debug(MSG);
213: assertEquals("LogTarget inherit debug output", R2MSG,
214: getResult(output2));
215:
216: bc.setLogTargets(targets1);
217: b.debug(MSG);
218: assertEquals("LogTarget inherit debug output", R2MSG,
219: getResult(output2));
220: bc.debug(MSG);
221: assertEquals("LogTarget inherit debug output", RMSG,
222: getResult(output1));
223: bcd.debug(MSG);
224: assertEquals("LogTarget inherit debug output", RMSG,
225: getResult(output1));
226:
227: bcd.setLogTargets(targets2);
228: b.debug(MSG);
229: assertEquals("LogTarget inherit debug output", R2MSG,
230: getResult(output2));
231: bc.debug(MSG);
232: assertEquals("LogTarget inherit debug output", RMSG,
233: getResult(output1));
234: bcd.debug(MSG);
235: assertEquals("LogTarget inherit debug output", R2MSG,
236: getResult(output2));
237:
238: bcd.unsetLogTargets();
239: b.debug(MSG);
240: assertEquals("LogTarget inherit debug output", R2MSG,
241: getResult(output2));
242: bc.debug(MSG);
243: assertEquals("LogTarget inherit debug output", RMSG,
244: getResult(output1));
245: bcd.debug(MSG);
246: assertEquals("LogTarget inherit debug output", RMSG,
247: getResult(output1));
248:
249: bc.unsetLogTargets();
250: b.debug(MSG);
251: assertEquals("LogTarget inherit debug output", R2MSG,
252: getResult(output2));
253: bc.debug(MSG);
254: assertEquals("LogTarget inherit debug output", R2MSG,
255: getResult(output2));
256: bcd.debug(MSG);
257: assertEquals("LogTarget inherit debug output", R2MSG,
258: getResult(output2));
259:
260: b.unsetLogTargets();
261: b.debug(MSG);
262: assertEquals("LogTarget inherit debug output", RMSG,
263: getResult(output1));
264: bc.debug(MSG);
265: assertEquals("LogTarget inherit debug output", RMSG,
266: getResult(output1));
267: bcd.debug(MSG);
268: assertEquals("LogTarget inherit debug output", RMSG,
269: getResult(output1));
270:
271: bc.setLogTargets(targets2);
272: b.debug(MSG);
273: assertEquals("LogTarget inherit debug output", RMSG,
274: getResult(output1));
275: bc.debug(MSG);
276: assertEquals("LogTarget inherit debug output", R2MSG,
277: getResult(output2));
278: bcd.debug(MSG);
279: assertEquals("LogTarget inherit debug output", R2MSG,
280: getResult(output2));
281:
282: b.unsetLogTargets(true);
283: b.debug(MSG);
284: assertEquals("LogTarget inherit debug output", RMSG,
285: getResult(output1));
286: bc.debug(MSG);
287: assertEquals("LogTarget inherit debug output", RMSG,
288: getResult(output1));
289: bcd.debug(MSG);
290: assertEquals("LogTarget inherit debug output", RMSG,
291: getResult(output1));
292: }
293:
294: public void testAdditivity() throws Exception {
295: final Hierarchy hierarchy = new Hierarchy();
296: final ByteArrayOutputStream output = new ByteArrayOutputStream();
297: final StreamTarget target = new StreamTarget(output, FORMATTER);
298: final LogTarget[] targets = new LogTarget[] { target };
299:
300: final Logger b = hierarchy.getLoggerFor("b");
301: final Logger bc = hierarchy.getLoggerFor("b.c");
302: final Logger bcd = hierarchy.getLoggerFor("b.c.d");
303:
304: b.setLogTargets(targets);
305: bc.setLogTargets(targets);
306: bcd.setLogTargets(targets);
307:
308: b.debug(MSG);
309: assertEquals("Additivity debug output", RMSG, getResult(output));
310: bc.debug(MSG);
311: assertEquals("Additivity debug output", RMSG, getResult(output));
312: bcd.debug(MSG);
313: assertEquals("Additivity debug output", RMSG, getResult(output));
314:
315: b.setAdditivity(true);
316: b.debug(MSG);
317: assertEquals("Additivity debug output", RMSG, getResult(output));
318: bc.debug(MSG);
319: assertEquals("Additivity debug output", RMSG, getResult(output));
320: bcd.debug(MSG);
321: assertEquals("Additivity debug output", RMSG, getResult(output));
322:
323: bc.setAdditivity(true);
324: b.debug(MSG);
325: assertEquals("Additivity debug output", RMSG, getResult(output));
326: bc.debug(MSG);
327: assertEquals("Additivity debug output", RMSG + RMSG,
328: getResult(output));
329: bcd.debug(MSG);
330: assertEquals("Additivity debug output", RMSG, getResult(output));
331:
332: bcd.setAdditivity(true);
333: b.debug(MSG);
334: assertEquals("Additivity debug output", RMSG, getResult(output));
335: bc.debug(MSG);
336: assertEquals("Additivity debug output", RMSG + RMSG,
337: getResult(output));
338: bcd.debug(MSG);
339: assertEquals("Additivity debug output", RMSG + RMSG + RMSG,
340: getResult(output));
341:
342: bcd.setAdditivity(false);
343: b.debug(MSG);
344: assertEquals("Additivity debug output", RMSG, getResult(output));
345: bc.debug(MSG);
346: assertEquals("Additivity debug output", RMSG + RMSG,
347: getResult(output));
348: bcd.debug(MSG);
349: assertEquals("Additivity debug output", RMSG, getResult(output));
350:
351: bc.setAdditivity(false);
352: b.debug(MSG);
353: assertEquals("Additivity debug output", RMSG, getResult(output));
354: bc.debug(MSG);
355: assertEquals("Additivity debug output", RMSG, getResult(output));
356: bcd.debug(MSG);
357: assertEquals("Additivity debug output", RMSG, getResult(output));
358:
359: b.setAdditivity(false);
360: b.debug(MSG);
361: assertEquals("Additivity debug output", RMSG, getResult(output));
362: bc.debug(MSG);
363: assertEquals("Additivity debug output", RMSG, getResult(output));
364: bcd.debug(MSG);
365: assertEquals("Additivity debug output", RMSG, getResult(output));
366: }
367:
368: public void testChainedAdditivity() throws Exception {
369: final Hierarchy hierarchy = new Hierarchy();
370: final ByteArrayOutputStream output1 = new ByteArrayOutputStream();
371: final ByteArrayOutputStream output2 = new ByteArrayOutputStream();
372: final StreamTarget target1 = new StreamTarget(output1,
373: FORMATTER);
374: final StreamTarget target2 = new StreamTarget(output2,
375: FORMATTER);
376:
377: final LogTarget[] targets1 = new LogTarget[] { target1 };
378: final LogTarget[] targets2 = new LogTarget[] { target2 };
379:
380: final Logger b = hierarchy.getLoggerFor("b");
381: final Logger bc = hierarchy.getLoggerFor("b.c");
382: final Logger bcd = hierarchy.getLoggerFor("b.c.d");
383:
384: b.setLogTargets(targets1);
385: bc.setLogTargets(targets2);
386: bc.setAdditivity(true);
387: bcd.setAdditivity(true);
388:
389: b.debug(MSG);
390: assertEquals("Additivity debug output1", RMSG,
391: getResult(output1));
392: bc.debug(MSG);
393: assertEquals("Additivity debug output1", RMSG,
394: getResult(output1));
395: assertEquals("Additivity debug output2", RMSG,
396: getResult(output2));
397: bcd.debug(MSG);
398: assertEquals("Additivity debug output1", RMSG,
399: getResult(output1));
400: assertEquals("Additivity debug output2", RMSG,
401: getResult(output2));
402: }
403: }
|