Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ser units #440

Merged
merged 7 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void testWriteReadObjectWithoutClass() throws IOException, ClassNotFoundE
avroObjectOutput.flushBuffer();
pos.close();

//这里会丢失所有信息
//All the information is lost here
Object result = avroObjectInput.readObject();

assertThat(result, not(nullValue()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.fury;

import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.ObjectOutput;
import org.apache.dubbo.common.serialize.fury.dubbo.FuryObjectInput;
import org.apache.dubbo.common.serialize.fury.dubbo.FuryObjectOutput;
import org.apache.dubbo.common.serialize.fury.dubbo.FurySerialization;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static org.apache.dubbo.common.serialize.fury.dubbo.FurySerialization.FURY_SERIALIZATION_ID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;

public class FurySerializationTest {
private FurySerialization FurySerialization;

@BeforeEach
public void setUp() {
this.FurySerialization = new FurySerialization();
}

@Test
public void testContentType() {
assertThat(FurySerialization.getContentType(), is("fury/consistent"));
}

@Test
public void testContentTypeId() {
assertThat(FurySerialization.getContentTypeId(), is(FURY_SERIALIZATION_ID));
}

@Test
public void testObjectOutput() throws IOException {
ObjectOutput objectOutput = FurySerialization.serialize(null, mock(OutputStream.class));
assertThat(objectOutput, Matchers.<ObjectOutput>instanceOf(FuryObjectOutput.class));
}

@Test
public void testObjectInput() throws IOException {
ObjectInput objectInput = FurySerialization.deserialize(null, mock(InputStream.class));
assertThat(objectInput, Matchers.<ObjectInput>instanceOf(FuryObjectInput.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* 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.kryo;


import org.apache.dubbo.common.serialize.model.Person;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.nullValue;


public class KryObjectInputOutputTest {
private KryoObjectInput kryoObjectInput;
private KryoObjectOutput kryoObjectOutput;

private PipedOutputStream pos;
private PipedInputStream pis;

@BeforeEach
public void setup() throws IOException {
pis = new PipedInputStream();
pos = new PipedOutputStream();
pis.connect(pos);

kryoObjectOutput = new KryoObjectOutput(pos);
kryoObjectInput = new KryoObjectInput(pis);
}

@AfterEach
public void clean() throws IOException {
if (pos != null) {
pos.close();
pos = null;
}
if (pis != null) {
pis.close();
pis = null;
}
}

@Test
public void testWriteReadBool() throws IOException, InterruptedException {
kryoObjectOutput.writeBool(true);
kryoObjectOutput.flushBuffer();
pos.close();

boolean result = kryoObjectInput.readBool();
assertThat(result, is(true));
}

@Test
public void testWriteReadByte() throws IOException {
kryoObjectOutput.writeByte((byte) 'a');
kryoObjectOutput.flushBuffer();
pos.close();

Byte result = kryoObjectInput.readByte();

assertThat(result, is((byte) 'a'));
}

@Test
public void testWriteReadBytes() throws IOException {
kryoObjectOutput.writeBytes("123456".getBytes());
kryoObjectOutput.flushBuffer();
pos.close();

byte[] result = kryoObjectInput.readBytes();

assertThat(result, is("123456".getBytes()));
}

@Test
public void testWriteReadShort() throws IOException {
kryoObjectOutput.writeShort((short) 1);
kryoObjectOutput.flushBuffer();
pos.close();

short result = kryoObjectInput.readShort();

assertThat(result, is((short) 1));
}

@Test
public void testWriteReadInt() throws IOException {
kryoObjectOutput.writeInt(1);
kryoObjectOutput.flushBuffer();
pos.close();

Integer result = kryoObjectInput.readInt();

assertThat(result, is(1));
}

@Test
public void testReadDouble() throws IOException {
kryoObjectOutput.writeDouble(3.14d);
kryoObjectOutput.flushBuffer();
pos.close();

Double result = kryoObjectInput.readDouble();

assertThat(result, is(3.14d));
}

@Test
public void testReadLong() throws IOException {
kryoObjectOutput.writeLong(10L);
kryoObjectOutput.flushBuffer();
pos.close();

Long result = kryoObjectInput.readLong();

assertThat(result, is(10L));
}

@Test
public void testWriteReadFloat() throws IOException {
kryoObjectOutput.writeFloat(1.66f);
kryoObjectOutput.flushBuffer();
pos.close();

Float result = kryoObjectInput.readFloat();

assertThat(result, is(1.66F));
}

@Test
public void testWriteReadUTF() throws IOException {
kryoObjectOutput.writeUTF("wording");
kryoObjectOutput.flushBuffer();
pos.close();

String result = kryoObjectInput.readUTF();

assertThat(result, is("wording"));
}

@Test
public void testWriteReadObject() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(30);
p.setName("abc");

kryoObjectOutput.writeObject(p);
kryoObjectOutput.flushBuffer();
pos.close();

Person result = kryoObjectInput.readObject(Person.class);

assertThat(result, not(nullValue()));
assertThat(result.getName(), is("abc"));
assertThat(result.getAge(), is(30));
}

@Test
public void testWriteReadObjectWithoutClass() throws IOException, ClassNotFoundException {
Person p = new Person();
p.setAge(30);
p.setName("abc");

kryoObjectOutput.writeObject(p);
kryoObjectOutput.flushBuffer();
pos.close();

//All the information is lost here
Object result = kryoObjectInput.readObject();

assertThat(result, not(nullValue()));
// assertThat(result.getName(), is("abc"));
// assertThat(result.getAge(), is(30));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.msgpack;

import org.apache.dubbo.common.serialize.ObjectInput;
import org.apache.dubbo.common.serialize.ObjectOutput;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;

public class MsgpackSerializationTest {
private MsgpackSerialization msgpackSerialization;

@BeforeEach
public void setUp() {
this.msgpackSerialization = new MsgpackSerialization();
}

@Test
public void testContentType() {
assertThat(msgpackSerialization.getContentType(), is("text/json"));
}

@Test
public void testContentTypeId() {
byte result = 27;
assertThat(msgpackSerialization.getContentTypeId(), is(result));
}

@Test
public void testObjectOutput() throws IOException {
ObjectOutput objectOutput = msgpackSerialization.serialize(null, mock(OutputStream.class));
assertThat(objectOutput, Matchers.<ObjectOutput>instanceOf(MsgpackObjectOutput.class));
}

@Test
public void testObjectInput() throws IOException {
ObjectInput objectInput = msgpackSerialization.deserialize(null, mock(InputStream.class));
assertThat(objectInput, Matchers.<ObjectInput>instanceOf(MsgpackObjectInput.class));
}
}
Loading
Loading