1 23 package com.sun.enterprise.management.j2ee; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import java.util.HashSet ; 29 import java.util.Collection ; 30 import java.util.List ; 31 import java.util.ArrayList ; 32 33 import java.io.IOException ; 34 import java.io.OutputStream ; 35 import java.io.ObjectOutputStream ; 36 import java.io.ObjectInputStream ; 37 import java.io.ByteArrayOutputStream ; 38 import java.io.ByteArrayInputStream ; 39 import java.io.Serializable ; 40 41 42 import javax.management.j2ee.statistics.Statistic ; 43 44 import com.sun.appserv.management.base.AMX; 45 import com.sun.appserv.management.j2ee.statistics.*; 46 47 import com.sun.appserv.management.util.misc.ClassUtil; 48 import com.sun.appserv.management.util.misc.MapUtil; 49 import com.sun.appserv.management.util.misc.TypeCast; 50 51 52 import com.sun.enterprise.management.Capabilities; 53 54 55 56 60 public final class SerializableTest extends junit.framework.TestCase 61 { 62 public 63 SerializableTest( ) 64 { 65 } 66 67 static final Class [] TESTEES = new Class [] 68 { 69 StatsImpl.class, 70 CountStatisticImpl.class, 71 RangeStatisticImpl.class, 72 BoundedRangeStatisticImpl.class, 73 BoundaryStatisticImpl.class, 74 MapGetterInvocationHandler.class, 75 GetterInvocationHandler.class, 76 }; 77 78 protected void 79 serializeTest( final Object toSerialize ) 80 throws IOException , ClassNotFoundException 81 { 82 final ByteArrayOutputStream os = new ByteArrayOutputStream ( 2048 ); 83 84 final ObjectOutputStream oos = new ObjectOutputStream ( os ); 85 86 oos.writeObject( toSerialize ); 87 oos.close(); 88 89 final byte[] bytes = os.toByteArray(); 90 91 final ObjectInputStream is = new ObjectInputStream ( new ByteArrayInputStream ( bytes ) ); 92 93 final Object result = is.readObject(); 94 95 assert( result.equals( toSerialize ) ) : 96 "Deserialized object not equal: " + toSerialize + " != " + result; 97 } 98 99 100 public void 101 testChecked() 102 { 103 final Collection <String > c = new HashSet <String >(); 104 assert( TypeCast.checkedStringCollection(c) instanceof Serializable ); 105 106 final Set <String > s = new HashSet <String >(); 107 assert( TypeCast.checkedStringSet(s) instanceof Serializable ); 108 109 final List <String > l = new ArrayList <String >(); 110 assert( TypeCast.checkedStringList(l) instanceof Serializable ); 111 112 final Map <String ,String > m = new HashMap <String ,String >(); 113 assert( TypeCast.checkedStringMap(m) instanceof Serializable ); 114 } 115 116 public void 117 testStatsImplRequiresStatistics() 118 throws IOException , ClassNotFoundException 119 { 120 try 121 { 122 Statistic x = null; 123 124 final Statistic s = new CountStatisticImpl( "x","x","x",0,0,0); 125 final Map <String ,Statistic > m = MapUtil.newMap( "foo", s ); 126 final StatsImpl si = new StatsImpl( m ); 127 serializeTest( si ); 128 } 129 catch( IllegalArgumentException e ) 130 { 131 } 133 } 134 135 public void 136 testStatsImpl() 137 throws IOException , ClassNotFoundException 138 { 139 final Map <String ,Statistic > m = new HashMap <String ,Statistic >(); 140 141 final CountStatisticImpl c = new CountStatisticImpl( "Count", "", "number", 0, 0, 99); 142 final RangeStatisticImpl r = new RangeStatisticImpl( "Range", "", "number", 0, 0, 0, 50, 100); 143 final BoundaryStatisticImpl b = new BoundaryStatisticImpl( "Boundary", "", "number", 0, 0, 0, 100); 144 final BoundedRangeStatisticImpl br = new BoundedRangeStatisticImpl( "BoundedRange", "", "number", 0, 0, 0, 50, 100, 0, 100); 145 final TimeStatisticImpl t = new TimeStatisticImpl( "Time", "", "number", 0, 0, 0, 10, 100, 1000 ); 146 147 m.put( c.getName(), c ); 148 m.put( r.getName(), r ); 149 m.put( br.getName(), br ); 150 m.put( b.getName(), b ); 151 m.put( t.getName(), t ); 152 153 final StatsImpl si = new StatsImpl( m ); 154 serializeTest( si ); 155 } 156 157 } 158 159 | Popular Tags |