CodecTest.java revision 420c9859
1420c9859SShuo Chenpackage muduo.codec.tests; 2420c9859SShuo Chen 3420c9859SShuo Chenimport static org.junit.Assert.assertEquals; 4420c9859SShuo Chenimport muduo.codec.ProtobufDecoder; 5420c9859SShuo Chenimport muduo.codec.ProtobufEncoder; 6420c9859SShuo Chenimport muduo.codec.tests.QueryProtos.Empty; 7420c9859SShuo Chenimport muduo.codec.tests.QueryProtos.Query; 8420c9859SShuo Chen 9420c9859SShuo Chenimport org.jboss.netty.buffer.BigEndianHeapChannelBuffer; 10420c9859SShuo Chenimport org.jboss.netty.buffer.ChannelBuffer; 11420c9859SShuo Chenimport org.junit.Test; 12420c9859SShuo Chen 13420c9859SShuo Chenimport com.google.protobuf.Message; 14420c9859SShuo Chen 15420c9859SShuo Chenpublic class CodecTest { 16420c9859SShuo Chen 17420c9859SShuo Chen @Test 18420c9859SShuo Chen public void testEncoderEmpty() throws Exception { 19420c9859SShuo Chen ProtobufEncoder encoder = new ProtobufEncoder(); 20420c9859SShuo Chen Empty empty = Empty.getDefaultInstance(); 21420c9859SShuo Chen encoder.encode(null, null, empty); 22420c9859SShuo Chen } 23420c9859SShuo Chen 24420c9859SShuo Chen @Test 25420c9859SShuo Chen public void testDecodeEmpty() throws Exception { 26420c9859SShuo Chen ProtobufEncoder encoder = new ProtobufEncoder(); 27420c9859SShuo Chen Empty empty = Empty.getDefaultInstance(); 28420c9859SShuo Chen ChannelBuffer buf = (ChannelBuffer) encoder.encode(null, null, empty); 29420c9859SShuo Chen 30420c9859SShuo Chen ProtobufDecoder decoder = new ProtobufDecoder(); 31420c9859SShuo Chen decoder.addMessageType(Empty.getDefaultInstance()); 32420c9859SShuo Chen Message message = (Message) decoder.decode(null, null, buf); 33420c9859SShuo Chen assertEquals(empty, message); 34420c9859SShuo Chen } 35420c9859SShuo Chen 36420c9859SShuo Chen @Test 37420c9859SShuo Chen public void testQuery() throws Exception { 38420c9859SShuo Chen ProtobufEncoder encoder = new ProtobufEncoder(); 39420c9859SShuo Chen Query query = Query.newBuilder() 40420c9859SShuo Chen .setId(1) 41420c9859SShuo Chen .setQuestioner("Chen Shuo") 42420c9859SShuo Chen .addQuestion("Running?") 43420c9859SShuo Chen .build(); 44420c9859SShuo Chen ChannelBuffer buf = (ChannelBuffer) encoder.encode(null, null, query); 45420c9859SShuo Chen 46420c9859SShuo Chen ProtobufDecoder decoder = new ProtobufDecoder(); 47420c9859SShuo Chen decoder.addMessageType(Query.getDefaultInstance()); 48420c9859SShuo Chen Message message = (Message) decoder.decode(null, null, buf); 49420c9859SShuo Chen assertEquals(query, message); 50420c9859SShuo Chen } 51420c9859SShuo Chen 52420c9859SShuo Chen @Test 53420c9859SShuo Chen public void testQuery2() throws Exception { 54420c9859SShuo Chen ProtobufEncoder encoder = new ProtobufEncoder(); 55420c9859SShuo Chen Query query = Query.newBuilder() 56420c9859SShuo Chen .setId(1) 57420c9859SShuo Chen .setQuestioner("Chen Shuo") 58420c9859SShuo Chen .addQuestion("Running?") 59420c9859SShuo Chen .build(); 60420c9859SShuo Chen ChannelBuffer buf = (ChannelBuffer) encoder.encode(null, null, query); 61420c9859SShuo Chen ChannelBuffer buf2 = new BigEndianHeapChannelBuffer(buf.readableBytes() + 8); 62420c9859SShuo Chen buf2.writeInt(123); 63420c9859SShuo Chen buf2.writeBytes(buf); 64420c9859SShuo Chen 65420c9859SShuo Chen buf2.readInt(); 66420c9859SShuo Chen ProtobufDecoder decoder = new ProtobufDecoder(); 67420c9859SShuo Chen decoder.addMessageType(Query.getDefaultInstance()); 68420c9859SShuo Chen Message message = (Message) decoder.decode(null, null, buf2); 69420c9859SShuo Chen assertEquals(query, message); 70420c9859SShuo Chen } 71420c9859SShuo Chen} 72