Skip to content

Commit

Permalink
Add unit tests for org.asynchttpclient.netty.util.ByteBufUtils
Browse files Browse the repository at this point in the history
These tests were written using Diffblue Cover.
  • Loading branch information
EricHetti committed Jun 11, 2019
1 parent 8b65942 commit 8b10510
Showing 1 changed file with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2019 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.netty.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.nio.charset.Charset;
import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.internal.junit.ArrayAsserts;

public class ByteBufUtilsTests {

@Test
public void testByteBuf2BytesEmptyByteBuf() {
ByteBuf buf = Unpooled.buffer();
ArrayAsserts.assertArrayEquals(new byte[]{},
ByteBufUtils.byteBuf2Bytes(buf));
buf.release();
}

@Test
public void testByteBuf2BytesNotEmptyByteBuf() {
ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[]{'f', 'o', 'o'});
ArrayAsserts.assertArrayEquals(new byte[]{'f', 'o', 'o'},
ByteBufUtils.byteBuf2Bytes(byteBuf));
byteBuf.release();
}

@Test
public void testByteBuf2String() {
ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[]{'f', 'o', 'o'});
Charset charset = Charset.forName("US-ASCII");

Assert.assertEquals(
ByteBufUtils.byteBuf2String(charset, byteBuf), "foo");
byteBuf.release();
}

@Test
public void testByteBuf2StringWithByteBufArray() {
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{'f'});
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o', 'o'});

Assert.assertEquals(ByteBufUtils.byteBuf2String(
Charset.forName("ISO-8859-1"), byteBuf1, byteBuf2), "foo");
byteBuf1.release();
byteBuf2.release();
}

@Test
public void testByteBuf2Chars() {
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{});
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o'});

ArrayAsserts.assertArrayEquals(new char[]{}, ByteBufUtils
.byteBuf2Chars(Charset.forName("ISO-8859-1"), byteBuf1));
ArrayAsserts.assertArrayEquals(new char[]{'o'}, ByteBufUtils
.byteBuf2Chars(Charset.forName("ISO-8859-1"), byteBuf2));
byteBuf1.release();
byteBuf2.release();
}

@Test
public void testByteBuf2CharsWithByteBufArray() {
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{'f', 'o'});
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'%', '*'});

ArrayAsserts.assertArrayEquals(new char[]{'f', 'o', '%', '*'},
ByteBufUtils.byteBuf2Chars(Charset.forName("ISO-8859-1"),
byteBuf1, byteBuf2));
byteBuf1.release();
byteBuf2.release();
}

@Test
public void testByteBuf2CharsWithEmptyByteBufArray() {
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{});
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o'});

ArrayAsserts.assertArrayEquals(new char[]{'o'}, ByteBufUtils
.byteBuf2Chars(Charset.forName("ISO-8859-1"),
byteBuf1, byteBuf2));
byteBuf1.release();
byteBuf2.release();
}
}

0 comments on commit 8b10510

Please sign in to comment.