1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
20 package org
.apache
.james
.mime4j
;
22 import junit
.framework
.Test
;
23 import junit
.framework
.TestCase
;
24 import junit
.framework
.TestSuite
;
27 * Unit tests for {@link CharArrayBuffer}.
29 public class TestCharArrayBuffer
extends TestCase
{
31 public TestCharArrayBuffer(String testName
) {
35 public static void main(String args
[]) {
36 String
[] testCaseName
= { TestCharArrayBuffer
.class.getName() };
37 junit
.textui
.TestRunner
.main(testCaseName
);
40 public static Test
suite() {
41 return new TestSuite(TestCharArrayBuffer
.class);
44 public void testConstructor() throws Exception
{
45 CharArrayBuffer buffer
= new CharArrayBuffer(16);
46 assertEquals(16, buffer
.capacity());
47 assertEquals(0, buffer
.length());
48 assertNotNull(buffer
.buffer());
49 assertEquals(16, buffer
.buffer().length
);
51 new CharArrayBuffer(-1);
52 fail("IllegalArgumentException should have been thrown");
53 } catch (IllegalArgumentException ex
) {
58 public void testSimpleAppend() throws Exception
{
59 CharArrayBuffer buffer
= new CharArrayBuffer(16);
60 assertEquals(16, buffer
.capacity());
61 assertEquals(0, buffer
.length());
62 char[] b1
= buffer
.toCharArray();
64 assertEquals(0, b1
.length
);
65 assertTrue(buffer
.isEmpty());
66 assertFalse(buffer
.isFull());
68 char[] tmp
= new char[] { '1', '2', '3', '4'};
69 buffer
.append(tmp
, 0, tmp
.length
);
70 assertEquals(16, buffer
.capacity());
71 assertEquals(4, buffer
.length());
72 assertFalse(buffer
.isEmpty());
73 assertFalse(buffer
.isFull());
75 char[] b2
= buffer
.toCharArray();
77 assertEquals(4, b2
.length
);
78 for (int i
= 0; i
< tmp
.length
; i
++) {
79 assertEquals(tmp
[i
], b2
[i
]);
80 assertEquals(tmp
[i
], buffer
.charAt(i
));
82 assertEquals("1234", buffer
.toString());
85 assertEquals(16, buffer
.capacity());
86 assertEquals(0, buffer
.length());
87 assertTrue(buffer
.isEmpty());
88 assertFalse(buffer
.isFull());
91 public void testExpandAppend() throws Exception
{
92 CharArrayBuffer buffer
= new CharArrayBuffer(4);
93 assertEquals(4, buffer
.capacity());
95 char[] tmp
= new char[] { '1', '2', '3', '4'};
96 buffer
.append(tmp
, 0, 2);
97 buffer
.append(tmp
, 0, 4);
98 buffer
.append(tmp
, 0, 0);
100 assertEquals(8, buffer
.capacity());
101 assertEquals(6, buffer
.length());
103 buffer
.append(tmp
, 0, 4);
105 assertEquals(16, buffer
.capacity());
106 assertEquals(10, buffer
.length());
108 assertEquals("1212341234", buffer
.toString());
111 public void testAppendString() throws Exception
{
112 CharArrayBuffer buffer
= new CharArrayBuffer(8);
113 buffer
.append("stuff");
114 buffer
.append(" and more stuff");
115 assertEquals("stuff and more stuff", buffer
.toString());
118 public void testAppendNullString() throws Exception
{
119 CharArrayBuffer buffer
= new CharArrayBuffer(8);
120 buffer
.append((String
)null);
121 assertEquals("null", buffer
.toString());
124 public void testAppendCharArrayBuffer() throws Exception
{
125 CharArrayBuffer buffer1
= new CharArrayBuffer(8);
126 buffer1
.append(" and more stuff");
127 CharArrayBuffer buffer2
= new CharArrayBuffer(8);
128 buffer2
.append("stuff");
129 buffer2
.append(buffer1
);
130 assertEquals("stuff and more stuff", buffer2
.toString());
133 public void testAppendNullCharArrayBuffer() throws Exception
{
134 CharArrayBuffer buffer
= new CharArrayBuffer(8);
135 buffer
.append((CharArrayBuffer
)null);
136 buffer
.append((CharArrayBuffer
)null, 0, 0);
137 assertEquals("", buffer
.toString());
140 public void testAppendSingleChar() throws Exception
{
141 CharArrayBuffer buffer
= new CharArrayBuffer(4);
148 assertEquals("123456", buffer
.toString());
151 public void testInvalidCharArrayAppend() throws Exception
{
152 CharArrayBuffer buffer
= new CharArrayBuffer(4);
153 buffer
.append((char[])null, 0, 0);
155 char[] tmp
= new char[] { '1', '2', '3', '4'};
157 buffer
.append(tmp
, -1, 0);
158 fail("IndexOutOfBoundsException should have been thrown");
159 } catch (IndexOutOfBoundsException ex
) {
163 buffer
.append(tmp
, 0, -1);
164 fail("IndexOutOfBoundsException should have been thrown");
165 } catch (IndexOutOfBoundsException ex
) {
169 buffer
.append(tmp
, 0, 8);
170 fail("IndexOutOfBoundsException should have been thrown");
171 } catch (IndexOutOfBoundsException ex
) {
175 buffer
.append(tmp
, 10, Integer
.MAX_VALUE
);
176 fail("IndexOutOfBoundsException should have been thrown");
177 } catch (IndexOutOfBoundsException ex
) {
181 buffer
.append(tmp
, 2, 4);
182 fail("IndexOutOfBoundsException should have been thrown");
183 } catch (IndexOutOfBoundsException ex
) {
188 public void testSetLength() throws Exception
{
189 CharArrayBuffer buffer
= new CharArrayBuffer(4);
191 assertEquals(2, buffer
.length());
194 public void testSetInvalidLength() throws Exception
{
195 CharArrayBuffer buffer
= new CharArrayBuffer(4);
197 buffer
.setLength(-2);
198 fail("IndexOutOfBoundsException should have been thrown");
199 } catch (IndexOutOfBoundsException ex
) {
203 buffer
.setLength(200);
204 fail("IndexOutOfBoundsException should have been thrown");
205 } catch (IndexOutOfBoundsException ex
) {
210 public void testEnsureCapacity() throws Exception
{
211 CharArrayBuffer buffer
= new CharArrayBuffer(4);
212 buffer
.ensureCapacity(2);
213 assertEquals(4, buffer
.capacity());
214 buffer
.ensureCapacity(8);
215 assertEquals(8, buffer
.capacity());
218 public void testIndexOf() {
219 CharArrayBuffer buffer
= new CharArrayBuffer(16);
220 buffer
.append("name: value");
221 assertEquals(4, buffer
.indexOf(':'));
222 assertEquals(-1, buffer
.indexOf(','));
223 assertEquals(4, buffer
.indexOf(':', -1, 11));
224 assertEquals(4, buffer
.indexOf(':', 0, 1000));
225 assertEquals(-1, buffer
.indexOf(':', 2, 1));
228 public void testSubstring() {
229 CharArrayBuffer buffer
= new CharArrayBuffer(16);
230 buffer
.append(" name: value ");
231 assertEquals(5, buffer
.indexOf(':'));
232 assertEquals(" name", buffer
.substring(0, 5));
233 assertEquals(" value ", buffer
.substring(6, buffer
.length()));
234 assertEquals("name", buffer
.substringTrimmed(0, 5));
235 assertEquals("value", buffer
.substringTrimmed(6, buffer
.length()));
236 assertEquals("", buffer
.substringTrimmed(13, buffer
.length()));
239 public void testSubstringIndexOfOutBound() {
240 CharArrayBuffer buffer
= new CharArrayBuffer(16);
241 buffer
.append("stuff");
243 buffer
.substring(-2, 10);
244 fail("IndexOutOfBoundsException should have been thrown");
245 } catch (IndexOutOfBoundsException ex
) {
249 buffer
.substringTrimmed(-2, 10);
250 fail("IndexOutOfBoundsException should have been thrown");
251 } catch (IndexOutOfBoundsException ex
) {
255 buffer
.substring(12, 10);
256 fail("IndexOutOfBoundsException should have been thrown");
257 } catch (IndexOutOfBoundsException ex
) {
261 buffer
.substringTrimmed(12, 10);
262 fail("IndexOutOfBoundsException should have been thrown");
263 } catch (IndexOutOfBoundsException ex
) {
267 buffer
.substring(2, 1);
268 fail("IndexOutOfBoundsException should have been thrown");
269 } catch (IndexOutOfBoundsException ex
) {
273 buffer
.substringTrimmed(2, 1);
274 fail("IndexOutOfBoundsException should have been thrown");
275 } catch (IndexOutOfBoundsException ex
) {
280 public void testAppendAsciiByteArray() throws Exception
{
282 String s2
= " and more stuff";
283 byte[] b1
= s1
.getBytes("US-ASCII");
284 byte[] b2
= s2
.getBytes("US-ASCII");
286 CharArrayBuffer buffer
= new CharArrayBuffer(8);
287 buffer
.append(b1
, 0, b1
.length
);
288 buffer
.append(b2
, 0, b2
.length
);
290 assertEquals("stuff and more stuff", buffer
.toString());
293 public void testAppendISOByteArray() throws Exception
{
294 byte[] b
= new byte[] {0x00, 0x20, 0x7F, -0x80, -0x01};
296 CharArrayBuffer buffer
= new CharArrayBuffer(8);
297 buffer
.append(b
, 0, b
.length
);
298 char[] ch
= buffer
.toCharArray();
300 assertEquals(5, ch
.length
);
301 assertEquals(0x00, ch
[0]);
302 assertEquals(0x20, ch
[1]);
303 assertEquals(0x7F, ch
[2]);
304 assertEquals(0x80, ch
[3]);
305 assertEquals(0xFF, ch
[4]);
308 public void testAppendNullByteArray() throws Exception
{
309 CharArrayBuffer buffer
= new CharArrayBuffer(8);
310 buffer
.append((byte[])null, 0, 0);
311 assertEquals("", buffer
.toString());
314 public void testAppendNullByteArrayBuffer() throws Exception
{
315 CharArrayBuffer buffer
= new CharArrayBuffer(8);
316 buffer
.append((ByteArrayBuffer
)null, 0, 0);
317 assertEquals("", buffer
.toString());
320 public void testInvalidAppendAsciiByteArray() throws Exception
{
321 CharArrayBuffer buffer
= new CharArrayBuffer(4);
322 buffer
.append((byte[])null, 0, 0);
324 byte[] tmp
= new byte[] { '1', '2', '3', '4'};
326 buffer
.append(tmp
, -1, 0);
327 fail("IndexOutOfBoundsException should have been thrown");
328 } catch (IndexOutOfBoundsException ex
) {
332 buffer
.append(tmp
, 0, -1);
333 fail("IndexOutOfBoundsException should have been thrown");
334 } catch (IndexOutOfBoundsException ex
) {
338 buffer
.append(tmp
, 0, 8);
339 fail("IndexOutOfBoundsException should have been thrown");
340 } catch (IndexOutOfBoundsException ex
) {
344 buffer
.append(tmp
, 10, Integer
.MAX_VALUE
);
345 fail("IndexOutOfBoundsException should have been thrown");
346 } catch (IndexOutOfBoundsException ex
) {
350 buffer
.append(tmp
, 2, 4);
351 fail("IndexOutOfBoundsException should have been thrown");
352 } catch (IndexOutOfBoundsException ex
) {