KickJava   Java API By Example, From Geeks To Geeks.

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


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

32 public class CounterInstrumentTestCase
33     extends TestCase
34 {
35     /*---------------------------------------------------------------
36      * Constructors
37      *-------------------------------------------------------------*/

38     public CounterInstrumentTestCase( String JavaDoc name )
39     {
40         super( name );
41     }
42     
43     /*---------------------------------------------------------------
44      * TestCase Methods
45      *-------------------------------------------------------------*/

46     
47     /*---------------------------------------------------------------
48      * Test Cases
49      *-------------------------------------------------------------*/

50     public void testSimpleIncrementDisconnected() throws Exception JavaDoc
51     {
52         CounterInstrument ci = new CounterInstrument( "testInstrument" );
53         assertEquals( "A disconnected instrument should not be active.", ci.isActive(), false );
54         
55         ci.increment( 1 );
56     }
57     
58     public void testCount1IncrementDisconnected() throws Exception JavaDoc
59     {
60         CounterInstrument ci = new CounterInstrument( "testInstrument" );
61         assertEquals( "A disconnected instrument should not be active.", ci.isActive(), false );
62         
63         ci.increment( 1 );
64     }
65     
66     public void testCount0IncrementDisconnected() throws Exception JavaDoc
67     {
68         CounterInstrument ci = new CounterInstrument( "testInstrument" );
69         try
70         {
71             ci.increment( 0 );
72             fail( "calling increment with a count of 0 should fail." );
73         }
74         catch ( IllegalArgumentException JavaDoc e )
75         {
76             // Ok
77
}
78     }
79     
80     public void testCountNegIncrementDisconnected() throws Exception JavaDoc
81     {
82         CounterInstrument ci = new CounterInstrument( "testInstrument" );
83         try
84         {
85             ci.increment( -1 );
86             fail( "calling increment with a negative count should fail." );
87         }
88         catch ( IllegalArgumentException JavaDoc e )
89         {
90             // Ok
91
}
92     }
93     
94     public void testSimpleIncrementConnectedInactive() throws Exception JavaDoc
95     {
96         CounterInstrument ci = new CounterInstrument( "testInstrument" );
97         TestInstrumentProxy proxy = new TestInstrumentProxy();
98         ci.setInstrumentProxy( proxy );
99         
100         assertEquals( "The instrument should not be active.", ci.isActive(), false );
101         
102         ci.increment();
103         
104         assertEquals( "The expected count was incorrect.", proxy.getValue(), 1 );
105         
106         ci.increment();
107         assertEquals( "The expected count was incorrect.", proxy.getValue(), 2 );
108     }
109     
110     public void testCount1IncrementConnectedInactive() throws Exception JavaDoc
111     {
112         CounterInstrument ci = new CounterInstrument( "testInstrument" );
113         TestInstrumentProxy proxy = new TestInstrumentProxy();
114         ci.setInstrumentProxy( proxy );
115         
116         assertEquals( "The instrument should not be active.", ci.isActive(), false );
117         
118         ci.increment( 1 );
119         
120         assertEquals( "The expected count was incorrect.", proxy.getValue(), 1 );
121         
122         ci.increment( 2 );
123         assertEquals( "The expected count was incorrect.", proxy.getValue(), 3 );
124     }
125     
126     public void testSimpleIncrementConnectedActive() throws Exception JavaDoc
127     {
128         CounterInstrument ci = new CounterInstrument( "testInstrument" );
129         TestInstrumentProxy proxy = new TestInstrumentProxy();
130         ci.setInstrumentProxy( proxy );
131         proxy.activate();
132         
133         assertEquals( "The instrument should br active.", ci.isActive(), true );
134         
135         ci.increment();
136         
137         assertEquals( "The expected count was incorrect.", proxy.getValue(), 1 );
138     }
139     
140     public void testCount1IncrementConnectedActive() throws Exception JavaDoc
141     {
142         CounterInstrument ci = new CounterInstrument( "testInstrument" );
143         TestInstrumentProxy proxy = new TestInstrumentProxy();
144         ci.setInstrumentProxy( proxy );
145         proxy.activate();
146
147         assertEquals( "The instrument should br active.", ci.isActive(), true );
148         
149         ci.increment( 1 );
150         
151         assertEquals( "The expected count was incorrect.", proxy.getValue(), 1 );
152     }
153 }
154
155
Popular Tags