~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore/src/test/java/org/apache/http/util/TestCharArrayBuffer.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore/src/test/java/org/apache/http/util/TestCharArrayBuffer.java $
 
3
 * $Revision: 744517 $
 
4
 * $Date: 2009-02-14 17:39:33 +0100 (Sat, 14 Feb 2009) $
 
5
 * 
 
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
 
14
 *
 
15
 *   http://www.apache.org/licenses/LICENSE-2.0
 
16
 *
 
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
 
22
 * under the License.
 
23
 * ====================================================================
 
24
 *
 
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/>.
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.http.util;
 
33
 
 
34
import org.apache.http.util.ByteArrayBuffer;
 
35
import org.apache.http.util.CharArrayBuffer;
 
36
 
 
37
import junit.framework.Test;
 
38
import junit.framework.TestCase;
 
39
import junit.framework.TestSuite;
 
40
 
 
41
/**
 
42
 * Unit tests for {@link CharArrayBuffer}.
 
43
 *
 
44
 */
 
45
public class TestCharArrayBuffer extends TestCase {
 
46
 
 
47
    public TestCharArrayBuffer(String testName) {
 
48
        super(testName);
 
49
    }
 
50
 
 
51
    public static void main(String args[]) {
 
52
        String[] testCaseName = { TestCharArrayBuffer.class.getName() };
 
53
        junit.textui.TestRunner.main(testCaseName);
 
54
    }
 
55
 
 
56
    public static Test suite() {
 
57
        return new TestSuite(TestCharArrayBuffer.class);
 
58
    }
 
59
 
 
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);
 
66
        try {
 
67
            new CharArrayBuffer(-1);
 
68
            fail("IllegalArgumentException should have been thrown");
 
69
        } catch (IllegalArgumentException ex) {
 
70
            // expected
 
71
        }
 
72
    }
 
73
    
 
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();
 
79
        assertNotNull(b1);
 
80
        assertEquals(0, b1.length);
 
81
        assertTrue(buffer.isEmpty());
 
82
        assertFalse(buffer.isFull());
 
83
        
 
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());
 
90
        
 
91
        char[] b2 = buffer.toCharArray();
 
92
        assertNotNull(b2);
 
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));
 
97
        }
 
98
        assertEquals("1234", buffer.toString());
 
99
        
 
100
        buffer.clear();
 
101
        assertEquals(16, buffer.capacity()); 
 
102
        assertEquals(0, buffer.length());
 
103
        assertTrue(buffer.isEmpty());
 
104
        assertFalse(buffer.isFull());
 
105
    }
 
106
    
 
107
    public void testExpandAppend() throws Exception {
 
108
        CharArrayBuffer buffer = new CharArrayBuffer(4);
 
109
        assertEquals(4, buffer.capacity()); 
 
110
        
 
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);
 
115
 
 
116
        assertEquals(8, buffer.capacity()); 
 
117
        assertEquals(6, buffer.length());
 
118
        
 
119
        buffer.append(tmp, 0, 4);
 
120
        
 
121
        assertEquals(16, buffer.capacity()); 
 
122
        assertEquals(10, buffer.length());
 
123
        
 
124
        assertEquals("1212341234", buffer.toString());
 
125
    }
 
126
 
 
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());
 
132
    }
 
133
    
 
134
    public void testAppendNullString() throws Exception {
 
135
        CharArrayBuffer buffer = new CharArrayBuffer(8);
 
136
        buffer.append((String)null);
 
137
        assertEquals("null", buffer.toString());
 
138
    }
 
139
    
 
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());
 
147
    }
 
148
    
 
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());
 
154
    }
 
155
    
 
156
    public void testAppendSingleChar() throws Exception {
 
157
        CharArrayBuffer buffer = new CharArrayBuffer(4);
 
158
        buffer.append('1');
 
159
        buffer.append('2');
 
160
        buffer.append('3');
 
161
        buffer.append('4');
 
162
        buffer.append('5');
 
163
        buffer.append('6');
 
164
        assertEquals("123456", buffer.toString());
 
165
    }
 
