1 16 package org.apache.commons.lang; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.InputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.lang.reflect.Constructor ; 23 import java.lang.reflect.Modifier ; 24 import java.util.HashMap ; 25 26 import junit.framework.Test; 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 import junit.textui.TestRunner; 30 31 38 public class SerializationUtilsTest extends TestCase { 39 private String iString; 40 private Integer iInteger; 41 private HashMap iMap; 42 43 public SerializationUtilsTest(String name) { 44 super(name); 45 } 46 47 public static void main(String [] 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 { 58 super.setUp(); 59 60 iString = "foo"; 61 iInteger = new Integer (7); 62 iMap = new HashMap (); 63 iMap.put("FOO", iString); 64 iMap.put("BAR", iInteger); 65 } 66 67 protected void tearDown() throws Exception { 68 super.tearDown(); 69 } 70 71 public void testConstructor() { 73 assertNotNull(new SerializationUtils()); 74 Constructor [] 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 ex = new Exception (); 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 public void testSerializeStream() throws Exception { 104 ByteArrayOutputStream streamTest = new ByteArrayOutputStream (); 105 SerializationUtils.serialize(iMap, streamTest); 106 107 ByteArrayOutputStream streamReal = new ByteArrayOutputStream (); 108 ObjectOutputStream oos = new ObjectOutputStream (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 { 122 ByteArrayOutputStream streamTest = new ByteArrayOutputStream (); 123 try { 124 iMap.put(new Object (), new Object ()); 125 SerializationUtils.serialize(iMap, streamTest); 126 } catch (SerializationException ex) { 127 return; 128 } 129 fail(); 130 } 131 132 public void testSerializeStreamNullObj() throws Exception { 133 ByteArrayOutputStream streamTest = new ByteArrayOutputStream (); 134 SerializationUtils.serialize(null, streamTest); 135 136 ByteArrayOutputStream streamReal = new ByteArrayOutputStream (); 137 ObjectOutputStream oos = new ObjectOutputStream (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 { 151 ByteArrayOutputStream streamTest = new ByteArrayOutputStream (); 152 try { 153 SerializationUtils.serialize(iMap, null); 154 } catch (IllegalArgumentException ex) { 155 return; 156 } 157 fail(); 158 } 159 160 public void testSerializeStreamNullNull() throws Exception { 161 ByteArrayOutputStream streamTest = new ByteArrayOutputStream (); 162 try { 163 SerializationUtils.serialize(null, null); 164 } catch (IllegalArgumentException ex) { 165 return; 166 } 167 fail(); 168 } 169 170 172 public void testDeserializeStream() throws Exception { 173 ByteArrayOutputStream streamReal = new ByteArrayOutputStream (); 174 ObjectOutputStream oos = new ObjectOutputStream (streamReal); 175 oos.writeObject(iMap); 176 oos.flush(); 177 oos.close(); 178 179 ByteArrayInputStream inTest = new ByteArrayInputStream (streamReal.toByteArray()); 180 Object test = SerializationUtils.deserialize(inTest); 181 assertNotNull(test); 182 assertTrue(test instanceof HashMap ); 183 assertTrue(test != iMap); 184 HashMap testMap = (HashMap ) 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 { 193 ByteArrayOutputStream streamReal = new ByteArrayOutputStream (); 194 ObjectOutputStream oos = new ObjectOutputStream (streamReal); 195 oos.writeObject(null); 196 oos.flush(); 197 oos.close(); 198 199 ByteArrayInputStream inTest = new ByteArrayInputStream (streamReal.toByteArray()); 200 Object test = SerializationUtils.deserialize(inTest); 201 assertNull(test); 202 } 203 204 public void testDeserializeStreamNull() throws Exception { 205 try { 206 SerializationUtils.deserialize((InputStream ) null); 207 } catch (IllegalArgumentException ex) { 208 return; 209 } 210 fail(); 211 } 212 213 public void testDeserializeStreamBadStream() throws Exception { 214 try { 215 SerializationUtils.deserialize(new ByteArrayInputStream (new byte[0])); 216 } catch (SerializationException ex) { 217 return; 218 } 219 fail(); 220 } 221 222 224 public void testSerializeBytes() throws Exception { 225 byte[] testBytes = SerializationUtils.serialize(iMap); 226 227 ByteArrayOutputStream streamReal = new ByteArrayOutputStream (); 228 ObjectOutputStream oos = new ObjectOutputStream (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 { 241 try { 242 iMap.put(new Object (), new Object ()); 243 SerializationUtils.serialize(iMap); 244 } catch (SerializationException ex) { 245 return; 246 } 247 fail(); 248 } 249 250 public void testSerializeBytesNull() throws Exception { 251 byte[] testBytes = SerializationUtils.serialize(null); 252 253 ByteArrayOutputStream streamReal = new ByteArrayOutputStream (); 254 ObjectOutputStream oos = new ObjectOutputStream (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 268 public void testDeserializeBytes() throws Exception { 269 ByteArrayOutputStream streamReal = new ByteArrayOutputStream (); 270 ObjectOutputStream oos = new ObjectOutputStream (streamReal); 271 oos.writeObject(iMap); 272 oos.flush(); 273 oos.close(); 274 275 Object test = SerializationUtils.deserialize(streamReal.toByteArray()); 276 assertNotNull(test); 277 assertTrue(test instanceof HashMap ); 278 assertTrue(test != iMap); 279 HashMap testMap = (HashMap ) 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 { 288 ByteArrayOutputStream streamReal = new ByteArrayOutputStream (); 289 ObjectOutputStream oos = new ObjectOutputStream (streamReal); 290 oos.writeObject(null); 291 oos.flush(); 292 oos.close(); 293 294 Object test = SerializationUtils.deserialize(streamReal.toByteArray()); 295 assertNull(test); 296 } 297 298 public void testDeserializeBytesNull() throws Exception { 299 try { 300 SerializationUtils.deserialize((byte[]) null); 301 } catch (IllegalArgumentException ex) { 302 return; 303 } 304 fail(); 305 } 306 307 public void testDeserializeBytesBadStream() throws Exception { 308 try { 309 SerializationUtils.deserialize(new byte[0]); 310 } catch (SerializationException ex) { 311 return; 312 } 313 fail(); 314 } 315 316 318 public void testClone() throws Exception { 319 Object test = SerializationUtils.clone(iMap); 320 assertNotNull(test); 321 assertTrue(test instanceof HashMap ); 322 assertTrue(test != iMap); 323 HashMap testMap = (HashMap ) 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 { 332 Object test = SerializationUtils.clone(null); 333 assertNull(test); 334 } 335 336 public void testCloneUnserializable() throws Exception { 337 try { 338 iMap.put(new Object (), new Object ()); 339 SerializationUtils.clone(iMap); 340 } catch (SerializationException ex) { 341 return; 342 } 343 fail(); 344 } 345 346 } 347 | Popular Tags |