-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for org.asynchttpclient.netty.util.ByteBufUtils
These tests were written using Diffblue Cover.
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
netty-utils/src/test/java/org/asynchttpclient/netty/util/ByteBufUtilsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |