KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
20 import org.apache.log.LogEvent;
21 import org.apache.log.LogTarget;
22 import org.apache.log.output.AbstractWrappingTarget;
23 import org.apache.log.util.Closeable;
24
25 /**
26  * Test suite for wrapping targets.
27  *
28  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
29  */

30 public final class WrappingTargetTestCase
31     extends TestCase
32 {
33     public WrappingTargetTestCase(String JavaDoc name)
34     {
35         super(name);
36     }
37     
38     static private class DummyTarget implements LogTarget
39     {
40         private boolean closed = false;
41         
42         public void close()
43         {
44             closed = true;
45         }
46         
47         public boolean isClosed()
48         {
49             return closed;
50         }
51         
52         public void processEvent( LogEvent event )
53         {
54             // Do nothing
55
}
56     }
57     
58     static private class CloseableDummyTarget extends DummyTarget implements Closeable
59     {
60     }
61     
62     static private class DummyTargetWrapper extends AbstractWrappingTarget
63     {
64         public DummyTargetWrapper( final LogTarget logTarget )
65         {
66             super( logTarget );
67         }
68         
69         public DummyTargetWrapper( final LogTarget logTarget, final boolean closeWrappedTarget )
70         {
71             super( logTarget, closeWrappedTarget );
72         }
73         
74         public void doProcessEvent( LogEvent event )
75         {
76             // Do nothing
77
}
78     }
79     
80     public void testNonCloseable()
81     {
82         DummyTarget dummyTargetNonClose = new DummyTarget();
83         DummyTarget dummyTargetNonClose2 = new DummyTarget();
84         DummyTarget dummyTargetClose = new DummyTarget();
85         
86         DummyTargetWrapper wrapperNonClose = new DummyTargetWrapper(dummyTargetNonClose, false);
87         DummyTargetWrapper wrapperNonClose2 = new DummyTargetWrapper(dummyTargetNonClose2); // should default to false
88
DummyTargetWrapper wrapperClose = new DummyTargetWrapper(dummyTargetClose, true);
89         
90         assertTrue( !dummyTargetNonClose.isClosed() );
91         assertTrue( !dummyTargetNonClose2.isClosed() );
92         assertTrue( !dummyTargetClose.isClosed() );
93         
94         wrapperNonClose.close();
95         wrapperNonClose2.close();
96         wrapperClose.close();
97         
98         // The close() should have no effect, since neither target implements closeable.
99

100         assertTrue( !dummyTargetNonClose.isClosed() );
101         assertTrue( !dummyTargetNonClose2.isClosed() );
102         assertTrue( !dummyTargetClose.isClosed() );
103     }
104     
105     public void testCloseable()
106     {
107         DummyTarget dummyTargetNonClose = new CloseableDummyTarget();
108         DummyTarget dummyTargetNonClose2 = new CloseableDummyTarget();
109         DummyTarget dummyTargetClose = new CloseableDummyTarget();
110         
111         DummyTargetWrapper wrapperNonClose = new DummyTargetWrapper(dummyTargetNonClose, false);
112         DummyTargetWrapper wrapperNonClose2 = new DummyTargetWrapper(dummyTargetNonClose2); // should default to false
113
DummyTargetWrapper wrapperClose = new DummyTargetWrapper(dummyTargetClose, true);
114         
115         assertTrue( !dummyTargetNonClose.isClosed() );
116         assertTrue( !dummyTargetNonClose2.isClosed() );
117         assertTrue( !dummyTargetClose.isClosed() );
118         
119         wrapperNonClose.close();
120         wrapperNonClose2.close();
121         wrapperClose.close();
122         
123         // Only the target that was wrapped with the closeWrapped parameter
124
// set to true should be closed.
125

126         assertTrue( !dummyTargetNonClose.isClosed() );
127         assertTrue( !dummyTargetNonClose2.isClosed() );
128         assertTrue( dummyTargetClose.isClosed() );
129     }
130 }
131
Popular Tags