2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/test/java/org/apache/http/util/TestCharArrayBuffer.java $
4
* $Date: 2009-02-14 17:39:33 +0100 (Sat, 14 Feb 2009) $
6
* ====================================================================
7
* Licensed to the Apache Software Foundation (ASF) under one
8
* or more contributor license agreements. See the NOTICE file
9
* distributed with this work for additional information
10
* regarding copyright ownership. The ASF licenses this file
11
* to you under the Apache License, Version 2.0 (the
12
* "License"); you may not use this file except in compliance
13
* with the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing,
18
* software distributed under the License is distributed on an
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
* KIND, either express or implied. See the License for the
21
* specific language governing permissions and limitations
23
* ====================================================================
25
* This software consists of voluntary contributions made by many
26
* individuals on behalf of the Apache Software Foundation. For more
27
* information on the Apache Software Foundation, please see
28
* <http://www.apache.org/>.
32
package org.apache.http.util;
34
import org.apache.http.util.ByteArrayBuffer;
35
import org.apache.http.util.CharArrayBuffer;
37
import junit.framework.Test;
38
import junit.framework.TestCase;
39
import junit.framework.TestSuite;
42
* Unit tests for {@link CharArrayBuffer}.
45
public class TestCharArrayBuffer extends TestCase {
47
public TestCharArrayBuffer(String testName) {
51
public static void main(String args[]) {
52
String[] testCaseName = { TestCharArrayBuffer.class.getName() };
53
junit.textui.TestRunner.main(testCaseName);
56
public static Test suite() {
57
return new TestSuite(TestCharArrayBuffer.class);
60
public void testConstructor() throws Exception {
61
CharArrayBuffer buffer = new CharArrayBuffer(16);
62
assertEquals(16, buffer.capacity());
63
assertEquals(0, buffer.length());
64
assertNotNull(buffer.buffer());
65
assertEquals(16, buffer.buffer().length);
67
new CharArrayBuffer(-1);
68
fail("IllegalArgumentException should have been thrown");
69
} catch (IllegalArgumentException ex) {
74
public void testSimpleAppend() throws Exception {
75
CharArrayBuffer buffer = new CharArrayBuffer(16);
76
assertEquals(16, buffer.capacity());
77
assertEquals(0, buffer.length());
78
char[] b1 = buffer.toCharArray();
80
assertEquals(0, b1.length);
81
assertTrue(buffer.isEmpty());
82
assertFalse(buffer.isFull());
84
char[] tmp = new char[] { '1', '2', '3', '4'};
85
buffer.append(tmp, 0, tmp.length);
86
assertEquals(16, buffer.capacity());
87
assertEquals(4, buffer.length());
88
assertFalse(buffer.isEmpty());
89
assertFalse(buffer.isFull());
91
char[] b2 = buffer.toCharArray();
93
assertEquals(4, b2.length);
94
for (int i = 0; i < tmp.length; i++) {
95
assertEquals(tmp[i], b2[i]);
96
assertEquals(tmp[i], buffer.charAt(i));
98
assertEquals("1234", buffer.toString());
101
assertEquals(16, buffer.capacity());
102
assertEquals(0, buffer.length());
103
assertTrue(buffer.isEmpty());
104
assertFalse(buffer.isFull());
107
public void testExpandAppend() throws Exception {
108
CharArrayBuffer buffer = new CharArrayBuffer(4);
109
assertEquals(4, buffer.capacity());
111
char[] tmp = new char[] { '1', '2', '3', '4'};
112
buffer.append(tmp, 0, 2);
113
buffer.append(tmp, 0, 4);
114
buffer.append(tmp, 0, 0);
116
assertEquals(8, buffer.capacity());
117
assertEquals(6, buffer.length());
119
buffer.append(tmp, 0, 4);
121
assertEquals(16, buffer.capacity());
122
assertEquals(10, buffer.length());
124
assertEquals("1212341234", buffer.toString());
127
public void testAppendString() throws Exception {
128
CharArrayBuffer buffer = new CharArrayBuffer(8);
129
buffer.append("stuff");
130
buffer.append(" and more stuff");
131
assertEquals("stuff and more stuff", buffer.toString());
134
public void testAppendNullString() throws Exception {
135
CharArrayBuffer buffer = new CharArrayBuffer(8);
136
buffer.append((String)null);
137
assertEquals("null", buffer.toString());
140
public void testAppendCharArrayBuffer() throws Exception {
141
CharArrayBuffer buffer1 = new CharArrayBuffer(8);
142
buffer1.append(" and more stuff");
143
CharArrayBuffer buffer2 = new CharArrayBuffer(8);
144
buffer2.append("stuff");
145
buffer2.append(buffer1);
146
assertEquals("stuff and more stuff", buffer2.toString());
149
public void testAppendNullCharArrayBuffer() throws Exception {
150
CharArrayBuffer buffer = new CharArrayBuffer(8);
151
buffer.append((CharArrayBuffer)null);
152
buffer.append((CharArrayBuffer)null, 0, 0);
153
assertEquals("", buffer.toString());
156
public void testAppendSingleChar() throws Exception {
157
CharArrayBuffer buffer = new CharArrayBuffer(4);
164
assertEquals("123456", buffer.toString());
167
public void testInvalidCharArrayAppend() throws Exception {
168
CharArrayBuffer buffer = new CharArrayBuffer(4);
169
buffer.append((char[])null, 0, 0);
171
char[] tmp = new char[] { '1', '2', '3', '4'};
173
buffer.append(tmp, -1, 0);
174
fail("IndexOutOfBoundsException should have been thrown");
175
} catch (IndexOutOfBoundsException ex) {
179
buffer.append(tmp, 0, -1);
180
fail("IndexOutOfBoundsException should have been thrown");
181
} catch (IndexOutOfBoundsException ex) {
185
buffer.append(tmp, 0, 8);
186
fail("IndexOutOfBoundsException should have been thrown");
187
} catch (IndexOutOfBoundsException ex) {
191
buffer.append(tmp, 10, Integer.MAX_VALUE);
192
fail("IndexOutOfBoundsException should have been thrown");
193
} catch (IndexOutOfBoundsException ex) {
197
buffer.append(tmp, 2, 4);
198
fail("IndexOutOfBoundsException should have been thrown");
199
} catch (IndexOutOfBoundsException ex) {
204
public void testSetLength() throws Exception {
205
CharArrayBuffer buffer = new CharArrayBuffer(4);
207
assertEquals(2, buffer.length());
210
public void testSetInvalidLength() throws Exception {
211
CharArrayBuffer buffer = new CharArrayBuffer(4);
213
buffer.setLength(-2);
214
fail("IndexOutOfBoundsException should have been thrown");
215
} catch (IndexOutOfBoundsException ex) {
219
buffer.setLength(200);
220
fail("IndexOutOfBoundsException should have been thrown");
221
} catch (IndexOutOfBoundsException ex) {
226
public void testEnsureCapacity() throws Exception {
227
CharArrayBuffer buffer = new CharArrayBuffer(4);
228
buffer.ensureCapacity(2);
229
assertEquals(4, buffer.capacity());
230
buffer.ensureCapacity(8);
231
assertEquals(8, buffer.capacity());
234
public void testIndexOf() {
235
CharArrayBuffer buffer = new CharArrayBuffer(16);
236
buffer.append("name: value");
237
assertEquals(4, buffer.indexOf(':'));
238
assertEquals(-1, buffer.indexOf(','));
239
assertEquals(4, buffer.indexOf(':', -1, 11));
240
assertEquals(4, buffer.indexOf(':', 0, 1000));
241
assertEquals(-1, buffer.indexOf(':', 2, 1));
244
public void testSubstring() {
245
CharArrayBuffer buffer = new CharArrayBuffer(16);
246
buffer.append(" name: value ");
247
assertEquals(5, buffer.indexOf(':'));
248
assertEquals(" name", buffer.substring(0, 5));
249
assertEquals(" value ", buffer.substring(6, buffer.length()));
250
assertEquals("name", buffer.substringTrimmed(0, 5));
251
assertEquals("value", buffer.substringTrimmed(6, buffer.length()));
252
assertEquals("", buffer.substringTrimmed(13, buffer.length()));
255
public void testSubstringIndexOfOutBound() {
256
CharArrayBuffer buffer = new CharArrayBuffer(16);
257
buffer.append("stuff");
259
buffer.substring(-2, 10);
260
fail("IndexOutOfBoundsException should have been thrown");
261
} catch (IndexOutOfBoundsException ex) {
265
buffer.substringTrimmed(-2, 10);
266
fail("IndexOutOfBoundsException should have been thrown");
267
} catch (IndexOutOfBoundsException ex) {
271
buffer.substring(12, 10);
272
fail("IndexOutOfBoundsException should have been thrown");
273
} catch (IndexOutOfBoundsException ex) {
277
buffer.substringTrimmed(12, 10);
278
fail("IndexOutOfBoundsException should have been thrown");
279
} catch (IndexOutOfBoundsException ex) {
283
buffer.substring(2, 1);
284
fail("IndexOutOfBoundsException should have been thrown");
285
} catch (IndexOutOfBoundsException ex) {
289
buffer.substringTrimmed(2, 1);
290
fail("IndexOutOfBoundsException should have been thrown");
291
} catch (IndexOutOfBoundsException ex) {
296
public void testAppendAsciiByteArray() throws Exception {
298
String s2 = " and more stuff";
299
byte[] b1 = s1.getBytes("US-ASCII");
300
byte[] b2 = s2.getBytes("US-ASCII");
302
CharArrayBuffer buffer = new CharArrayBuffer(8);
303
buffer.append(b1, 0, b1.length);
304
buffer.append(b2, 0, b2.length);
306
assertEquals("stuff and more stuff", buffer.toString());
309
public void testAppendISOByteArray() throws Exception {
310
byte[] b = new byte[] {0x00, 0x20, 0x7F, -0x80, -0x01};
312
CharArrayBuffer buffer = new CharArrayBuffer(8);
313
buffer.append(b, 0, b.length);
314
char[] ch = buffer.toCharArray();
316
assertEquals(5, ch.length);
317
assertEquals(0x00, ch[0]);
318
assertEquals(0x20, ch[1]);
319
assertEquals(0x7F, ch[2]);
320
assertEquals(0x80, ch[3]);
321
assertEquals(0xFF, ch[4]);
324
public void testAppendNullByteArray() throws Exception {
325
CharArrayBuffer buffer = new CharArrayBuffer(8);
326
buffer.append((byte[])null, 0, 0);
327
assertEquals("", buffer.toString());
330
public void testAppendNullByteArrayBuffer() throws Exception {
331
CharArrayBuffer buffer = new CharArrayBuffer(8);
332
buffer.append((ByteArrayBuffer)null, 0, 0);
333
assertEquals("", buffer.toString());
336
public void testInvalidAppendAsciiByteArray() throws Exception {
337
CharArrayBuffer buffer = new CharArrayBuffer(4);
338
buffer.append((byte[])null, 0, 0);
340
byte[] tmp = new byte[] { '1', '2', '3', '4'};
342
buffer.append(tmp, -1, 0);
343
fail("IndexOutOfBoundsException should have been thrown");
344
} catch (IndexOutOfBoundsException ex) {
348
buffer.append(tmp, 0, -1);
349
fail("IndexOutOfBoundsException should have been thrown");
350
} catch (IndexOutOfBoundsException ex) {
354
buffer.append(tmp, 0, 8);
355
fail("IndexOutOfBoundsException should have been thrown");
356
} catch (IndexOutOfBoundsException ex) {
360
buffer.append(tmp, 10, Integer.MAX_VALUE);
361
fail("IndexOutOfBoundsException should have been thrown");
362
} catch (IndexOutOfBoundsException ex) {
366
buffer.append(tmp, 2, 4);
367
fail("IndexOutOfBoundsException should have been thrown");
368
} catch (IndexOutOfBoundsException ex) {