/** * Copyright 2007-2015, Kaazing Corporation. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.kaazing.k3po.driver.internal.behavior.handler.codec; import static org.jboss.netty.buffer.ChannelBuffers.copiedBuffer; import static org.jboss.netty.channel.Channels.pipeline; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import org.jboss.netty.buffer.ChannelBuffer; import org.jboss.netty.channel.ChannelFactory; import org.jboss.netty.channel.ChannelPipeline; import org.jboss.netty.channel.SimpleChannelHandler; import org.jboss.netty.channel.local.DefaultLocalClientChannelFactory; import org.junit.Before; import org.junit.Test; import org.kaazing.k3po.driver.internal.behavior.ScriptProgressException; public class ReadExactBytesDecoderTest { @Before public void setUp() { ChannelPipeline pipeline = pipeline(new SimpleChannelHandler()); ChannelFactory channelFactory = new DefaultLocalClientChannelFactory(); channelFactory.newChannel(pipeline); pipeline.getContext(SimpleChannelHandler.class); } @Test public void completeMatchOK() throws Exception { MessageDecoder decoder = new ReadExactBytesDecoder(new byte[]{0x01, 0x02, 0x03}); ChannelBuffer remainingBuffer = decoder.decode(copiedBuffer(new byte[]{0x01, 0x02, 0x03})); assertNotNull(remainingBuffer); assertEquals(0, remainingBuffer.readableBytes()); } @Test( expected = ScriptProgressException.class) public void NoMatchOK() throws Exception { MessageDecoder decoder = new ReadExactBytesDecoder(new byte[]{0x01, 0x02, 0x03}); decoder.decode(copiedBuffer(new byte[]{0x01, 0x02, 0x04})); } @Test public void fragmentedMatchOK() throws Exception { MessageDecoder decoder = new ReadExactBytesDecoder(new byte[]{0x01, 0x02, 0x03}); ChannelBuffer remainingBuffer = decoder.decode(copiedBuffer(new byte[]{0x01, 0x02})); assertNull(remainingBuffer); remainingBuffer = decoder.decode(copiedBuffer(new byte[]{0x03})); assertNotNull(remainingBuffer); assertEquals(0, remainingBuffer.readableBytes()); } @Test( expected = ScriptProgressException.class) public void onlyPartialMatchOK() throws Exception { MessageDecoder decoder = new ReadExactBytesDecoder(new byte[]{0x01, 0x02, 0x03}); ChannelBuffer remainingBuffer = decoder.decode(copiedBuffer(new byte[]{0x01, 0x02})); assertNull(remainingBuffer); decoder.decode(copiedBuffer(new byte[]{0x04})); } @Test public void completeMatchWithBytesLeftOverOK() throws Exception { MessageDecoder decoder = new ReadExactBytesDecoder(new byte[]{0x01, 0x02, 0x03}); ChannelBuffer remainingBuffer = decoder.decode(copiedBuffer(new byte[]{0x01, 0x02, 0x03, 0x04, 0x05})); assertNotNull(remainingBuffer); assertEquals(copiedBuffer(new byte[]{0x04, 0x05}), remainingBuffer); } }