Skip to content

Commit 7f7173f

Browse files
committed
added copy of SerializationTestUtils to beans module
1 parent 209f43b commit 7f7173f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
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+
17+
package org.springframework.util;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.io.NotSerializableException;
23+
import java.io.ObjectInputStream;
24+
import java.io.ObjectOutputStream;
25+
import java.io.OutputStream;
26+
27+
/**
28+
* Utilities for testing serializability of objects.
29+
* Exposes static methods for use in other test cases.
30+
*
31+
* @author Rod Johnson
32+
*/
33+
public class SerializationTestUtils {
34+
35+
public static void testSerialization(Object o) throws IOException {
36+
OutputStream baos = new ByteArrayOutputStream();
37+
ObjectOutputStream oos = new ObjectOutputStream(baos);
38+
oos.writeObject(o);
39+
}
40+
41+
public static boolean isSerializable(Object o) throws IOException {
42+
try {
43+
testSerialization(o);
44+
return true;
45+
}
46+
catch (NotSerializableException ex) {
47+
return false;
48+
}
49+
}
50+
51+
public static Object serializeAndDeserialize(Object o) throws IOException, ClassNotFoundException {
52+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
53+
ObjectOutputStream oos = new ObjectOutputStream(baos);
54+
oos.writeObject(o);
55+
oos.flush();
56+
baos.flush();
57+
byte[] bytes = baos.toByteArray();
58+
59+
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
60+
ObjectInputStream ois = new ObjectInputStream(is);
61+
Object o2 = ois.readObject();
62+
return o2;
63+
}
64+
65+
}

0 commit comments

Comments
 (0)