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
.TestCase
;
25 * Unit tests for {@link ByteArrayBuffer}.
27 public class TestByteArrayBuffer
extends TestCase
{
29 public void testConstructor() throws Exception
{
30 ByteArrayBuffer buffer
= new ByteArrayBuffer(16);
31 assertEquals(16, buffer
.capacity());
32 assertEquals(0, buffer
.length());
33 assertNotNull(buffer
.buffer());
34 assertEquals(16, buffer
.buffer().length
);
36 new ByteArrayBuffer(-1);
37 fail("IllegalArgumentException should have been thrown");
38 } catch (IllegalArgumentException ex
) {
43 public void testSimpleAppend() throws Exception
{
44 ByteArrayBuffer buffer
= new ByteArrayBuffer(16);
45 assertEquals(16, buffer
.capacity());
46 assertEquals(0, buffer
.length());
47 byte[] b1
= buffer
.toByteArray();
49 assertEquals(0, b1
.length
);
50 assertTrue(buffer
.isEmpty());
51 assertFalse(buffer
.isFull());
53 byte[] tmp
= new byte[] { 1, 2, 3, 4};
54 buffer
.append(tmp
, 0, tmp
.length
);
55 assertEquals(16, buffer
.capacity());
56 assertEquals(4, buffer
.length());
57 assertFalse(buffer
.isEmpty());
58 assertFalse(buffer
.isFull());
60 byte[] b2
= buffer
.toByteArray();
62 assertEquals(4, b2
.length
);
63 for (int i
= 0; i
< tmp
.length
; i
++) {
64 assertEquals(tmp
[i
], b2
[i
]);
65 assertEquals(tmp
[i
], buffer
.byteAt(i
));
68 assertEquals(16, buffer
.capacity());
69 assertEquals(0, buffer
.length());
70 assertTrue(buffer
.isEmpty());
71 assertFalse(buffer
.isFull());
74 public void testExpandAppend() throws Exception
{
75 ByteArrayBuffer buffer
= new ByteArrayBuffer(4);
76 assertEquals(4, buffer
.capacity());
78 byte[] tmp
= new byte[] { 1, 2, 3, 4};
79 buffer
.append(tmp
, 0, 2);
80 buffer
.append(tmp
, 0, 4);
81 buffer
.append(tmp
, 0, 0);
83 assertEquals(8, buffer
.capacity());
84 assertEquals(6, buffer
.length());
86 buffer
.append(tmp
, 0, 4);
88 assertEquals(16, buffer
.capacity());
89 assertEquals(10, buffer
.length());
92 public void testInvalidAppend() throws Exception
{
93 ByteArrayBuffer buffer
= new ByteArrayBuffer(4);
94 buffer
.append((byte[])null, 0, 0);
96 byte[] tmp
= new byte[] { 1, 2, 3, 4};
98 buffer
.append(tmp
, -1, 0);
99 fail("IndexOutOfBoundsException should have been thrown");
100 } catch (IndexOutOfBoundsException ex
) {
104 buffer
.append(tmp
, 0, -1);
105 fail("IndexOutOfBoundsException should have been thrown");
106 } catch (IndexOutOfBoundsException ex
) {
110 buffer
.append(tmp
, 0, 8);
111 fail("IndexOutOfBoundsException should have been thrown");
112 } catch (IndexOutOfBoundsException ex
) {
116 buffer
.append(tmp
, 10, Integer
.MAX_VALUE
);
117 fail("IndexOutOfBoundsException should have been thrown");
118 } catch (IndexOutOfBoundsException ex
) {
122 buffer
.append(tmp
, 2, 4);
123 fail("IndexOutOfBoundsException should have been thrown");
124 } catch (IndexOutOfBoundsException ex
) {
129 public void testAppendOneByte() throws Exception
{
130 ByteArrayBuffer buffer
= new ByteArrayBuffer(4);
131 assertEquals(4, buffer
.capacity());
133 byte[] tmp
= new byte[] { 1, 127, -1, -128, 1, -2};
134 for (int i
= 0; i
< tmp
.length
; i
++) {
135 buffer
.append(tmp
[i
]);
137 assertEquals(8, buffer
.capacity());
138 assertEquals(6, buffer
.length());
140 for (int i
= 0; i
< tmp
.length
; i
++) {
141 assertEquals(tmp
[i
], buffer
.byteAt(i
));
145 public void testSetLength() throws Exception
{
146 ByteArrayBuffer buffer
= new ByteArrayBuffer(4);
148 assertEquals(2, buffer
.length());
151 public void testSetInvalidLength() throws Exception
{
152 ByteArrayBuffer buffer
= new ByteArrayBuffer(4);
154 buffer
.setLength(-2);
155 fail("IndexOutOfBoundsException should have been thrown");
156 } catch (IndexOutOfBoundsException ex
) {
160 buffer
.setLength(200);
161 fail("IndexOutOfBoundsException should have been thrown");
162 } catch (IndexOutOfBoundsException ex
) {
167 public void testAppendCharArrayAsAscii() throws Exception
{
169 String s2
= " and more stuff";
170 char[] b1
= s1
.toCharArray();
171 char[] b2
= s2
.toCharArray();
173 ByteArrayBuffer buffer
= new ByteArrayBuffer(8);
174 buffer
.append(b1
, 0, b1
.length
);
175 buffer
.append(b2
, 0, b2
.length
);
177 assertEquals(s1
+ s2
, new String(buffer
.toByteArray(), "US-ASCII"));
180 public void testAppendNullCharArray() throws Exception
{
181 ByteArrayBuffer buffer
= new ByteArrayBuffer(8);
182 buffer
.append((char[])null, 0, 0);
183 assertEquals(0, buffer
.length());
186 public void testAppendEmptyCharArray() throws Exception
{
187 ByteArrayBuffer buffer
= new ByteArrayBuffer(8);
188 buffer
.append(new char[] {}, 0, 0);
189 assertEquals(0, buffer
.length());
192 public void testInvalidAppendCharArrayAsAscii() throws Exception
{
193 ByteArrayBuffer buffer
= new ByteArrayBuffer(4);
194 buffer
.append((char[])null, 0, 0);
196 char[] tmp
= new char[] { '1', '2', '3', '4'};
198 buffer
.append(tmp
, -1, 0);
199 fail("IndexOutOfBoundsException should have been thrown");
200 } catch (IndexOutOfBoundsException ex
) {
204 buffer
.append(tmp
, 0, -1);
205 fail("IndexOutOfBoundsException should have been thrown");
206 } catch (IndexOutOfBoundsException ex
) {
210 buffer
.append(tmp
, 0, 8);
211 fail("IndexOutOfBoundsException should have been thrown");
212 } catch (IndexOutOfBoundsException ex
) {
216 buffer
.append(tmp
, 10, Integer
.MAX_VALUE
);
217 fail("IndexOutOfBoundsException should have been thrown");
218 } catch (IndexOutOfBoundsException ex
) {
222 buffer
.append(tmp
, 2, 4);
223 fail("IndexOutOfBoundsException should have been thrown");
224 } catch (IndexOutOfBoundsException ex
) {