KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > services > instrumentation > statistics > test > DefaultStatisticsInterceptorTest


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.services.instrumentation.statistics.test;
19
20
21 import java.util.Random JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.sape.carbon.core.component.Lookup;
26 import org.sape.carbon.services.instrumentation.statistics.StatisticsInterceptor;
27 import org.sape.carbon.services.instrumentation.statistics.DefaultStatisticsInterceptor;
28
29 import junit.extensions.ActiveTestSuite;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 /**
35  * This test harness does some basic testing of the functionality of the
36  * StatisticsInterceptor.
37  *
38  * Copyright 2003 Sapient
39  * @since carbon 1.0
40  * @author Greg Hinkle, January 2002
41  * @version $Revision: 1.8 $($Author: ghinkl $ / $Date: 2003/10/09 19:57:53 $)
42  */

43 public class DefaultStatisticsInterceptorTest extends TestCase {
44
45     private static int THREAD_COUNT = 50;
46
47     private static int ITERATION_COUNT = 1000;
48
49     private static final String JavaDoc TEST_COMPONENT_NAME =
50         "/stats/TestStatisticsComponent";
51
52
53     public DefaultStatisticsInterceptorTest(String JavaDoc name) {
54         super(name);
55     }
56
57
58     public void testStatisticsLog() {
59         TestStatisticsComponent component =
60             (TestStatisticsComponent) Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME);
61
62         ((StatisticsInterceptor) component).logStats();
63     }
64
65     public void testStatisticsReset() {
66         TestStatisticsComponent component =
67             (TestStatisticsComponent) Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME);
68
69         ((StatisticsInterceptor) component).resetStats();
70     }
71
72     public void testStatisticsInThreads() {
73         TestStatisticsComponent component =
74             (TestStatisticsComponent)
75             Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME);
76
77         for (int i = 0; i < ITERATION_COUNT; i++) {
78             component.succeed();
79         }
80         for (int i = 0; i < ITERATION_COUNT; i++) {
81             try {
82                 component.fail();
83             } catch (UnsupportedOperationException JavaDoc uoe) { }
84         }
85         ((StatisticsInterceptor) component).logStats();
86     }
87
88     public void testCallCount() {
89         TestStatisticsComponent component =
90                     (TestStatisticsComponent)
91                     Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME);
92
93         long calls = ((StatisticsInterceptor) component).getCallCounts();
94         assertEquals("Total call count was not as expected",
95             calls, 2 * (THREAD_COUNT * ITERATION_COUNT));
96     }
97
98     public void testStatisticsInThreadsWithFailures() {
99         TestStatisticsComponent TestStatisticsComponent =
100             (TestStatisticsComponent) Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME);
101         Random JavaDoc rand = new Random JavaDoc();
102         for (int i = 0; i < ITERATION_COUNT; i++) {
103             try {
104                 TestStatisticsComponent.fail();
105             } catch (Exception JavaDoc e) {
106                 // Expected
107
}
108         }
109
110         ((StatisticsInterceptor) TestStatisticsComponent).logStats();
111     }
112
113     public void testFailureCount() {
114         TestStatisticsComponent TestStatisticsComponent =
115             (TestStatisticsComponent) Lookup.getInstance().fetchComponent(TEST_COMPONENT_NAME);
116         Collection JavaDoc methodStats = ((StatisticsInterceptor)TestStatisticsComponent).getMethodStats();
117         for (Iterator JavaDoc iterator = methodStats.iterator(); iterator.hasNext();) {
118             DefaultStatisticsInterceptor.MethodStats stat = (DefaultStatisticsInterceptor.MethodStats) iterator.next();
119             if (stat.getMethod().getName().equals("fail")) {
120                 long failFailures = stat.getFailures();
121                 assertEquals("The number of failures counted does not equal the number expected",
122                     failFailures, THREAD_COUNT * ITERATION_COUNT);
123             }
124         }
125     }
126
127     /**
128      * Method called by jUnit to get all the tests in this test case.
129      * @return Test the suite of tests in this test case
130      */

131     public static Test suite() {
132         TestSuite masterSuite = new TestSuite("DefaultStatisticsInterceptorTest");
133
134         // Clear the stats
135
masterSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsReset"));
136
137         // Add the multi-thread tests
138
TestSuite activeSuite = new ActiveTestSuite();
139         for (int i = 0; i < THREAD_COUNT; i++) {
140             activeSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsInThreads"));
141         }
142         masterSuite.addTest(activeSuite);
143         masterSuite.addTest(new DefaultStatisticsInterceptorTest("testCallCount"));
144
145         // Log the stats
146
masterSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsLog"));
147
148
149
150         // Clear the stats
151
masterSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsReset"));
152
153         // Add the multi-thread tests
154
TestSuite activeSuite2 = new ActiveTestSuite();
155         for (int i = 0; i < THREAD_COUNT; i++) {
156             activeSuite2.addTest(new DefaultStatisticsInterceptorTest("testStatisticsInThreadsWithFailures"));
157         }
158         masterSuite.addTest(activeSuite2);
159
160         masterSuite.addTest(new DefaultStatisticsInterceptorTest("testFailureCount"));
161
162         // Log the stats
163
masterSuite.addTest(new DefaultStatisticsInterceptorTest("testStatisticsLog"));
164
165
166         return masterSuite;
167     }
168
169 }
170
Popular Tags