KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log > test > InheritanceTestCase


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.log.test;
18
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import junit.framework.TestCase;
21 import org.apache.log.Hierarchy;
22 import org.apache.log.LogTarget;
23 import org.apache.log.Logger;
24 import org.apache.log.Priority;
25 import org.apache.log.format.PatternFormatter;
26 import org.apache.log.output.io.StreamTarget;
27
28 /**
29  * Test suite for inheritance features of Logger.
30  *
31  * @author Peter Donald
32  */

33 public final class InheritanceTestCase
34     extends TestCase
35 {
36     private static final String JavaDoc PATTERN = "%{priority}-%{message}";
37     private static final PatternFormatter FORMATTER = new PatternFormatter( PATTERN );
38
39     private static final String JavaDoc PATTERN2 = "Simon saids %{priority}-%{message}";
40     private static final PatternFormatter FORMATTER2 = new PatternFormatter( PATTERN2 );
41
42     private static final String JavaDoc MSG = "No soup for you!";
43     private static final String JavaDoc RMSG = "DEBUG-" + MSG;
44     private static final String JavaDoc R2MSG = "Simon saids DEBUG-" + MSG;
45
46     public InheritanceTestCase( final String JavaDoc name )
47     {
48         super( name );
49     }
50
51     private String JavaDoc getResult( final ByteArrayOutputStream JavaDoc output )
52     {
53         final String JavaDoc result = output.toString();
54         output.reset();
55         return result;
56     }
57
58     public void testPriorityInheritance()
59         throws Exception JavaDoc
60     {
61         final Hierarchy hierarchy = new Hierarchy();
62         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
63         final StreamTarget target = new StreamTarget( output, FORMATTER );
64         hierarchy.setDefaultLogTarget( target );
65
66         final Logger b = hierarchy.getLoggerFor( "b" );
67         final Logger bc = hierarchy.getLoggerFor( "b.c" );
68         final Logger bcd = hierarchy.getLoggerFor( "b.c.d" );
69
70         b.debug( MSG );
71         assertEquals( "Priority debug output", RMSG, getResult( output ) );
72         bc.debug( MSG );
73         assertEquals( "Priority debug output", RMSG, getResult( output ) );
74         bcd.debug( MSG );
75         assertEquals( "Priority debug output", RMSG, getResult( output ) );
76
77         b.setPriority( Priority.WARN );
78         b.debug( MSG );
79         assertEquals( "Priority debug output", "", getResult( output ) );
80         bc.debug( MSG );
81         assertEquals( "Priority debug output", "", getResult( output ) );
82         bcd.debug( MSG );
83         assertEquals( "Priority debug output", "", getResult( output ) );
84
85         bc.setPriority( Priority.DEBUG );
86         b.debug( MSG );
87         assertEquals( "Priority debug output", "", getResult( output ) );
88         bc.debug( MSG );
89         assertEquals( "Priority debug output", RMSG, getResult( output ) );
90         bcd.debug( MSG );
91         assertEquals( "Priority debug output", RMSG, getResult( output ) );
92
93         bcd.setPriority( Priority.WARN );
94         b.debug( MSG );
95         assertEquals( "Priority debug output", "", getResult( output ) );
96         bc.debug( MSG );
97         assertEquals( "Priority debug output", RMSG, getResult( output ) );
98         bcd.debug( MSG );
99         assertEquals( "Priority debug output", "", getResult( output ) );
100
101         bcd.unsetPriority();
102         b.debug( MSG );
103         assertEquals( "Priority debug output", "", getResult( output ) );
104         bc.debug( MSG );
105         assertEquals( "Priority debug output", RMSG, getResult( output ) );
106         bcd.debug( MSG );
107         assertEquals( "Priority debug output", RMSG, getResult( output ) );
108
109         bc.unsetPriority();
110         b.debug( MSG );
111         assertEquals( "Priority debug output", "", getResult( output ) );
112         bc.debug( MSG );
113         assertEquals( "Priority debug output", "", getResult( output ) );
114         bcd.debug( MSG );
115         assertEquals( "Priority debug output", "", getResult( output ) );
116
117         b.unsetPriority();
118         b.debug( MSG );
119         assertEquals( "Priority debug output", RMSG, getResult( output ) );
120         bc.debug( MSG );
121         assertEquals( "Priority debug output", RMSG, getResult( output ) );
122         bcd.debug( MSG );
123         assertEquals( "Priority debug output", RMSG, getResult( output ) );
124
125         bc.setPriority( Priority.WARN );
126         b.debug( MSG );
127         assertEquals( "Priority debug output", RMSG, getResult( output ) );
128         bc.debug( MSG );
129         assertEquals( "Priority debug output", "", getResult( output ) );
130         bcd.debug( MSG );
131         assertEquals( "Priority debug output", "", getResult( output ) );
132
133         b.unsetPriority( true );
134         b.debug( MSG );
135         assertEquals( "Priority debug output", RMSG, getResult( output ) );
136         bc.debug( MSG );
137         assertEquals( "Priority debug output", RMSG, getResult( output ) );
138         bcd.debug( MSG );
139         assertEquals( "Priority debug output", RMSG, getResult( output ) );
140     }
141
142     public void testLogTargetInheritance()
143     {
144         final ByteArrayOutputStream JavaDoc output1 = new ByteArrayOutputStream JavaDoc();
145         final StreamTarget target1 = new StreamTarget( output1, FORMATTER );
146         final ByteArrayOutputStream JavaDoc output2 = new ByteArrayOutputStream JavaDoc();
147         final StreamTarget target2 = new StreamTarget( output2, FORMATTER2 );
148
149         final LogTarget[] targets1 = new LogTarget[]{target1};
150         final LogTarget[] targets2 = new LogTarget[]{target2};
151
152         final Hierarchy hierarchy = new Hierarchy();
153         hierarchy.setDefaultLogTarget( target1 );
154
155         final Logger b = hierarchy.getLoggerFor( "b" );
156         final Logger bc = hierarchy.getLoggerFor( "b.c" );
157         final Logger bcd = hierarchy.getLoggerFor( "b.c.d" );
158
159         b.setLogTargets( targets1 );
160         b.debug( MSG );
161         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
162         bc.debug( MSG );
163         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
164         bcd.debug( MSG );
165         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
166
167         b.setLogTargets( targets2 );
168         b.debug( MSG );
169         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
170         bc.debug( MSG );
171         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
172         bcd.debug( MSG );
173         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
174
175         bc.setLogTargets( targets1 );
176         b.debug( MSG );
177         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
178         bc.debug( MSG );
179         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
180         bcd.debug( MSG );
181         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
182
183         bcd.setLogTargets( targets2 );
184         b.debug( MSG );
185         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
186         bc.debug( MSG );
187         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
188         bcd.debug( MSG );
189         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
190
191         bcd.unsetLogTargets();
192         b.debug( MSG );
193         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
194         bc.debug( MSG );
195         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
196         bcd.debug( MSG );
197         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
198
199         bc.unsetLogTargets();
200         b.debug( MSG );
201         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
202         bc.debug( MSG );
203         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
204         bcd.debug( MSG );
205         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
206
207         b.unsetLogTargets();
208         b.debug( MSG );
209         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
210         bc.debug( MSG );
211         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
212         bcd.debug( MSG );
213         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
214
215         bc.setLogTargets( targets2 );
216         b.debug( MSG );
217         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
218         bc.debug( MSG );
219         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
220         bcd.debug( MSG );
221         assertEquals( "LogTarget inherit debug output", R2MSG, getResult( output2 ) );
222
223         b.unsetLogTargets( true );
224         b.debug( MSG );
225         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
226         bc.debug( MSG );
227         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
228         bcd.debug( MSG );
229         assertEquals( "LogTarget inherit debug output", RMSG, getResult( output1 ) );
230     }
231
232     public void testAdditivity()
233         throws Exception JavaDoc
234     {
235         final Hierarchy hierarchy = new Hierarchy();
236         final ByteArrayOutputStream JavaDoc output = new ByteArrayOutputStream JavaDoc();
237         final StreamTarget target = new StreamTarget( output, FORMATTER );
238         final LogTarget[] targets = new LogTarget[]{target};
239
240         final Logger b = hierarchy.getLoggerFor( "b" );
241         final Logger bc = hierarchy.getLoggerFor( "b.c" );
242         final Logger bcd = hierarchy.getLoggerFor( "b.c.d" );
243
244         b.setLogTargets( targets );
245         bc.setLogTargets( targets );
246         bcd.setLogTargets( targets );
247
248         b.debug( MSG );
249         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
250         bc.debug( MSG );
251         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
252         bcd.debug( MSG );
253         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
254
255         b.setAdditivity( true );
256         b.debug( MSG );
257         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
258         bc.debug( MSG );
259         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
260         bcd.debug( MSG );
261         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
262
263         bc.setAdditivity( true );
264         b.debug( MSG );
265         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
266         bc.debug( MSG );
267         assertEquals( "Additivity debug output", RMSG + RMSG, getResult( output ) );
268         bcd.debug( MSG );
269         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
270
271         bcd.setAdditivity( true );
272         b.debug( MSG );
273         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
274         bc.debug( MSG );
275         assertEquals( "Additivity debug output", RMSG + RMSG, getResult( output ) );
276         bcd.debug( MSG );
277         assertEquals( "Additivity debug output", RMSG + RMSG + RMSG, getResult( output ) );
278
279         bcd.setAdditivity( false );
280         b.debug( MSG );
281         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
282         bc.debug( MSG );
283         assertEquals( "Additivity debug output", RMSG + RMSG, getResult( output ) );
284         bcd.debug( MSG );
285         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
286
287         bc.setAdditivity( false );
288         b.debug( MSG );
289         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
290         bc.debug( MSG );
291         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
292         bcd.debug( MSG );
293         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
294
295         b.setAdditivity( false );
296         b.debug( MSG );
297         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
298         bc.debug( MSG );
299         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
300         bcd.debug( MSG );
301         assertEquals( "Additivity debug output", RMSG, getResult( output ) );
302     }
303
304     public void testChainedAdditivity()
305         throws Exception JavaDoc
306     {
307         final Hierarchy hierarchy = new Hierarchy();
308         final ByteArrayOutputStream JavaDoc output1 = new ByteArrayOutputStream JavaDoc();
309         final ByteArrayOutputStream JavaDoc output2 = new ByteArrayOutputStream JavaDoc();
310         final StreamTarget target1 = new StreamTarget( output1, FORMATTER );
311         final StreamTarget target2 = new StreamTarget( output2, FORMATTER );
312
313         final LogTarget[] targets1 = new LogTarget[]{target1};
314         final LogTarget[] targets2 = new LogTarget[]{target2};
315
316         final Logger b = hierarchy.getLoggerFor( "b" );
317         final Logger bc = hierarchy.getLoggerFor( "b.c" );
318         final Logger bcd = hierarchy.getLoggerFor( "b.c.d" );
319
320         b.setLogTargets( targets1 );
321         bc.setLogTargets( targets2 );
322         bc.setAdditivity( true );
323         bcd.setAdditivity( true );
324
325         b.debug( MSG );
326         assertEquals( "Additivity debug output1", RMSG, getResult( output1 ) );
327         bc.debug( MSG );
328         assertEquals( "Additivity debug output1", RMSG, getResult( output1 ) );
329         assertEquals( "Additivity debug output2", RMSG, getResult( output2 ) );
330         bcd.debug( MSG );
331         assertEquals( "Additivity debug output1", RMSG, getResult( output1 ) );
332         assertEquals( "Additivity debug output2", RMSG, getResult( output2 ) );
333     }
334 }
335
Popular Tags