Skip to content

Commit febe50c

Browse files
EricHettislandelle
authored andcommitted
Add unit tests for org.asynchttpclient.netty.util.ByteBufUtils (AsyncHttpClient#1639)
These tests were written using Diffblue Cover.
1 parent c426c54 commit febe50c

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2019 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package org.asynchttpclient.netty.util;
15+
16+
import io.netty.buffer.ByteBuf;
17+
import io.netty.buffer.Unpooled;
18+
import java.nio.charset.Charset;
19+
import org.testng.annotations.Test;
20+
import org.testng.Assert;
21+
import org.testng.internal.junit.ArrayAsserts;
22+
23+
public class ByteBufUtilsTests {
24+
25+
@Test
26+
public void testByteBuf2BytesEmptyByteBuf() {
27+
ByteBuf buf = Unpooled.buffer();
28+
29+
try {
30+
ArrayAsserts.assertArrayEquals(new byte[]{},
31+
ByteBufUtils.byteBuf2Bytes(buf));
32+
} finally {
33+
buf.release();
34+
}
35+
}
36+
37+
@Test
38+
public void testByteBuf2BytesNotEmptyByteBuf() {
39+
ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[]{'f', 'o', 'o'});
40+
41+
try {
42+
ArrayAsserts.assertArrayEquals(new byte[]{'f', 'o', 'o'},
43+
ByteBufUtils.byteBuf2Bytes(byteBuf));
44+
} finally {
45+
byteBuf.release();
46+
}
47+
}
48+
49+
@Test
50+
public void testByteBuf2String() {
51+
ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[]{'f', 'o', 'o'});
52+
Charset charset = Charset.forName("US-ASCII");
53+
54+
try {
55+
Assert.assertEquals(
56+
ByteBufUtils.byteBuf2String(charset, byteBuf), "foo");
57+
} finally {
58+
byteBuf.release();
59+
}
60+
}
61+
62+
@Test
63+
public void testByteBuf2StringWithByteBufArray() {
64+
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{'f'});
65+
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o', 'o'});
66+
67+
try {
68+
Assert.assertEquals(ByteBufUtils.byteBuf2String(
69+
Charset.forName("ISO-8859-1"), byteBuf1, byteBuf2), "foo");
70+
} finally {
71+
byteBuf1.release();
72+
byteBuf2.release();
73+
}
74+
}
75+
76+
@Test
77+
public void testByteBuf2Chars() {
78+
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{});
79+
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o'});
80+
81+
try {
82+
ArrayAsserts.assertArrayEquals(new char[]{}, ByteBufUtils
83+
.byteBuf2Chars(Charset.forName("US-ASCII"), byteBuf1));
84+
ArrayAsserts.assertArrayEquals(new char[]{}, ByteBufUtils
85+
.byteBuf2Chars(Charset.forName("ISO-8859-1"), byteBuf1));
86+
ArrayAsserts.assertArrayEquals(new char[]{'o'}, ByteBufUtils
87+
.byteBuf2Chars(Charset.forName("ISO-8859-1"), byteBuf2));
88+
} finally {
89+
byteBuf1.release();
90+
byteBuf2.release();
91+
}
92+
}
93+
94+
@Test
95+
public void testByteBuf2CharsWithByteBufArray() {
96+
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{'f', 'o'});
97+
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'%', '*'});
98+
99+
try {
100+
ArrayAsserts.assertArrayEquals(new char[]{'f', 'o', '%', '*'},
101+
ByteBufUtils.byteBuf2Chars(Charset.forName("US-ASCII"),
102+
byteBuf1, byteBuf2));
103+
ArrayAsserts.assertArrayEquals(new char[]{'f', 'o', '%', '*'},
104+
ByteBufUtils.byteBuf2Chars(Charset.forName("ISO-8859-1"),
105+
byteBuf1, byteBuf2));
106+
} finally {
107+
byteBuf1.release();
108+
byteBuf2.release();
109+
}
110+
}
111+
112+
@Test
113+
public void testByteBuf2CharsWithEmptyByteBufArray() {
114+
ByteBuf byteBuf1 = Unpooled.wrappedBuffer(new byte[]{});
115+
ByteBuf byteBuf2 = Unpooled.wrappedBuffer(new byte[]{'o'});
116+
117+
try {
118+
ArrayAsserts.assertArrayEquals(new char[]{'o'}, ByteBufUtils
119+
.byteBuf2Chars(Charset.forName("ISO-8859-1"),
120+
byteBuf1, byteBuf2));
121+
} finally {
122+
byteBuf1.release();
123+
byteBuf2.release();
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)