@@ -0,0 +1,2524 @@
+package org.menacheri.protocols.impl;%0D%0A%0D%0Aimport static org.junit.Assert.assertEquals;%0D%0Aimport static org.junit.Assert.assertFalse;%0D%0A%0D%0Aimport org.jboss.netty.buffer.ChannelBuffer;%0D%0Aimport org.jboss.netty.handler.codec.embedder.DecoderEmbedder;%0D%0Aimport org.jboss.netty.handler.codec.embedder.EncoderEmbedder;%0D%0Aimport org.jboss.netty.handler.codec.frame.LengthFieldBasedFrameDecoder;%0D%0Aimport org.jboss.netty.handler.codec.frame.LengthFieldPrepender;%0D%0Aimport org.junit.Before;%0D%0Aimport org.junit.Test;%0D%0Aimport org.menacheri.communication.NettyMessageBuffer;%0D%0Aimport org.menacheri.event.Events;%0D%0Aimport org.menacheri.event.IEvent;%0D%0Aimport org.menacheri.handlers.netty.MessageBufferEventDecoder;%0D%0Aimport org.menacheri.handlers.netty.MessageBufferEventEncoder;%0D%0A%0D%0Apublic class MessageBufferProtocolTest %7B%0D%0A%0D%0A%09private MessageBufferProtocol messageBufferProtocol;%0D%0A%09private LengthFieldBasedFrameDecoder frameDecoder;%0D%0A%09%0D%0A%09@Before%0D%0A%09public void setUp()%0D%0A%09%7B%0D%0A%09%09messageBufferProtocol = new MessageBufferProtocol();%0D%0A%09%09messageBufferProtocol.setLengthFieldPrepender(new LengthFieldPrepender(2, false));%0D%0A%09%09messageBufferProtocol.setMessageBufferEventDecoder(new MessageBufferEventDecoder());%0D%0A%09%09messageBufferProtocol.setMessageBufferEventEncoder(new MessageBufferEventEncoder());%0D%0A%09%09frameDecoder = messageBufferProtocol.createLengthBasedFrameDecoder();%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void verifyEventEncodingAndDecoding() throws InterruptedException%0D%0A%09%7B%0D%0A%09%09DecoderEmbedder%3CIEvent%3E decoder = new DecoderEmbedder%3CIEvent%3E(%0D%0A%09%09%09%09frameDecoder,%0D%0A%09%09%09%09messageBufferProtocol.getMessageBufferEventDecoder());%0D%0A%09%09EncoderEmbedder%3CChannelBuffer%3E encoder = new EncoderEmbedder%3CChannelBuffer%3E(%0D%0A%09%09%09%09messageBufferProtocol.getLengthFieldPrepender(),%0D%0A%09%09%09%09messageBufferProtocol.getMessageBufferEventEncoder());%0D%0A%09%09NettyMessageBuffer payload = new NettyMessageBuffer();%0D%0A%09%09payload.writeStrings(%22user%22,%22pass%22,%22TestRoom1%22);%0D%0A%09%09IEvent event = Events.event(payload, Events.LOG_IN);%0D%0A%09%09encoder.offer(event);%0D%0A%09%09ChannelBuffer encoded = encoder.peek();%0D%0A%09%09%0D%0A%09%09Thread.sleep(10);// so that timestamps will differ.%0D%0A%09%09decoder.offer(encoded);%0D%0A%09%09IEvent decoded = decoder.peek();%0D%0A%09%09assertEquals(decoded.getType(),Events.LOG_IN);%0D%0A%09%09assertFalse(%22Timestamps should not be same%22,decoded.getTimeStamp() == event.getTimeStamp());%0D%0A%09%09NettyMessageBuffer decodedPayload = (NettyMessageBuffer)decoded.getSource();%0D%0A%09%09assertEquals(%22user%22,decodedPayload.readString());%0D%0A%09%09assertEquals(%22pass%22,decodedPayload.readString());%0D%0A%09%09assertEquals(%22TestRoom1%22,decodedPayload.readString());%0D%0A%09%7D%0D%0A%7D%0D%0A

Test to see if the encoding and decoding of default MessageBuffer protocol works as expected.