com.baidu.bjf.remoting.protobuf.Codec Java Examples

The following examples show how to use com.baidu.bjf.remoting.protobuf.Codec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnyTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Test any.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void testAny() throws IOException {
    StringTypePOJOClass pojo = new StringTypePOJOClass();
    pojo.setStr("hello world");

    com.baidu.bjf.remoting.protobuf.Any any = com.baidu.bjf.remoting.protobuf.Any.pack(pojo);

    Codec<com.baidu.bjf.remoting.protobuf.Any> codec =
            ProtobufProxy.create(com.baidu.bjf.remoting.protobuf.Any.class);
    byte[] bytes = codec.encode(any);
    com.baidu.bjf.remoting.protobuf.Any any2 = codec.decode(bytes);

    boolean b = any2.is(StringTypePOJOClass.class);
    Assert.assertTrue(b);
    StringTypePOJOClass unpack = any2.unpack(StringTypePOJOClass.class);
    Assert.assertEquals(pojo.getStr(), unpack.getStr());

}
 
Example #2
Source File: RequrieRepeatedNumberType3Test.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecode() {
    
    InterClassName icn = InterClassName.newBuilder().addList2(10000D).addList2(20000.1D).build();
    
    byte[] oldbb = icn.toByteArray();
    System.out.println(Arrays.toString(oldbb));
    
    
    RequrieRepeatedNumberTypePOJOClass3 type = new RequrieRepeatedNumberTypePOJOClass3();
    
    type.list2 = new ArrayList<Double>();
    type.list2.add(10000D);
    type.list2.add(20000.1D);
    
    
    Codec proxy = ProtobufProxy.create(RequrieRepeatedNumberTypePOJOClass3.class);
    try {
        byte[] bb = proxy.encode(type);
        System.out.println(Arrays.toString(bb));
        Assert.assertArrayEquals(oldbb, bb);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: EnumClassTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnum() throws IOException {

    Codec<EnumPOJOClass> codec = ProtobufProxy.create(EnumPOJOClass.class, false);
    EnumPOJOClass ec = new EnumPOJOClass();
    ec.enumAttr = EnumAttrPOJO.INT;

    byte[] bytes = codec.encode(ec);
    System.out.println(Arrays.toString(bytes));
    EnumPOJOClass decode = codec.decode(bytes);
    Assert.assertEquals(EnumAttrPOJO.INT, decode.enumAttr);

    byte[] byteArray = EnumClassInternal.newBuilder()
            .setStatus(com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT).build().toByteArray();
    Assert.assertArrayEquals(bytes, byteArray);

    EnumClassInternal enumClass =
            com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumClassInternal.parseFrom(bytes);

    Assert.assertEquals(com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT, enumClass.getStatus());
    
    // process default
    decode = codec.decode(new byte[0]);
    Assert.assertEquals(EnumAttrPOJO.STRING, decode.enumAttr);
}
 
Example #4
Source File: RequrieRepeatedNumberTypeTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecode() {
    
    InterClassName icn = InterClassName.newBuilder().addList3("a").addList3("b").build();
    
    byte[] oldbb = icn.toByteArray();
    System.out.println(Arrays.toString(oldbb));
    
    
    RequrieRepeatedNumberTypePOJOClass type = new RequrieRepeatedNumberTypePOJOClass();
    
    type.list3 = new ArrayList<String>();
    type.list3.add("a");
    type.list3.add("b");
    
    
    Codec<RequrieRepeatedNumberTypePOJOClass> proxy = ProtobufProxy.create(RequrieRepeatedNumberTypePOJOClass.class);
    try {
        byte[] bb = proxy.encode(type);
        Assert.assertArrayEquals(oldbb, bb);
        System.out.println(Arrays.toString(bb));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #5
Source File: StringSetTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Test string set POJO.
 */
@Test
public void testStringSetPOJO() {
    Codec<StringSetDojoClass> codec = ProtobufProxy.create(StringSetDojoClass.class, false);
    
    StringSetDojoClass stringSet = new StringSetDojoClass();
    stringSet.stringSet = new HashSet<String>();
    
    stringSet.stringSet.add("hello");
    stringSet.stringSet.add("world");
    stringSet.stringSet.add("xiemalin");
    
    
    try {
        byte[] bs = codec.encode(stringSet);
        StringSetDojoClass stringSetDojoClass = codec.decode(bs);
        Assert.assertEquals(3, stringSetDojoClass.stringSet.size());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #6
Source File: RequrieRepeatedNumberTypeTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecode() {
    
    InterClassName icn = InterClassName.newBuilder().addList3("a").addList3("b").build();
    
    byte[] oldbb = icn.toByteArray();
    System.out.println(Arrays.toString(oldbb));
    
    
    RequrieRepeatedNumberTypePOJOClass type = new RequrieRepeatedNumberTypePOJOClass();
    
    type.list3 = new ArrayList<String>();
    type.list3.add("a");
    type.list3.add("b");
    
    
    Codec proxy = ProtobufProxy.create(RequrieRepeatedNumberTypePOJOClass.class);
    try {
        byte[] bb = proxy.encode(type);
        Assert.assertArrayEquals(oldbb, bb);
        System.out.println(Arrays.toString(bb));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #7
Source File: DescritporTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
private byte[] getProtoBytes2() throws IOException {
    Codec<AddressBookProtosPOJO> simpleMapPojoCodec = ProtobufProxy.create(AddressBookProtosPOJO.class, false);

    // initialize

    AddressBookProtosPOJO addressBook = new AddressBookProtosPOJO();
    List<PersonPOJO> list = new ArrayList<PersonPOJO>();
    addressBook.setList(list);

    PersonPOJO pojo = new PersonPOJO();
    pojo.name = "xiemalin";
    pojo.id = 100;
    list.add(pojo);

    byte[] bytes = simpleMapPojoCodec.encode(addressBook);
    return bytes;
}
 
Example #8
Source File: CodedConstant.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Compute object size no tag.
 *
 * @param o the o
 * @return the int
 */
public static int computeObjectSizeNoTag(Object o) {
    int size = 0;
    if (o == null) {
        return size;
    }

    Class cls = o.getClass();
    Codec target = ProtobufProxy.create(cls);
    try {
        size = target.size(o);
        size = size + CodedOutputStream.computeRawVarint32Size(size);
        return size;
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
Example #9
Source File: EnumClassTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnum2() throws IOException {

    Codec<EnumPOJOClass2> codec = ProtobufProxy.create(EnumPOJOClass2.class);
    EnumPOJOClass2 ec = new EnumPOJOClass2();
    ec.setEnumAttr(EnumAttrPOJO.INT);

    byte[] bytes = codec.encode(ec);
    EnumPOJOClass2 decode = codec.decode(bytes);
    Assert.assertEquals(EnumAttrPOJO.INT, decode.getEnumAttr());

    byte[] byteArray = EnumClassInternal.newBuilder()
            .setStatus(com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT).build().toByteArray();
    Assert.assertArrayEquals(bytes, byteArray);
    EnumClassInternal enumClass =
            com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumClassInternal.parseFrom(bytes);

    Assert.assertEquals(com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT, enumClass.getStatus());
}
 
Example #10
Source File: IDLProxyObjectTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimplePOJO() {
    
    Codec codec = ProtobufProxy.create(SimplePOJO.class);
    
    SimplePOJO  target = new SimplePOJO();
    
    IDLProxyObject idlProxyObject = new IDLProxyObject(codec, target, SimplePOJO.class);
    
    idlProxyObject.put("name", "hello");
    Assert.assertEquals("hello", idlProxyObject.get("name"));
    Assert.assertEquals("hello", target.getName());
    Assert.assertEquals("hello", ((SimplePOJO) idlProxyObject.getTarget()).getName());
    
    
    idlProxyObject.put("age", 100);
    Assert.assertEquals(100, idlProxyObject.get("age"));
    Assert.assertEquals(100, target.getAge());
    Assert.assertEquals(100, ((SimplePOJO) idlProxyObject.getTarget()).getAge());
}
 
Example #11
Source File: RequrieRepeatedNumberType3Test.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecode() {
    
    InterClassName icn = InterClassName.newBuilder().addList2(10000D).addList2(20000.1D).build();
    
    byte[] oldbb = icn.toByteArray();
    System.out.println(Arrays.toString(oldbb));
    
    
    RequrieRepeatedNumberTypePOJOClass3 type = new RequrieRepeatedNumberTypePOJOClass3();
    
    type.list2 = new ArrayList<Double>();
    type.list2.add(10000D);
    type.list2.add(20000.1D);
    
    
    Codec<RequrieRepeatedNumberTypePOJOClass3> proxy = ProtobufProxy.create(RequrieRepeatedNumberTypePOJOClass3.class);
    try {
        byte[] bb = proxy.encode(type);
        System.out.println(Arrays.toString(bb));
        Assert.assertArrayEquals(oldbb, bb);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #12
Source File: RequrieRepeatedNumberType2Test.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecode() {
    
    InterClassName icn = InterClassName.newBuilder().addList1(10000).addList1(20000).build();
    
    byte[] oldbb = icn.toByteArray();
    System.out.println(Arrays.toString(oldbb));
    
    
    RequrieRepeatedNumberTypePOJOClass2 type = new RequrieRepeatedNumberTypePOJOClass2();
    
    type.list1 = new ArrayList<Integer>();
    type.list1.add(10000);
    type.list1.add(20000);
    
    
    Codec<RequrieRepeatedNumberTypePOJOClass2> proxy;
    proxy = ProtobufProxy.create(RequrieRepeatedNumberTypePOJOClass2.class, false);
    try {
        byte[] bb = proxy.encode(type);
        Assert.assertArrayEquals(oldbb, bb);
        System.out.println(Arrays.toString(bb));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #13
Source File: ListWithExtendsGenericValueTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testWildtypeGenericType() {
    Codec<ListWithExtendsGenericValue> codec = ProtobufProxy.create(ListWithExtendsGenericValue.class);
    
    
    ListWithExtendsGenericValue value = new ListWithExtendsGenericValue();
    
    List<PersonPOJO> list = new ArrayList<PersonPOJO>();
    
    value.list = list;
    value.list2 = list;
    
    PersonPOJO personPOJO = new PersonPOJO();
    personPOJO.name = "xiemalin";
    personPOJO.id = 100;
    
    list.add(personPOJO);
    
    try {
        byte[] encode = codec.encode(value);
        Assert.assertNotNull(encode);
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    
}
 
Example #14
Source File: IgnoredAnnotationTests.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Test ignore POJO class 3.
 */
@Test
public void testProxybufProxyOnIgnoredPOJOClass3() {
    Codec<IgnoredPOJOClass3> codec = ProtobufProxy.create(IgnoredPOJOClass3.class);
    
    IgnoredPOJOClass3 pojo = new IgnoredPOJOClass3();
    pojo.name = "abc";
    
    IgnoredPOJOClass3 pojo2 = new IgnoredPOJOClass3();
    pojo2.name = "abc";
    pojo2.address = "hello";
    
    try {
        byte[] encode = codec.encode(pojo2);
        byte[] encode2 = codec.encode(pojo);
        Assert.assertArrayEquals(encode, encode2);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
}
 
Example #15
Source File: StringTypeClassTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecode() {
    
    StringMessage message = StringMessage.newBuilder().setList("你好!").build();
    
    byte[] byteArray = message.toByteArray();
    System.out.println(Arrays.toString(byteArray));
    
    StringTypePOJOClass pojo = new StringTypePOJOClass();
    pojo.setStr("你好!");
    
    Codec codec = ProtobufProxy.create(StringTypePOJOClass.class);
    try {
        byte[] bb = codec.encode(pojo);
        System.out.println(Arrays.toString(bb));
        Assert.assertArrayEquals(byteArray, bb);
        
        StringTypePOJOClass newPojo = (StringTypePOJOClass) codec.decode(bb);
        System.out.println(newPojo.getStr());
        Assert.assertEquals("你好!", newPojo.getStr());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
 
Example #16
Source File: MapTypeIDLProxyTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testSimpleMap() throws IOException {
    InputStream is = MapTypeIDLProxyTest.class.getResourceAsStream("simple-map.proto");
    IDLProxyObject idlProxyMap = ProtobufIDLProxy.createSingle(is);
    
    
    idlProxyMap.put("name", "hello");
    Map<String, String> map = new HashMap<String, String>();
    
    map.put("hello", "world");
    idlProxyMap.put("stringMap", map);
    
    byte[] bytes = idlProxyMap.encode();
    
    Codec<SimpleMapPOJO> codec = ProtobufProxy.create(SimpleMapPOJO.class);
    SimpleMapPOJO simpleMapPOJO = codec.decode(bytes);
    Assert.assertEquals("hello", simpleMapPOJO.name);
    Assert.assertEquals(1, simpleMapPOJO.getStringMap().size());
}
 
Example #17
Source File: IDLProxyObjectTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimplePOJO() {
    
    Codec<SimplePOJO> codec = ProtobufProxy.create(SimplePOJO.class);
    
    SimplePOJO  target = new SimplePOJO();
    
    IDLProxyObject idlProxyObject = new IDLProxyObject(codec, target, SimplePOJO.class);
    
    idlProxyObject.put("name", "hello");
    Assert.assertEquals("hello", idlProxyObject.get("name"));
    Assert.assertEquals("hello", target.getName());
    Assert.assertEquals("hello", ((SimplePOJO) idlProxyObject.getTarget()).getName());
    
    
    idlProxyObject.put("age", 100);
    Assert.assertEquals(100, idlProxyObject.get("age"));
    Assert.assertEquals(100, target.getAge());
    Assert.assertEquals(100, ((SimplePOJO) idlProxyObject.getTarget()).getAge());
}
 
Example #18
Source File: IDLProxyObjectTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testComplexPOJO() {
    Codec<ComplexPOJO> codec = ProtobufProxy.create(ComplexPOJO.class);
    
    ComplexPOJO  target = new ComplexPOJO();
    
    IDLProxyObject idlProxyObject = new IDLProxyObject(codec, target, ComplexPOJO.class);
    
    idlProxyObject.put("name", "hello");
    Assert.assertEquals("hello", idlProxyObject.get("name"));
    Assert.assertEquals("hello", target.getName());
    Assert.assertEquals("hello", ((ComplexPOJO) idlProxyObject.getTarget()).getName());
    
    
    idlProxyObject.put("age", 100);
    Assert.assertEquals(100, idlProxyObject.get("age"));
    Assert.assertEquals(100, target.getAge());
    Assert.assertEquals(100, ((ComplexPOJO) idlProxyObject.getTarget()).getAge());
    
    //test sub
    idlProxyObject.put("simplePOJO.name", "hello");
    Assert.assertEquals("hello", idlProxyObject.get("simplePOJO.name"));
    Assert.assertEquals("hello", target.getSimplePOJO().getName());
    Assert.assertEquals("hello", ((ComplexPOJO) idlProxyObject.getTarget()).getSimplePOJO().getName());
}
 
Example #19
Source File: RequrieRepeatedNumberType2Test.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncodeDecode() {
    
    InterClassName icn = InterClassName.newBuilder().addList1(10000).addList1(20000).build();
    
    byte[] oldbb = icn.toByteArray();
    System.out.println(Arrays.toString(oldbb));
    
    
    RequrieRepeatedNumberTypePOJOClass2 type = new RequrieRepeatedNumberTypePOJOClass2();
    
    type.list1 = new ArrayList<Integer>();
    type.list1.add(10000);
    type.list1.add(20000);
    
    
    Codec proxy;
    proxy = ProtobufProxy.create(RequrieRepeatedNumberTypePOJOClass2.class);
    try {
        byte[] bb = proxy.encode(type);
        Assert.assertArrayEquals(oldbb, bb);
        System.out.println(Arrays.toString(bb));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #20
Source File: DateTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Test time stamp.
 */
@Test
public void testTimeStamp() {
    long secs = System.currentTimeMillis() / 1000;
    int nanos = (int) (System.currentTimeMillis() % 1000) * 1000000;

    Timestamp ts = Timestamp.newBuilder().setSeconds(secs).setNanos(nanos).build();

    com.baidu.bjf.remoting.protobuf.Timestamp ts2 = new com.baidu.bjf.remoting.protobuf.Timestamp();
    ts2.setSeconds(secs);
    ts2.setNanos(nanos);

    Codec<com.baidu.bjf.remoting.protobuf.Timestamp> codec =
            ProtobufProxy.create(com.baidu.bjf.remoting.protobuf.Timestamp.class);

    try {
        byte[] encode = codec.encode(ts2);
        Assert.assertEquals(Arrays.toString(encode), Arrays.toString(ts.toByteArray()));
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #21
Source File: EnumClassTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testEnum2() throws IOException {
    
    
    Codec codec = ProtobufProxy.create(EnumPOJOClass2.class);
    EnumPOJOClass2 ec = new EnumPOJOClass2();
    ec.setEnumAttr(EnumAttrPOJO.INT);
    
    byte[] bytes = codec.encode(ec);
    EnumPOJOClass2 decode = (EnumPOJOClass2) codec.decode(bytes);
    Assert.assertEquals(EnumAttrPOJO.INT, decode.getEnumAttr());
    
    byte[] byteArray = EnumClassInternal.newBuilder().setStatus(
            com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT).build().toByteArray();
    Assert.assertArrayEquals(bytes, byteArray);
    EnumClassInternal enumClass = com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumClassInternal.parseFrom(bytes);
 
    Assert.assertEquals(com.baidu.bjf.remoting.protobuf.enumeration.EnumClass.EnumAttr.INT, enumClass.getStatus());
}
 
Example #22
Source File: AnyTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Test use any.
 * 
 * @throws IOException
 */
@Test
public void testDecodeUseOriginAny() throws IOException {

    StringTypePOJOClass pojo = new StringTypePOJOClass();
    pojo.setStr("hello world");

    com.baidu.bjf.remoting.protobuf.Any any =
            com.baidu.bjf.remoting.protobuf.Any.pack(pojo, StringMessage.class.getName());

    Codec<com.baidu.bjf.remoting.protobuf.Any> codec =
            ProtobufProxy.create(com.baidu.bjf.remoting.protobuf.Any.class);
    byte[] byteArray = codec.encode(any);

    AnyObject anyObject = AnyProtos.AnyObject.parseFrom(byteArray);

    List<Any> detailsList = anyObject.getDetailsList();

    for (Any any2 : detailsList) {
        if (any2.is(StringMessage.class)) {
            StringMessage unpack = any2.unpack(StringMessage.class);

            Assert.assertEquals(pojo.getStr(), unpack.getList());
        }
    }
}
 
Example #23
Source File: ProtobufMessageConverter.java    From common-project with Apache License 2.0 6 votes vote down vote up
@Override
public Message toMessage(Object obj, MessageProperties messageProperties) throws MessageConversionException {
    String messageType = obj.getClass().getSimpleName();
    Codec<Object> codec = codecMap.get(messageType);
    if (codec == null) {
        throw new MessageConversionException("不支持转换的消息类型:" + messageType);
    }
    messageProperties.setHeader("messageType", messageType);
    byte[] body;
    try {
        body = codec.encode(obj);
    } catch (Exception e) {
        throw new MessageConversionException("编码失败:" + messageType, e);
    }
    return new Message(body, messageProperties);
}
 
Example #24
Source File: ByteTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeClass4() throws IOException  {
    Codec<ByteTypeClass4> codec = ProtobufProxy.create(ByteTypeClass4.class);
    
    ByteTypeClass4 o = new ByteTypeClass4();
    byte[] bb = codec.encode(o);
    
    ByteTypeClass4 class1 =  codec.decode(bb);
}
 
Example #25
Source File: AllTypesPressureTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamiceEncode() throws IOException {
    long time = System.currentTimeMillis();
    Codec dojoClassProxy = ProtobufProxy.create(AllTypesDojoClass.class);
    //AllTypesDojoClass$$BJFProtoBufClass dojoClassProxy = new AllTypesDojoClass$$BJFProtoBufClass();
    AllTypesDojoClass c = new AllTypesDojoClass();
    c.boolF = false;
    c.bytesF = new byte[] {1,2};
    c.doubleF = 101D;
    c.fixed32F = 1;
    c.fixed64F = 2L;
    c.floatF = 102F;
    c.int32F = 3;
    c.int64F = 4L;
    c.sfixed32F = 5;
    c.sfixed64F = 6L;
    c.sint32F = 7;
    c.sint64F = 8L;
    c.stringF = "hello";
    c.uint32F = 9;
    c.uint64F = 10L;
    
    long averageEncode = 0;
    
    for (int i = 0; i < times; i++) {
        byte[] bb = dojoClassProxy.encode(c);
        
    }
    averageEncode = System.currentTimeMillis() - time;
    System.out.println("dynamic encode average time:" + (averageEncode / times));
}
 
Example #26
Source File: RequrieRepeatedTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeListFields() throws IOException {
    InterClassName icn = InterClassName.newBuilder().addList("abc").build();
    byte[] byteArray = icn.toByteArray();
    
    Codec create = ProtobufProxy.create(RequrieRepeatedDojoClass.class);
    RequrieRepeatedDojoClass decode = (RequrieRepeatedDojoClass) create.decode(byteArray);
    
    Assert.assertEquals(1, decode.list.size());
    Assert.assertEquals("abc", decode.list.get(0));
}
 
Example #27
Source File: ProtobufMessageConverter.java    From common-project with Apache License 2.0 5 votes vote down vote up
@Override
public Object fromMessage(Message message) throws MessageConversionException {
    String messageType = message.getMessageProperties().getHeaders().get("messageType").toString();
    Codec<Object> codec = codecMap.get(messageType);
    if (codec == null) {
        throw new MessageConversionException("不支持转换的消息类型:" + messageType);
    }
    Object obj;
    try {
        obj = codec.decode(message.getBody());
    } catch (Exception e) {
        throw new MessageConversionException("解码失败:" + messageType, e);
    }
    return obj;
}
 
Example #28
Source File: DateTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Test date encode with orignal PB class.
 */
@Test
public void testDateEncodeWithOrignalPBClass() {

    long secs = System.currentTimeMillis() / 1000;
    int nanos = (int) (System.currentTimeMillis() % 1000) * 1000000;

    Timestamp ts = Timestamp.newBuilder().setSeconds(secs).setNanos(nanos).build();

    Person person = Person.newBuilder().setTs(ts).build();

    byte[] byteArray = person.toByteArray();

    String expected = Arrays.toString(byteArray);

    DatePOJO datePojo = new DatePOJO();
    com.baidu.bjf.remoting.protobuf.Timestamp ts2 = new com.baidu.bjf.remoting.protobuf.Timestamp();
    ts2.setSeconds(secs);
    ts2.setNanos(nanos);
    datePojo.setTimeStamp(ts2);

    Codec<DatePOJO> codec = ProtobufProxy.create(DatePOJO.class);
    try {
        byte[] encode = codec.encode(datePojo);
        String actual = Arrays.toString(encode);
        DatePOJO decode = codec.decode(byteArray);
        Assert.assertEquals(expected, actual);
        Assert.assertEquals(decode.getTimeStamp().getSeconds().longValue(), person.getTs().getSeconds());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example #29
Source File: SimpleMapTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testPOJOEncode() throws IOException {

    Codec<SimpleMapPOJO> simpleMapPojoCodec = ProtobufProxy.create(SimpleMapPOJO.class, false);

    // initialize map
    SimpleMapPOJO pojo = new SimpleMapPOJO();
    pojo.name = "xiemalin";

    Map<String, String> stringMap = new HashMap<String, String>();
    stringMap.put("hello", "world");
    stringMap.put("welcome", "China");
    pojo.setStringMap(stringMap);

    Map<Integer, Integer> intMap = new HashMap<Integer, Integer>();
    intMap.put(100, 200);
    pojo.setMyIntMap(intMap);

    pojo.longMap = new HashMap<Long, Long>();
    pojo.longMap.put(Long.MIN_VALUE, Long.MAX_VALUE);

    pojo.booleanMap = new HashMap<Boolean, Boolean>();
    pojo.booleanMap.put(Boolean.TRUE, Boolean.FALSE);

    byte[] bytes = simpleMapPojoCodec.encode(pojo);

    Assert.assertNotNull(bytes);

    // to validate
    Person person = AddressBookProtos.Person.parseFrom(bytes);

    Assert.assertEquals(pojo.name, person.getName());

    Assert.assertEquals(pojo.getStringMap(), person.getStringMap());

    Assert.assertEquals(pojo.getMyIntMap(), person.getIntMap());
    Assert.assertEquals(pojo.longMap, person.getLongMap());
    Assert.assertEquals(pojo.booleanMap, person.getBooleanMap());
}
 
Example #30
Source File: RequrieRepeatedDojoClassWithLargeIndexTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
private byte[] decodeByJprotobuf() throws IOException {
    Codec codec = ProtobufProxy.create(RequrieRepeatedDojoClassWithLargeIndex.class);
    
    RequrieRepeatedDojoClassWithLargeIndex pojo = new RequrieRepeatedDojoClassWithLargeIndex();
    pojo.list = new ArrayList<String>(2);
    pojo.list.add("hello");
    pojo.list.add("world");
    
    System.out.println(codec.size(pojo));
    
    byte[] byteArray = codec.encode(pojo);
    
    return byteArray;
}