1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package org.apache.hc.client5.http.utils;
28
29 import java.nio.ByteBuffer;
30 import java.nio.charset.StandardCharsets;
31
32 import org.junit.Assert;
33 import org.junit.Test;
34
35
36
37
38 public class TestByteArrayBuilder {
39
40 @Test
41 public void testEmptyBuffer() throws Exception {
42 final ByteArrayBuilder buffer = new ByteArrayBuilder();
43 final ByteBuffer byteBuffer = buffer.toByteBuffer();
44 Assert.assertNotNull(byteBuffer);
45 Assert.assertEquals(0, byteBuffer.capacity());
46
47 final byte[] bytes = buffer.toByteArray();
48 Assert.assertNotNull(bytes);
49 Assert.assertEquals(0, bytes.length);
50 }
51
52 @Test
53 public void testAppendBytes() throws Exception {
54 final ByteArrayBuilder buffer = new ByteArrayBuilder();
55 buffer.append(new byte[]{1, 2, 3, 4, 5});
56 buffer.append(new byte[]{3, 4, 5, 6, 7, 8, 9, 10, 11}, 3, 5);
57 buffer.append((byte[]) null);
58
59 final byte[] bytes = buffer.toByteArray();
60 Assert.assertNotNull(bytes);
61 Assert.assertArrayEquals(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, bytes);
62 }
63
64 @Test
65 public void testInvalidAppendBytes() throws Exception {
66 final ByteArrayBuilder buffer = new ByteArrayBuilder();
67 buffer.append((byte[])null, 0, 0);
68
69 final byte[] tmp = new byte[] { 1, 2, 3, 4};
70 try {
71 buffer.append(tmp, -1, 0);
72 Assert.fail("IndexOutOfBoundsException should have been thrown");
73 } catch (final IndexOutOfBoundsException ex) {
74
75 }
76 try {
77 buffer.append(tmp, 0, -1);
78 Assert.fail("IndexOutOfBoundsException should have been thrown");
79 } catch (final IndexOutOfBoundsException ex) {
80
81 }
82 try {
83 buffer.append(tmp, 0, 8);
84 Assert.fail("IndexOutOfBoundsException should have been thrown");
85 } catch (final IndexOutOfBoundsException ex) {
86
87 }
88 try {
89 buffer.append(tmp, 10, Integer.MAX_VALUE);
90 Assert.fail("IndexOutOfBoundsException should have been thrown");
91 } catch (final IndexOutOfBoundsException ex) {
92
93 }
94 try {
95 buffer.append(tmp, 2, 4);
96 Assert.fail("IndexOutOfBoundsException should have been thrown");
97 } catch (final IndexOutOfBoundsException ex) {
98
99 }
100 }
101
102 @Test
103 public void testEnsureCapacity() throws Exception {
104 final ByteArrayBuilder buffer = new ByteArrayBuilder();
105 buffer.ensureFreeCapacity(10);
106 Assert.assertEquals(10, buffer.capacity());
107 buffer.ensureFreeCapacity(5);
108 Assert.assertEquals(10, buffer.capacity());
109 buffer.append(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
110 buffer.ensureFreeCapacity(5);
111 Assert.assertEquals(13, buffer.capacity());
112 buffer.ensureFreeCapacity(15);
113 Assert.assertEquals(23, buffer.capacity());
114 }
115
116 @Test
117 public void testAppendText() throws Exception {
118 final ByteArrayBuilder buffer = new ByteArrayBuilder();
119 buffer.append(new char[]{'1', '2', '3', '4', '5'});
120 buffer.append(new char[]{'3', '4', '5', '6', '7', '8', '9', 'a', 'b'}, 3, 5);
121 buffer.append("bcd");
122 buffer.append("e");
123 buffer.append("f");
124 buffer.append("");
125 buffer.append((String) null);
126 buffer.append((char[]) null);
127
128 final byte[] bytes = buffer.toByteArray();
129 Assert.assertNotNull(bytes);
130 Assert.assertEquals("123456789abcdef", new String(bytes, StandardCharsets.US_ASCII));
131 }
132
133 @Test
134 public void testInvalidAppendChars() throws Exception {
135 final ByteArrayBuilder buffer = new ByteArrayBuilder();
136 buffer.append((char[])null, 0, 0);
137
138 final char[] tmp = new char[] { 1, 2, 3, 4};
139 try {
140 buffer.append(tmp, -1, 0);
141 Assert.fail("IndexOutOfBoundsException should have been thrown");
142 } catch (final IndexOutOfBoundsException ex) {
143
144 }
145 try {
146 buffer.append(tmp, 0, -1);
147 Assert.fail("IndexOutOfBoundsException should have been thrown");
148 } catch (final IndexOutOfBoundsException ex) {
149
150 }
151 try {
152 buffer.append(tmp, 0, 8);
153 Assert.fail("IndexOutOfBoundsException should have been thrown");
154 } catch (final IndexOutOfBoundsException ex) {
155
156 }
157 try {
158 buffer.append(tmp, 10, Integer.MAX_VALUE);
159 Assert.fail("IndexOutOfBoundsException should have been thrown");
160 } catch (final IndexOutOfBoundsException ex) {
161
162 }
163 try {
164 buffer.append(tmp, 2, 4);
165 Assert.fail("IndexOutOfBoundsException should have been thrown");
166 } catch (final IndexOutOfBoundsException ex) {
167
168 }
169 }
170
171 @Test
172 public void testReset() throws Exception {
173 final ByteArrayBuilder buffer = new ByteArrayBuilder();
174 buffer.append("abcd");
175 buffer.append("e");
176 buffer.append("f");
177
178 final byte[] bytes1 = buffer.toByteArray();
179 Assert.assertNotNull(bytes1);
180 Assert.assertEquals("abcdef", new String(bytes1, StandardCharsets.US_ASCII));
181
182 buffer.reset();
183
184 final byte[] bytes2 = buffer.toByteArray();
185 Assert.assertNotNull(bytes2);
186 Assert.assertEquals("", new String(bytes2, StandardCharsets.US_ASCII));
187 }
188
189 @Test
190 public void testNonAsciiCharset() throws Exception {
191 final int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc };
192 final StringBuilder tmp = new StringBuilder();
193 for (final int germanChar : germanChars) {
194 tmp.append((char) germanChar);
195 }
196 final String umlauts = tmp.toString();
197
198
199 final ByteArrayBuilder buffer = new ByteArrayBuilder();
200 buffer.append(umlauts);
201
202 final byte[] bytes1 = buffer.toByteArray();
203 Assert.assertNotNull(bytes1);
204 Assert.assertEquals("?-?-?", new String(bytes1, StandardCharsets.US_ASCII));
205
206 buffer.reset();
207 buffer.charset(StandardCharsets.UTF_8);
208 buffer.append(umlauts);
209
210 final byte[] bytes2 = buffer.toByteArray();
211 Assert.assertNotNull(bytes2);
212 Assert.assertEquals(umlauts, new String(bytes2, StandardCharsets.UTF_8));
213 }
214
215 }