MessageHeaders serialization preserves non-serializable entries in original instance

Issue: SPR-13541
This commit is contained in:
Juergen Hoeller
2015-10-06 13:24:05 +02:00
parent 7ac6af0fb0
commit 181533c464
2 changed files with 61 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,10 +16,6 @@
package org.springframework.messaging;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -29,6 +25,8 @@ import java.util.concurrent.atomic.AtomicLong;
import org.junit.Test;
import org.springframework.util.SerializationTestUtils;
import static org.junit.Assert.*;
/**
@@ -36,10 +34,10 @@ import static org.junit.Assert.*;
*
* @author Rossen Stoyanchev
* @author Gary Russell
* @author Juergen Hoeller
*/
public class MessageHeadersTests {
@Test
public void testTimestamp() {
MessageHeaders headers = new MessageHeaders(null);
@@ -164,9 +162,11 @@ public class MessageHeadersTests {
map.put("name", "joe");
map.put("age", 42);
MessageHeaders input = new MessageHeaders(map);
MessageHeaders output = (MessageHeaders) serializeAndDeserialize(input);
MessageHeaders output = (MessageHeaders) SerializationTestUtils.serializeAndDeserialize(input);
assertEquals("joe", output.get("name"));
assertEquals(42, output.get("age"));
assertEquals("joe", input.get("name"));
assertEquals(42, input.get("age"));
}
@Test
@@ -176,37 +176,25 @@ public class MessageHeadersTests {
map.put("name", "joe");
map.put("address", address);
MessageHeaders input = new MessageHeaders(map);
MessageHeaders output = (MessageHeaders) serializeAndDeserialize(input);
MessageHeaders output = (MessageHeaders) SerializationTestUtils.serializeAndDeserialize(input);
assertEquals("joe", output.get("name"));
assertNull(output.get("address"));
assertEquals("joe", input.get("name"));
assertSame(address, input.get("address"));
}
@Test
public void subClassWithCustomIdAndNoTimestamp() {
public void subclassWithCustomIdAndNoTimestamp() {
final AtomicLong id = new AtomicLong();
@SuppressWarnings("serial")
class MyMH extends MessageHeaders {
public MyMH() {
super(null, new UUID(0, id.incrementAndGet()), -1L);
}
}
MessageHeaders headers = new MyMH();
assertEquals("00000000-0000-0000-0000-000000000001", headers.getId().toString());
assertEquals(1, headers.size());
}
private static Object serializeAndDeserialize(Object object) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(baos);
out.writeObject(object);
out.close();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
Object result = in.readObject();
in.close();
return result;
}
}