001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-main/src/test/java/org/apache/http/util/TestCharArrayBuffer.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 CharArrayBuffer}.
043: *
044: * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
045: */
046: public class TestCharArrayBuffer extends TestCase {
047:
048: public TestCharArrayBuffer(String testName) {
049: super (testName);
050: }
051:
052: public static void main(String args[]) {
053: String[] testCaseName = { TestCharArrayBuffer.class.getName() };
054: junit.textui.TestRunner.main(testCaseName);
055: }
056:
057: public static Test suite() {
058: return new TestSuite(TestCharArrayBuffer.class);
059: }
060:
061: public void testConstructor() throws Exception {
062: CharArrayBuffer buffer = new CharArrayBuffer(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 CharArrayBuffer(-1);
069: fail("IllegalArgumentException should have been thrown");
070: } catch (IllegalArgumentException ex) {
071: // expected
072: }
073: }
074:
075: public void testSimpleAppend() throws Exception {
076: CharArrayBuffer buffer = new CharArrayBuffer(16);
077: assertEquals(16, buffer.capacity());
078: assertEquals(0, buffer.length());
079: char[] b1 = buffer.toCharArray();
080: assertNotNull(b1);
081: assertEquals(0, b1.length);
082: assertTrue(buffer.isEmpty());
083: assertFalse(buffer.isFull());
084:
085: char[] tmp = new char[] { '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: char[] b2 = buffer.toCharArray();
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.charAt(i));
098: }
099: assertEquals("1234", buffer.toString());
100:
101: buffer.clear();
102: assertEquals(16, buffer.capacity());
103: assertEquals(0, buffer.length());
104: assertTrue(buffer.isEmpty());
105: assertFalse(buffer.isFull());
106: }
107:
108: public void testExpandAppend() throws Exception {
109: CharArrayBuffer buffer = new CharArrayBuffer(4);
110: assertEquals(4, buffer.capacity());
111:
112: char[] tmp = new char[] { '1', '2', '3', '4' };
113: buffer.append(tmp, 0, 2);
114: buffer.append(tmp, 0, 4);
115: buffer.append(tmp, 0, 0);
116:
117: assertEquals(8, buffer.capacity());
118: assertEquals(6, buffer.length());
119:
120: buffer.append(tmp, 0, 4);
121:
122: assertEquals(16, buffer.capacity());
123: assertEquals(10, buffer.length());
124:
125: assertEquals("1212341234", buffer.toString());
126: }
127:
128: public void testAppendString() throws Exception {
129: CharArrayBuffer buffer = new CharArrayBuffer(8);
130: buffer.append("stuff");
131: buffer.append(" and more stuff");
132: assertEquals("stuff and more stuff", buffer.toString());
133: }
134:
135: public void testAppendNullString() throws Exception {
136: CharArrayBuffer buffer = new CharArrayBuffer(8);
137: buffer.append((String) null);
138: assertEquals("null", buffer.toString());
139: }
140:
141: public void testAppendCharArrayBuffer() throws Exception {
142: CharArrayBuffer buffer1 = new CharArrayBuffer(8);
143: buffer1.append(" and more stuff");
144: CharArrayBuffer buffer2 = new CharArrayBuffer(8);
145: buffer2.append("stuff");
146: buffer2.append(buffer1);
147: assertEquals("stuff and more stuff", buffer2.toString());
148: }
149:
150: public void testAppendNullCharArrayBuffer() throws Exception {
151: CharArrayBuffer buffer = new CharArrayBuffer(8);
152: buffer.append((CharArrayBuffer) null);
153: buffer.append((CharArrayBuffer) null, 0, 0);
154: assertEquals("", buffer.toString());
155: }
156:
157: public void testAppendSingleChar() throws Exception {
158: CharArrayBuffer buffer = new CharArrayBuffer(4);
159: buffer.append('1');
160: buffer.append('2');
161: buffer.append('3');
162: buffer.append('4');
163: buffer.append('5');
164: buffer.append('6');
165: assertEquals("123456", buffer.toString());
166: }
167:
168: public void testInvalidCharArrayAppend() throws Exception {
169: CharArrayBuffer buffer = new CharArrayBuffer(4);
170: buffer.append((char[]) null, 0, 0);
171:
172: char[] tmp = new char[] { '1', '2', '3', '4' };
173: try {
174: buffer.append(tmp, -1, 0);
175: fail("IndexOutOfBoundsException should have been thrown");
176: } catch (IndexOutOfBoundsException ex) {
177: // expected
178: }
179: try {
180: buffer.append(tmp, 0, -1);
181: fail("IndexOutOfBoundsException should have been thrown");
182: } catch (IndexOutOfBoundsException ex) {
183: // expected
184: }
185: try {
186: buffer.append(tmp, 0, 8);
187: fail("IndexOutOfBoundsException should have been thrown");
188: } catch (IndexOutOfBoundsException ex) {
189: // expected
190: }
191: try {
192: buffer.append(tmp, 10, Integer.MAX_VALUE);
193: fail("IndexOutOfBoundsException should have been thrown");
194: } catch (IndexOutOfBoundsException ex) {
195: // expected
196: }
197: try {
198: buffer.append(tmp, 2, 4);
199: fail("IndexOutOfBoundsException should have been thrown");
200: } catch (IndexOutOfBoundsException ex) {
201: // expected
202: }
203: }
204:
205: public void testSetLength() throws Exception {
206: CharArrayBuffer buffer = new CharArrayBuffer(4);
207: buffer.setLength(2);
208: assertEquals(2, buffer.length());
209: }
210:
211: public void testSetInvalidLength() throws Exception {
212: CharArrayBuffer buffer = new CharArrayBuffer(4);
213: try {
214: buffer.setLength(-2);
215: fail("IndexOutOfBoundsException should have been thrown");
216: } catch (IndexOutOfBoundsException ex) {
217: // expected
218: }
219: try {
220: buffer.setLength(200);
221: fail("IndexOutOfBoundsException should have been thrown");
222: } catch (IndexOutOfBoundsException ex) {
223: // expected
224: }
225: }
226:
227: public void testEnsureCapacity() throws Exception {
228: CharArrayBuffer buffer = new CharArrayBuffer(4);
229: buffer.ensureCapacity(2);
230: assertEquals(4, buffer.capacity());
231: buffer.ensureCapacity(8);
232: assertEquals(8, buffer.capacity());
233: }
234:
235: public void testIndexOf() {
236: CharArrayBuffer buffer = new CharArrayBuffer(16);
237: buffer.append("name: value");
238: assertEquals(4, buffer.indexOf(':'));
239: assertEquals(-1, buffer.indexOf(','));
240: assertEquals(4, buffer.indexOf(':', -1, 11));
241: assertEquals(4, buffer.indexOf(':', 0, 1000));
242: assertEquals(-1, buffer.indexOf(':', 2, 1));
243: }
244:
245: public void testSubstring() {
246: CharArrayBuffer buffer = new CharArrayBuffer(16);
247: buffer.append(" name: value ");
248: assertEquals(5, buffer.indexOf(':'));
249: assertEquals(" name", buffer.substring(0, 5));
250: assertEquals(" value ", buffer
251: .substring(6, buffer.length()));
252: assertEquals("name", buffer.substringTrimmed(0, 5));
253: assertEquals("value", buffer.substringTrimmed(6, buffer
254: .length()));
255: assertEquals("", buffer.substringTrimmed(13, buffer.length()));
256: }
257:
258: public void testSubstringIndexOfOutBound() {
259: CharArrayBuffer buffer = new CharArrayBuffer(16);
260: buffer.append("stuff");
261: try {
262: buffer.substring(-2, 10);
263: fail("IndexOutOfBoundsException should have been thrown");
264: } catch (IndexOutOfBoundsException ex) {
265: // expected
266: }
267: try {
268: buffer.substringTrimmed(-2, 10);
269: fail("IndexOutOfBoundsException should have been thrown");
270: } catch (IndexOutOfBoundsException ex) {
271: // expected
272: }
273: try {
274: buffer.substring(12, 10);
275: fail("IndexOutOfBoundsException should have been thrown");
276: } catch (IndexOutOfBoundsException ex) {
277: // expected
278: }
279: try {
280: buffer.substringTrimmed(12, 10);
281: fail("IndexOutOfBoundsException should have been thrown");
282: } catch (IndexOutOfBoundsException ex) {
283: // expected
284: }
285: try {
286: buffer.substring(2, 1);
287: fail("IndexOutOfBoundsException should have been thrown");
288: } catch (IndexOutOfBoundsException ex) {
289: // expected
290: }
291: try {
292: buffer.substringTrimmed(2, 1);
293: fail("IndexOutOfBoundsException should have been thrown");
294: } catch (IndexOutOfBoundsException ex) {
295: // expected
296: }
297: }
298:
299: public void testAppendAsciiByteArray() throws Exception {
300: String s1 = "stuff";
301: String s2 = " and more stuff";
302: byte[] b1 = s1.getBytes("US-ASCII");
303: byte[] b2 = s2.getBytes("US-ASCII");
304:
305: CharArrayBuffer buffer = new CharArrayBuffer(8);
306: buffer.append(b1, 0, b1.length);
307: buffer.append(b2, 0, b2.length);
308:
309: assertEquals("stuff and more stuff", buffer.toString());
310: }
311:
312: public void testAppendISOByteArray() throws Exception {
313: byte[] b = new byte[] { 0x00, 0x20, 0x7F, -0x80, -0x01 };
314:
315: CharArrayBuffer buffer = new CharArrayBuffer(8);
316: buffer.append(b, 0, b.length);
317: char[] ch = buffer.toCharArray();
318: assertNotNull(ch);
319: assertEquals(5, ch.length);
320: assertEquals(0x00, ch[0]);
321: assertEquals(0x20, ch[1]);
322: assertEquals(0x7F, ch[2]);
323: assertEquals(0x80, ch[3]);
324: assertEquals(0xFF, ch[4]);
325: }
326:
327: public void testAppendNullByteArray() throws Exception {
328: CharArrayBuffer buffer = new CharArrayBuffer(8);
329: buffer.append((byte[]) null, 0, 0);
330: assertEquals("", buffer.toString());
331: }
332:
333: public void testAppendNullByteArrayBuffer() throws Exception {
334: CharArrayBuffer buffer = new CharArrayBuffer(8);
335: buffer.append((ByteArrayBuffer) null, 0, 0);
336: assertEquals("", buffer.toString());
337: }
338:
339: public void testInvalidAppendAsciiByteArray() throws Exception {
340: CharArrayBuffer buffer = new CharArrayBuffer(4);
341: buffer.append((byte[]) null, 0, 0);
342:
343: byte[] tmp = new byte[] { '1', '2', '3', '4' };
344: try {
345: buffer.append(tmp, -1, 0);
346: fail("IndexOutOfBoundsException should have been thrown");
347: } catch (IndexOutOfBoundsException ex) {
348: // expected
349: }
350: try {
351: buffer.append(tmp, 0, -1);
352: fail("IndexOutOfBoundsException should have been thrown");
353: } catch (IndexOutOfBoundsException ex) {
354: // expected
355: }
356: try {
357: buffer.append(tmp, 0, 8);
358: fail("IndexOutOfBoundsException should have been thrown");
359: } catch (IndexOutOfBoundsException ex) {
360: // expected
361: }
362: try {
363: buffer.append(tmp, 10, Integer.MAX_VALUE);
364: fail("IndexOutOfBoundsException should have been thrown");
365: } catch (IndexOutOfBoundsException ex) {
366: // expected
367: }
368: try {
369: buffer.append(tmp, 2, 4);
370: fail("IndexOutOfBoundsException should have been thrown");
371: } catch (IndexOutOfBoundsException ex) {
372: // expected
373: }
374: }
375:
376: }
|