KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > management > mbeans > TestPerformanceMonitorMBean


1 // Copyright 2005 The Apache Software Foundation
2
//
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 implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.management.mbeans;
16
17 import java.util.HashSet JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import junit.framework.TestCase;
21
22 import org.apache.hivemind.service.MethodSignature;
23
24 /**
25  * Test of {@link org.apache.hivemind.management.mbeans.PerformanceMonitorMBean}
26  *
27  * @author Achim Huegen
28  */

29 public class TestPerformanceMonitorMBean extends TestCase
30 {
31     /**
32      * Creates a monitor mbean, adds some measurements
33      * and checks the results
34      */

35     public void testMeasurement() throws Exception JavaDoc
36     {
37         Set JavaDoc methods = new HashSet JavaDoc();
38
39         // Build a set of methods
40
MethodSignature method1 = new MethodSignature(void.class, "method1",
41                 new Class JavaDoc[] {}, new Class JavaDoc[] {} );
42         MethodSignature method2 = new MethodSignature(void.class, "method2",
43                 new Class JavaDoc[] {}, new Class JavaDoc[] {} );
44         methods.add(method1);
45         methods.add(method2);
46         
47         PerformanceMonitorMBean monitor = new PerformanceMonitorMBean(methods);
48         monitor.addMeasurement(method1, 100);
49         monitor.addMeasurement(method1, 50);
50
51         // Add a second measurement to ensure that methods don't mix up
52
monitor.addMeasurement(method2, 1000);
53         monitor.addMeasurement(method2, 500);
54         
55         checkAttributeValue(monitor, method1, PerformanceMonitorMBean.DATA_TYPE_MAXIMUM_TIME, 100);
56         checkAttributeValue(monitor, method1, PerformanceMonitorMBean.DATA_TYPE_AVERAGE_TIME, 75);
57         checkAttributeValue(monitor, method1, PerformanceMonitorMBean.DATA_TYPE_MINIMUM_TIME, 50);
58         checkAttributeValue(monitor, method1, PerformanceMonitorMBean.DATA_TYPE_COUNT, 2);
59         checkAttributeValue(monitor, method1, PerformanceMonitorMBean.DATA_TYPE_LAST_TIME, 50);
60     }
61     
62     private void checkAttributeValue(PerformanceMonitorMBean monitor,
63             MethodSignature method, String JavaDoc dataType, long expectedValue) throws Exception JavaDoc
64     {
65         String JavaDoc attrName = monitor.buildAttributeName(method,
66                 dataType);
67         Long JavaDoc attrValue = (Long JavaDoc) monitor.getAttribute(attrName);
68         assertEquals(expectedValue, attrValue.longValue() );
69     }
70     
71 }
72
Popular Tags