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

The following examples show how to use com.baidu.bjf.remoting.protobuf.ProtobufProxy. 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 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 #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: 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 #4
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 #5
Source File: DescritporTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDescriptor() throws IOException {
    Descriptor descriptor2 = AddressBookProtos.AddressBook.getDescriptor();

    FieldDescriptor stringMapFD = descriptor2.findFieldByName("person");
    byte[] bytes = getProtoBytes2();
    DynamicMessage parseFrom = DynamicMessage.parseFrom(descriptor2, bytes);
    Object field = parseFrom.getField(stringMapFD);
    Assert.assertTrue(field instanceof List);
    
    Codec<AddressBookProtosPOJO> codec = ProtobufProxy.create(AddressBookProtosPOJO.class, true);
    Descriptor descriptor = codec.getDescriptor();
    
    stringMapFD = descriptor.findFieldByName("list");

    bytes = getProtoBytes2();

    parseFrom = DynamicMessage.parseFrom(descriptor, bytes);

    Object field2 = parseFrom.getField(stringMapFD);
    Assert.assertTrue(field2 instanceof List);

}
 
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: 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 #8
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 #9
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 #10
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 #11
Source File: SimpleMapTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
private byte[] getProtoBytes2() 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);
    return bytes;
}
 
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 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 #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: 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 #15
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 #16
Source File: JProtobufTest.java    From jforgame with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequest() {
	ReqAccountLogin request = new ReqAccountLogin();
	request.setAccountId(123456L);
	request.setPassword("kingston");
	Codec<ReqAccountLogin> simpleTypeCodec = ProtobufProxy
			.create(ReqAccountLogin.class);
	try {
		// 序列化
		byte[] bb = simpleTypeCodec.encode(request);
		// 反序列化
		ReqAccountLogin request2 = simpleTypeCodec.decode(bb);
		Assert.assertTrue(request2.getAccountId() == request.getAccountId());
		Assert.assertTrue(request2.getPassword().equals(request.getPassword()));
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
Example #17
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 #18
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 #19
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 #20
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 #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: IDLProxyObjectTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
@Test
public void testComplexPOJO() {
    Codec 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 #23
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 #24
Source File: ComplextListIncludeTest.java    From jprotobuf with Apache License 2.0 6 votes vote down vote up
/**
 * Test empty map case.
 */
@Test
public void testEmptyMapCase() {
    ListWithNull listWithNull = new ListWithNull();
    listWithNull.map = new HashMap<String, String>();
    
    Codec<ListWithNull> codec = ProtobufProxy.create(ListWithNull.class);
    try {
        
        byte[] encode = codec.encode(listWithNull);
        Assert.assertTrue(encode.length == 0);
        ListWithNull listWithNull2 = codec.decode(encode);
        Assert.assertTrue(listWithNull2.map.isEmpty());
    } catch (Exception e) {
        org.junit.Assert.fail(e.getMessage());
    }
}
 
Example #25
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 #26
Source File: ComplexMapTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testOriginalMap() {
    java.util.Map<java.lang.String, AddressBookProtos.Person.PhoneNumber> personMap;
    personMap = new HashMap<java.lang.String, AddressBookProtos.Person.PhoneNumber>();

    personMap.put("a", AddressBookProtos.Person.PhoneNumber.newBuilder().setNumber("10000000000")
            .setType(PhoneType.HOME).build());

    java.util.Map<java.lang.String, AddressBookProtos.Person.PhoneType> personTypeMap;
    personTypeMap = new HashMap<java.lang.String, AddressBookProtos.Person.PhoneType>();

    personTypeMap.put("key1", AddressBookProtos.Person.PhoneType.MOBILE);

    Person person = AddressBookProtos.Person.newBuilder().putAllPhoneNumberObjectValueMap(personMap)
            .setName("xiemalin").putAllPhoneTypeEnumValueMap(personTypeMap).build();
    
    byte[] bytes = person.toByteArray();
    System.out.println(Arrays.toString(bytes));
    
    Codec<ComplexMapPOJO> complexMapPOJOCodec = ProtobufProxy.create(ComplexMapPOJO.class, false);
    
    try {
        ComplexMapPOJO decode = complexMapPOJOCodec.decode(bytes);
        Assert.assertEquals("xiemalin", decode.name);
        Assert.assertEquals(1, decode.phoneNumberObjectValueMap.size());
        Assert.assertEquals("10000000000", decode.phoneNumberObjectValueMap.get("a").number);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
}
 
Example #27
Source File: SimpleMapTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testProtobufDynamicParser() throws IOException {
    java.lang.String[] descriptorDataParts = { "\n\021addressbook.proto\022\"com.baidu.bjf.remot"
            + "ing.protobuf.v3\"\212\004\n\006Person\022\014\n\004name\030\001 \002(\t"
            + "\022L\n\tstringMap\030\002 \003(\01329.com.baidu.bjf.remo"
            + "ting.protobuf.v3.Person.StringMapEntry\022F"
            + "\n\006intMap\030\003 \003(\01326.com.baidu.bjf.remoting."
            + "protobuf.v3.Person.IntMapEntry\022H\n\007longMa"
            + "p\030\004 \003(\01327.com.baidu.bjf.remoting.protobu"
            + "f.v3.Person.LongMapEntry\022N\n\nbooleanMap\030\005"
            + " \003(\0132:.com.baidu.bjf.remoting.protobuf.v" + "3.Person.BooleanMapEntry\0320\n\016StringMapEnt",
            "ry\022\013\n\003key\030\001 \001(\t\022\r\n\005value\030\002 \001(\t:\0028\001\032-\n\013In"
                    + "tMapEntry\022\013\n\003key\030\001 \001(\005\022\r\n\005value\030\002 \001(\005:\0028"
                    + "\001\032.\n\014LongMapEntry\022\013\n\003key\030\001 \001(\003\022\r\n\005value\030"
                    + "\002 \001(\003:\0028\001\0321\n\017BooleanMapEntry\022\013\n\003key\030\001 \001("
                    + "\010\022\r\n\005value\030\002 \001(\010:\0028\001BA\n,com.baidu.bjf.re"
                    + "moting.protobuf.v3.simplemapB\021AddressBoo" + "kProtos" };

    StringBuilder descriptorData = new StringBuilder();
    for (String part : descriptorDataParts) {
        descriptorData.append(part);
    }

    final byte[] descriptorBytes;
    try {
        descriptorBytes = descriptorData.toString().getBytes("ISO-8859-1");
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Standard encoding ISO-8859-1 not supported by JVM.", e);
    }

    Codec<FileDescriptorProtoPOJO> codec = ProtobufProxy.create(FileDescriptorProtoPOJO.class, false);

    FileDescriptorProtoPOJO fileDescriptorProtoPOJO = codec.decode(descriptorBytes);

    System.out.println(fileDescriptorProtoPOJO);

}
 
Example #28
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 #29
Source File: ComplextListIncludeTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * test encode and decode with JProtobuf and Protobuf java.
 */
@Test
public void testEncode() {

    Codec<AddressBookProtosPOJO> codec = ProtobufProxy.create(AddressBookProtosPOJO.class, false);

    // jprotobuf -> protobuf
    AddressBookProtosPOJO pojo = new AddressBookProtosPOJO();

    PersonPOJO person = new PersonPOJO();
    person.name = "xiemalin";
    person.id = 100;
    person.boolF = true;
    person.bytesF = new byte[] { 1, 2 };
    person.email = "[email protected]";

    List<PersonPOJO> list = new ArrayList<PersonPOJO>();
    list.add(person);
    list.add(person);
    pojo.setList(list);
    
    pojo.typeList = new ArrayList<TypeDefEnum>();
    pojo.typeList.add(TypeDefEnum.DECIMAL);
    pojo.typeList.add(TypeDefEnum.URL);

    byte[] bb = null;
    try {
        bb = codec.encode(pojo);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

}
 
Example #30
Source File: StringTypeClassTest.java    From jprotobuf with Apache License 2.0 5 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<StringTypePOJOClass> codec = ProtobufProxy.create(StringTypePOJOClass.class, false);
    try {
        byte[] bb = codec.encode(pojo);
        System.out.println(Arrays.toString(bb));
        Assert.assertArrayEquals(byteArray, bb);

        StringTypePOJOClass newPojo = codec.decode(bb);
        System.out.println(newPojo.getStr());
        Assert.assertEquals("你好!", newPojo.getStr());

        OutputStream os = new ByteArrayOutputStream();
        CodedOutputStream newInstance = CodedOutputStream.newInstance(os);
        codec.writeTo(newPojo, newInstance);
        newInstance.flush();
        System.out.println(os.toString());

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}