-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add unit tests for org.asynchttpclient.netty.util.ByteBufUtils #1639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
slandelle
merged 1 commit into
AsyncHttpClient:master
from
Diffblue-benchmarks:add-diffblue-tests
Jun 25, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
netty-utils/src/test/java/org/asynchttpclient/netty/util/ByteBufUtilsTests.java
This file contains hidden or 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,126 @@ | ||
/* | ||
* 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(); | ||
|
||
try { | ||
ArrayAsserts.assertArrayEquals(new byte[]{}, | ||
ByteBufUtils.byteBuf2Bytes(buf)); | ||
} finally { | ||
buf.release(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testByteBuf2BytesNotEmptyByteBuf() { | ||
ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[]{'f', 'o', 'o'}); | ||
|
||
try { | ||
ArrayAsserts.assertArrayEquals(new byte[]{'f', 'o', 'o'}, | ||
ByteBufUtils.byteBuf2Bytes(byteBuf)); | ||
} finally { | ||
byteBuf.release(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testByteBuf2String() { | ||
ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[]{'f', 'o', 'o'}); | ||
Charset charset = Charset.forName("US-ASCII"); | ||
|
||
try { | ||
Assert.assertEquals( | ||
ByteBufUtils.byteBuf2String(charset, byteBuf), "foo"); | ||
} finally { | ||
byteBuf.release(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testByteBuf2StringWithByteBufArray() { | ||
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{'f'}); | ||
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o', 'o'}); | ||
|
||
try { | ||
Assert.assertEquals(ByteBufUtils.byteBuf2String( | ||
Charset.forName("ISO-8859-1"), byteBuf1, byteBuf2), "foo"); | ||
} finally { | ||
byteBuf1.release(); | ||
byteBuf2.release(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testByteBuf2Chars() { | ||
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{}); | ||
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o'}); | ||
|
||
try { | ||
ArrayAsserts.assertArrayEquals(new char[]{}, ByteBufUtils | ||
.byteBuf2Chars(Charset.forName("US-ASCII"), byteBuf1)); | ||
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)); | ||
} finally { | ||
byteBuf1.release(); | ||
byteBuf2.release(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testByteBuf2CharsWithByteBufArray() { | ||
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{'f', 'o'}); | ||
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'%', '*'}); | ||
|
||
try { | ||
ArrayAsserts.assertArrayEquals(new char[]{'f', 'o', '%', '*'}, | ||
ByteBufUtils.byteBuf2Chars(Charset.forName("US-ASCII"), | ||
byteBuf1, byteBuf2)); | ||
ArrayAsserts.assertArrayEquals(new char[]{'f', 'o', '%', '*'}, | ||
ByteBufUtils.byteBuf2Chars(Charset.forName("ISO-8859-1"), | ||
byteBuf1, byteBuf2)); | ||
} finally { | ||
byteBuf1.release(); | ||
byteBuf2.release(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testByteBuf2CharsWithEmptyByteBufArray() { | ||
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{}); | ||
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o'}); | ||
|
||
try { | ||
ArrayAsserts.assertArrayEquals(new char[]{'o'}, ByteBufUtils | ||
.byteBuf2Chars(Charset.forName("ISO-8859-1"), | ||
byteBuf1, byteBuf2)); | ||
} finally { | ||
byteBuf1.release(); | ||
byteBuf2.release(); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.