001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.ObjectInputStream;
024: import java.io.ObjectOutputStream;
025: import java.io.OutputStream;
026: import java.io.Serializable;
027: import java.lang.reflect.Constructor;
028: import java.lang.reflect.Modifier;
029: import java.util.HashMap;
030:
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034: import junit.textui.TestRunner;
035:
036: /**
037: * Unit tests {@link org.apache.commons.lang.SerializationUtils}.
038: *
039: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
040: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
041: * @version $Id: SerializationUtilsTest.java 437554 2006-08-28 06:21:41Z bayard $
042: */
043: public class SerializationUtilsTest extends TestCase {
044:
045: static final String CLASS_NOT_FOUND_MESSAGE = "ClassNotFoundSerializationTest.readObject fake exception";
046: protected static final String SERIALIZE_IO_EXCEPTION_MESSAGE = "Anonymous OutputStream I/O exception";
047:
048: private String iString;
049: private Integer iInteger;
050: private HashMap iMap;
051:
052: public SerializationUtilsTest(String name) {
053: super (name);
054: }
055:
056: public static void main(String[] args) {
057: TestRunner.run(suite());
058: }
059:
060: public static Test suite() {
061: TestSuite suite = new TestSuite(SerializationUtilsTest.class);
062: suite.setName("SerializationUtils Tests");
063: return suite;
064: }
065:
066: protected void setUp() throws Exception {
067: super .setUp();
068:
069: iString = "foo";
070: iInteger = new Integer(7);
071: iMap = new HashMap();
072: iMap.put("FOO", iString);
073: iMap.put("BAR", iInteger);
074: }
075:
076: protected void tearDown() throws Exception {
077: super .tearDown();
078: }
079:
080: //-----------------------------------------------------------------------
081: public void testConstructor() {
082: assertNotNull(new SerializationUtils());
083: Constructor[] cons = SerializationUtils.class
084: .getDeclaredConstructors();
085: assertEquals(1, cons.length);
086: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
087: assertEquals(true, Modifier.isPublic(SerializationUtils.class
088: .getModifiers()));
089: assertEquals(false, Modifier.isFinal(SerializationUtils.class
090: .getModifiers()));
091: }
092:
093: public void testException() {
094: SerializationException serEx;
095: Exception ex = new Exception();
096:
097: serEx = new SerializationException();
098: assertSame(null, serEx.getMessage());
099: assertSame(null, serEx.getCause());
100:
101: serEx = new SerializationException("Message");
102: assertSame("Message", serEx.getMessage());
103: assertSame(null, serEx.getCause());
104:
105: serEx = new SerializationException(ex);
106: assertEquals("java.lang.Exception", serEx.getMessage());
107: assertSame(ex, serEx.getCause());
108:
109: serEx = new SerializationException("Message", ex);
110: assertSame("Message", serEx.getMessage());
111: assertSame(ex, serEx.getCause());
112: }
113:
114: //-----------------------------------------------------------------------
115: public void testSerializeStream() throws Exception {
116: ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
117: SerializationUtils.serialize(iMap, streamTest);
118:
119: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
120: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
121: oos.writeObject(iMap);
122: oos.flush();
123: oos.close();
124:
125: byte[] testBytes = streamTest.toByteArray();
126: byte[] realBytes = streamReal.toByteArray();
127: assertEquals(testBytes.length, realBytes.length);
128: for (int i = 0; i < realBytes.length; i++) {
129: assertEquals(realBytes[i], testBytes[i]);
130: }
131: }
132:
133: public void testSerializeStreamUnserializable() throws Exception {
134: ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
135: try {
136: iMap.put(new Object(), new Object());
137: SerializationUtils.serialize(iMap, streamTest);
138: } catch (SerializationException ex) {
139: return;
140: }
141: fail();
142: }
143:
144: public void testSerializeStreamNullObj() throws Exception {
145: ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
146: SerializationUtils.serialize(null, streamTest);
147:
148: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
149: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
150: oos.writeObject(null);
151: oos.flush();
152: oos.close();
153:
154: byte[] testBytes = streamTest.toByteArray();
155: byte[] realBytes = streamReal.toByteArray();
156: assertEquals(testBytes.length, realBytes.length);
157: for (int i = 0; i < realBytes.length; i++) {
158: assertEquals(realBytes[i], testBytes[i]);
159: }
160: }
161:
162: public void testSerializeStreamObjNull() throws Exception {
163: ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
164: try {
165: SerializationUtils.serialize(iMap, null);
166: } catch (IllegalArgumentException ex) {
167: return;
168: }
169: fail();
170: }
171:
172: public void testSerializeStreamNullNull() throws Exception {
173: ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
174: try {
175: SerializationUtils.serialize(null, null);
176: } catch (IllegalArgumentException ex) {
177: return;
178: }
179: fail();
180: }
181:
182: public void testSerializeIOException() throws Exception {
183: // forces an IOException when the ObjectOutputStream is created, to test not closing the stream
184: // in the finally block
185: OutputStream streamTest = new OutputStream() {
186: public void write(int arg0) throws IOException {
187: throw new IOException(SERIALIZE_IO_EXCEPTION_MESSAGE);
188: }
189: };
190: try {
191: SerializationUtils.serialize(iMap, streamTest);
192: } catch (SerializationException e) {
193: assertEquals("java.io.IOException: "
194: + SERIALIZE_IO_EXCEPTION_MESSAGE, e.getMessage());
195: }
196: }
197:
198: //-----------------------------------------------------------------------
199:
200: public void testDeserializeStream() throws Exception {
201: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
202: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
203: oos.writeObject(iMap);
204: oos.flush();
205: oos.close();
206:
207: ByteArrayInputStream inTest = new ByteArrayInputStream(
208: streamReal.toByteArray());
209: Object test = SerializationUtils.deserialize(inTest);
210: assertNotNull(test);
211: assertTrue(test instanceof HashMap);
212: assertTrue(test != iMap);
213: HashMap testMap = (HashMap) test;
214: assertEquals(iString, testMap.get("FOO"));
215: assertTrue(iString != testMap.get("FOO"));
216: assertEquals(iInteger, testMap.get("BAR"));
217: assertTrue(iInteger != testMap.get("BAR"));
218: assertEquals(iMap, testMap);
219: }
220:
221: public void testDeserializeStreamOfNull() throws Exception {
222: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
223: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
224: oos.writeObject(null);
225: oos.flush();
226: oos.close();
227:
228: ByteArrayInputStream inTest = new ByteArrayInputStream(
229: streamReal.toByteArray());
230: Object test = SerializationUtils.deserialize(inTest);
231: assertNull(test);
232: }
233:
234: public void testDeserializeStreamNull() throws Exception {
235: try {
236: SerializationUtils.deserialize((InputStream) null);
237: } catch (IllegalArgumentException ex) {
238: return;
239: }
240: fail();
241: }
242:
243: public void testDeserializeStreamBadStream() throws Exception {
244: try {
245: SerializationUtils.deserialize(new ByteArrayInputStream(
246: new byte[0]));
247: } catch (SerializationException ex) {
248: return;
249: }
250: fail();
251: }
252:
253: public void testDeserializeStreamClassNotFound() throws Exception {
254: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
255: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
256: oos.writeObject(new ClassNotFoundSerializationTest());
257: oos.flush();
258: oos.close();
259:
260: ByteArrayInputStream inTest = new ByteArrayInputStream(
261: streamReal.toByteArray());
262: try {
263: Object test = SerializationUtils.deserialize(inTest);
264: } catch (SerializationException se) {
265: assertEquals("java.lang.ClassNotFoundException: "
266: + CLASS_NOT_FOUND_MESSAGE, se.getMessage());
267: }
268: }
269:
270: //-----------------------------------------------------------------------
271:
272: public void testSerializeBytes() throws Exception {
273: byte[] testBytes = SerializationUtils.serialize(iMap);
274:
275: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
276: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
277: oos.writeObject(iMap);
278: oos.flush();
279: oos.close();
280:
281: byte[] realBytes = streamReal.toByteArray();
282: assertEquals(testBytes.length, realBytes.length);
283: for (int i = 0; i < realBytes.length; i++) {
284: assertEquals(realBytes[i], testBytes[i]);
285: }
286: }
287:
288: public void testSerializeBytesUnserializable() throws Exception {
289: try {
290: iMap.put(new Object(), new Object());
291: SerializationUtils.serialize(iMap);
292: } catch (SerializationException ex) {
293: return;
294: }
295: fail();
296: }
297:
298: public void testSerializeBytesNull() throws Exception {
299: byte[] testBytes = SerializationUtils.serialize(null);
300:
301: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
302: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
303: oos.writeObject(null);
304: oos.flush();
305: oos.close();
306:
307: byte[] realBytes = streamReal.toByteArray();
308: assertEquals(testBytes.length, realBytes.length);
309: for (int i = 0; i < realBytes.length; i++) {
310: assertEquals(realBytes[i], testBytes[i]);
311: }
312: }
313:
314: //-----------------------------------------------------------------------
315:
316: public void testDeserializeBytes() throws Exception {
317: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
318: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
319: oos.writeObject(iMap);
320: oos.flush();
321: oos.close();
322:
323: Object test = SerializationUtils.deserialize(streamReal
324: .toByteArray());
325: assertNotNull(test);
326: assertTrue(test instanceof HashMap);
327: assertTrue(test != iMap);
328: HashMap testMap = (HashMap) test;
329: assertEquals(iString, testMap.get("FOO"));
330: assertTrue(iString != testMap.get("FOO"));
331: assertEquals(iInteger, testMap.get("BAR"));
332: assertTrue(iInteger != testMap.get("BAR"));
333: assertEquals(iMap, testMap);
334: }
335:
336: public void testDeserializeBytesOfNull() throws Exception {
337: ByteArrayOutputStream streamReal = new ByteArrayOutputStream();
338: ObjectOutputStream oos = new ObjectOutputStream(streamReal);
339: oos.writeObject(null);
340: oos.flush();
341: oos.close();
342:
343: Object test = SerializationUtils.deserialize(streamReal
344: .toByteArray());
345: assertNull(test);
346: }
347:
348: public void testDeserializeBytesNull() throws Exception {
349: try {
350: SerializationUtils.deserialize((byte[]) null);
351: } catch (IllegalArgumentException ex) {
352: return;
353: }
354: fail();
355: }
356:
357: public void testDeserializeBytesBadStream() throws Exception {
358: try {
359: SerializationUtils.deserialize(new byte[0]);
360: } catch (SerializationException ex) {
361: return;
362: }
363: fail();
364: }
365:
366: //-----------------------------------------------------------------------
367:
368: public void testClone() throws Exception {
369: Object test = SerializationUtils.clone(iMap);
370: assertNotNull(test);
371: assertTrue(test instanceof HashMap);
372: assertTrue(test != iMap);
373: HashMap testMap = (HashMap) test;
374: assertEquals(iString, testMap.get("FOO"));
375: assertTrue(iString != testMap.get("FOO"));
376: assertEquals(iInteger, testMap.get("BAR"));
377: assertTrue(iInteger != testMap.get("BAR"));
378: assertEquals(iMap, testMap);
379: }
380:
381: public void testCloneNull() throws Exception {
382: Object test = SerializationUtils.clone(null);
383: assertNull(test);
384: }
385:
386: public void testCloneUnserializable() throws Exception {
387: try {
388: iMap.put(new Object(), new Object());
389: SerializationUtils.clone(iMap);
390: } catch (SerializationException ex) {
391: return;
392: }
393: fail();
394: }
395:
396: }
397:
398: class ClassNotFoundSerializationTest implements Serializable {
399:
400: private void readObject(ObjectInputStream in) throws IOException,
401: ClassNotFoundException {
402: throw new ClassNotFoundException(
403: SerializationUtilsTest.CLASS_NOT_FOUND_MESSAGE);
404: }
405: }
|