1 23 package com.sun.enterprise.management.monitor; 24 25 import javax.management.ObjectName ; 26 27 import java.util.Set ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 31 import javax.management.openmbean.CompositeData ; 32 import javax.management.openmbean.CompositeType ; 33 import javax.management.openmbean.CompositeDataSupport ; 34 import javax.management.openmbean.OpenDataException ; 35 36 import javax.management.j2ee.statistics.Statistic ; 37 import javax.management.j2ee.statistics.Stats ; 38 import javax.management.j2ee.statistics.CountStatistic ; 39 import javax.management.j2ee.statistics.BoundaryStatistic ; 40 import javax.management.j2ee.statistics.RangeStatistic ; 41 import javax.management.j2ee.statistics.BoundedRangeStatistic ; 42 import javax.management.j2ee.statistics.TimeStatistic ; 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 s ) 64 throws OpenDataException 65 { 66 final CompositeData d = J2EEUtil.statisticToCompositeData( s ); 67 final Statistic 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 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 getBar() { return( Bar ); } 91 } 92 93 public void 94 testAnyOpenDataConversion() 95 throws OpenDataException 96 { 97 final TestStatistic test = new TestStatistic(); 99 final MapStatisticImpl testMap = new MapStatisticImpl( test ); 100 assert( testMap.getValue( "Foo" ).equals( new Integer ( test.getFoo() ) ) ); 101 assert( testMap.getValue( "Bar" ).equals( test.getBar() ) ); 102 103 final CompositeData d = J2EEUtil.statisticToCompositeData( testMap ); 104 final CompositeType t = d.getCompositeType(); 105 final Set <String > 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 ( test.getFoo() ).equals( roundTrip.getValue( "Foo" ) ) ); 112 assert( test.getBar().equals( roundTrip.getValue( "Bar" ) ) ); 113 } 114 115 public void 116 testStdOpenDataConversion() 117 throws OpenDataException 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 |