From d936541eccadd3a43287b1c1cfbf9715548ef36f Mon Sep 17 00:00:00 2001 From: zhangfei Date: Tue, 26 Mar 2019 13:37:34 +0800 Subject: [PATCH 1/3] fix: #3727 --- .../protostuff/ProtostuffObjectOutput.java | 2 +- .../ProtostuffObjectOutputTest.java | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java diff --git a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutput.java b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutput.java index f52c755d5d1..ab94d3ba8df 100644 --- a/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutput.java +++ b/dubbo-serialization/dubbo-serialization-protostuff/src/main/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutput.java @@ -47,7 +47,7 @@ public void writeObject(Object obj) throws IOException { byte[] classNameBytes; try { - if (WrapperUtils.needWrapper(obj)) { + if (obj == null || WrapperUtils.needWrapper(obj)) { Schema schema = RuntimeSchema.getSchema(Wrapper.class); Wrapper wrapper = new Wrapper(obj); bytes = GraphIOUtil.toByteArray(wrapper, schema, buffer); diff --git a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java new file mode 100644 index 00000000000..630029d6244 --- /dev/null +++ b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java @@ -0,0 +1,43 @@ +package org.apache.dubbo.common.serialize.protostuff; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.nullValue; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import org.apache.dubbo.common.serialize.fastjson.FastJsonObjectInput; +import org.apache.dubbo.common.serialize.fastjson.FastJsonObjectOutput; +import org.hamcrest.core.IsNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ProtostuffObjectOutputTest { + + private ByteArrayOutputStream byteArrayOutputStream; + private ProtostuffObjectOutput protostuffObjectOutput; + private ProtostuffObjectInput protostuffObjectInput; + private ByteArrayInputStream byteArrayInputStream; + + @BeforeEach + public void setUp() throws Exception { + this.byteArrayOutputStream = new ByteArrayOutputStream(); + this.protostuffObjectOutput = new ProtostuffObjectOutput(byteArrayOutputStream); + } + + @Test + public void testWriteObjectNull() throws IOException, ClassNotFoundException { + this.protostuffObjectOutput.writeObject(null); + this.flushToInput(); + + assertThat(protostuffObjectInput.readObject(), nullValue()); + } + + private void flushToInput() throws IOException { + this.protostuffObjectOutput.flushBuffer(); + this.byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); + this.protostuffObjectInput = new ProtostuffObjectInput(byteArrayInputStream); + } +} From d75d4852db6adb7d4256e1e39c472eb1a9c508eb Mon Sep 17 00:00:00 2001 From: zhangfei Date: Tue, 26 Mar 2019 13:41:08 +0800 Subject: [PATCH 2/3] style: code tidy up --- .../serialize/protostuff/ProtostuffObjectOutputTest.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java index 630029d6244..7e88933f59b 100644 --- a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java +++ b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java @@ -1,16 +1,11 @@ package org.apache.dubbo.common.serialize.protostuff; -import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.nullValue; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.OutputStream; -import org.apache.dubbo.common.serialize.fastjson.FastJsonObjectInput; -import org.apache.dubbo.common.serialize.fastjson.FastJsonObjectOutput; -import org.hamcrest.core.IsNull; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; From 3b97be1cf41c3f9ab36b1ee0c0450f5c14b042d5 Mon Sep 17 00:00:00 2001 From: zhangfei Date: Tue, 26 Mar 2019 14:21:04 +0800 Subject: [PATCH 3/3] style: add apache license --- .../protostuff/ProtostuffObjectOutputTest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java index 7e88933f59b..bb28386271d 100644 --- a/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java +++ b/dubbo-serialization/dubbo-serialization-test/src/test/java/org/apache/dubbo/common/serialize/protostuff/ProtostuffObjectOutputTest.java @@ -1,3 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.dubbo.common.serialize.protostuff; import static org.hamcrest.MatcherAssert.assertThat;