MIME4J-5 Performance patch 3, https://issues.apache.org/jira/browse/MIME4J-5. Contrib...
[mime4j.git] / src / test / java / org / apache / james / mime4j / TestByteArrayBuffer.java
blobc5d872b32229b0044c1c28379dd82adaf1214b38
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 *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
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;
24 /**
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);
35 try {
36 new ByteArrayBuffer(-1);
37 fail("IllegalArgumentException should have been thrown");
38 } catch (IllegalArgumentException ex) {
39 // expected
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();
48 assertNotNull(b1);
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();
61 assertNotNull(b2);
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));
67 buffer.clear();
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};
97 try {
98 buffer.append(tmp, -1, 0);
99 fail("IndexOutOfBoundsException should have been thrown");
100 } catch (IndexOutOfBoundsException ex) {
101 // expected
103 try {
104 buffer.append(tmp, 0, -1);
105 fail("IndexOutOfBoundsException should have been thrown");
106 } catch (IndexOutOfBoundsException ex) {
107 // expected
109 try {
110 buffer.append(tmp, 0, 8);
111 fail("IndexOutOfBoundsException should have been thrown");
112 } catch (IndexOutOfBoundsException ex) {
113 // expected
115 try {
116 buffer.append(tmp, 10, Integer.MAX_VALUE);
117 fail("IndexOutOfBoundsException should have been thrown");
118 } catch (IndexOutOfBoundsException ex) {
119 // expected
121 try {
122 buffer.append(tmp, 2, 4);
123 fail("IndexOutOfBoundsException should have been thrown");
124 } catch (IndexOutOfBoundsException ex) {
125 // expected
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);
147 buffer.setLength(2);
148 assertEquals(2, buffer.length());
151 public void testSetInvalidLength() throws Exception {
152 ByteArrayBuffer buffer = new ByteArrayBuffer(4);
153 try {
154 buffer.setLength(-2);
155 fail("IndexOutOfBoundsException should have been thrown");
156 } catch (IndexOutOfBoundsException ex) {
157 // expected
159 try {
160 buffer.setLength(200);
161 fail("IndexOutOfBoundsException should have been thrown");
162 } catch (IndexOutOfBoundsException ex) {
163 // expected
167 public void testAppendCharArrayAsAscii() throws Exception {
168 String s1 = "stuff";
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'};
197 try {
198 buffer.append(tmp, -1, 0);
199 fail("IndexOutOfBoundsException should have been thrown");
200 } catch (IndexOutOfBoundsException ex) {
201 // expected
203 try {
204 buffer.append(tmp, 0, -1);
205 fail("IndexOutOfBoundsException should have been thrown");
206 } catch (IndexOutOfBoundsException ex) {
207 // expected
209 try {
210 buffer.append(tmp, 0, 8);
211 fail("IndexOutOfBoundsException should have been thrown");
212 } catch (IndexOutOfBoundsException ex) {
213 // expected
215 try {
216 buffer.append(tmp, 10, Integer.MAX_VALUE);
217 fail("IndexOutOfBoundsException should have been thrown");
218 } catch (IndexOutOfBoundsException ex) {
219 // expected
221 try {
222 buffer.append(tmp, 2, 4);
223 fail("IndexOutOfBoundsException should have been thrown");
224 } catch (IndexOutOfBoundsException ex) {
225 // expected