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.jupiter.api.Assertions;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 class TestByteArrayBuilder {
39
40 @Test
41 void testEmptyBuffer() {
42 final ByteArrayBuilder buffer = new ByteArrayBuilder();
43 final ByteBuffer byteBuffer = buffer.toByteBuffer();
44 Assertions.assertNotNull(byteBuffer);
45 Assertions.assertEquals(0, byteBuffer.capacity());
46
47 final byte[] bytes = buffer.toByteArray();
48 Assertions.assertNotNull(bytes);
49 Assertions.assertEquals(0, bytes.length);
50 }
51
52 @Test
53 void testAppendBytes() {
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 Assertions.assertNotNull(bytes);
61 Assertions.assertArrayEquals(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, bytes);
62 }
63
64 @Test
65 void testInvalidAppendBytes() {
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 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, -1, 0));
71 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, -1));
72 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, 8));
73 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 10, Integer.MAX_VALUE));
74 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 2, 4));
75 }
76
77 @Test
78 void testEnsureCapacity() {
79 final ByteArrayBuilder buffer = new ByteArrayBuilder();
80 buffer.ensureFreeCapacity(10);
81 Assertions.assertEquals(10, buffer.capacity());
82 buffer.ensureFreeCapacity(5);
83 Assertions.assertEquals(10, buffer.capacity());
84 buffer.append(new byte[]{1, 2, 3, 4, 5, 6, 7, 8});
85 buffer.ensureFreeCapacity(5);
86 Assertions.assertEquals(13, buffer.capacity());
87 buffer.ensureFreeCapacity(15);
88 Assertions.assertEquals(23, buffer.capacity());
89 }
90
91 @Test
92 void testAppendText() {
93 final ByteArrayBuilder buffer = new ByteArrayBuilder();
94 buffer.append(new char[]{'1', '2', '3', '4', '5'});
95 buffer.append(new char[]{'3', '4', '5', '6', '7', '8', '9', 'a', 'b'}, 3, 5);
96 buffer.append("bcd");
97 buffer.append("e");
98 buffer.append("f");
99 buffer.append("");
100 buffer.append((String) null);
101 buffer.append((char[]) null);
102
103 final byte[] bytes = buffer.toByteArray();
104 Assertions.assertNotNull(bytes);
105 Assertions.assertEquals("123456789abcdef", new String(bytes, StandardCharsets.US_ASCII));
106 }
107
108 @Test
109 void testInvalidAppendChars() {
110 final ByteArrayBuilder buffer = new ByteArrayBuilder();
111 buffer.append((char[])null, 0, 0);
112
113 final char[] tmp = new char[] { 1, 2, 3, 4};
114 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, -1, 0));
115 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, -1));
116 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, 8));
117 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 10, Integer.MAX_VALUE));
118 Assertions.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 2, 4));
119 }
120
121 @Test
122 void testReset() {
123 final ByteArrayBuilder buffer = new ByteArrayBuilder();
124 buffer.append("abcd");
125 buffer.append("e");
126 buffer.append("f");
127
128 final byte[] bytes1 = buffer.toByteArray();
129 Assertions.assertNotNull(bytes1);
130 Assertions.assertEquals("abcdef", new String(bytes1, StandardCharsets.US_ASCII));
131
132 buffer.reset();
133
134 final byte[] bytes2 = buffer.toByteArray();
135 Assertions.assertNotNull(bytes2);
136 Assertions.assertEquals("", new String(bytes2, StandardCharsets.US_ASCII));
137 }
138
139 @Test
140 void testNonAsciiCharset() {
141 final int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc };
142 final StringBuilder tmp = new StringBuilder();
143 for (final int germanChar : germanChars) {
144 tmp.append((char) germanChar);
145 }
146 final String umlauts = tmp.toString();
147
148
149 final ByteArrayBuilder buffer = new ByteArrayBuilder();
150 buffer.append(umlauts);
151
152 final byte[] bytes1 = buffer.toByteArray();
153 Assertions.assertNotNull(bytes1);
154 Assertions.assertEquals("?-?-?", new String(bytes1, StandardCharsets.US_ASCII));
155
156 buffer.reset();
157 buffer.charset(StandardCharsets.UTF_8);
158 buffer.append(umlauts);
159
160 final byte[] bytes2 = buffer.toByteArray();
161 Assertions.assertNotNull(bytes2);
162 Assertions.assertEquals(umlauts, new String(bytes2, StandardCharsets.UTF_8));
163 }
164
165 }