2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.netconf.nettyutil.handler;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
14 import com.google.common.base.Charsets;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import io.netty.channel.ChannelHandlerContext;
18 import java.nio.ByteBuffer;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
24 public class ChunkedFramingMechanismEncoderTest {
26 private int chunkSize;
28 private ChannelHandlerContext ctx;
31 public void setUp() throws Exception {
32 MockitoAnnotations.initMocks(this);
36 @Test(expected = IllegalArgumentException.class)
37 public void testIllegalSize() throws Exception {
38 new ChunkedFramingMechanismEncoder(10);
41 @Test(expected = IllegalArgumentException.class)
42 public void testIllegalSizeMax() throws Exception {
43 new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE);
47 public void testEncode() throws Exception {
48 final ChunkedFramingMechanismEncoder encoder = new ChunkedFramingMechanismEncoder(chunkSize);
49 final int lastChunkSize = 20;
50 final ByteBuf src = Unpooled.wrappedBuffer(getByteArray(chunkSize * 4 + lastChunkSize));
51 final ByteBuf destination = Unpooled.buffer();
52 encoder.encode(ctx, src, destination);
54 assertEquals(1077, destination.readableBytes());
56 byte[] buf = new byte[destination.readableBytes()];
57 destination.readBytes(buf);
58 String s = Charsets.US_ASCII.decode(ByteBuffer.wrap(buf)).toString();
60 assertTrue(s.startsWith("\n#256\na"));
61 assertTrue(s.endsWith("\n#20\naaaaaaaaaaaaaaaaaaaa\n##\n"));
64 private static byte[] getByteArray(final int size) {
65 final byte[] bytes = new byte[size];
66 for (int i = 0; i < size; i++) {