Java Code Examples for com.baidu.bjf.remoting.protobuf.Codec#decode()

The following examples show how to use com.baidu.bjf.remoting.protobuf.Codec#decode() . 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: 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 2
Source File: SimpleDatePOJOTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/** The test simple date. */
@Test
public void testSimpleDate() {
    Codec<SimpleDatePOJO> create = ProtobufProxy.create(SimpleDatePOJO.class, false);
    
    SimpleDatePOJO pojo = new SimpleDatePOJO();
    
    pojo.setDate1(new Date());
    pojo.date = new Date();
    
    try {
        byte[] encode = create.encode(pojo);
        SimpleDatePOJO decode = create.decode(encode);
        Assert.assertEquals(pojo.getDate1().getTime(), decode.getDate1().getTime());
        Assert.assertEquals(pojo.date.getTime(), decode.date.getTime());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example 3
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 4
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 5
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 6
Source File: ComplextListIncludeTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Test empty list case.
 */
@Test
public void testEmptyListCase() {
    ListWithNull listWithNull = new ListWithNull();
    listWithNull.list = new ArrayList();
    
    Codec<ListWithNull> codec = ProtobufProxy.create(ListWithNull.class, false);
    try {
        
        byte[] encode = codec.encode(listWithNull);
        Assert.assertTrue(encode.length == 0);
        ListWithNull listWithNull2 = codec.decode(encode);
        Assert.assertTrue(listWithNull2.list.isEmpty());
    } catch (Exception e) {
        e.printStackTrace();
        org.junit.Assert.fail(e.getMessage());
    }
}
 
Example 7
Source File: ByteTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeClass1() throws IOException  {
    Codec<ByteTypeClass1> codec = ProtobufProxy.create(ByteTypeClass1.class);
    
    ByteTypeClass1 o = new ByteTypeClass1();
    byte[] bb = codec.encode(o);
    
    ByteTypeClass1 class1 = codec.decode(bb);
}
 
Example 8
Source File: SubClassPOJOTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Test sub class POJO.
 */
@Test
public void testSubClassPOJO() {
    
    SubOne subOne = new SubOne();
    subOne.setSubOneName("stringOneName");
    subOne.setName("hello");
    subOne.setAge(100);
    
    ParentClassPOJO subClassPOJO = new ParentClassPOJO();
    subClassPOJO.add(subOne);
    
    
    Codec<ParentClassPOJO> codec = ProtobufProxy.create(ParentClassPOJO.class, false);
    
    Codec<SubClassPOJO> codec2 = ProtobufProxy.create(SubClassPOJO.class, false);
    try {
        byte[] encode = codec.encode(subClassPOJO);
        
        SubClassPOJO decode = codec2.decode(encode);
        
        Assert.assertEquals(subClassPOJO.parents, decode.parents);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
}
 
Example 9
Source File: AnyTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Encode origin decode jprotobuf.
 *
 * @throws IOException Signals that an I/O exception has occurred.
 */
@Test
public void encodeOriginDecodeJprotobuf() 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);

    String m = "hello xiemalin.";
    AnyPOJO anyPojo = new AnyPOJO();
    anyPojo.setMessage(m);

    List<com.baidu.bjf.remoting.protobuf.Any> details = new ArrayList<com.baidu.bjf.remoting.protobuf.Any>();
    details.add(any);
    anyPojo.setDetails(details);

    Codec<AnyPOJO> codec = ProtobufProxy.create(AnyPOJO.class);
    // do encode and decode
    byte[] bytes = codec.encode(anyPojo);
    AnyPOJO anyPojo2 = codec.decode(bytes);

    Assert.assertEquals(m, anyPojo2.getMessage());
    List<com.baidu.bjf.remoting.protobuf.Any> details2 = anyPojo2.getDetails();
    Assert.assertEquals(1, details2.size());

    for (com.baidu.bjf.remoting.protobuf.Any any3 : details2) {
        boolean b = any3.is(StringTypePOJOClass.class);
        Assert.assertTrue(b);
        if (b) {
            StringTypePOJOClass unpack = any3.unpack(StringTypePOJOClass.class);
            Assert.assertEquals(pojo.getStr(), unpack.getStr());
        }
    }

}
 
Example 10
Source File: ByteTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeClass1() throws IOException  {
    Codec codec = ProtobufProxy.create(ByteTypeClass1.class);
    
    ByteTypeClass1 o = new ByteTypeClass1();
    byte[] bb = codec.encode(o);
    
    ByteTypeClass1 class1 = (ByteTypeClass1) codec.decode(bb);
}
 
Example 11
Source File: EnumIDLGeneratorTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testEnumIDLProxy() throws IOException {
    String idl = ProtobufIDLGenerator.getIDL(EnumPOJOClass.class);
    IDLProxyObject idlProxyObject = ProtobufIDLProxy.createSingle(idl);

    idlProxyObject.put("enumAttr", "STRING");

    byte[] bytes = idlProxyObject.encode();
    Codec<EnumPOJOClass> codec = ProtobufProxy.create(EnumPOJOClass.class);
    EnumPOJOClass enumPOJOClass = codec.decode(bytes);
    Assert.assertEquals(enumPOJOClass.enumAttr.value(), EnumAttrPOJO.STRING.value());
}
 
Example 12
Source File: RequrieRepeatedTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeListFields2() throws IOException {
    InterClassName icn = InterClassName.newBuilder().addList("abc").build();
    byte[] byteArray = icn.toByteArray();
    
    Codec create = ProtobufProxy.create(RequrieRepeatedDojoClass2.class);
    RequrieRepeatedDojoClass2 decode = (RequrieRepeatedDojoClass2) create.decode(byteArray);
    
    Assert.assertEquals(1, decode.getList().size());
    Assert.assertEquals("abc", decode.getList().get(0)); 
}
 
Example 13
Source File: SimpleMapTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testPOJODescriptorWorksWell() throws IOException {
    Descriptor descriptor2 = AddressBookProtos.Person.getDescriptor();
    DescriptorProto proto = descriptor2.toProto();
    byte[] byteArray = proto.toByteArray();
    Codec<DescriptorProtoPOJO> codec = ProtobufProxy.create(DescriptorProtoPOJO.class, true);

    DescriptorProtoPOJO decode = codec.decode(byteArray);

    byte[] encode = codec.encode(decode);

    Assert.assertArrayEquals(byteArray, encode);

}
 
Example 14
Source File: PackedValueTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testIncludeEmptyList() {
    PackedProtosPOJO pojo = new PackedProtosPOJO();
    Codec<PackedProtosPOJO> codec = ProtobufProxy.create(PackedProtosPOJO.class);

    List<Integer> list = new ArrayList<Integer>(10000);
    int begin = Integer.MAX_VALUE - 10000;
    for (int i = 0; i < 1; i++) {
        list.add(i + begin);
    }
    pojo.setId(list);

    Builder b1 = Person.newBuilder();
    b1.addAllId(list);
    Person person = b1.build();


    try {
        byte[] byteArray1 = person.toByteArray();
        System.out.println(Arrays.toString(byteArray1));
        byte[] byteArray2 = codec.encode(pojo);
        System.out.println(Arrays.toString(byteArray2));
        Assert.assertArrayEquals(byteArray1, byteArray2);
        
        PackedProtosPOJO pojo2 = codec.decode(person.toByteArray());
        Assert.assertEquals(pojo.getId(), pojo2.getId());
        
        codec.decode(person.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: ProtobufDecoder.java    From jforgame with Apache License 2.0 5 votes vote down vote up
@Override
public Message readMessage(short module, byte cmd, byte[] body) {
	Class<?> msgClazz = MessageFactory.INSTANCE.getMessage(module, cmd);
	try {
		Codec<?> codec = ProtobufProxy.create(msgClazz);
		Message message = (Message) codec.decode(body);
		return message;
	} catch (IOException e) {
		logger.error("读取消息出错,模块号{},类型{},异常{}", new Object[]{module, cmd ,e});
	}
	return null;
}
 
Example 16
Source File: RequrieRepeatedTypeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeListFields3() throws IOException {
    InterClassName icn = InterClassName.newBuilder().addList("abc").build();
    byte[] byteArray = icn.toByteArray();
    
    Codec create = ProtobufProxy.create(RequrieRepeatedDojoClass3.class);
    RequrieRepeatedDojoClass3 decode = (RequrieRepeatedDojoClass3) create.decode(byteArray);
    
    Assert.assertEquals(1, decode.getList().size());
    Assert.assertEquals("abc", decode.getList().get(0)); 
}
 
Example 17
Source File: ProtobufRedisSerializer.java    From jforgame with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T deserialize(byte[] src, Class<T> cls) {
	Codec<T> codec = ProtobufProxy.create(cls);
	try {
		return codec.decode(src);
	} catch (IOException e) {
		throw new IllegalArgumentException("deserialize error", e);
	}
}
 
Example 18
Source File: MapTypeIDLProxyTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testMapWithEnum() throws IOException {
    
    Codec<EnumMapPOJO> codec = ProtobufProxy.create(EnumMapPOJO.class);
    
    EnumMapPOJO pojo = new EnumMapPOJO();
    Map<String, EnumAttrPOJO> map = new HashMap<String, EnumAttrPOJO>();
    map.put("abc", EnumAttrPOJO.STRING);
    pojo.setStringMap(map);
    
    
    byte[] bs = codec.encode(pojo);
    
    EnumMapPOJO pojo2 = codec.decode(bs);
    Assert.assertEquals(EnumAttrPOJO.STRING, pojo2.getStringMap().get("abc"));
}
 
Example 19
Source File: EnumClassTest.java    From jprotobuf with Apache License 2.0 3 votes vote down vote up
@Test
public void testEnumNull() throws IOException {
    Codec<EnumPOJOClass> codec = ProtobufProxy.create(EnumPOJOClass.class, false);
    
    EnumPOJOClass enumPOJOClass = new EnumPOJOClass();
    
    byte[] bs = codec.encode(enumPOJOClass);
    
    EnumPOJOClass enumPOJOClass2 = codec.decode(bs);
    
    
    Assert.assertNotNull(enumPOJOClass2.enumAttr);
}
 
Example 20
Source File: SimpleMapTest.java    From jprotobuf with Apache License 2.0 3 votes vote down vote up
@Test
public void testPOJODecode() throws IOException {

    Builder personBuilder = AddressBookProtos.Person.newBuilder().setName("xiemalin");

    // initialize map
    personBuilder.getMutableStringMap().put("hello", "world");
    personBuilder.getMutableStringMap().put("welcome", "China");

    personBuilder.getMutableIntMap().put(100, 200);

    personBuilder.getMutableLongMap().put(Long.MIN_VALUE, Long.MAX_VALUE);
    personBuilder.getMutableBooleanMap().put(Boolean.TRUE, Boolean.FALSE);

    Person person = personBuilder.build();

    byte[] bytes = person.toByteArray();
    Assert.assertNotNull(bytes);

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

    SimpleMapPOJO pojo = simpleMapPojoCodec.decode(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());
}