001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/util/TestByteArrayBuffer.java $
003: * $Revision: 496069 $
004: * $Date: 2007-01-14 13:03:05 +0100 (Sun, 14 Jan 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.util;
033:
034: import org.apache.http.util.ByteArrayBuffer;
035: import org.apache.http.util.CharArrayBuffer;
036:
037: import junit.framework.Test;
038: import junit.framework.TestCase;
039: import junit.framework.TestSuite;
040:
041: /**
042: * Unit tests for {@link ByteArrayBuffer}.
043: *
044: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
045: */
046: public class TestByteArrayBuffer extends TestCase {
047:
048: public TestByteArrayBuffer(String testName) {
049: super (testName);
050: }
051:
052: public static void main(String args[]) {
053: String[] testCaseName = { TestByteArrayBuffer.class.getName() };
054: junit.textui.TestRunner.main(testCaseName);
055: }
056:
057: public static Test suite() {
058: return new TestSuite(TestByteArrayBuffer.class);
059: }
060:
061: public void testConstructor() throws Exception {
062: ByteArrayBuffer buffer = new ByteArrayBuffer(16);
063: assertEquals(16, buffer.capacity());
064: assertEquals(0, buffer.length());
065: assertNotNull(buffer.buffer());
066: assertEquals(16, buffer.buffer().length);
067: try {
068: new ByteArrayBuffer(-1);
069: fail("IllegalArgumentException should have been thrown");
070: } catch (IllegalArgumentException ex) {
071: // expected
072: }
073: }
074:
075: public void testSimpleAppend() throws Exception {
076: ByteArrayBuffer buffer = new ByteArrayBuffer(16);
077: assertEquals(16, buffer.capacity());
078: assertEquals(0, buffer.length());
079: byte[] b1 = buffer.toByteArray();
080: assertNotNull(b1);
081: assertEquals(0, b1.length);
082: assertTrue(buffer.isEmpty());
083: assertFalse(buffer.isFull());
084:
085: byte[] tmp = new byte[] { 1, 2, 3, 4 };
086: buffer.append(tmp, 0, tmp.length);
087: assertEquals(16, buffer.capacity());
088: assertEquals(4, buffer.length());
089: assertFalse(buffer.isEmpty());
090: assertFalse(buffer.isFull());
091:
092: byte[] b2 = buffer.toByteArray();
093: assertNotNull(b2);
094: assertEquals(4, b2.length);
095: for (int i = 0; i < tmp.length; i++) {
096: assertEquals(tmp[i], b2[i]);
097: assertEquals(tmp[i], buffer.byteAt(i));
098: }
099: buffer.clear();
100: assertEquals(16, buffer.capacity());
101: assertEquals(0, buffer.length());
102: assertTrue(buffer.isEmpty());
103: assertFalse(buffer.isFull());
104: }
105:
106: public void testExpandAppend() throws Exception {
107: ByteArrayBuffer buffer = new ByteArrayBuffer(4);
108: assertEquals(4, buffer.capacity());
109:
110: byte[] tmp = new byte[] { 1, 2, 3, 4 };
111: buffer.append(tmp, 0, 2);
112: buffer.append(tmp, 0, 4);
113: buffer.append(tmp, 0, 0);
114:
115: assertEquals(8, buffer.capacity());
116: assertEquals(6, buffer.length());
117:
118: buffer.append(tmp, 0, 4);
119:
120: assertEquals(16, buffer.capacity());
121: assertEquals(10, buffer.length());
122: }
123:
124: public void testInvalidAppend() throws Exception {
125: ByteArrayBuffer buffer = new ByteArrayBuffer(4);
126: buffer.append((byte[]) null, 0, 0);
127:
128: byte[] tmp = new byte[] { 1, 2, 3, 4 };
129: try {
130: buffer.append(tmp, -1, 0);
131: fail("IndexOutOfBoundsException should have been thrown");
132: } catch (IndexOutOfBoundsException ex) {
133: // expected
134: }
135: try {
136: buffer.append(tmp, 0, -1);
137: fail("IndexOutOfBoundsException should have been thrown");
138: } catch (IndexOutOfBoundsException ex) {
139: // expected
140: }
141: try {
142: buffer.append(tmp, 0, 8);
143: fail("IndexOutOfBoundsException should have been thrown");
144: } catch (IndexOutOfBoundsException ex) {
145: // expected
146: }
147: try {
148: buffer.append(tmp, 10, Integer.MAX_VALUE);
149: fail("IndexOutOfBoundsException should have been thrown");
150: } catch (IndexOutOfBoundsException ex) {
151: // expected
152: }
153: try {
154: buffer.append(tmp, 2, 4);
155: fail("IndexOutOfBoundsException should have been thrown");
156: } catch (IndexOutOfBoundsException ex) {
157: // expected
158: }
159: }
160:
161: public void testAppendOneByte() throws Exception {
162: ByteArrayBuffer buffer = new ByteArrayBuffer(4);
163: assertEquals(4, buffer.capacity());
164:
165: byte[] tmp = new byte[] { 1, 127, -1, -128, 1, -2 };
166: for (int i = 0; i < tmp.length; i++) {
167: buffer.append(tmp[i]);
168: }
169: assertEquals(8, buffer.capacity());
170: assertEquals(6, buffer.length());
171:
172: for (int i = 0; i < tmp.length; i++) {
173: assertEquals(tmp[i], buffer.byteAt(i));
174: }
175: }
176:
177: public void testSetLength() throws Exception {
178: ByteArrayBuffer buffer = new ByteArrayBuffer(4);
179: buffer.setLength(2);
180: assertEquals(2, buffer.length());
181: }
182:
183: public void testSetInvalidLength() throws Exception {
184: ByteArrayBuffer buffer = new ByteArrayBuffer(4);
185: try {
186: buffer.setLength(-2);
187: fail("IndexOutOfBoundsException should have been thrown");
188: } catch (IndexOutOfBoundsException ex) {
189: // expected
190: }
191: try {
192: buffer.setLength(200);
193: fail("IndexOutOfBoundsException should have been thrown");
194: } catch (IndexOutOfBoundsException ex) {
195: // expected
196: }
197: }
198:
199: public void testAppendCharArrayAsAscii() throws Exception {
200: String s1 = "stuff";
201: String s2 = " and more stuff";
202: char[] b1 = s1.toCharArray();
203: char[] b2 = s2.toCharArray();
204:
205: ByteArrayBuffer buffer = new ByteArrayBuffer(8);
206: buffer.append(b1, 0, b1.length);
207: buffer.append(b2, 0, b2.length);
208:
209: assertEquals(s1 + s2, new String(buffer.toByteArray(),
210: "US-ASCII"));
211: }
212:
213: public void testAppendNullCharArray() throws Exception {
214: ByteArrayBuffer buffer = new ByteArrayBuffer(8);
215: buffer.append((char[]) null, 0, 0);
216: assertEquals(0, buffer.length());
217: }
218:
219: public void testAppendEmptyCharArray() throws Exception {
220: ByteArrayBuffer buffer = new ByteArrayBuffer(8);
221: buffer.append(new char[] {}, 0, 0);
222: assertEquals(0, buffer.length());
223: }
224:
225: public void testAppendNullCharArrayBuffer() throws Exception {
226: ByteArrayBuffer buffer = new ByteArrayBuffer(8);
227: buffer.append((CharArrayBuffer) null, 0, 0);
228: assertEquals(0, buffer.length());
229: }
230:
231: public void testInvalidAppendCharArrayAsAscii() throws Exception {
232: ByteArrayBuffer buffer = new ByteArrayBuffer(4);
233: buffer.append((char[]) null, 0, 0);
234:
235: char[] tmp = new char[] { '1', '2', '3', '4' };
236: try {
237: buffer.append(tmp, -1, 0);
238: fail("IndexOutOfBoundsException should have been thrown");
239: } catch (IndexOutOfBoundsException ex) {
240: // expected
241: }
242: try {
243: buffer.append(tmp, 0, -1);
244: fail("IndexOutOfBoundsException should have been thrown");
245: } catch (IndexOutOfBoundsException ex) {
246: // expected
247: }
248: try {
249: buffer.append(tmp, 0, 8);
250: fail("IndexOutOfBoundsException should have been thrown");
251: } catch (IndexOutOfBoundsException ex) {
252: // expected
253: }
254: try {
255: buffer.append(tmp, 10, Integer.MAX_VALUE);
256: fail("IndexOutOfBoundsException should have been thrown");
257: } catch (IndexOutOfBoundsException ex) {
258: // expected
259: }
260: try {
261: buffer.append(tmp, 2, 4);
262: fail("IndexOutOfBoundsException should have been thrown");
263: } catch (IndexOutOfBoundsException ex) {
264: // expected
265: }
266: }
267:
268: }
|