KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > SerializationUtilsTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.lang.reflect.Constructor JavaDoc;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 import junit.textui.TestRunner;
30
31 /**
32  * Unit tests {@link org.apache.commons.lang.SerializationUtils}.
33  *
34  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
35  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
36  * @version $Id: SerializationUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
37  */

38 public class SerializationUtilsTest extends TestCase {
39     private String JavaDoc iString;
40     private Integer JavaDoc iInteger;
41     private HashMap JavaDoc iMap;
42
43     public SerializationUtilsTest(String JavaDoc name) {
44         super(name);
45     }
46
47     public static void main(String JavaDoc[] args) {
48         TestRunner.run(suite());
49     }
50
51     public static Test suite() {
52         TestSuite suite = new TestSuite(SerializationUtilsTest.class);
53         suite.setName("SerializationUtils Tests");
54         return suite;
55     }
56
57     protected void setUp() throws Exception JavaDoc {
58         super.setUp();
59
60         iString = "foo";
61         iInteger = new Integer JavaDoc(7);
62         iMap = new HashMap JavaDoc();
63         iMap.put("FOO", iString);
64         iMap.put("BAR", iInteger);
65     }
66
67     protected void tearDown() throws Exception JavaDoc {
68         super.tearDown();
69     }
70
71     //-----------------------------------------------------------------------
72
public void testConstructor() {
73         assertNotNull(new SerializationUtils());
74         Constructor JavaDoc[] cons = SerializationUtils.class.getDeclaredConstructors();
75         assertEquals(1, cons.length);
76         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
77         assertEquals(true, Modifier.isPublic(SerializationUtils.class.getModifiers()));
78         assertEquals(false, Modifier.isFinal(SerializationUtils.class.getModifiers()));
79     }
80     
81     public void testException() {
82         SerializationException serEx;
83         Exception JavaDoc ex = new Exception JavaDoc();
84         
85         serEx = new SerializationException();
86         assertSame(null, serEx.getMessage());
87         assertSame(null, serEx.getCause());
88         
89         serEx = new SerializationException("Message");
90         assertSame("Message", serEx.getMessage());
91         assertSame(null, serEx.getCause());
92         
93         serEx = new SerializationException(ex);
94         assertEquals("java.lang.Exception", serEx.getMessage());
95         assertSame(ex, serEx.getCause());
96         
97         serEx = new SerializationException("Message", ex);
98         assertSame("Message", serEx.getMessage());
99         assertSame(ex, serEx.getCause());
100     }
101     
102     //-----------------------------------------------------------------------
103
public void testSerializeStream() throws Exception JavaDoc {
104         ByteArrayOutputStream JavaDoc streamTest = new ByteArrayOutputStream JavaDoc();
105         SerializationUtils.serialize(iMap, streamTest);
106
107         ByteArrayOutputStream JavaDoc streamReal = new ByteArrayOutputStream JavaDoc();
108         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(streamReal);
109         oos.writeObject(iMap);
110         oos.flush();
111         oos.close();
112
113         byte[] testBytes = streamTest.toByteArray();
114         byte[] realBytes = streamReal.toByteArray();
115         assertEquals(testBytes.length, realBytes.length);
116         for (int i = 0; i < realBytes.length; i++) {
117             assertEquals(realBytes[i], testBytes[i]);
118         }
119     }
120
121     public void testSerializeStreamUnserializable() throws Exception JavaDoc {
122         ByteArrayOutputStream JavaDoc streamTest = new ByteArrayOutputStream JavaDoc();
123         try {
124             iMap.put(new Object JavaDoc(), new Object JavaDoc());
125             SerializationUtils.serialize(iMap, streamTest);
126         } catch (SerializationException ex) {
127             return;
128         }
129         fail();
130     }
131
132     public void testSerializeStreamNullObj() throws Exception JavaDoc {
133         ByteArrayOutputStream JavaDoc streamTest = new ByteArrayOutputStream JavaDoc();
134         SerializationUtils.serialize(null, streamTest);
135
136         ByteArrayOutputStream JavaDoc streamReal = new ByteArrayOutputStream JavaDoc();
137         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(streamReal);
138         oos.writeObject(null);
139         oos.flush();
140         oos.close();
141
142         byte[] testBytes = streamTest.toByteArray();
143         byte[] realBytes = streamReal.toByteArray();
144         assertEquals(testBytes.length, realBytes.length);
145         for (int i = 0; i < realBytes.length; i++) {
146             assertEquals(realBytes[i], testBytes[i]);
147         }
148     }
149
150     public void testSerializeStreamObjNull() throws Exception JavaDoc {
151         ByteArrayOutputStream JavaDoc streamTest = new ByteArrayOutputStream JavaDoc();
152         try {
153             SerializationUtils.serialize(iMap, null);
154         } catch (IllegalArgumentException JavaDoc ex) {
155             return;
156         }
157         fail();
158     }
159
160     public void testSerializeStreamNullNull() throws Exception JavaDoc {
161         ByteArrayOutputStream JavaDoc streamTest = new ByteArrayOutputStream JavaDoc();
162         try {
163             SerializationUtils.serialize(null, null);
164         } catch (IllegalArgumentException JavaDoc ex) {
165             return;
166         }
167         fail();
168     }
169
170     //-----------------------------------------------------------------------
171

172     public void testDeserializeStream() throws Exception JavaDoc {
173         ByteArrayOutputStream JavaDoc streamReal = new ByteArrayOutputStream JavaDoc();
174         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(streamReal);
175         oos.writeObject(iMap);
176         oos.flush();
177         oos.close();
178
179         ByteArrayInputStream JavaDoc inTest = new ByteArrayInputStream JavaDoc(streamReal.toByteArray());
180         Object JavaDoc test = SerializationUtils.deserialize(inTest);
181         assertNotNull(test);
182         assertTrue(test instanceof HashMap JavaDoc);
183         assertTrue(test != iMap);
184         HashMap JavaDoc testMap = (HashMap JavaDoc) test;
185         assertEquals(iString, testMap.get("FOO"));
186         assertTrue(iString != testMap.get("FOO"));
187         assertEquals(iInteger, testMap.get("BAR"));
188         assertTrue(iInteger != testMap.get("BAR"));
189         assertEquals(iMap, testMap);
190     }
191
192     public void testDeserializeStreamOfNull() throws Exception JavaDoc {
193         ByteArrayOutputStream JavaDoc streamReal = new ByteArrayOutputStream JavaDoc();
194         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(streamReal);
195         oos.writeObject(null);
196         oos.flush();
197         oos.close();
198
199         ByteArrayInputStream JavaDoc inTest = new ByteArrayInputStream JavaDoc(streamReal.toByteArray());
200         Object JavaDoc test = SerializationUtils.deserialize(inTest);
201         assertNull(test);
202     }
203
204     public void testDeserializeStreamNull() throws Exception JavaDoc {
205         try {
206             SerializationUtils.deserialize((InputStream JavaDoc) null);
207         } catch (IllegalArgumentException JavaDoc ex) {
208             return;
209         }
210         fail();
211     }
212
213     public void testDeserializeStreamBadStream() throws Exception JavaDoc {
214         try {
215             SerializationUtils.deserialize(new ByteArrayInputStream JavaDoc(new byte[0]));
216         } catch (SerializationException ex) {
217             return;
218         }
219         fail();
220     }
221
222     //-----------------------------------------------------------------------
223

224     public void testSerializeBytes() throws Exception JavaDoc {
225         byte[] testBytes = SerializationUtils.serialize(iMap);
226
227         ByteArrayOutputStream JavaDoc streamReal = new ByteArrayOutputStream JavaDoc();
228         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(streamReal);
229         oos.writeObject(iMap);
230         oos.flush();
231         oos.close();
232
233         byte[] realBytes = streamReal.toByteArray();
234         assertEquals(testBytes.length, realBytes.length);
235         for (int i = 0; i < realBytes.length; i++) {
236             assertEquals(realBytes[i], testBytes[i]);
237         }
238     }
239
240     public void testSerializeBytesUnserializable() throws Exception JavaDoc {
241         try {
242             iMap.put(new Object JavaDoc(), new Object JavaDoc());
243             SerializationUtils.serialize(iMap);
244         } catch (SerializationException ex) {
245             return;
246         }
247         fail();
248     }
249
250     public void testSerializeBytesNull() throws Exception JavaDoc {
251         byte[] testBytes = SerializationUtils.serialize(null);
252
253         ByteArrayOutputStream JavaDoc streamReal = new ByteArrayOutputStream JavaDoc();
254         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(streamReal);
255         oos.writeObject(null);
256         oos.flush();
257         oos.close();
258
259         byte[] realBytes = streamReal.toByteArray();
260         assertEquals(testBytes.length, realBytes.length);
261         for (int i = 0; i < realBytes.length; i++) {
262             assertEquals(realBytes[i], testBytes[i]);
263         }
264     }
265
266     //-----------------------------------------------------------------------
267

268     public void testDeserializeBytes() throws Exception JavaDoc {
269         ByteArrayOutputStream JavaDoc streamReal = new ByteArrayOutputStream JavaDoc();
270         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(streamReal);
271         oos.writeObject(iMap);
272         oos.flush();
273         oos.close();
274
275         Object JavaDoc test = SerializationUtils.deserialize(streamReal.toByteArray());
276         assertNotNull(test);
277         assertTrue(test instanceof HashMap JavaDoc);
278         assertTrue(test != iMap);
279         HashMap JavaDoc testMap = (HashMap JavaDoc) test;
280         assertEquals(iString, testMap.get("FOO"));
281         assertTrue(iString != testMap.get("FOO"));
282         assertEquals(iInteger, testMap.get("BAR"));
283         assertTrue(iInteger != testMap.get("BAR"));
284         assertEquals(iMap, testMap);
285     }
286
287     public void testDeserializeBytesOfNull() throws Exception JavaDoc {
288         ByteArrayOutputStream JavaDoc streamReal = new ByteArrayOutputStream JavaDoc();
289         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(streamReal);
290         oos.writeObject(null);
291         oos.flush();
292         oos.close();
293
294         Object JavaDoc test = SerializationUtils.deserialize(streamReal.toByteArray());
295         assertNull(test);
296     }
297
298     public void testDeserializeBytesNull() throws Exception JavaDoc {
299         try {
300             SerializationUtils.deserialize((byte[]) null);
301         } catch (IllegalArgumentException JavaDoc ex) {
302             return;
303         }
304         fail();
305     }
306
307     public void testDeserializeBytesBadStream() throws Exception JavaDoc {
308         try {
309             SerializationUtils.deserialize(new byte[0]);
310         } catch (SerializationException ex) {
311             return;
312         }
313         fail();
314     }
315
316     //-----------------------------------------------------------------------
317

318     public void testClone() throws Exception JavaDoc {
319         Object JavaDoc test = SerializationUtils.clone(iMap);
320         assertNotNull(test);
321         assertTrue(test instanceof HashMap JavaDoc);
322         assertTrue(test != iMap);
323         HashMap JavaDoc testMap = (HashMap JavaDoc) test;
324         assertEquals(iString, testMap.get("FOO"));
325         assertTrue(iString != testMap.get("FOO"));
326         assertEquals(iInteger, testMap.get("BAR"));
327         assertTrue(iInteger != testMap.get("BAR"));
328         assertEquals(iMap, testMap);
329     }
330
331     public void testCloneNull() throws Exception JavaDoc {
332         Object JavaDoc test = SerializationUtils.clone(null);
333         assertNull(test);
334     }
335
336     public void testCloneUnserializable() throws Exception JavaDoc {
337         try {
338             iMap.put(new Object JavaDoc(), new Object JavaDoc());
339             SerializationUtils.clone(iMap);
340         } catch (SerializationException ex) {
341             return;
342         }
343         fail();
344     }
345
346 }
347
Popular Tags