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 | ****************************************************************/ |
---|
19 | |
---|
20 | package org.apache.james.mime4j.util; |
---|
21 | |
---|
22 | import org.apache.james.mime4j.util.ByteArrayBuffer; |
---|
23 | |
---|
24 | import junit.framework.TestCase; |
---|
25 | |
---|
26 | /** |
---|
27 | * Unit tests for {@link ByteArrayBuffer}. |
---|
28 | */ |
---|
29 | public class TestByteArrayBuffer extends TestCase { |
---|
30 | |
---|
31 | public void testConstructor() throws Exception { |
---|
32 | ByteArrayBuffer buffer = new ByteArrayBuffer(16); |
---|
33 | assertEquals(16, buffer.capacity()); |
---|
34 | assertEquals(0, buffer.length()); |
---|
35 | assertNotNull(buffer.buffer()); |
---|
36 | assertEquals(16, buffer.buffer().length); |
---|
37 | try { |
---|
38 | new ByteArrayBuffer(-1); |
---|
39 | fail("IllegalArgumentException should have been thrown"); |
---|
40 | } catch (IllegalArgumentException ex) { |
---|
41 | // expected |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | public void testSimpleAppend() throws Exception { |
---|
46 | ByteArrayBuffer buffer = new ByteArrayBuffer(16); |
---|
47 | assertEquals(16, buffer.capacity()); |
---|
48 | assertEquals(0, buffer.length()); |
---|
49 | byte[] b1 = buffer.toByteArray(); |
---|
50 | assertNotNull(b1); |
---|
51 | assertEquals(0, b1.length); |
---|
52 | assertTrue(buffer.isEmpty()); |
---|
53 | assertFalse(buffer.isFull()); |
---|
54 | |
---|
55 | byte[] tmp = new byte[] { 1, 2, 3, 4}; |
---|
56 | buffer.append(tmp, 0, tmp.length); |
---|
57 | assertEquals(16, buffer.capacity()); |
---|
58 | assertEquals(4, buffer.length()); |
---|
59 | assertFalse(buffer.isEmpty()); |
---|
60 | assertFalse(buffer.isFull()); |
---|
61 | |
---|
62 | byte[] b2 = buffer.toByteArray(); |
---|
63 | assertNotNull(b2); |
---|
64 | assertEquals(4, b2.length); |
---|
65 | for (int i = 0; i < tmp.length; i++) { |
---|
66 | assertEquals(tmp[i], b2[i]); |
---|
67 | assertEquals(tmp[i], buffer.byteAt(i)); |
---|
68 | } |
---|
69 | buffer.clear(); |
---|
70 | assertEquals(16, buffer.capacity()); |
---|
71 | assertEquals(0, buffer.length()); |
---|
72 | assertTrue(buffer.isEmpty()); |
---|
73 | assertFalse(buffer.isFull()); |
---|
74 | } |
---|
75 | |
---|
76 | public void testExpandAppend() throws Exception { |
---|
77 | ByteArrayBuffer buffer = new ByteArrayBuffer(4); |
---|
78 | assertEquals(4, buffer.capacity()); |
---|
79 | |
---|
80 | byte[] tmp = new byte[] { 1, 2, 3, 4}; |
---|
81 | buffer.append(tmp, 0, 2); |
---|
82 | buffer.append(tmp, 0, 4); |
---|
83 | buffer.append(tmp, 0, 0); |
---|
84 | |
---|
85 | assertEquals(8, buffer.capacity()); |
---|
86 | assertEquals(6, buffer.length()); |
---|
87 | |
---|
88 | buffer.append(tmp, 0, 4); |
---|
89 | |
---|
90 | assertEquals(16, buffer.capacity()); |
---|
91 | assertEquals(10, buffer.length()); |
---|
92 | } |
---|
93 | |
---|
94 | public void testInvalidAppend() throws Exception { |
---|
95 | ByteArrayBuffer buffer = new ByteArrayBuffer(4); |
---|
96 | buffer.append((byte[])null, 0, 0); |
---|
97 | |
---|
98 | byte[] tmp = new byte[] { 1, 2, 3, 4}; |
---|
99 | try { |
---|
100 | buffer.append(tmp, -1, 0); |
---|
101 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
102 | } catch (IndexOutOfBoundsException ex) { |
---|
103 | // expected |
---|
104 | } |
---|
105 | try { |
---|
106 | buffer.append(tmp, 0, -1); |
---|
107 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
108 | } catch (IndexOutOfBoundsException ex) { |
---|
109 | // expected |
---|
110 | } |
---|
111 | try { |
---|
112 | buffer.append(tmp, 0, 8); |
---|
113 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
114 | } catch (IndexOutOfBoundsException ex) { |
---|
115 | // expected |
---|
116 | } |
---|
117 | try { |
---|
118 | buffer.append(tmp, 10, Integer.MAX_VALUE); |
---|
119 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
120 | } catch (IndexOutOfBoundsException ex) { |
---|
121 | // expected |
---|
122 | } |
---|
123 | try { |
---|
124 | buffer.append(tmp, 2, 4); |
---|
125 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
126 | } catch (IndexOutOfBoundsException ex) { |
---|
127 | // expected |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | public void testAppendOneByte() throws Exception { |
---|
132 | ByteArrayBuffer buffer = new ByteArrayBuffer(4); |
---|
133 | assertEquals(4, buffer.capacity()); |
---|
134 | |
---|
135 | byte[] tmp = new byte[] { 1, 127, -1, -128, 1, -2}; |
---|
136 | for (byte b : tmp) { |
---|
137 | buffer.append(b); |
---|
138 | } |
---|
139 | assertEquals(8, buffer.capacity()); |
---|
140 | assertEquals(6, buffer.length()); |
---|
141 | |
---|
142 | for (int i = 0; i < tmp.length; i++) { |
---|
143 | assertEquals(tmp[i], buffer.byteAt(i)); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | public void testSetLength() throws Exception { |
---|
148 | ByteArrayBuffer buffer = new ByteArrayBuffer(4); |
---|
149 | buffer.setLength(2); |
---|
150 | assertEquals(2, buffer.length()); |
---|
151 | } |
---|
152 | |
---|
153 | public void testSetInvalidLength() throws Exception { |
---|
154 | ByteArrayBuffer buffer = new ByteArrayBuffer(4); |
---|
155 | try { |
---|
156 | buffer.setLength(-2); |
---|
157 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
158 | } catch (IndexOutOfBoundsException ex) { |
---|
159 | // expected |
---|
160 | } |
---|
161 | try { |
---|
162 | buffer.setLength(200); |
---|
163 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
164 | } catch (IndexOutOfBoundsException ex) { |
---|
165 | // expected |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | public void testRemove() throws Exception { |
---|
170 | ByteArrayBuffer b = new ByteArrayBuffer(16); |
---|
171 | byte tmp[] = "--+++-".getBytes("US-ASCII"); |
---|
172 | b.append(tmp, 0, tmp.length); |
---|
173 | |
---|
174 | b.remove(2, 3); |
---|
175 | assertEquals(3, b.length()); |
---|
176 | assertEquals("---", new String(b.buffer(), 0, b.length(), "US-ASCII")); |
---|
177 | b.remove(2, 1); |
---|
178 | b.remove(1, 1); |
---|
179 | b.remove(0, 1); |
---|
180 | assertEquals(0, b.length()); |
---|
181 | |
---|
182 | tmp = "+++---".getBytes("US-ASCII"); |
---|
183 | b.append(tmp, 0, tmp.length); |
---|
184 | |
---|
185 | b.remove(0, 3); |
---|
186 | assertEquals(3, b.length()); |
---|
187 | assertEquals("---", new String(b.buffer(), 0, b.length(), "US-ASCII")); |
---|
188 | b.remove(0, 3); |
---|
189 | assertEquals(0, b.length()); |
---|
190 | |
---|
191 | tmp = "---+++".getBytes("US-ASCII"); |
---|
192 | b.append(tmp, 0, tmp.length); |
---|
193 | |
---|
194 | b.remove(3, 3); |
---|
195 | assertEquals(3, b.length()); |
---|
196 | assertEquals("---", new String(b.buffer(), 0, b.length(), "US-ASCII")); |
---|
197 | b.remove(0, 3); |
---|
198 | |
---|
199 | assertEquals(0, b.length()); |
---|
200 | } |
---|
201 | |
---|
202 | public void testInvalidRemove() throws Exception { |
---|
203 | ByteArrayBuffer buffer = new ByteArrayBuffer(16); |
---|
204 | buffer.setLength(8); |
---|
205 | try { |
---|
206 | buffer.remove(-1, 0); |
---|
207 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
208 | } catch (IndexOutOfBoundsException ex) { |
---|
209 | // expected |
---|
210 | } |
---|
211 | try { |
---|
212 | buffer.remove(0, -1); |
---|
213 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
214 | } catch (IndexOutOfBoundsException ex) { |
---|
215 | // expected |
---|
216 | } |
---|
217 | try { |
---|
218 | buffer.remove(0, 9); |
---|
219 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
220 | } catch (IndexOutOfBoundsException ex) { |
---|
221 | // expected |
---|
222 | } |
---|
223 | try { |
---|
224 | buffer.remove(10, 2); |
---|
225 | fail("IndexOutOfBoundsException should have been thrown"); |
---|
226 | } catch (IndexOutOfBoundsException ex) { |
---|
227 | // expected |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | } |
---|