SerializationTest.java 4.5 KB
Newer Older
武汉红喜's avatar
武汉红喜 已提交
1 2 3 4 5
package org.hongxi.whatsmars.common.serialize;

import org.junit.Test;

/**
武汉红喜's avatar
武汉红喜 已提交
6
 * @author shenhongxi 2019/8/5
武汉红喜's avatar
武汉红喜 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
 */
public class SerializationTest {

    @Test
    public void testHessian() throws Exception {
        Serialization serialization = new HessianSerialization();
        User user = new User("hongxi", 30);
        byte[] bytes = serialization.serialize(user);
        user = serialization.deserialize(bytes, User.class);
        assert "hongxi".equals(user.name) && user.age == 30;
    }

    @Test
    public void testJava() throws Exception {
        Serialization serialization = new JavaSerialization();
        User user = new User("hongxi", 30);
        byte[] bytes = serialization.serialize(user);
        user = serialization.deserialize(bytes, User.class);
        assert "hongxi".equals(user.name) && user.age == 30;
    }

    @Test
    public void testJson() throws Exception {
        Serialization serialization = new FastJsonSerialization();
        User user = new User("hongxi", 30);
        byte[] bytes = serialization.serialize(user);
        user = serialization.deserialize(bytes, User.class);
        assert "hongxi".equals(user.name) && user.age == 30;
    }

    @Test
    public void testMsgpack() throws Exception {
        Serialization serialization = new MsgpackSerialization();
        User user = new User("hongxi", 30);
        byte[] bytes = serialization.serialize(user);
        user = serialization.deserialize(bytes, User.class);
        assert "hongxi".equals(user.name) && user.age == 30;
    }

    @Test
    public void testProtobuf() throws Exception {
        Serialization serialization = new ProtobufSerialization();
        // 9种基本类型之外的其他类型必须实现 MessageLite 接口
        Integer data = 1;
        byte[] bytes = serialization.serialize(data);
        data = serialization.deserialize(bytes, Integer.class);
        assert data == 1;
    }

    @Test
    public void testFST() throws Exception {
        Serialization serialization = new FSTSerialization();
        User user = new User("hongxi", 30);
        byte[] bytes = serialization.serialize(user);
        user = serialization.deserialize(bytes, User.class);
        assert "hongxi".equals(user.name) && user.age == 30;
    }

    @Test
    public void testHessianMulti() throws Exception {
        Serialization serialization = new HessianSerialization();
        User user = new User("hongxi", 30);
        Object[] data = new Object[]{user, 123, "xxx", false};
        byte[] bytes = serialization.serializeMulti(data);

        Class[] classes = new Class[data.length];
        for (int i = 0; i < data.length; i++) {
            classes[i] = data[i].getClass();
        }
        data = serialization.deserializeMulti(bytes, classes);
        assert data.length == 4 && data[2].toString().equals("xxx");
    }

    @Test
    public void testJavaMulti() throws Exception {
        Serialization serialization = new JavaSerialization();
        User user = new User("hongxi", 30);
        Object[] data = new Object[]{user, 123, "xxx", false};
        byte[] bytes = serialization.serializeMulti(data);

        Class[] classes = new Class[data.length];
        for (int i = 0; i < data.length; i++) {
            classes[i] = data[i].getClass();
        }
        data = serialization.deserializeMulti(bytes, classes);
        assert data.length == 4 && data[2].toString().equals("xxx");
    }

    @Test
    public void testJsonMulti() throws Exception {
        Serialization serialization = new FastJsonSerialization();
        User user = new User("hongxi", 30);
        Object[] data = new Object[]{user, 123, "xxx", false};
        byte[] bytes = serialization.serializeMulti(data);

        Class[] classes = new Class[data.length];
        for (int i = 0; i < data.length; i++) {
            classes[i] = data[i].getClass();
        }
        data = serialization.deserializeMulti(bytes, classes);
        assert data.length == 4 && data[2].toString().equals("xxx");
    }

    @Test
    public void testProtobufMulti() throws Exception {
        Serialization serialization = new ProtobufSerialization();
        Object[] data = new Object[]{9999L, 123, "xxx", false};
        byte[] bytes = serialization.serializeMulti(data);

        Class[] classes = new Class[data.length];
        for (int i = 0; i < data.length; i++) {
            classes[i] = data[i].getClass();
        }
        data = serialization.deserializeMulti(bytes, classes);
        assert data.length == 4 && data[2].toString().equals("xxx");
    }

}