166
    
 
167
    public void testInvalidCharArrayAppend() throws Exception {
 
168
        CharArrayBuffer buffer = new CharArrayBuffer(4);
 
169
        buffer.append((char[])null, 0, 0);
 
170
 
 
171
        char[] tmp = new char[] { '1', '2', '3', '4'};
 
172
        try {
 
173
            buffer.append(tmp, -1, 0);
 
174
            fail("IndexOutOfBoundsException should have been thrown");
 
175
        } catch (IndexOutOfBoundsException ex) {
 
176
            // expected
 
177
        }
 
178
        try {
 
179
            buffer.append(tmp, 0, -1);
 
180
            fail("IndexOutOfBoundsException should have been thrown");
 
181
        } catch (IndexOutOfBoundsException ex) {
 
182
            // expected
 
183
        }
 
184
        try {
 
185
            buffer.append(tmp, 0, 8);
 
186
            fail("IndexOutOfBoundsException should have been thrown");
 
187
        } catch (IndexOutOfBoundsException ex) {
 
188
            // expected
 
189
        }
 
190
        try {
 
191
            buffer.append(tmp, 10, Integer.MAX_VALUE);
 
192
            fail("IndexOutOfBoundsException should have been thrown");
 
193
        } catch (IndexOutOfBoundsException ex) {
 
194
            // expected
 
195
        }
 
196
        try {
 
197
            buffer.append(tmp, 2, 4);
 
198
            fail("IndexOutOfBoundsException should have been thrown");
 
199
        } catch (IndexOutOfBoundsException ex) {
 
200
            // expected
 
201
        }
 
202
    }
 
203
 
 
204
    public void testSetLength() throws Exception {
 
205
        CharArrayBuffer buffer = new CharArrayBuffer(4);
 
206
        buffer.setLength(2);
 
207
        assertEquals(2, buffer.length());
 
208
    }
 
209
    
 
210
    public void testSetInvalidLength() throws Exception {
 
211
        CharArrayBuffer buffer = new CharArrayBuffer(4);
 
212
        try {
 
213
            buffer.setLength(-2);
 
214
            fail("IndexOutOfBoundsException should have been thrown");
 
215
        } catch (IndexOutOfBoundsException ex) {
 
216
            // expected
 
217
        }
 
218
        try {
 
219
            buffer.setLength(200);
 
220
            fail("IndexOutOfBoundsException should have been thrown");
 
221
        } catch (IndexOutOfBoundsException ex) {
 
222
            // expected
 
223
        }
 
224
    }
 
225
 
 
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());
 
232
    }
 
233
 
 
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));
 
242
    }
 
243
    
 
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()));
 
253
    }
 
254
    
 
255
    public void testSubstringIndexOfOutBound() {
 
256
        CharArrayBuffer buffer = new CharArrayBuffer(16);
 
257
        buffer.append("stuff");
 
258
        try {
 
259
            buffer.substring(-2, 10);
 
260
            fail("IndexOutOfBoundsException should have been thrown");
 
261
        } catch (IndexOutOfBoundsException ex) {
 
262
            // expected
 
263
        }
 
264
        try {
 
265
            buffer.substringTrimmed(-2, 10);
 
266
            fail("IndexOutOfBoundsException should have been thrown");
 
267
        } catch (IndexOutOfBoundsException ex) {
 
268
            // expected
 
269
        }
 
270
        try {
 
271
            buffer.substring(12, 10);
 
272
            fail("IndexOutOfBoundsException should have been thrown");
 
273
        } catch (IndexOutOfBoundsException ex) {
 
274
            // expected
 
275
        }
 
276
        try {
 
277
            buffer.substringTrimmed(12, 10);
 
278
            fail("IndexOutOfBoundsException should have been thrown");
 
279
        } catch (IndexOutOfBoundsException ex) {
 
280
            // expected
 
281
        }
 
282
        try {
 
283
            buffer.substring(2, 1);
 
284
            fail("IndexOutOfBoundsException should have been thrown");
 
285
        } catch (IndexOutOfBoundsException ex) {
 
286
            // expected
 
287
        }
 
288
        try {
 
289
            buffer.substringTrimmed(2, 1);
 
290
            fail("IndexOutOfBoundsException should have been thrown");
 
291
        } catch (IndexOutOfBoundsException ex) {
 
292
            // expected
 
293
        }
 
294
    }    
 
