测试序列化和反序列化对象 Map

This commit is contained in:
YuCheng Hu 2019-08-09 16:13:55 -04:00
parent d6269c73cb
commit 342ed21ffe
1 changed files with 51 additions and 3 deletions

View File

@ -12,9 +12,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
import static org.junit.Assert.assertEquals;
@ -93,6 +91,56 @@ public class MessagePackSerializer {
}
/**
* SerializationMap
*/
@Test
public void testMessagePackSerializationMap() {
byte[] bytes = new byte[0];
String uuid_a = UUID.randomUUID().toString();
String uuid_b = UUID.randomUUID().toString();
// Instantiate ObjectMapper for MessagePack
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
Map<String, MessageData> map = new HashMap<>();
MessageData messageData = new MessageData();
// Element A in MAP
messageData.setUuid(UUID.randomUUID().toString());
messageData.setName("CWIKI.US - A");
map.put(uuid_a, messageData);
// Element B in MAP
messageData = new MessageData();
messageData.setUuid(UUID.randomUUID().toString());
messageData.setName("CWIKI.US - B");
map.put(uuid_b, messageData);
try {
// Serialize a Java object to byte array
bytes = objectMapper.writeValueAsBytes(map);
logger.debug("Length of Bytes: [{}]", bytes.length);
// Deserialize the byte array to a MAP
Map<String, MessageData> deserialized = objectMapper.readValue(bytes, new TypeReference<Map<String, MessageData>>() {
});
logger.debug("Deserialized MAP Count: [{}]", deserialized.size());
logger.debug("MAP index 0: [{}]", deserialized.get(uuid_a).getName());
assertEquals("CWIKI.US - A", deserialized.get(uuid_a).getName());
} catch (JsonProcessingException ex) {
logger.error("Serialize Error", ex);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testMessagePackSerialization() {
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());