You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by so...@apache.org on 2024/07/06 03:34:37 UTC
(dubbo-spi-extensions) branch 3.3 updated: add FastJsonInOutTest (#439)
This is an automated email from the ASF dual-hosted git repository.
songxiaosheng pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo-spi-extensions.git
The following commit(s) were added to refs/heads/3.3 by this push:
new 3ad6252 add FastJsonInOutTest (#439)
3ad6252 is described below
commit 3ad6252e44551b22adf0ee1488b915e4dfce3062
Author: 王聪洋 <56...@users.noreply.github.com>
AuthorDate: Sat Jul 6 11:34:33 2024 +0800
add FastJsonInOutTest (#439)
* add FastJsonInOutTest
* add License
---
.../fastjson/FastJsonObjectInputOutputTest.java | 203 +++++++++++++++++++++
1 file changed, 203 insertions(+)
diff --git a/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInputOutputTest.java b/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInputOutputTest.java
new file mode 100644
index 0000000..781e3a2
--- /dev/null
+++ b/dubbo-serialization-extensions/dubbo-serialization-fastjson/src/test/java/org/apache/dubbo/common/serialize/fastjson/FastJsonObjectInputOutputTest.java
@@ -0,0 +1,203 @@
+/*
+ * 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.fastjson;
+
+import com.example.test.TestPojo;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Assertions;
+import java.io.IOException;
+import java.io.PipedInputStream;
+import java.io.PipedOutputStream;
+
+public class FastJsonObjectInputOutputTest {
+ private FastJsonObjectOutput fastJsonObjectOutput;
+ private FastJsonObjectInput fastJsonObjectInput;
+
+ private PipedOutputStream pos;
+ private PipedInputStream pis;
+
+
+ /**
+ * Sets up the test environment by initializing PipedInputStream,
+ * PipedOutputStream, and connecting them before each test.
+ *
+ * @throws IOException if there's an issue initializing the streams.
+ */
+ @BeforeEach
+ public void setup() throws IOException {
+ pis = new PipedInputStream();
+ pos = new PipedOutputStream();
+ pis.connect(pos);
+
+ fastJsonObjectOutput = new FastJsonObjectOutput(pos);
+ fastJsonObjectInput = new FastJsonObjectInput(pis);
+ }
+
+ /**
+ * Cleans up the test environment by closing PipedInputStream and
+ * PipedOutputStream after each test.
+ *
+ * @throws IOException if there's an issue closing the streams.
+ */
+ @AfterEach
+ public void clean() throws IOException {
+ if (pos != null) {
+ pos.close();
+ }
+ if (pis != null) {
+ pis.close();
+ }
+ }
+
+ @Test
+ public void testWriteReadString() throws IOException {
+ String testData = "Test String";
+ fastJsonObjectOutput.writeUTF(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ String result = fastJsonObjectInput.readObject(String.class);
+ Assertions.assertEquals(testData, result);
+ }
+
+ @Test
+ public void testWriteReadBool() throws IOException {
+ fastJsonObjectOutput.writeBool(true);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ boolean result = fastJsonObjectInput.readObject(Boolean.class);
+ Assertions.assertTrue(result);
+ }
+
+ @Test
+ public void testWriteReadByte() throws IOException {
+ byte testData = 42;
+ fastJsonObjectOutput.writeByte(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ Byte result = fastJsonObjectInput.readObject(Byte.class);
+ Assertions.assertEquals(testData, result);
+ }
+
+ @Test
+ public void testWriteReadBytes() throws IOException {
+ byte[] testData = "example".getBytes();
+ fastJsonObjectOutput.writeBytes(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ byte[] result = fastJsonObjectInput.readBytes();
+ Assertions.assertArrayEquals(testData, result);
+ }
+
+ @Test
+ public void testWriteReadShort() throws IOException {
+ short testData = 32767;
+ fastJsonObjectOutput.writeShort(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ Short result = fastJsonObjectInput.readObject(Short.class);
+ Assertions.assertEquals(testData, result);
+ }
+
+ @Test
+ public void testWriteReadInt() throws IOException {
+ int testData = 123456;
+ fastJsonObjectOutput.writeInt(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ Integer result = fastJsonObjectInput.readObject(Integer.class);
+ Assertions.assertEquals(testData, result);
+ }
+
+ @Test
+ public void testWriteReadLong() throws IOException {
+ long testData = 123456789L;
+ fastJsonObjectOutput.writeLong(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ Long result = fastJsonObjectInput.readObject(Long.class);
+ Assertions.assertEquals(testData, result);
+ }
+
+ @Test
+ public void testWriteReadFloat() throws IOException {
+ float testData = 3.14f;
+ fastJsonObjectOutput.writeFloat(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ Float result = fastJsonObjectInput.readObject(Float.class);
+ Assertions.assertEquals(testData, result, 0.0001f);
+ }
+
+ @Test
+ public void testWriteReadDouble() throws IOException {
+ double testData = 3.14159;
+ fastJsonObjectOutput.writeDouble(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ Double result = fastJsonObjectInput.readObject(Double.class);
+ Assertions.assertEquals(testData, result, 0.000001);
+ }
+
+ @Test
+ public void testWriteReadUTF() throws IOException {
+ String testData = "Hello, World!";
+ fastJsonObjectOutput.writeUTF(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ String result = fastJsonObjectInput.readObject(String.class);
+ Assertions.assertEquals(testData, result);
+ }
+
+ @Test
+ public void testWriteReadObject() throws IOException {
+ TestPojo testData = new TestPojo("Alice");
+ fastJsonObjectOutput.writeObject(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ TestPojo result = fastJsonObjectInput.readObject(TestPojo.class);
+ Assertions.assertNotNull(result);
+ Assertions.assertEquals(testData.getData(), result.getData());
+ }
+
+ @Test
+ public void testWriteReadObjectWithoutClass() throws IOException {
+
+ TestPojo testData = new TestPojo("Bob");
+ fastJsonObjectOutput.writeObject(testData);
+ fastJsonObjectOutput.flushBuffer();
+ pos.close();
+
+ Object result = fastJsonObjectInput.readObject();
+ Assertions.assertNotNull(result);
+
+ Assertions.assertEquals("Bob", ((TestPojo) result).getData());
+ }
+}
+