From 9e5c4ebccf17335fc4b2637f5a4d1cc3f9e3a781 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?y=C3=AC=20j=C3=AD?= Date: Fri, 21 Jun 2019 15:56:10 +0800 Subject: [PATCH] fix unit test failed. (#681) * fix unit test failed. * Avoid null key transfer to headers --- .../sofa/rpc/codec/bolt/SimpleMapSerializer.java | 9 +++++++-- .../sofa/rpc/codec/bolt/SimpleMapSerializerTest.java | 10 +++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/extension-impl/remoting-bolt/src/main/java/com/alipay/sofa/rpc/codec/bolt/SimpleMapSerializer.java b/extension-impl/remoting-bolt/src/main/java/com/alipay/sofa/rpc/codec/bolt/SimpleMapSerializer.java index 3ddab0115..e944a5a12 100644 --- a/extension-impl/remoting-bolt/src/main/java/com/alipay/sofa/rpc/codec/bolt/SimpleMapSerializer.java +++ b/extension-impl/remoting-bolt/src/main/java/com/alipay/sofa/rpc/codec/bolt/SimpleMapSerializer.java @@ -55,8 +55,13 @@ public byte[] encode(Map map) throws SerializationException { for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); - writeSupportEmpty(key, out); - writeSupportEmpty(value, out); + /** + * 排除不写null作为key + */ + if(key != null){ + writeSupportEmpty(key, out); + writeSupportEmpty(value, out); + } } return out.toByteArray(); } catch (IOException ex) { diff --git a/extension-impl/remoting-bolt/src/test/java/com/alipay/sofa/rpc/codec/bolt/SimpleMapSerializerTest.java b/extension-impl/remoting-bolt/src/test/java/com/alipay/sofa/rpc/codec/bolt/SimpleMapSerializerTest.java index 6e50764f8..009dd780e 100644 --- a/extension-impl/remoting-bolt/src/test/java/com/alipay/sofa/rpc/codec/bolt/SimpleMapSerializerTest.java +++ b/extension-impl/remoting-bolt/src/test/java/com/alipay/sofa/rpc/codec/bolt/SimpleMapSerializerTest.java @@ -44,15 +44,15 @@ public void encode() throws Exception { map.put("a", ""); map.put("b", null); bs = simpleMapSerializer.encode(map); - Assert.assertEquals(10, bs.length); + Assert.assertEquals(37, bs.length); Map map1 = simpleMapSerializer.decode(bs); Assert.assertNotNull(map1); - Assert.assertEquals(1, map1.size()); + Assert.assertEquals(4, map1.size()); Assert.assertEquals("2", map1.get("1")); - Assert.assertEquals(null, map1.get("")); - Assert.assertEquals(null, map1.get("a")); - Assert.assertEquals(null, map1.get("b")); + Assert.assertEquals("x", map1.get("")); + Assert.assertEquals("", map1.get("a")); + Assert.assertEquals("", map1.get("b")); map1 = simpleMapSerializer.decode(null); Assert.assertNotNull(map1);