KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > management > j2ee > SerializableTest


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.j2ee;
24
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.io.ObjectOutputStream JavaDoc;
36 import java.io.ObjectInputStream JavaDoc;
37 import java.io.ByteArrayOutputStream JavaDoc;
38 import java.io.ByteArrayInputStream JavaDoc;
39 import java.io.Serializable JavaDoc;
40
41
42 import javax.management.j2ee.statistics.Statistic JavaDoc;
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 /**
57     Test serialization on the AMX Stats/Statistics classes which travel
58     from server to client.
59  */

60 public final class SerializableTest extends junit.framework.TestCase
61 {
62         public
63     SerializableTest( )
64     {
65     }
66     
67     static final Class JavaDoc[] TESTEES = new Class JavaDoc[]
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 JavaDoc toSerialize )
80         throws IOException JavaDoc, ClassNotFoundException JavaDoc
81     {
82         final ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc( 2048 );
83         
84         final ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc( os );
85         
86         oos.writeObject( toSerialize );
87         oos.close();
88         
89         final byte[] bytes = os.toByteArray();
90         
91         final ObjectInputStream JavaDoc is = new ObjectInputStream JavaDoc( new ByteArrayInputStream JavaDoc( bytes ) );
92         
93         final Object JavaDoc 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 JavaDoc<String JavaDoc> c = new HashSet JavaDoc<String JavaDoc>();
104         assert( TypeCast.checkedStringCollection(c) instanceof Serializable JavaDoc );
105         
106         final Set JavaDoc<String JavaDoc> s = new HashSet JavaDoc<String JavaDoc>();
107         assert( TypeCast.checkedStringSet(s) instanceof Serializable JavaDoc );
108         
109         final List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
110         assert( TypeCast.checkedStringList(l) instanceof Serializable JavaDoc );
111         
112         final Map JavaDoc<String JavaDoc,String JavaDoc> m = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
113         assert( TypeCast.checkedStringMap(m) instanceof Serializable JavaDoc );
114     }
115     
116         public void
117     testStatsImplRequiresStatistics()
118         throws IOException JavaDoc, ClassNotFoundException JavaDoc
119     {
120         try
121         {
122             Statistic JavaDoc x = null;
123             
124             final Statistic JavaDoc s = new CountStatisticImpl( "x","x","x",0,0,0);
125             final Map JavaDoc<String JavaDoc,Statistic JavaDoc> m = MapUtil.newMap( "foo", s );
126             final StatsImpl si = new StatsImpl( m );
127             serializeTest( si );
128         }
129         catch( IllegalArgumentException JavaDoc e )
130         {
131             // good
132
}
133     }
134     
135         public void
136     testStatsImpl()
137         throws IOException JavaDoc, ClassNotFoundException JavaDoc
138     {
139         final Map JavaDoc<String JavaDoc,Statistic JavaDoc> m = new HashMap JavaDoc<String JavaDoc,Statistic JavaDoc>();
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