|
| 1 | +package org.asynchttpclient.netty.util; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.netty.buffer.Unpooled; |
| 5 | +import java.nio.charset.Charset; |
| 6 | +import org.testng.annotations.Test; |
| 7 | +import org.testng.Assert; |
| 8 | +import org.testng.internal.junit.ArrayAsserts; |
| 9 | + |
| 10 | +public class ByteBufUtilsTests { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void testByteBuf2Bytes() { |
| 14 | + ByteBuf byteBuf = Unpooled.copiedBuffer(new byte[]{'f', 'o', 'o'}); |
| 15 | + ArrayAsserts.assertArrayEquals(new byte[]{'f', 'o', 'o'}, |
| 16 | + ByteBufUtils.byteBuf2Bytes(byteBuf)); |
| 17 | + |
| 18 | + ByteBuf buf = Unpooled.buffer(); |
| 19 | + ArrayAsserts.assertArrayEquals(new byte[]{}, |
| 20 | + ByteBufUtils.byteBuf2Bytes(buf)); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testByteBuf2String1() { |
| 25 | + ByteBuf byteBuf = Unpooled.copiedBuffer(new byte[]{'f', 'o', 'o'}); |
| 26 | + Charset charset = Charset.forName("US-ASCII"); |
| 27 | + |
| 28 | + Assert.assertEquals( |
| 29 | + ByteBufUtils.byteBuf2String(charset, byteBuf), "foo"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testByteBuf2String2() { |
| 34 | + ByteBuf byteBuf1 = Unpooled.copiedBuffer(new byte[]{'f'}); |
| 35 | + ByteBuf byteBuf2 = Unpooled.copiedBuffer(new byte[]{'o', 'o'}); |
| 36 | + |
| 37 | + Assert.assertEquals(ByteBufUtils.byteBuf2String( |
| 38 | + Charset.forName("ISO-8859-1"), byteBuf1, byteBuf2), "foo"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testByteBuf2Chars1() { |
| 43 | + ByteBuf byteBuf1 = Unpooled.copiedBuffer(new byte[]{}); |
| 44 | + ByteBuf byteBuf2 = Unpooled.copiedBuffer(new byte[]{'o'}); |
| 45 | + |
| 46 | + ArrayAsserts.assertArrayEquals(new char[]{}, ByteBufUtils |
| 47 | + .byteBuf2Chars(Charset.forName("ISO-8859-1"), byteBuf1)); |
| 48 | + ArrayAsserts.assertArrayEquals(new char[]{'o'}, ByteBufUtils |
| 49 | + .byteBuf2Chars(Charset.forName("ISO-8859-1"), byteBuf2)); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testByteBuf2Chars2() { |
| 54 | + ByteBuf byteBuf1 = Unpooled.copiedBuffer(new byte[]{}); |
| 55 | + ByteBuf byteBuf2 = Unpooled.copiedBuffer(new byte[]{'o'}); |
| 56 | + |
| 57 | + ArrayAsserts.assertArrayEquals(new char[]{'o'}, ByteBufUtils |
| 58 | + .byteBuf2Chars(Charset.forName("ISO-8859-1"), |
| 59 | + byteBuf1, byteBuf2)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testByteBuf2Chars3() { |
| 64 | + ByteBuf byteBuf1 = Unpooled.copiedBuffer(new byte[]{'f', 'o'}); |
| 65 | + ByteBuf byteBuf2 = Unpooled.copiedBuffer(new byte[]{'%', '*'}); |
| 66 | + |
| 67 | + ArrayAsserts.assertArrayEquals(new char[]{'f', 'o', '%', '*'}, |
| 68 | + ByteBufUtils.byteBuf2Chars(Charset.forName("ISO-8859-1"), |
| 69 | + byteBuf1, byteBuf2)); |
| 70 | + } |
| 71 | +} |
0 commit comments