KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > monitor > StatisticTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.management.monitor;
24
25 import javax.management.ObjectName JavaDoc;
26
27 import java.util.Set JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30
31 import javax.management.openmbean.CompositeData JavaDoc;
32 import javax.management.openmbean.CompositeType JavaDoc;
33 import javax.management.openmbean.CompositeDataSupport JavaDoc;
34 import javax.management.openmbean.OpenDataException JavaDoc;
35
36 import javax.management.j2ee.statistics.Statistic JavaDoc;
37 import javax.management.j2ee.statistics.Stats JavaDoc;
38 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
39 import javax.management.j2ee.statistics.BoundaryStatistic JavaDoc;
40 import javax.management.j2ee.statistics.RangeStatistic JavaDoc;
41 import javax.management.j2ee.statistics.BoundedRangeStatistic JavaDoc;
42 import javax.management.j2ee.statistics.TimeStatistic JavaDoc;
43
44 import com.sun.appserv.management.util.j2ee.J2EEUtil;
45 import com.sun.appserv.management.util.misc.TypeCast;
46
47 import com.sun.appserv.management.j2ee.statistics.*;
48
49
50 import com.sun.enterprise.management.AMXTestBase;
51 import com.sun.enterprise.management.Capabilities;
52
53
54 public final class StatisticTest
55     extends AMXTestBase
56 {
57         public
58     StatisticTest()
59     {
60     }
61     
62         private void
63     checkOpenDataConversion( final Statistic JavaDoc s )
64         throws OpenDataException JavaDoc
65     {
66         final CompositeData JavaDoc d = J2EEUtil.statisticToCompositeData( s );
67         final Statistic JavaDoc roundTrip = StatisticFactory.create( d );
68         
69         assert( s.equals( roundTrip ) ) :
70             "Conversion to CompositeData and back to Statistic failed:\n" +
71             toString( s ) + " != " + toString( roundTrip );
72     }
73     
74     public static final class TestStatistic extends StatisticImpl
75     {
76         public static final long serialVersionUID = 9999999;
77         
78         private final int Foo;
79         private final String JavaDoc Bar;
80         
81         public TestStatistic()
82         {
83             super( "Test", "test dummy", "none", 0, System.currentTimeMillis() );
84             
85             Foo = 999;
86             Bar = "Bar";
87         }
88         
89         public int getFoo() { return( Foo ); }
90         public String JavaDoc getBar() { return( Bar ); }
91     }
92     
93         public void
94     testAnyOpenDataConversion()
95         throws OpenDataException JavaDoc
96     {
97         // verify that anything implementing Statistic works correctly
98
final TestStatistic test = new TestStatistic();
99         final MapStatisticImpl testMap = new MapStatisticImpl( test );
100         assert( testMap.getValue( "Foo" ).equals( new Integer JavaDoc( test.getFoo() ) ) );
101         assert( testMap.getValue( "Bar" ).equals( test.getBar() ) );
102         
103         final CompositeData JavaDoc d = J2EEUtil.statisticToCompositeData( testMap );
104         final CompositeType JavaDoc t = d.getCompositeType();
105         final Set JavaDoc<String JavaDoc> values = TypeCast.asSet( t.keySet() );
106         assert( values.contains( "Name" ) );
107         assert( values.contains( "Foo" ) );
108         assert( values.contains( "Bar" ) );
109         
110         final MapStatisticImpl roundTrip = (MapStatisticImpl)StatisticFactory.create( d );
111         assert( new Integer JavaDoc( test.getFoo() ).equals( roundTrip.getValue( "Foo" ) ) );
112         assert( test.getBar().equals( roundTrip.getValue( "Bar" ) ) );
113     }
114     
115         public void
116     testStdOpenDataConversion()
117         throws OpenDataException JavaDoc
118     {
119         final CountStatisticImpl c =
120             new CountStatisticImpl( "Count", "desc", "number", 0, now(), 99);
121             
122         final RangeStatisticImpl r =
123             new RangeStatisticImpl( "Range", "desc", "number", 0, now(), 0, 50, 100);
124             
125         final BoundaryStatisticImpl b =
126             new BoundaryStatisticImpl( "Boundary", "desc", "number", 0, now(), 0, 100);
127             
128         final BoundedRangeStatisticImpl br =
129             new BoundedRangeStatisticImpl( "BoundedRange", "desc", "number", 0, now(), 0, 50, 100, 0, 100);
130             
131         final TimeStatisticImpl t =
132             new TimeStatisticImpl( "Time", "desc", "seconds", 0, now(), 1, 10, 100, 1000);
133             
134         final StringStatisticImpl s =
135             new StringStatisticImpl( "String", "desc", "chars", 0, now(), "hello" );
136         
137         final NumberStatisticImpl n =
138             new NumberStatisticImpl( "Number", "desc", "number", 0, now(), 1234.56 );
139                 
140         checkOpenDataConversion( c );
141         checkOpenDataConversion( r );
142         checkOpenDataConversion( b );
143         checkOpenDataConversion( br );
144         checkOpenDataConversion( t );
145         checkOpenDataConversion( s );
146         checkOpenDataConversion( n );
147     }
148  
149 }
150
151
152
153
154
155
156
Popular Tags