Java Code Examples for com.baidu.bjf.remoting.protobuf.ProtobufProxy#create()

The following examples show how to use com.baidu.bjf.remoting.protobuf.ProtobufProxy#create() . 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: 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 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<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 3
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 4
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 5
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 6
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 7
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 8
Source File: AllTypesPressureTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamiceDecode() throws IOException {
    long time = System.currentTimeMillis();
    InterClassName icn = InterClassName.newBuilder()
    .setBoolF(true)
    .setBytesF(ByteString.copyFrom(new byte[] {1, 2}))
    .setDoubleF(202D)
    .setFixed32F(1)
    .setFixed64F(2L)
    .setFloatF(303F)
    .setInt32F(4)
    .setInt64F(5L)
    .setSfixed32F(6)
    .setSfixed64F(7L)
    .setSint32F(8)
    .setSint64F(9L)
    .setStringF("world")
    .setUint32F(10)
    .setUint64F(11L)
    .build();

    long averageEncode = 0;
    byte[] bb = icn.toByteArray();
    
    Codec dojoClassProxy = ProtobufProxy.create(AllTypesDojoClassWithDefault.class);
    for (int i = 0; i < times; i++) {
        dojoClassProxy.decode(bb);
    }
    averageEncode = System.currentTimeMillis() - time;
    System.out.println("dynamic decode average time:" + (averageEncode / times));
}
 
Example 9
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 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: 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<RequrieRepeatedDojoClass3> create = ProtobufProxy.create(RequrieRepeatedDojoClass3.class);
    RequrieRepeatedDojoClass3 decode = create.decode(byteArray);
    
    Assert.assertEquals(1, decode.getList().size());
    Assert.assertEquals("abc", decode.getList().get(0)); 
}
 
Example 12
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 13
Source File: RequrieRepeatedDojoClassWithLargeIndexTest.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
private byte[] decodeByJprotobuf() throws IOException {
    Codec<RequrieRepeatedDojoClassWithLargeIndex> codec = ProtobufProxy.create(RequrieRepeatedDojoClassWithLargeIndex.class);
    
    RequrieRepeatedDojoClassWithLargeIndex pojo = new RequrieRepeatedDojoClassWithLargeIndex();
    pojo.list = new ArrayList<String>(2);
    pojo.list.add("hello");
    pojo.list.add("world");
    
    byte[] byteArray = codec.encode(pojo);
    
    return byteArray;
}
 
Example 14
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 15
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 16
Source File: IgnoredAnnotationTests.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
/**
 * Test ignore POJO class.
 */
@Test
public void testProxybufProxyOnIgnoredPOJOClass() {
    Codec<IgnoredPOJOClass> codec = ProtobufProxy.create(IgnoredPOJOClass.class);
    Assert.assertNull(codec);
}
 
Example 17
Source File: AllTypesTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void  testRequiredMutliTypeEncode() throws IOException {
	Codec<AllTypesPojoClass> dojoClassProxy = ProtobufProxy.create(AllTypesPojoClass.class);
    //AllTypesDojoClass$$BJFProtoBufClass dojoClassProxy = new AllTypesDojoClass$$BJFProtoBufClass();
	AllTypesPojoClass c = new AllTypesPojoClass();
	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;
	c.typeDefEnum = TypeDefEnum.DECIMAL;
	
	byte[] bb = dojoClassProxy.encode(c);
	
	InterClassName icn = InterClassName.parseFrom(bb);
	Assert.assertEquals(c.doubleF, icn.getDoubleF());
	byte[] bbb = icn.getBytesF().toByteArray();
	Assert.assertEquals(2, bbb.length);
	Assert.assertEquals(1, bbb[0]);
	Assert.assertEquals(2, bbb[1]);
	Assert.assertEquals(101D, icn.getDoubleF());
	Assert.assertEquals(1, icn.getFixed32F());
	Assert.assertEquals(2L, icn.getFixed64F());
	Assert.assertEquals(102F, icn.getFloatF());
	Assert.assertEquals(3, icn.getInt32F());
	Assert.assertEquals(4L, icn.getInt64F());
    Assert.assertEquals(5, icn.getSfixed32F());
    Assert.assertEquals(6L, icn.getSfixed64F());
       Assert.assertEquals(7, icn.getSint32F());
       Assert.assertEquals(8L, icn.getSint64F());
       Assert.assertEquals("hello", icn.getStringF());
       Assert.assertEquals(false, icn.getBoolF());
       Assert.assertEquals(9, icn.getUint32F());
       Assert.assertEquals(10L, icn.getUint64F());
       Assert.assertEquals(TypeDefEnum.DECIMAL.value(), icn.getEnumT().getNumber());
}
 
Example 18
Source File: RequrieRepeatedTypeTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeListFields2() throws IOException {
    
    
    Codec<RequrieRepeatedDojoClass2> create = ProtobufProxy.create(RequrieRepeatedDojoClass2.class);
    
    
    RequrieRepeatedDojoClass2 dc = new RequrieRepeatedDojoClass2();
    dc.setList( new ArrayList<String>());
    dc.getList().add("abc");
    
    byte[] bb = create.encode(dc);
    
    InterClassName parseFrom = InterClassName.parseFrom(bb);
    List<String> listList = parseFrom.getListList();
    
    Assert.assertEquals(1, listList.size());
    Assert.assertEquals("abc", listList.get(0));
    
    
}
 
Example 19
Source File: EnumClassTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEnum() throws IOException {
    
    
    Codec codec = ProtobufProxy.create(EnumPOJOClass.class);
    EnumPOJOClass ec = new EnumPOJOClass();
    ec.enumAttr = EnumAttrPOJO.INT;
    
    byte[] bytes = codec.encode(ec);
    
    EnumPOJOClass decode = (EnumPOJOClass) 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());
}
 
Example 20
Source File: RequrieRepeatedTypeTest.java    From jprotobuf with Apache License 2.0 4 votes vote down vote up
@Test
public void testEncodeListFields() throws IOException {
    
    
    Codec<RequrieRepeatedDojoClass> create = ProtobufProxy.create(RequrieRepeatedDojoClass.class);
    
    
    RequrieRepeatedDojoClass dc = new RequrieRepeatedDojoClass();
    dc.list = new ArrayList<String>();
    dc.list.add("abc");
    
    byte[] bb = create.encode(dc);
    
    InterClassName parseFrom = InterClassName.parseFrom(bb);
    List<String> listList = parseFrom.getListList();
    
    Assert.assertEquals(1, listList.size());
    Assert.assertEquals("abc", listList.get(0));
    
    
}