KickJava   Java API By Example, From Geeks To Geeks.

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


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.excalibur.instrument.CounterInstrument;
25 import org.apache.excalibur.instrument.Instrument;
26 import org.apache.excalibur.instrument.Instrumentable;
27 import org.apache.excalibur.instrument.ValueInstrument;
28
29 /**
30  * Test of the AbstractInstrumentable instrument.
31  *
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:34 $
34  */

35 public class InstrumentableTestCase
36     extends TestCase
37 {
38     /*---------------------------------------------------------------
39      * Constructors
40      *-------------------------------------------------------------*/

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

49     
50     /*---------------------------------------------------------------
51      * Methods
52      *-------------------------------------------------------------*/

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

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