001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.luni.tests.java.lang;
018:
019: import java.io.Serializable;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.harmony.testframework.serialization.SerializationTest;
024: import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
025:
026: public class StringBufferTest extends TestCase {
027:
028: /**
029: * @tests java.lang.StringBuffer#setLength(int)
030: */
031: public void test_setLengthI() {
032: // Regression for HARMONY-90
033: StringBuffer buffer = new StringBuffer("abcde");
034: try {
035: buffer.setLength(-1);
036: fail("Assert 0: IndexOutOfBoundsException must be thrown");
037: } catch (IndexOutOfBoundsException e) {
038: // expected
039: }
040: }
041:
042: /**
043: * @tests StringBuffer.StringBuffer(CharSequence);
044: */
045: public void test_constructorLjava_lang_CharSequence() {
046: try {
047: new StringBuffer((CharSequence) null);
048: fail("Assert 0: NPE must be thrown.");
049: } catch (NullPointerException e) {
050: }
051:
052: assertEquals("Assert 1: must equal 'abc'.", "abc",
053: new StringBuffer((CharSequence) "abc").toString());
054: }
055:
056: public void test_trimToSize() {
057: StringBuffer buffer = new StringBuffer(25);
058: buffer.append("abc");
059: int origCapacity = buffer.capacity();
060: buffer.trimToSize();
061: int trimCapacity = buffer.capacity();
062: assertTrue("Assert 0: capacity must be smaller.",
063: trimCapacity < origCapacity);
064: assertEquals("Assert 1: length must still be 3", 3, buffer
065: .length());
066: assertEquals("Assert 2: value must still be 'abc'.", "abc",
067: buffer.toString());
068: }
069:
070: /**
071: * @tests java.lang.StringBuffer.append(CharSequence)
072: */
073: public void test_appendLjava_lang_CharSequence() {
074: StringBuffer sb = new StringBuffer();
075: assertSame(sb, sb.append((CharSequence) "ab"));
076: assertEquals("ab", sb.toString());
077: sb.setLength(0);
078: assertSame(sb, sb.append((CharSequence) "cd"));
079: assertEquals("cd", sb.toString());
080: sb.setLength(0);
081: assertSame(sb, sb.append((CharSequence) null));
082: assertEquals("null", sb.toString());
083: }
084:
085: /**
086: * @tests java.lang.StringBuffer.append(CharSequence, int, int)
087: */
088: @SuppressWarnings("cast")
089: public void test_appendLjava_lang_CharSequenceII() {
090: StringBuffer sb = new StringBuffer();
091: assertSame(sb, sb.append((CharSequence) "ab", 0, 2));
092: assertEquals("ab", sb.toString());
093: sb.setLength(0);
094: assertSame(sb, sb.append((CharSequence) "cd", 0, 2));
095: assertEquals("cd", sb.toString());
096: sb.setLength(0);
097: assertSame(sb, sb.append((CharSequence) "abcd", 0, 2));
098: assertEquals("ab", sb.toString());
099: sb.setLength(0);
100: assertSame(sb, sb.append((CharSequence) "abcd", 2, 4));
101: assertEquals("cd", sb.toString());
102: sb.setLength(0);
103: assertSame(sb, sb.append((CharSequence) null, 0, 2));
104: assertEquals("nu", sb.toString());
105: }
106:
107: /**
108: * @tests java.lang.StringBuffer.append(char[], int, int)
109: */
110: public void test_append$CII_2() {
111: StringBuffer obj = new StringBuffer();
112: try {
113: obj.append(new char[0], -1, -1);
114: fail("ArrayIndexOutOfBoundsException expected");
115: } catch (ArrayIndexOutOfBoundsException e) {
116: // expected
117: }
118: }
119:
120: /**
121: * @tests java.lang.StringBuffer.append(char[], int, int)
122: */
123: public void test_append$CII_3() throws Exception {
124: StringBuffer obj = new StringBuffer();
125: try {
126: obj.append((char[]) null, -1, -1);
127: fail("NullPointerException expected");
128: } catch (NullPointerException e) {
129: // expected
130: }
131: }
132:
133: /**
134: * @tests java.lang.StringBuffer.insert(int, CharSequence)
135: */
136: public void test_insertILjava_lang_CharSequence() {
137: final String fixture = "0000";
138: StringBuffer sb = new StringBuffer(fixture);
139: assertSame(sb, sb.insert(0, (CharSequence) "ab"));
140: assertEquals("ab0000", sb.toString());
141: assertEquals(6, sb.length());
142:
143: sb = new StringBuffer(fixture);
144: assertSame(sb, sb.insert(2, (CharSequence) "ab"));
145: assertEquals("00ab00", sb.toString());
146: assertEquals(6, sb.length());
147:
148: sb = new StringBuffer(fixture);
149: assertSame(sb, sb.insert(4, (CharSequence) "ab"));
150: assertEquals("0000ab", sb.toString());
151: assertEquals(6, sb.length());
152:
153: sb = new StringBuffer(fixture);
154: assertSame(sb, sb.insert(4, (CharSequence) null));
155: assertEquals("0000null", sb.toString());
156: assertEquals(8, sb.length());
157:
158: try {
159: sb = new StringBuffer(fixture);
160: sb.insert(-1, (CharSequence) "ab");
161: fail("no IOOBE, negative index");
162: } catch (IndexOutOfBoundsException e) {
163: // Expected
164: }
165:
166: try {
167: sb = new StringBuffer(fixture);
168: sb.insert(5, (CharSequence) "ab");
169: fail("no IOOBE, index too large index");
170: } catch (IndexOutOfBoundsException e) {
171: // Expected
172: }
173: }
174:
175: /**
176: * @tests java.lang.StringBuffer.insert(int, CharSequence, int, int)
177: */
178: @SuppressWarnings("cast")
179: public void test_insertILjava_lang_CharSequenceII() {
180: final String fixture = "0000";
181: StringBuffer sb = new StringBuffer(fixture);
182: assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 2));
183: assertEquals("ab0000", sb.toString());
184: assertEquals(6, sb.length());
185:
186: sb = new StringBuffer(fixture);
187: assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 1));
188: assertEquals("a0000", sb.toString());
189: assertEquals(5, sb.length());
190:
191: sb = new StringBuffer(fixture);
192: assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 2));
193: assertEquals("00ab00", sb.toString());
194: assertEquals(6, sb.length());
195:
196: sb = new StringBuffer(fixture);
197: assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 1));
198: assertEquals("00a00", sb.toString());
199: assertEquals(5, sb.length());
200:
201: sb = new StringBuffer(fixture);
202: assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 2));
203: assertEquals("0000ab", sb.toString());
204: assertEquals(6, sb.length());
205:
206: sb = new StringBuffer(fixture);
207: assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 1));
208: assertEquals("0000a", sb.toString());
209: assertEquals(5, sb.length());
210:
211: sb = new StringBuffer(fixture);
212: assertSame(sb, sb.insert(4, (CharSequence) null, 0, 2));
213: assertEquals("0000nu", sb.toString());
214: assertEquals(6, sb.length());
215:
216: try {
217: sb = new StringBuffer(fixture);
218: sb.insert(-1, (CharSequence) "ab", 0, 2);
219: fail("no IOOBE, negative index");
220: } catch (IndexOutOfBoundsException e) {
221: // Expected
222: }
223:
224: try {
225: sb = new StringBuffer(fixture);
226: sb.insert(5, (CharSequence) "ab", 0, 2);
227: fail("no IOOBE, index too large index");
228: } catch (IndexOutOfBoundsException e) {
229: // Expected
230: }
231:
232: try {
233: sb = new StringBuffer(fixture);
234: sb.insert(5, (CharSequence) "ab", -1, 2);
235: fail("no IOOBE, negative offset");
236: } catch (IndexOutOfBoundsException e) {
237: // Expected
238: }
239:
240: try {
241: sb = new StringBuffer(fixture);
242: sb.insert(5, new char[] { 'a', 'b' }, 0, -1);
243: fail("no IOOBE, negative length");
244: } catch (IndexOutOfBoundsException e) {
245: // Expected
246: }
247:
248: try {
249: sb = new StringBuffer(fixture);
250: sb.insert(5, new char[] { 'a', 'b' }, 0, 3);
251: fail("no IOOBE, too long");
252: } catch (IndexOutOfBoundsException e) {
253: // Expected
254: }
255: }
256:
257: /**
258: * @tests java.lang.StringBuffer.insert(int, char)
259: */
260: public void test_insertIC() {
261: StringBuffer obj = new StringBuffer();
262: try {
263: obj.insert(-1, ' ');
264: fail("ArrayIndexOutOfBoundsException expected");
265: } catch (ArrayIndexOutOfBoundsException e) {
266: // expected
267: }
268: }
269:
270: /**
271: * @tests java.lang.StringBuffer.appendCodePoint(int)'
272: */
273: public void test_appendCodePointI() {
274: StringBuffer sb = new StringBuffer();
275: sb.appendCodePoint(0x10000);
276: assertEquals("\uD800\uDC00", sb.toString());
277: sb.append("fixture");
278: assertEquals("\uD800\uDC00fixture", sb.toString());
279: sb.appendCodePoint(0x00010FFFF);
280: assertEquals("\uD800\uDC00fixture\uDBFF\uDFFF", sb.toString());
281: }
282:
283: /**
284: * @tests java.lang.StringBuffer.codePointAt(int)
285: */
286: public void test_codePointAtI() {
287: StringBuffer sb = new StringBuffer("abc");
288: assertEquals('a', sb.codePointAt(0));
289: assertEquals('b', sb.codePointAt(1));
290: assertEquals('c', sb.codePointAt(2));
291:
292: sb = new StringBuffer("\uD800\uDC00");
293: assertEquals(0x10000, sb.codePointAt(0));
294: assertEquals('\uDC00', sb.codePointAt(1));
295:
296: try {
297: sb.codePointAt(-1);
298: fail("No IOOBE on negative index.");
299: } catch (IndexOutOfBoundsException e) {
300:
301: }
302:
303: try {
304: sb.codePointAt(sb.length());
305: fail("No IOOBE on index equal to length.");
306: } catch (IndexOutOfBoundsException e) {
307:
308: }
309:
310: try {
311: sb.codePointAt(sb.length() + 1);
312: fail("No IOOBE on index greater than length.");
313: } catch (IndexOutOfBoundsException e) {
314:
315: }
316: }
317:
318: /**
319: * @tests java.lang.StringBuffer.codePointBefore(int)
320: */
321: public void test_codePointBeforeI() {
322: StringBuffer sb = new StringBuffer("abc");
323: assertEquals('a', sb.codePointBefore(1));
324: assertEquals('b', sb.codePointBefore(2));
325: assertEquals('c', sb.codePointBefore(3));
326:
327: sb = new StringBuffer("\uD800\uDC00");
328: assertEquals(0x10000, sb.codePointBefore(2));
329: assertEquals('\uD800', sb.codePointBefore(1));
330:
331: try {
332: sb.codePointBefore(0);
333: fail("No IOOBE on zero index.");
334: } catch (IndexOutOfBoundsException e) {
335:
336: }
337:
338: try {
339: sb.codePointBefore(-1);
340: fail("No IOOBE on negative index.");
341: } catch (IndexOutOfBoundsException e) {
342:
343: }
344:
345: try {
346: sb.codePointBefore(sb.length() + 1);
347: fail("No IOOBE on index greater than length.");
348: } catch (IndexOutOfBoundsException e) {
349:
350: }
351: }
352:
353: /**
354: * @tests java.lang.StringBuffer.codePointCount(int, int)
355: */
356: public void test_codePointCountII() {
357: assertEquals(1, new StringBuffer("\uD800\uDC00")
358: .codePointCount(0, 2));
359: assertEquals(1, new StringBuffer("\uD800\uDC01")
360: .codePointCount(0, 2));
361: assertEquals(1, new StringBuffer("\uD801\uDC01")
362: .codePointCount(0, 2));
363: assertEquals(1, new StringBuffer("\uDBFF\uDFFF")
364: .codePointCount(0, 2));
365:
366: assertEquals(3, new StringBuffer("a\uD800\uDC00b")
367: .codePointCount(0, 4));
368: assertEquals(4, new StringBuffer("a\uD800\uDC00b\uD800")
369: .codePointCount(0, 5));
370:
371: StringBuffer sb = new StringBuffer("abc");
372: try {
373: sb.codePointCount(-1, 2);
374: fail("No IOOBE for negative begin index.");
375: } catch (IndexOutOfBoundsException e) {
376:
377: }
378:
379: try {
380: sb.codePointCount(0, 4);
381: fail("No IOOBE for end index that's too large.");
382: } catch (IndexOutOfBoundsException e) {
383:
384: }
385:
386: try {
387: sb.codePointCount(3, 2);
388: fail("No IOOBE for begin index larger than end index.");
389: } catch (IndexOutOfBoundsException e) {
390:
391: }
392: }
393:
394: /**
395: * @tests java.lang.StringBuffer.getChars(int, int, char[], int)
396: */
397: public void test_getCharsII$CI() {
398: StringBuffer obj = new StringBuffer();
399: try {
400: obj.getChars(0, 0, new char[0], -1);
401: fail("ArrayIndexOutOfBoundsException expected");
402: } catch (ArrayIndexOutOfBoundsException e) {
403: // expected
404: }
405: }
406:
407: /**
408: * @tests java.lang.StringBuffer.offsetByCodePoints(int, int)'
409: */
410: public void test_offsetByCodePointsII() {
411: int result = new StringBuffer("a\uD800\uDC00b")
412: .offsetByCodePoints(0, 2);
413: assertEquals(3, result);
414:
415: result = new StringBuffer("abcd").offsetByCodePoints(3, -1);
416: assertEquals(2, result);
417:
418: result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(
419: 0, 3);
420: assertEquals(4, result);
421:
422: result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(
423: 3, -1);
424: assertEquals(1, result);
425:
426: result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(
427: 3, 0);
428: assertEquals(3, result);
429:
430: result = new StringBuffer("\uD800\uDC00bc").offsetByCodePoints(
431: 3, 0);
432: assertEquals(3, result);
433:
434: result = new StringBuffer("a\uDC00bc")
435: .offsetByCodePoints(3, -1);
436: assertEquals(2, result);
437:
438: result = new StringBuffer("a\uD800bc")
439: .offsetByCodePoints(3, -1);
440: assertEquals(2, result);
441:
442: StringBuffer sb = new StringBuffer("abc");
443: try {
444: sb.offsetByCodePoints(-1, 1);
445: fail("No IOOBE for negative index.");
446: } catch (IndexOutOfBoundsException e) {
447:
448: }
449:
450: try {
451: sb.offsetByCodePoints(0, 4);
452: fail("No IOOBE for offset that's too large.");
453: } catch (IndexOutOfBoundsException e) {
454:
455: }
456:
457: try {
458: sb.offsetByCodePoints(3, -4);
459: fail("No IOOBE for offset that's too small.");
460: } catch (IndexOutOfBoundsException e) {
461:
462: }
463:
464: try {
465: sb.offsetByCodePoints(3, 1);
466: fail("No IOOBE for index that's too large.");
467: } catch (IndexOutOfBoundsException e) {
468:
469: }
470:
471: try {
472: sb.offsetByCodePoints(4, -1);
473: fail("No IOOBE for index that's too large.");
474: } catch (IndexOutOfBoundsException e) {
475:
476: }
477: }
478:
479: /**
480: * @tests {@link java.lang.StringBuffer#indexOf(String, int)}
481: */
482: @SuppressWarnings("nls")
483: public void test_IndexOfStringInt() {
484: final String fixture = "0123456789";
485: StringBuffer sb = new StringBuffer(fixture);
486: assertEquals(0, sb.indexOf("0"));
487: assertEquals(0, sb.indexOf("012"));
488: assertEquals(-1, sb.indexOf("02"));
489: assertEquals(8, sb.indexOf("89"));
490:
491: assertEquals(0, sb.indexOf("0"), 0);
492: assertEquals(0, sb.indexOf("012"), 0);
493: assertEquals(-1, sb.indexOf("02"), 0);
494: assertEquals(8, sb.indexOf("89"), 0);
495:
496: assertEquals(-1, sb.indexOf("0"), 5);
497: assertEquals(-1, sb.indexOf("012"), 5);
498: assertEquals(-1, sb.indexOf("02"), 0);
499: assertEquals(8, sb.indexOf("89"), 5);
500:
501: try {
502: sb.indexOf(null, 0);
503: fail("Should throw a NullPointerExceptionE");
504: } catch (NullPointerException e) {
505: // Expected
506: }
507: }
508:
509: /**
510: * @tests {@link java.lang.StringBuffer#lastIndexOf(String, int)}
511: */
512: @SuppressWarnings("nls")
513: public void test_lastIndexOfLjava_lang_StringI() {
514: final String fixture = "0123456789";
515: StringBuffer sb = new StringBuffer(fixture);
516: assertEquals(0, sb.lastIndexOf("0"));
517: assertEquals(0, sb.lastIndexOf("012"));
518: assertEquals(-1, sb.lastIndexOf("02"));
519: assertEquals(8, sb.lastIndexOf("89"));
520:
521: assertEquals(0, sb.lastIndexOf("0"), 0);
522: assertEquals(0, sb.lastIndexOf("012"), 0);
523: assertEquals(-1, sb.lastIndexOf("02"), 0);
524: assertEquals(8, sb.lastIndexOf("89"), 0);
525:
526: assertEquals(-1, sb.lastIndexOf("0"), 5);
527: assertEquals(-1, sb.lastIndexOf("012"), 5);
528: assertEquals(-1, sb.lastIndexOf("02"), 0);
529: assertEquals(8, sb.lastIndexOf("89"), 5);
530:
531: try {
532: sb.lastIndexOf(null, 0);
533: fail("Should throw a NullPointerException");
534: } catch (NullPointerException e) {
535: // Expected
536: }
537: }
538:
539: // comparator for StringBuffer objects
540: private static final SerializableAssert STRING_BUFFER_COMPARATOR = new SerializableAssert() {
541: public void assertDeserialized(Serializable initial,
542: Serializable deserialized) {
543:
544: StringBuffer init = (StringBuffer) initial;
545: StringBuffer desr = (StringBuffer) deserialized;
546:
547: // serializable fields are: 'count', 'shared', 'value'
548: // serialization of 'shared' is not verified
549: // 'count' + 'value' should result in required string
550: assertEquals("toString", init.toString(), desr.toString());
551: }
552: };
553:
554: /**
555: * @tests serialization/deserialization.
556: */
557: public void testSerializationSelf() throws Exception {
558:
559: SerializationTest.verifySelf(new StringBuffer("0123456789"),
560: STRING_BUFFER_COMPARATOR);
561: }
562:
563: /**
564: * @tests serialization/deserialization compatibility with RI.
565: */
566: public void testSerializationCompatibility() throws Exception {
567:
568: SerializationTest.verifyGolden(this , new StringBuffer(
569: "0123456789"), STRING_BUFFER_COMPARATOR);
570: }
571: }
|