1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.tests.java.lang;
18
19import java.io.Serializable;
20
21import junit.framework.TestCase;
22
23import org.apache.harmony.testframework.serialization.SerializationTest;
24import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
25
26public class StringBufferTest extends TestCase {
27
28    /**
29     * java.lang.StringBuffer#setLength(int)
30     */
31    public void test_setLengthI() {
32        // Regression for HARMONY-90
33        StringBuffer buffer = new StringBuffer("abcde");
34        try {
35            buffer.setLength(-1);
36            fail("Assert 0: IndexOutOfBoundsException must be thrown");
37        } catch (IndexOutOfBoundsException e) {
38            // expected
39        }
40
41        assertEquals("abcde", buffer.toString());
42        buffer.setLength(1);
43        buffer.append('f');
44        assertEquals("af", buffer.toString());
45
46        buffer = new StringBuffer("abcde");
47        assertEquals("cde", buffer.substring(2));
48        buffer.setLength(3);
49        buffer.append('f');
50        assertEquals("abcf", buffer.toString());
51
52        buffer = new StringBuffer("abcde");
53        buffer.setLength(2);
54        try {
55            buffer.charAt(3);
56            fail("should throw IndexOutOfBoundsException");
57        } catch (IndexOutOfBoundsException e) {
58            // Expected
59        }
60
61        buffer = new StringBuffer();
62        buffer.append("abcdefg");
63        buffer.setLength(2);
64        buffer.setLength(5);
65        for (int i = 2; i < 5; i++) {
66            assertEquals(0, buffer.charAt(i));
67        }
68
69        buffer = new StringBuffer();
70        buffer.append("abcdefg");
71        buffer.delete(2, 4);
72        buffer.setLength(7);
73        assertEquals('a', buffer.charAt(0));
74        assertEquals('b', buffer.charAt(1));
75        assertEquals('e', buffer.charAt(2));
76        assertEquals('f', buffer.charAt(3));
77        assertEquals('g', buffer.charAt(4));
78        for (int i = 5; i < 7; i++) {
79            assertEquals(0, buffer.charAt(i));
80        }
81
82        buffer = new StringBuffer();
83        buffer.append("abcdefg");
84        buffer.replace(2, 5, "z");
85        buffer.setLength(7);
86        for (int i = 5; i < 7; i++) {
87            assertEquals(0, buffer.charAt(i));
88        }
89    }
90
91    /**
92     * java.lang.StringBuffer#toString()
93     */
94    public void test_toString() throws Exception {
95        StringBuffer buffer = new StringBuffer();
96        assertEquals("", buffer.toString());
97
98        buffer.append("abcde");
99        assertEquals("abcde", buffer.toString());
100        buffer.setLength(1000);
101        byte[] bytes = buffer.toString().getBytes("GB18030");
102        for (int i = 5; i < bytes.length; i++) {
103            assertEquals(0, bytes[i]);
104        }
105
106        buffer.setLength(5);
107        buffer.append("fghij");
108        assertEquals("abcdefghij", buffer.toString());
109    }
110
111    /**
112     * StringBuffer.StringBuffer(CharSequence);
113     */
114    public void test_constructorLjava_lang_CharSequence() {
115        try {
116            new StringBuffer((CharSequence) null);
117            fail("Assert 0: NPE must be thrown.");
118        } catch (NullPointerException e) {
119        }
120
121        assertEquals("Assert 1: must equal 'abc'.", "abc", new StringBuffer((CharSequence) "abc").toString());
122    }
123
124    public void test_trimToSize() {
125        StringBuffer buffer = new StringBuffer(25);
126        buffer.append("abc");
127        int origCapacity = buffer.capacity();
128        buffer.trimToSize();
129        int trimCapacity = buffer.capacity();
130        assertTrue("Assert 0: capacity must be smaller.", trimCapacity < origCapacity);
131        assertEquals("Assert 1: length must still be 3", 3, buffer.length());
132        assertEquals("Assert 2: value must still be 'abc'.", "abc", buffer.toString());
133    }
134
135    /**
136     * java.lang.StringBuffer.append(CharSequence)
137     */
138    public void test_appendLjava_lang_CharSequence() {
139        StringBuffer sb = new StringBuffer();
140        assertSame(sb, sb.append((CharSequence) "ab"));
141        assertEquals("ab", sb.toString());
142        sb.setLength(0);
143        assertSame(sb, sb.append((CharSequence) "cd"));
144        assertEquals("cd", sb.toString());
145        sb.setLength(0);
146        assertSame(sb, sb.append((CharSequence) null));
147        assertEquals("null", sb.toString());
148    }
149
150    /**
151     * java.lang.StringBuffer.append(CharSequence, int, int)
152     */
153    @SuppressWarnings("cast")
154    public void test_appendLjava_lang_CharSequenceII() {
155        StringBuffer sb = new StringBuffer();
156        assertSame(sb, sb.append((CharSequence) "ab", 0, 2));
157        assertEquals("ab", sb.toString());
158        sb.setLength(0);
159        assertSame(sb, sb.append((CharSequence) "cd", 0, 2));
160        assertEquals("cd", sb.toString());
161        sb.setLength(0);
162        assertSame(sb, sb.append((CharSequence) "abcd", 0, 2));
163        assertEquals("ab", sb.toString());
164        sb.setLength(0);
165        assertSame(sb, sb.append((CharSequence) "abcd", 2, 4));
166        assertEquals("cd", sb.toString());
167        sb.setLength(0);
168        assertSame(sb, sb.append((CharSequence) null, 0, 2));
169        assertEquals("nu", sb.toString());
170    }
171
172    /**
173     * java.lang.StringBuffer.append(char[], int, int)
174     */
175    public void test_append$CII_2() {
176        StringBuffer obj = new StringBuffer();
177        try {
178            obj.append(new char[0], -1, -1);
179            fail("ArrayIndexOutOfBoundsException expected");
180        } catch (ArrayIndexOutOfBoundsException e) {
181            // expected
182        }
183    }
184
185    /**
186     * java.lang.StringBuffer.append(char[], int, int)
187     */
188    public void test_append$CII_3() throws Exception {
189        StringBuffer obj = new StringBuffer();
190        try {
191            obj.append((char[]) null, -1, -1);
192            fail("NullPointerException expected");
193        } catch (NullPointerException e) {
194            // expected
195        }
196    }
197
198    /**
199     * java.lang.StringBuffer.insert(int, CharSequence)
200     */
201    public void test_insertILjava_lang_CharSequence() {
202        final String fixture = "0000";
203        StringBuffer sb = new StringBuffer(fixture);
204        assertSame(sb, sb.insert(0, (CharSequence) "ab"));
205        assertEquals("ab0000", sb.toString());
206        assertEquals(6, sb.length());
207
208        sb = new StringBuffer(fixture);
209        assertSame(sb, sb.insert(2, (CharSequence) "ab"));
210        assertEquals("00ab00", sb.toString());
211        assertEquals(6, sb.length());
212
213        sb = new StringBuffer(fixture);
214        assertSame(sb, sb.insert(4, (CharSequence) "ab"));
215        assertEquals("0000ab", sb.toString());
216        assertEquals(6, sb.length());
217
218        sb = new StringBuffer(fixture);
219        assertSame(sb, sb.insert(4, (CharSequence) null));
220        assertEquals("0000null", sb.toString());
221        assertEquals(8, sb.length());
222
223        try {
224            sb = new StringBuffer(fixture);
225            sb.insert(-1, (CharSequence) "ab");
226            fail("no IOOBE, negative index");
227        } catch (IndexOutOfBoundsException e) {
228            // Expected
229        }
230
231        try {
232            sb = new StringBuffer(fixture);
233            sb.insert(5, (CharSequence) "ab");
234            fail("no IOOBE, index too large index");
235        } catch (IndexOutOfBoundsException e) {
236            // Expected
237        }
238    }
239
240    /**
241     * java.lang.StringBuffer.insert(int, CharSequence, int, int)
242     */
243    @SuppressWarnings("cast")
244    public void test_insertILjava_lang_CharSequenceII() {
245        final String fixture = "0000";
246        StringBuffer sb = new StringBuffer(fixture);
247        assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 2));
248        assertEquals("ab0000", sb.toString());
249        assertEquals(6, sb.length());
250
251        sb = new StringBuffer(fixture);
252        assertSame(sb, sb.insert(0, (CharSequence) "ab", 0, 1));
253        assertEquals("a0000", sb.toString());
254        assertEquals(5, sb.length());
255
256        sb = new StringBuffer(fixture);
257        assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 2));
258        assertEquals("00ab00", sb.toString());
259        assertEquals(6, sb.length());
260
261        sb = new StringBuffer(fixture);
262        assertSame(sb, sb.insert(2, (CharSequence) "ab", 0, 1));
263        assertEquals("00a00", sb.toString());
264        assertEquals(5, sb.length());
265
266        sb = new StringBuffer(fixture);
267        assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 2));
268        assertEquals("0000ab", sb.toString());
269        assertEquals(6, sb.length());
270
271        sb = new StringBuffer(fixture);
272        assertSame(sb, sb.insert(4, (CharSequence) "ab", 0, 1));
273        assertEquals("0000a", sb.toString());
274        assertEquals(5, sb.length());
275
276        sb = new StringBuffer(fixture);
277        assertSame(sb, sb.insert(4, (CharSequence) null, 0, 2));
278        assertEquals("0000nu", sb.toString());
279        assertEquals(6, sb.length());
280
281        try {
282            sb = new StringBuffer(fixture);
283            sb.insert(-1, (CharSequence) "ab", 0, 2);
284            fail("no IOOBE, negative index");
285        } catch (IndexOutOfBoundsException e) {
286            // Expected
287        }
288
289        try {
290            sb = new StringBuffer(fixture);
291            sb.insert(5, (CharSequence) "ab", 0, 2);
292            fail("no IOOBE, index too large index");
293        } catch (IndexOutOfBoundsException e) {
294            // Expected
295        }
296
297        try {
298            sb = new StringBuffer(fixture);
299            sb.insert(5, (CharSequence) "ab", -1, 2);
300            fail("no IOOBE, negative offset");
301        } catch (IndexOutOfBoundsException e) {
302            // Expected
303        }
304
305        try {
306            sb = new StringBuffer(fixture);
307            sb.insert(5, new char[] { 'a', 'b' }, 0, -1);
308            fail("no IOOBE, negative length");
309        } catch (IndexOutOfBoundsException e) {
310            // Expected
311        }
312
313        try {
314            sb = new StringBuffer(fixture);
315            sb.insert(5, new char[] { 'a', 'b' }, 0, 3);
316            fail("no IOOBE, too long");
317        } catch (IndexOutOfBoundsException e) {
318            // Expected
319        }
320    }
321
322    /**
323     * java.lang.StringBuffer.insert(int, char)
324     */
325    public void test_insertIC() {
326        StringBuffer obj = new StringBuffer();
327        try {
328            obj.insert(-1, ' ');
329            fail("ArrayIndexOutOfBoundsException expected");
330        } catch (ArrayIndexOutOfBoundsException e) {
331            // expected
332        }
333    }
334
335    /**
336     * java.lang.StringBuffer.appendCodePoint(int)'
337     */
338    public void test_appendCodePointI() {
339        StringBuffer sb = new StringBuffer();
340        sb.appendCodePoint(0x10000);
341        assertEquals("\uD800\uDC00", sb.toString());
342        sb.append("fixture");
343        assertEquals("\uD800\uDC00fixture", sb.toString());
344        sb.appendCodePoint(0x00010FFFF);
345        assertEquals("\uD800\uDC00fixture\uDBFF\uDFFF", sb.toString());
346    }
347
348    /**
349     * java.lang.StringBuffer.codePointAt(int)
350     */
351    public void test_codePointAtI() {
352        StringBuffer sb = new StringBuffer("abc");
353        assertEquals('a', sb.codePointAt(0));
354        assertEquals('b', sb.codePointAt(1));
355        assertEquals('c', sb.codePointAt(2));
356
357        sb = new StringBuffer("\uD800\uDC00");
358        assertEquals(0x10000, sb.codePointAt(0));
359        assertEquals('\uDC00', sb.codePointAt(1));
360
361        try {
362            sb.codePointAt(-1);
363            fail("No IOOBE on negative index.");
364        } catch (IndexOutOfBoundsException e) {
365
366        }
367
368        try {
369            sb.codePointAt(sb.length());
370            fail("No IOOBE on index equal to length.");
371        } catch (IndexOutOfBoundsException e) {
372
373        }
374
375        try {
376            sb.codePointAt(sb.length() + 1);
377            fail("No IOOBE on index greater than length.");
378        } catch (IndexOutOfBoundsException e) {
379
380        }
381    }
382
383    /**
384     * java.lang.StringBuffer.codePointBefore(int)
385     */
386    public void test_codePointBeforeI() {
387        StringBuffer sb = new StringBuffer("abc");
388        assertEquals('a', sb.codePointBefore(1));
389        assertEquals('b', sb.codePointBefore(2));
390        assertEquals('c', sb.codePointBefore(3));
391
392        sb = new StringBuffer("\uD800\uDC00");
393        assertEquals(0x10000, sb.codePointBefore(2));
394        assertEquals('\uD800', sb.codePointBefore(1));
395
396        try {
397            sb.codePointBefore(0);
398            fail("No IOOBE on zero index.");
399        } catch (IndexOutOfBoundsException e) {
400
401        }
402
403        try {
404            sb.codePointBefore(-1);
405            fail("No IOOBE on negative index.");
406        } catch (IndexOutOfBoundsException e) {
407
408        }
409
410        try {
411            sb.codePointBefore(sb.length() + 1);
412            fail("No IOOBE on index greater than length.");
413        } catch (IndexOutOfBoundsException e) {
414
415        }
416    }
417
418    /**
419     * java.lang.StringBuffer.codePointCount(int, int)
420     */
421    public void test_codePointCountII() {
422        assertEquals(1, new StringBuffer("\uD800\uDC00").codePointCount(0, 2));
423        assertEquals(1, new StringBuffer("\uD800\uDC01").codePointCount(0, 2));
424        assertEquals(1, new StringBuffer("\uD801\uDC01").codePointCount(0, 2));
425        assertEquals(1, new StringBuffer("\uDBFF\uDFFF").codePointCount(0, 2));
426
427        assertEquals(3, new StringBuffer("a\uD800\uDC00b").codePointCount(0, 4));
428        assertEquals(4, new StringBuffer("a\uD800\uDC00b\uD800").codePointCount(0, 5));
429
430        StringBuffer sb = new StringBuffer("abc");
431        try {
432            sb.codePointCount(-1, 2);
433            fail("No IOOBE for negative begin index.");
434        } catch (IndexOutOfBoundsException e) {
435
436        }
437
438        try {
439            sb.codePointCount(0, 4);
440            fail("No IOOBE for end index that's too large.");
441        } catch (IndexOutOfBoundsException e) {
442
443        }
444
445        try {
446            sb.codePointCount(3, 2);
447            fail("No IOOBE for begin index larger than end index.");
448        } catch (IndexOutOfBoundsException e) {
449
450        }
451    }
452
453    /**
454     * java.lang.StringBuffer.getChars(int, int, char[], int)
455     */
456    public void test_getCharsII$CI() {
457        StringBuffer obj = new StringBuffer();
458        try {
459            obj.getChars(0, 0, new char[0], -1);
460            fail("ArrayIndexOutOfBoundsException expected");
461        } catch (ArrayIndexOutOfBoundsException e) {
462            // expected
463        }
464    }
465
466    /**
467     * java.lang.StringBuffer.offsetByCodePoints(int, int)'
468     */
469    public void test_offsetByCodePointsII() {
470        int result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(0, 2);
471        assertEquals(3, result);
472
473        result = new StringBuffer("abcd").offsetByCodePoints(3, -1);
474        assertEquals(2, result);
475
476        result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(0, 3);
477        assertEquals(4, result);
478
479        result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(3, -1);
480        assertEquals(1, result);
481
482        result = new StringBuffer("a\uD800\uDC00b").offsetByCodePoints(3, 0);
483        assertEquals(3, result);
484
485        result = new StringBuffer("\uD800\uDC00bc").offsetByCodePoints(3, 0);
486        assertEquals(3, result);
487
488        result = new StringBuffer("a\uDC00bc").offsetByCodePoints(3, -1);
489        assertEquals(2, result);
490
491        result = new StringBuffer("a\uD800bc").offsetByCodePoints(3, -1);
492        assertEquals(2, result);
493
494        StringBuffer sb = new StringBuffer("abc");
495        try {
496            sb.offsetByCodePoints(-1, 1);
497            fail("No IOOBE for negative index.");
498        } catch (IndexOutOfBoundsException e) {
499
500        }
501
502        try {
503            sb.offsetByCodePoints(0, 4);
504            fail("No IOOBE for offset that's too large.");
505        } catch (IndexOutOfBoundsException e) {
506
507        }
508
509        try {
510            sb.offsetByCodePoints(3, -4);
511            fail("No IOOBE for offset that's too small.");
512        } catch (IndexOutOfBoundsException e) {
513
514        }
515
516        try {
517            sb.offsetByCodePoints(3, 1);
518            fail("No IOOBE for index that's too large.");
519        } catch (IndexOutOfBoundsException e) {
520
521        }
522
523        try {
524            sb.offsetByCodePoints(4, -1);
525            fail("No IOOBE for index that's too large.");
526        } catch (IndexOutOfBoundsException e) {
527
528        }
529    }
530
531    /**
532     * {@link java.lang.StringBuffer#indexOf(String, int)}
533     */
534    @SuppressWarnings("nls")
535    public void test_IndexOfStringInt() {
536        final String fixture = "0123456789";
537        StringBuffer sb = new StringBuffer(fixture);
538        assertEquals(0, sb.indexOf("0"));
539        assertEquals(0, sb.indexOf("012"));
540        assertEquals(-1, sb.indexOf("02"));
541        assertEquals(8, sb.indexOf("89"));
542
543        assertEquals(0, sb.indexOf("0"), 0);
544        assertEquals(0, sb.indexOf("012"), 0);
545        assertEquals(-1, sb.indexOf("02"), 0);
546        assertEquals(8, sb.indexOf("89"), 0);
547
548        assertEquals(-1, sb.indexOf("0"), 5);
549        assertEquals(-1, sb.indexOf("012"), 5);
550        assertEquals(-1, sb.indexOf("02"), 0);
551        assertEquals(8, sb.indexOf("89"), 5);
552
553        try {
554            sb.indexOf(null, 0);
555            fail("Should throw a NullPointerExceptionE");
556        } catch (NullPointerException e) {
557            // Expected
558        }
559    }
560
561    /**
562     * {@link java.lang.StringBuffer#lastIndexOf(String, int)}
563     */
564    @SuppressWarnings("nls")
565    public void test_lastIndexOfLjava_lang_StringI() {
566        final String fixture = "0123456789";
567        StringBuffer sb = new StringBuffer(fixture);
568        assertEquals(0, sb.lastIndexOf("0"));
569        assertEquals(0, sb.lastIndexOf("012"));
570        assertEquals(-1, sb.lastIndexOf("02"));
571        assertEquals(8, sb.lastIndexOf("89"));
572
573        assertEquals(0, sb.lastIndexOf("0"), 0);
574        assertEquals(0, sb.lastIndexOf("012"), 0);
575        assertEquals(-1, sb.lastIndexOf("02"), 0);
576        assertEquals(8, sb.lastIndexOf("89"), 0);
577
578        assertEquals(-1, sb.lastIndexOf("0"), 5);
579        assertEquals(-1, sb.lastIndexOf("012"), 5);
580        assertEquals(-1, sb.lastIndexOf("02"), 0);
581        assertEquals(8, sb.lastIndexOf("89"), 5);
582
583        try {
584            sb.lastIndexOf(null, 0);
585            fail("Should throw a NullPointerException");
586        } catch (NullPointerException e) {
587            // Expected
588        }
589    }
590
591    // comparator for StringBuffer objects
592    private static final SerializableAssert STRING_BUFFER_COMPARATOR = new SerializableAssert() {
593        public void assertDeserialized(Serializable initial,
594                Serializable deserialized) {
595
596            StringBuffer init = (StringBuffer) initial;
597            StringBuffer desr = (StringBuffer) deserialized;
598
599            // serializable fields are: 'count', 'shared', 'value'
600            // serialization of 'shared' is not verified
601            // 'count' + 'value' should result in required string
602            assertEquals("toString", init.toString(), desr.toString());
603        }
604    };
605
606    /**
607     * serialization/deserialization.
608     */
609    public void testSerializationSelf() throws Exception {
610
611        SerializationTest.verifySelf(new StringBuffer("0123456789"),
612                STRING_BUFFER_COMPARATOR);
613    }
614
615    /**
616     * serialization/deserialization compatibility with RI.
617     */
618    public void testSerializationCompatibility() throws Exception {
619
620        SerializationTest.verifyGolden(this, new StringBuffer("0123456789"),
621                STRING_BUFFER_COMPARATOR);
622    }
623}
624