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
28 package org.apache.hc.core5.util;
29
30 import java.io.ByteArrayInputStream;
31 import java.io.ByteArrayOutputStream;
32 import java.io.ObjectInputStream;
33 import java.io.ObjectOutputStream;
34 import java.nio.charset.StandardCharsets;
35
36 import org.junit.Assert;
37 import org.junit.Test;
38
39
40
41
42
43 public class TestCharArrayBuffer {
44
45 @Test
46 public void testConstructor() throws Exception {
47 final CharArrayBuffer buffer = new CharArrayBuffer(16);
48 Assert.assertEquals(16, buffer.capacity());
49 Assert.assertEquals(0, buffer.length());
50 Assert.assertNotNull(buffer.array());
51 Assert.assertEquals(16, buffer.array().length);
52 try {
53 new CharArrayBuffer(-1);
54 Assert.fail("IllegalArgumentException should have been thrown");
55 } catch (final IllegalArgumentException ex) {
56
57 }
58 }
59
60 @Test
61 public void testSimpleAppend() throws Exception {
62 final CharArrayBuffer buffer = new CharArrayBuffer(16);
63 Assert.assertEquals(16, buffer.capacity());
64 Assert.assertEquals(0, buffer.length());
65 final char[] b1 = buffer.toCharArray();
66 Assert.assertNotNull(b1);
67 Assert.assertEquals(0, b1.length);
68 Assert.assertTrue(buffer.isEmpty());
69 Assert.assertFalse(buffer.isFull());
70
71 final char[] tmp = new char[] { '1', '2', '3', '4'};
72 buffer.append(tmp, 0, tmp.length);
73 Assert.assertEquals(16, buffer.capacity());
74 Assert.assertEquals(4, buffer.length());
75 Assert.assertFalse(buffer.isEmpty());
76 Assert.assertFalse(buffer.isFull());
77
78 final char[] b2 = buffer.toCharArray();
79 Assert.assertNotNull(b2);
80 Assert.assertEquals(4, b2.length);
81 for (int i = 0; i < tmp.length; i++) {
82 Assert.assertEquals(tmp[i], b2[i]);
83 Assert.assertEquals(tmp[i], buffer.charAt(i));
84 }
85 Assert.assertEquals("1234", buffer.toString());
86
87 buffer.clear();
88 Assert.assertEquals(16, buffer.capacity());
89 Assert.assertEquals(0, buffer.length());
90 Assert.assertTrue(buffer.isEmpty());
91 Assert.assertFalse(buffer.isFull());
92 }
93
94 @Test
95 public void testExpandAppend() throws Exception {
96 final CharArrayBuffer buffer = new CharArrayBuffer(4);
97 Assert.assertEquals(4, buffer.capacity());
98
99 final char[] tmp = new char[] { '1', '2', '3', '4'};
100 buffer.append(tmp, 0, 2);
101 buffer.append(tmp, 0, 4);
102 buffer.append(tmp, 0, 0);
103
104 Assert.assertEquals(8, buffer.capacity());
105 Assert.assertEquals(6, buffer.length());
106
107 buffer.append(tmp, 0, 4);
108
109 Assert.assertEquals(16, buffer.capacity());
110 Assert.assertEquals(10, buffer.length());
111
112 Assert.assertEquals("1212341234", buffer.toString());
113 }
114
115 @Test
116 public void testAppendString() throws Exception {
117 final CharArrayBuffer buffer = new CharArrayBuffer(8);
118 buffer.append("stuff");
119 buffer.append(" and more stuff");
120 Assert.assertEquals("stuff and more stuff", buffer.toString());
121 }
122
123 @Test
124 public void testAppendNullString() throws Exception {
125 final CharArrayBuffer buffer = new CharArrayBuffer(8);
126 buffer.append((String)null);
127 Assert.assertEquals("null", buffer.toString());
128 }
129
130 @Test
131 public void testAppendCharArrayBuffer() throws Exception {
132 final CharArrayBuffer buffer1 = new CharArrayBuffer(8);
133 buffer1.append(" and more stuff");
134 final CharArrayBuffer buffer2 = new CharArrayBuffer(8);
135 buffer2.append("stuff");
136 buffer2.append(buffer1);
137 Assert.assertEquals("stuff and more stuff", buffer2.toString());
138 }
139
140 @Test
141 public void testAppendNullCharArrayBuffer() throws Exception {
142 final CharArrayBuffer buffer = new CharArrayBuffer(8);
143 buffer.append((CharArrayBuffer)null);
144 buffer.append((CharArrayBuffer)null, 0, 0);
145 Assert.assertEquals("", buffer.toString());
146 }
147
148 @Test
149 public void testAppendSingleChar() throws Exception {
150 final CharArrayBuffer buffer = new CharArrayBuffer(4);
151 buffer.append('1');
152 buffer.append('2');
153 buffer.append('3');
154 buffer.append('4');
155 buffer.append('5');
156 buffer.append('6');
157 Assert.assertEquals("123456", buffer.toString());
158 }
159
160 @Test
161 public void testInvalidCharArrayAppend() throws Exception {
162 final CharArrayBuffer buffer = new CharArrayBuffer(4);
163 buffer.append((char[])null, 0, 0);
164
165 final char[] tmp = new char[] { '1', '2', '3', '4'};
166 try {
167 buffer.append(tmp, -1, 0);
168 Assert.fail("IndexOutOfBoundsException should have been thrown");
169 } catch (final IndexOutOfBoundsException ex) {
170
171 }
172 try {
173 buffer.append(tmp, 0, -1);
174 Assert.fail("IndexOutOfBoundsException should have been thrown");
175 } catch (final IndexOutOfBoundsException ex) {
176
177 }
178 try {
179 buffer.append(tmp, 0, 8);
180 Assert.fail("IndexOutOfBoundsException should have been thrown");
181 } catch (final IndexOutOfBoundsException ex) {
182
183 }
184 try {
185 buffer.append(tmp, 10, Integer.MAX_VALUE);
186 Assert.fail("IndexOutOfBoundsException should have been thrown");
187 } catch (final IndexOutOfBoundsException ex) {
188
189 }
190 try {
191 buffer.append(tmp, 2, 4);
192 Assert.fail("IndexOutOfBoundsException should have been thrown");
193 } catch (final IndexOutOfBoundsException ex) {
194
195 }
196 }
197
198 @Test
199 public void testSetLength() throws Exception {
200 final CharArrayBuffer buffer = new CharArrayBuffer(4);
201 buffer.setLength(2);
202 Assert.assertEquals(2, buffer.length());
203 }
204
205 @Test
206 public void testSetInvalidLength() throws Exception {
207 final CharArrayBuffer buffer = new CharArrayBuffer(4);
208 try {
209 buffer.setLength(-2);
210 Assert.fail("IndexOutOfBoundsException should have been thrown");
211 } catch (final IndexOutOfBoundsException ex) {
212
213 }
214 try {
215 buffer.setLength(200);
216 Assert.fail("IndexOutOfBoundsException should have been thrown");
217 } catch (final IndexOutOfBoundsException ex) {
218
219 }
220 }
221
222 @Test
223 public void testEnsureCapacity() throws Exception {
224 final CharArrayBuffer buffer = new CharArrayBuffer(4);
225 buffer.ensureCapacity(2);
226 Assert.assertEquals(4, buffer.capacity());
227 buffer.ensureCapacity(8);
228 Assert.assertEquals(8, buffer.capacity());
229 }
230
231 @Test
232 public void testIndexOf() {
233 final CharArrayBuffer buffer = new CharArrayBuffer(16);
234 buffer.append("name: value");
235 Assert.assertEquals(4, buffer.indexOf(':'));
236 Assert.assertEquals(-1, buffer.indexOf(','));
237 Assert.assertEquals(4, buffer.indexOf(':', -1, 11));
238 Assert.assertEquals(4, buffer.indexOf(':', 0, 1000));
239 Assert.assertEquals(-1, buffer.indexOf(':', 2, 1));
240 }
241
242 @Test
243 public void testSubstring() {
244 final CharArrayBuffer buffer = new CharArrayBuffer(16);
245 buffer.append(" name: value ");
246 Assert.assertEquals(5, buffer.indexOf(':'));
247 Assert.assertEquals(" name", buffer.substring(0, 5));
248 Assert.assertEquals(" value ", buffer.substring(6, buffer.length()));
249 Assert.assertEquals("name", buffer.substringTrimmed(0, 5));
250 Assert.assertEquals("value", buffer.substringTrimmed(6, buffer.length()));
251 Assert.assertEquals("", buffer.substringTrimmed(13, buffer.length()));
252 }
253
254 @Test
255 public void testSubstringIndexOfOutBound() {
256 final CharArrayBuffer buffer = new CharArrayBuffer(16);
257 buffer.append("stuff");
258 try {
259 buffer.substring(-2, 10);
260 Assert.fail("IndexOutOfBoundsException should have been thrown");
261 } catch (final IndexOutOfBoundsException ex) {
262
263 }
264 try {
265 buffer.substringTrimmed(-2, 10);
266 Assert.fail("IndexOutOfBoundsException should have been thrown");
267 } catch (final IndexOutOfBoundsException ex) {
268
269 }
270 try {
271 buffer.substring(12, 10);
272 Assert.fail("IndexOutOfBoundsException should have been thrown");
273 } catch (final IndexOutOfBoundsException ex) {
274
275 }
276 try {
277 buffer.substringTrimmed(12, 10);
278 Assert.fail("IndexOutOfBoundsException should have been thrown");
279 } catch (final IndexOutOfBoundsException ex) {
280
281 }
282 try {
283 buffer.substring(2, 1);
284 Assert.fail("IndexOutOfBoundsException should have been thrown");
285 } catch (final IndexOutOfBoundsException ex) {
286
287 }
288 try {
289 buffer.substringTrimmed(2, 1);
290 Assert.fail("IndexOutOfBoundsException should have been thrown");
291 } catch (final IndexOutOfBoundsException ex) {
292
293 }
294 }
295
296 @Test
297 public void testAppendAsciiByteArray() throws Exception {
298 final String s1 = "stuff";
299 final String s2 = " and more stuff";
300 final byte[] b1 = s1.getBytes(StandardCharsets.US_ASCII);
301 final byte[] b2 = s2.getBytes(StandardCharsets.US_ASCII);
302
303 final CharArrayBuffer buffer = new CharArrayBuffer(8);
304 buffer.append(b1, 0, b1.length);
305 buffer.append(b2, 0, b2.length);
306
307 Assert.assertEquals("stuff and more stuff", buffer.toString());
308 }
309
310 @Test
311 public void testAppendISOByteArray() throws Exception {
312 final byte[] b = new byte[] {0x00, 0x20, 0x7F, -0x80, -0x01};
313
314 final CharArrayBuffer buffer = new CharArrayBuffer(8);
315 buffer.append(b, 0, b.length);
316 final char[] ch = buffer.toCharArray();
317 Assert.assertNotNull(ch);
318 Assert.assertEquals(5, ch.length);
319 Assert.assertEquals(0x00, ch[0]);
320 Assert.assertEquals(0x20, ch[1]);
321 Assert.assertEquals(0x7F, ch[2]);
322 Assert.assertEquals(0x80, ch[3]);
323 Assert.assertEquals(0xFF, ch[4]);
324 }
325
326 @Test
327 public void testAppendNullByteArray() throws Exception {
328 final CharArrayBuffer buffer = new CharArrayBuffer(8);
329 buffer.append((byte[])null, 0, 0);
330 Assert.assertEquals("", buffer.toString());
331 }
332
333 @Test
334 public void testAppendNullByteArrayBuffer() throws Exception {
335 final CharArrayBuffer buffer = new CharArrayBuffer(8);
336 buffer.append((ByteArrayBuffer)null, 0, 0);
337 Assert.assertEquals("", buffer.toString());
338 }
339
340 @Test
341 public void testInvalidAppendAsciiByteArray() throws Exception {
342 final CharArrayBuffer buffer = new CharArrayBuffer(4);
343 buffer.append((byte[])null, 0, 0);
344
345 final byte[] tmp = new byte[] { '1', '2', '3', '4'};
346 try {
347 buffer.append(tmp, -1, 0);
348 Assert.fail("IndexOutOfBoundsException should have been thrown");
349 } catch (final IndexOutOfBoundsException ex) {
350
351 }
352 try {
353 buffer.append(tmp, 0, -1);
354 Assert.fail("IndexOutOfBoundsException should have been thrown");
355 } catch (final IndexOutOfBoundsException ex) {
356
357 }
358 try {
359 buffer.append(tmp, 0, 8);
360 Assert.fail("IndexOutOfBoundsException should have been thrown");
361 } catch (final IndexOutOfBoundsException ex) {
362
363 }
364 try {
365 buffer.append(tmp, 10, Integer.MAX_VALUE);
366 Assert.fail("IndexOutOfBoundsException should have been thrown");
367 } catch (final IndexOutOfBoundsException ex) {
368
369 }
370 try {
371 buffer.append(tmp, 2, 4);
372 Assert.fail("IndexOutOfBoundsException should have been thrown");
373 } catch (final IndexOutOfBoundsException ex) {
374
375 }
376 }
377
378 @Test
379 public void testSerialization() throws Exception {
380 final CharArrayBuffer orig = new CharArrayBuffer(32);
381 orig.append('a');
382 orig.append('b');
383 orig.append('c');
384 final ByteArrayOutputStream outbuffer = new ByteArrayOutputStream();
385 try (final ObjectOutputStream outStream = new ObjectOutputStream(outbuffer)) {
386 outStream.writeObject(orig);
387 }
388 final byte[] raw = outbuffer.toByteArray();
389 final ByteArrayInputStream inBuffer = new ByteArrayInputStream(raw);
390 final ObjectInputStream inStream = new ObjectInputStream(inBuffer);
391 final CharArrayBuffer clone = (CharArrayBuffer) inStream.readObject();
392 Assert.assertEquals(orig.capacity(), clone.capacity());
393 Assert.assertEquals(orig.length(), clone.length());
394 final char[] data = clone.toCharArray();
395 Assert.assertNotNull(data);
396 Assert.assertEquals(3, data.length);
397 Assert.assertEquals('a', data[0]);
398 Assert.assertEquals('b', data[1]);
399 Assert.assertEquals('c', data[2]);
400 }
401
402 @Test
403 public void testSubSequence() {
404 final CharArrayBuffer buffer = new CharArrayBuffer(16);
405 buffer.append(" name: value ");
406 Assert.assertEquals(5, buffer.indexOf(':'));
407 Assert.assertEquals(" name", buffer.subSequence(0, 5).toString());
408 Assert.assertEquals(" value ",
409 buffer.subSequence(6, buffer.length()).toString());
410 }
411
412 @Test
413 public void testSubSequenceIndexOfOutBound() {
414 final CharArrayBuffer buffer = new CharArrayBuffer(16);
415 buffer.append("stuff");
416 try {
417 buffer.subSequence(-2, 10);
418 Assert.fail("IndexOutOfBoundsException should have been thrown");
419 } catch (final IndexOutOfBoundsException ex) {
420
421 }
422 try {
423 buffer.subSequence(12, 10);
424 Assert.fail("IndexOutOfBoundsException should have been thrown");
425 } catch (final IndexOutOfBoundsException ex) {
426
427 }
428 try {
429 buffer.subSequence(2, 1);
430 Assert.fail("IndexOutOfBoundsException should have been thrown");
431 } catch (final IndexOutOfBoundsException ex) {
432
433 }
434 }
435
436 }