KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > instrument > test > LogEnabledInstrumentableTestCase


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

19
20 package org.apache.excalibur.instrument.test;
21
22 import junit.framework.TestCase;
23
24 import org.apache.avalon.framework.logger.ConsoleLogger;
25 import org.apache.excalibur.instrument.CounterInstrument;
26 import org.apache.excalibur.instrument.Instrument;
27 import org.apache.excalibur.instrument.Instrumentable;
28 import org.apache.excalibur.instrument.ValueInstrument;
29
30 /**
31  * Test of the AbstractLogEnabledInstrumentable instrument.
32  *
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:34 $
35  */

36 public class LogEnabledInstrumentableTestCase
37     extends TestCase
38 {
39     /*---------------------------------------------------------------
40      * Constructors
41      *-------------------------------------------------------------*/

42     public LogEnabledInstrumentableTestCase( String JavaDoc name )
43     {
44         super( name );
45     }
46     
47     /*---------------------------------------------------------------
48      * TestCase Methods
49      *-------------------------------------------------------------*/

50     
51     /*---------------------------------------------------------------
52      * Methods
53      *-------------------------------------------------------------*/

54     private void generalTest( Instrument[] instruments, Instrumentable[] children )
55         throws Exception JavaDoc
56     {
57         AbstractLogEnabledInstrumentableImpl impl =
58             new AbstractLogEnabledInstrumentableImpl( "base" );
59         impl.enableLogging( new ConsoleLogger() );
60         
61         // Set the name
62
impl.setInstrumentableName( "test" );
63         
64         // Add the instruments
65
for ( int i = 0; i < instruments.length; i++ )
66         {
67             impl.addInstrument( instruments[i] );
68         }
69         
70         // Add the child instrumentables
71
for ( int i = 0; i < children.length; i++ )
72         {
73             impl.addChildInstrumentable( children[i] );
74         }
75         
76         // Verify the name
77
assertEquals( "Instrumentable name incorrect.", impl.getInstrumentableName(), "test" );
78         
79         
80         // Verify the instruments
81
Instrument[] implInstruments = impl.getInstruments();
82         assertEquals( "The number of instruments is not correct.",
83             implInstruments.length, instruments.length );
84         for ( int i = 0; i < instruments.length; i++ )
85         {
86             assertEquals( "Instrument[i] is not correct.", implInstruments[i], instruments[i] );
87         }
88         
89         // Make sure that instruments can no longer be added
90
try
91         {
92             impl.addInstrument( new CounterInstrument( "bad" ) );
93             fail( "Should not have been able to add more instruments" );
94         }
95         catch ( IllegalStateException JavaDoc e )
96         {
97             // Ok
98
}
99         
100         
101         // Verify the child instrumentables
102
Instrumentable[] implChildren = impl.getChildInstrumentables();
103         assertEquals( "The number of child instrumentables is not correct.",
104             implChildren.length, children.length );
105         for ( int i = 0; i < children.length; i++ )
106         {
107             assertEquals( "Child[i] is not correct.", implChildren[i], children[i] );
108         }
109         
110         // Make sure that child instrumentables can no longer be added
111
try
112         {
113             impl.addChildInstrumentable( new AbstractInstrumentableImpl( "bad" ) );
114             fail( "Should not have been able to add more child instrumentables" );
115         }
116         catch ( IllegalStateException JavaDoc e )
117         {
118             // Ok
119
}
120     }
121     
122     /*---------------------------------------------------------------
123      * Test Cases
124      *-------------------------------------------------------------*/

125     public void testEmpty() throws Exception JavaDoc
126     {
127         Instrument[] instruments = new Instrument[] {};
128         Instrumentable[] children = new Instrumentable[] {};
129         
130         generalTest( instruments, children );
131     }
132     
133     public void test1Instrument() throws Exception JavaDoc
134     {
135         Instrument[] instruments = new Instrument[]
136             {
137                 new CounterInstrument( "c1" )
138             };
139         Instrumentable[] children = new Instrumentable[] {};
140         
141         generalTest( instruments, children );
142     }
143     
144     public void testNInstrument() throws Exception JavaDoc
145     {
146         Instrument[] instruments = new Instrument[]
147             {
148                 new CounterInstrument( "c1" ),
149                 new ValueInstrument( "v1" ),
150                 new CounterInstrument( "c2" ),
151                 new ValueInstrument( "v2" ),
152                 new CounterInstrument( "c3" ),
153                 new ValueInstrument( "v3" ),
154                 new CounterInstrument( "c4" ),
155                 new ValueInstrument( "v4" )
156             };
157         Instrumentable[] children = new Instrumentable[] {};
158         
159         generalTest( instruments, children );
160     }
161     
162     public void test1ChildInstrumentable() throws Exception JavaDoc
163     {
164         Instrument[] instruments = new Instrument[] {};
165         Instrumentable[] children = new Instrumentable[]
166             {
167                 new AbstractInstrumentableImpl( "i1" )
168             };
169         
170         generalTest( instruments, children );
171     }
172     
173     public void testNChildInstrumentable() throws Exception JavaDoc
174     {
175         Instrument[] instruments = new Instrument[] {};
176         Instrumentable[] children = new Instrumentable[]
177             {
178                 new AbstractInstrumentableImpl( "i1" ),
179                 new AbstractInstrumentableImpl( "i2" ),
180                 new AbstractInstrumentableImpl( "i3" ),
181                 new AbstractInstrumentableImpl( "i4" ),
182                 new AbstractInstrumentableImpl( "i5" ),
183                 new AbstractInstrumentableImpl( "i6" )
184             };
185         
186         generalTest( instruments, children );
187     }
188 }
189
190
Popular Tags