295
    
 
296
    public void testAppendAsciiByteArray() throws Exception {
 
297
        String s1 = "stuff";
 
298
        String s2 = " and more stuff";
 
299
        byte[] b1 = s1.getBytes("US-ASCII");
 
300
        byte[] b2 = s2.getBytes("US-ASCII");
 
301
        
 
302
        CharArrayBuffer buffer = new CharArrayBuffer(8);
 
303
        buffer.append(b1, 0, b1.length);
 
304
        buffer.append(b2, 0, b2.length);
 
305
        
 
306
        assertEquals("stuff and more stuff", buffer.toString());
 
307
    }
 
308
    
 
309
    public void testAppendISOByteArray() throws Exception {
 
310
        byte[] b = new byte[] {0x00, 0x20, 0x7F, -0x80, -0x01};
 
311
        
 
312
        CharArrayBuffer buffer = new CharArrayBuffer(8);
 
313
        buffer.append(b, 0, b.length);
 
314
        char[] ch = buffer.toCharArray();
 
315
        assertNotNull(ch);
 
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]);
 
322
    }
 
323
    
 
324
    public void testAppendNullByteArray() throws Exception {
 
325
        CharArrayBuffer buffer = new CharArrayBuffer(8);
 
326
        buffer.append((byte[])null, 0, 0);
 
327
        assertEquals("", buffer.toString());
 
328
    }
 
329
 
 
330
    public void testAppendNullByteArrayBuffer() throws Exception {
 
331
        CharArrayBuffer buffer = new CharArrayBuffer(8);
 
332
        buffer.append((ByteArrayBuffer)null, 0, 0);
 
333
        assertEquals("", buffer.toString());
 
334
    }
 
335
 
 
336
    public void testInvalidAppendAsciiByteArray() throws Exception {
 
337
        CharArrayBuffer buffer = new CharArrayBuffer(4);
 
338
        buffer.append((byte[])null, 0, 0);
 
339
 
 
340
        byte[] tmp = new byte[] { '1', '2', '3', '4'};
 
341
        try {
 
342
            buffer.append(tmp, -1, 0);
 
343
            fail("IndexOutOfBoundsException should have been thrown");
 
344
        } catch (IndexOutOfBoundsException ex) {
 
345
            // expected
 
346
        }
 
347
        try {
 
348
            buffer.append(tmp, 0, -1);
 
349
            fail("IndexOutOfBoundsException should have been thrown");
 
350
        } catch (IndexOutOfBoundsException ex) {
 
351
            // expected
 
352
        }
 
353
        try {
 
354
            buffer.append(tmp, 0, 8);
 
355
            fail("IndexOutOfBoundsException should have been thrown");
 
356
        } catch (IndexOutOfBoundsException ex) {
 
357
            // expected
 
358
        }
 
359
        try {
 
360
            buffer.append(tmp, 10, Integer.MAX_VALUE);
 
361
            fail("IndexOutOfBoundsException should have been thrown");
 
362
        } catch (IndexOutOfBoundsException ex) {
 
363
            // expected
 
364
        }
 
365
        try {
 
366
            buffer.append(tmp, 2, 4);
 
367
            fail("IndexOutOfBoundsException should have been thrown");
 
368
        } catch (IndexOutOfBoundsException ex) {
 
369
            // expected
 
370
        }
 
371
    }
 
372
    
 
373
}