StringBuffer2Test.java revision e2e14ddec9683c2d5fe244cd8e715ecd390a999b
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.luni.tests.java.lang;
19
20import dalvik.annotation.KnownFailure;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetNew;
24import dalvik.annotation.TestTargetClass;
25
26@TestTargetClass(StringBuffer.class)
27public class StringBuffer2Test extends junit.framework.TestCase {
28
29    StringBuffer testBuffer;
30
31    /**
32     * @tests java.lang.StringBuffer#StringBuffer()
33     */
34    @TestTargetNew(
35        level = TestLevel.COMPLETE,
36        notes = "",
37        method = "StringBuffer",
38        args = {}
39    )
40    public void test_Constructor() {
41        // Test for method java.lang.StringBuffer()
42        new StringBuffer();
43        assertTrue("Invalid buffer created", true);
44    }
45
46    /**
47     * @tests java.lang.StringBuffer#StringBuffer(int)
48     */
49    @TestTargetNew(
50        level = TestLevel.COMPLETE,
51        notes = "",
52        method = "StringBuffer",
53        args = {int.class}
54    )
55    public void test_ConstructorI() {
56        // Test for method java.lang.StringBuffer(int)
57        StringBuffer sb = new StringBuffer(8);
58        assertEquals("Newly constructed buffer is of incorrect length", 0, sb
59                .length());
60    }
61
62    /**
63     * @tests java.lang.StringBuffer#StringBuffer(java.lang.String)
64     */
65    @TestTargetNew(
66        level = TestLevel.COMPLETE,
67        notes = "",
68        method = "StringBuffer",
69        args = {java.lang.String.class}
70    )
71    public void test_ConstructorLjava_lang_String() {
72        // Test for method java.lang.StringBuffer(java.lang.String)
73
74        StringBuffer sb = new StringBuffer("HelloWorld");
75
76        assertTrue("Invalid buffer created", sb.length() == 10
77                && (sb.toString().equals("HelloWorld")));
78
79        boolean pass = false;
80        try {
81            new StringBuffer(null);
82        } catch (NullPointerException e) {
83            pass = true;
84        }
85        assertTrue("Should throw NullPointerException", pass);
86    }
87
88    /**
89     * @tests java.lang.StringBuffer#append(char[])
90     */
91    @TestTargetNew(
92        level = TestLevel.COMPLETE,
93        notes = "",
94        method = "append",
95        args = {char[].class}
96    )
97    public void test_append$C() {
98        // Test for method java.lang.StringBuffer
99        // java.lang.StringBuffer.append(char [])
100        char buf[] = new char[4];
101        "char".getChars(0, 4, buf, 0);
102        testBuffer.append(buf);
103        assertEquals("Append of char[] failed",
104                "This is a test bufferchar", testBuffer.toString());
105    }
106
107    /**
108     * @tests java.lang.StringBuffer#append(char[], int, int)
109     */
110    @TestTargetNew(
111        level = TestLevel.PARTIAL_COMPLETE,
112        notes = "",
113        method = "append",
114        args = {char[].class, int.class, int.class}
115    )
116    public void test_append$CII() {
117        // Test for method java.lang.StringBuffer
118        // java.lang.StringBuffer.append(char [], int, int)
119        StringBuffer sb = new StringBuffer();
120        char[] buf1 = { 'H', 'e', 'l', 'l', 'o' };
121        char[] buf2 = { 'W', 'o', 'r', 'l', 'd' };
122        sb.append(buf1, 0, buf1.length);
123        assertEquals("Buffer is invalid length after append", 5, sb.length());
124        sb.append(buf2, 0, buf2.length);
125        assertEquals("Buffer is invalid length after append", 10, sb.length());
126        assertTrue("Buffer contains invalid chars", (sb.toString()
127                .equals("HelloWorld")));
128    }
129
130    /**
131     * @tests java.lang.StringBuffer#append(char)
132     */
133    @TestTargetNew(
134        level = TestLevel.COMPLETE,
135        notes = "",
136        method = "append",
137        args = {char.class}
138    )
139    public void test_appendC() {
140        // Test for method java.lang.StringBuffer
141        // java.lang.StringBuffer.append(char)
142        StringBuffer sb = new StringBuffer();
143        char buf1 = 'H';
144        char buf2 = 'W';
145        sb.append(buf1);
146        assertEquals("Buffer is invalid length after append", 1, sb.length());
147        sb.append(buf2);
148        assertEquals("Buffer is invalid length after append", 2, sb.length());
149        assertTrue("Buffer contains invalid chars",
150                (sb.toString().equals("HW")));
151    }
152
153    /**
154     * @tests java.lang.StringBuffer#append(double)
155     */
156    @TestTargetNew(
157        level = TestLevel.COMPLETE,
158        notes = "",
159        method = "append",
160        args = {double.class}
161    )
162    public void test_appendD() {
163        // Test for method java.lang.StringBuffer
164        // java.lang.StringBuffer.append(double)
165        StringBuffer sb = new StringBuffer();
166        sb.append(Double.MAX_VALUE);
167        assertEquals("Buffer is invalid length after append", 22, sb.length());
168        assertEquals("Buffer contains invalid characters",
169                "1.7976931348623157E308", sb.toString());
170    }
171
172    /**
173     * @tests java.lang.StringBuffer#append(float)
174     */
175    @TestTargetNew(
176        level = TestLevel.COMPLETE,
177        notes = "",
178        method = "append",
179        args = {float.class}
180    )
181    public void test_appendF() {
182        // Test for method java.lang.StringBuffer
183        // java.lang.StringBuffer.append(float)
184        StringBuffer sb = new StringBuffer();
185        final float floatNum = 900.87654F;
186        sb.append(floatNum);
187        assertTrue("Buffer is invalid length after append: " + sb.length(), sb
188                .length() == String.valueOf(floatNum).length());
189        assertTrue("Buffer contains invalid characters", sb.toString().equals(
190                String.valueOf(floatNum)));
191    }
192
193    /**
194     * @tests java.lang.StringBuffer#append(int)
195     */
196    @TestTargetNew(
197        level = TestLevel.COMPLETE,
198        notes = "",
199        method = "append",
200        args = {int.class}
201    )
202    public void test_appendI() {
203        // Test for method java.lang.StringBuffer
204        // java.lang.StringBuffer.append(int)
205        StringBuffer sb = new StringBuffer();
206        sb.append(9000);
207        assertEquals("Buffer is invalid length after append", 4, sb.length());
208        sb.append(1000);
209        assertEquals("Buffer is invalid length after append", 8, sb.length());
210        assertEquals("Buffer contains invalid characters",
211                "90001000", sb.toString());
212    }
213
214    /**
215     * @tests java.lang.StringBuffer#append(long)
216     */
217    @TestTargetNew(
218        level = TestLevel.COMPLETE,
219        notes = "",
220        method = "append",
221        args = {long.class}
222    )
223    public void test_appendJ() {
224        // Test for method java.lang.StringBuffer
225        // java.lang.StringBuffer.append(long)
226
227        StringBuffer sb = new StringBuffer();
228        long t = 927654321098L;
229        sb.append(t);
230        assertEquals("Buffer is of invlaid length", 12, sb.length());
231        assertEquals("Buffer contains invalid characters",
232                "927654321098", sb.toString());
233    }
234
235    /**
236     * @tests java.lang.StringBuffer#append(java.lang.Object)
237     */
238    @TestTargetNew(
239        level = TestLevel.COMPLETE,
240        notes = "",
241        method = "append",
242        args = {java.lang.Object.class}
243    )
244    public void test_appendLjava_lang_Object() {
245        // Test for method java.lang.StringBuffer
246        // java.lang.StringBuffer.append(java.lang.Object)
247        StringBuffer sb = new StringBuffer();
248        Object obj1 = new Object();
249        Object obj2 = new Object();
250        sb.append(obj1);
251        sb.append(obj2);
252        assertTrue("Buffer contains invalid characters", sb.toString().equals(
253                obj1.toString() + obj2.toString()));
254    }
255
256    /**
257     * @tests java.lang.StringBuffer#append(java.lang.String)
258     */
259    @TestTargetNew(
260        level = TestLevel.COMPLETE,
261        notes = "",
262        method = "append",
263        args = {java.lang.String.class}
264    )
265    public void test_appendLjava_lang_String() {
266        // Test for method java.lang.StringBuffer
267        // java.lang.StringBuffer.append(java.lang.String)
268        StringBuffer sb = new StringBuffer();
269        String buf1 = "Hello";
270        String buf2 = "World";
271        sb.append(buf1);
272        assertEquals("Buffer is invalid length after append", 5, sb.length());
273        sb.append(buf2);
274        assertEquals("Buffer is invalid length after append", 10, sb.length());
275        assertTrue("Buffer contains invalid chars", (sb.toString()
276                .equals("HelloWorld")));
277    }
278
279    /**
280     * @tests java.lang.StringBuffer#append(boolean)
281     */
282    @TestTargetNew(
283        level = TestLevel.COMPLETE,
284        notes = "",
285        method = "append",
286        args = {boolean.class}
287    )
288    public void test_appendZ() {
289        // Test for method java.lang.StringBuffer
290        // java.lang.StringBuffer.append(boolean)
291        StringBuffer sb = new StringBuffer();
292        sb.append(false);
293        assertEquals("Buffer is invalid length after append", 5, sb.length());
294        sb.append(true);
295        assertEquals("Buffer is invalid length after append", 9, sb.length());
296        assertTrue("Buffer is invalid length after append", (sb.toString()
297                .equals("falsetrue")));
298    }
299
300    /**
301     * @tests java.lang.StringBuffer#capacity()
302     */
303    @TestTargetNew(
304        level = TestLevel.COMPLETE,
305        notes = "",
306        method = "capacity",
307        args = {}
308    )
309    public void test_capacity() {
310        // Test for method int java.lang.StringBuffer.capacity()
311        StringBuffer sb = new StringBuffer(10);
312        assertEquals("Returned incorrect capacity", 10, sb.capacity());
313        sb.ensureCapacity(100);
314        assertTrue("Returned incorrect capacity", sb.capacity() >= 100);
315    }
316
317    /**
318     * @tests java.lang.StringBuffer#charAt(int)
319     */
320    @TestTargetNew(
321        level = TestLevel.COMPLETE,
322        notes = "",
323        method = "charAt",
324        args = {int.class}
325    )
326    public void test_charAtI() {
327        // Test for method char java.lang.StringBuffer.charAt(int)
328        assertEquals("Returned incorrect char", 's', testBuffer.charAt(3));
329
330        // Test for StringIndexOutOfBoundsException
331        boolean exception = false;
332        try {
333            testBuffer.charAt(-1);
334        } catch (StringIndexOutOfBoundsException e) {
335            exception = true;
336        } catch (ArrayIndexOutOfBoundsException e) {
337        }
338        assertTrue("Should throw StringIndexOutOfBoundsException", exception);
339    }
340
341    /**
342     * @tests java.lang.StringBuffer#delete(int, int)
343     */
344    @TestTargetNew(
345        level = TestLevel.COMPLETE,
346        notes = "",
347        method = "delete",
348        args = {int.class, int.class}
349    )
350    public void test_deleteII() {
351        // Test for method java.lang.StringBuffer
352        // java.lang.StringBuffer.delete(int, int)
353        testBuffer.delete(7, 7);
354        assertEquals("Deleted chars when start == end", "This is a test buffer", testBuffer.toString()
355                );
356        testBuffer.delete(4, 14);
357        assertEquals("Deleted incorrect chars",
358                "This buffer", testBuffer.toString());
359
360        testBuffer = new StringBuffer("This is a test buffer");
361        String sharedStr = testBuffer.toString();
362        testBuffer.delete(0, testBuffer.length());
363        assertEquals("Didn't clone shared buffer", "This is a test buffer", sharedStr
364                );
365        assertTrue("Deleted incorrect chars", testBuffer.toString().equals(""));
366        testBuffer.append("more stuff");
367        assertEquals("Didn't clone shared buffer 2", "This is a test buffer", sharedStr
368                );
369        assertEquals("Wrong contents", "more stuff", testBuffer.toString());
370        try {
371            testBuffer.delete(-5, 2);
372        } catch (IndexOutOfBoundsException e) {
373        }
374        assertEquals("Wrong contents 2",
375                "more stuff", testBuffer.toString());
376    }
377
378    /**
379     * @tests java.lang.StringBuffer#deleteCharAt(int)
380     */
381    @TestTargetNew(
382        level = TestLevel.COMPLETE,
383        notes = "",
384        method = "deleteCharAt",
385        args = {int.class}
386    )
387    public void test_deleteCharAtI() {
388        // Test for method java.lang.StringBuffer
389        // java.lang.StringBuffer.deleteCharAt(int)
390        testBuffer.deleteCharAt(3);
391        assertEquals("Deleted incorrect char",
392                "Thi is a test buffer", testBuffer.toString());
393        try {
394            testBuffer.deleteCharAt(testBuffer.length() + 1);
395            fail("StringIndexOutOfBoundsException was not thrown.");
396        } catch(StringIndexOutOfBoundsException sioobe) {
397            //expected
398        }
399
400        try {
401            testBuffer.deleteCharAt(-1);
402            fail("StringIndexOutOfBoundsException was not thrown.");
403        } catch(StringIndexOutOfBoundsException sioobe) {
404            //expected
405        }
406    }
407
408    /**
409     * @tests java.lang.StringBuffer#ensureCapacity(int)
410     */
411    @TestTargetNew(
412        level = TestLevel.COMPLETE,
413        notes = "",
414        method = "ensureCapacity",
415        args = {int.class}
416    )
417    public void test_ensureCapacityI() {
418        // Test for method void java.lang.StringBuffer.ensureCapacity(int)
419        StringBuffer sb = new StringBuffer(10);
420
421        sb.ensureCapacity(-2);
422        assertEquals("Failed to increase capacity.", 10, sb.capacity());
423
424        sb.ensureCapacity(100);
425        assertTrue("Failed to increase capacity", sb.capacity() >= 100);
426
427        try {
428            sb.ensureCapacity(Integer.MAX_VALUE);
429            fail("OutOfMemoryError should be thrown.");
430        } catch(java.lang.OutOfMemoryError oome) {
431            //expected
432        }
433    }
434
435    /**
436     * @tests java.lang.StringBuffer#getChars(int, int, char[], int)
437     */
438    @TestTargetNew(
439        level = TestLevel.PARTIAL_COMPLETE,
440        notes = "Doesn't check exceptions.",
441        method = "getChars",
442        args = {int.class, int.class, char[].class, int.class}
443    )
444    public void test_getCharsII$CI() {
445        // Test for method void java.lang.StringBuffer.getChars(int, int, char
446        // [], int)
447
448        char[] buf = new char[10];
449        testBuffer.getChars(4, 8, buf, 2);
450        assertTrue("Returned incorrect chars", new String(buf, 2, 4)
451                .equals(testBuffer.toString().substring(4, 8)));
452
453        StringBuffer buf2 = new StringBuffer("");
454        try {
455            buf2.getChars(-1, 0, new char[5], 2);
456            fail("IndexOutOfBoundsException is not thrown.");
457        } catch (IndexOutOfBoundsException e) {
458            //expected
459        }
460
461        try {
462            buf2.getChars(0, -1, new char[5], 2);
463            fail("IndexOutOfBoundsException is not thrown.");
464        } catch (IndexOutOfBoundsException e) {
465            //expected
466        }
467
468        try {
469            buf2.getChars(0, -1, new char[5], 2);
470            fail("IndexOutOfBoundsException is not thrown.");
471        } catch (IndexOutOfBoundsException e) {
472            //expected
473        }
474
475        try {
476            buf2.getChars(2, 1, new char[5], 2);
477            fail("IndexOutOfBoundsException is not thrown.");
478        } catch (IndexOutOfBoundsException e) {
479            //expected
480        }
481
482        try {
483            buf2.getChars(0, 6, new char[5], 2);
484            fail("IndexOutOfBoundsException is not thrown.");
485        } catch (IndexOutOfBoundsException e) {
486            //expected
487        }
488
489        try {
490            buf2.getChars(0, 6, new char[10], 5);
491            fail("IndexOutOfBoundsException is not thrown.");
492        } catch (IndexOutOfBoundsException e) {
493            //expected
494        }
495    }
496
497    /**
498     * @tests java.lang.StringBuffer#insert(int, char[])
499     */
500    @TestTargetNew(
501        level = TestLevel.COMPLETE,
502        notes = "",
503        method = "insert",
504        args = {int.class, char[].class}
505    )
506    public void test_insertI$C() {
507        // Test for method java.lang.StringBuffer
508        // java.lang.StringBuffer.insert(int, char [])
509        char buf[] = new char[4];
510        "char".getChars(0, 4, buf, 0);
511        testBuffer.insert(15, buf);
512        assertEquals("Insert test failed",
513                "This is a test charbuffer", testBuffer.toString());
514
515        boolean exception = false;
516        StringBuffer buf1 = new StringBuffer("abcd");
517        try {
518            buf1.insert(-1, (char[]) null);
519        } catch (StringIndexOutOfBoundsException e) {
520            exception = true;
521        } catch (NullPointerException e) {
522        }
523        assertTrue("Should throw StringIndexOutOfBoundsException", exception);
524    }
525
526    /**
527     * @tests java.lang.StringBuffer#insert(int, char[], int, int)
528     */
529    @TestTargetNew(
530        level = TestLevel.COMPLETE,
531        notes = "",
532        method = "insert",
533        args = {int.class, char[].class, int.class, int.class}
534    )
535    public void test_insertI$CII() {
536        // Test for method java.lang.StringBuffer
537        // java.lang.StringBuffer.insert(int, char [], int, int)
538        char[] c = new char[] { 'n', 'o', 't', ' ' };
539        testBuffer.insert(8, c, 0, 4);
540        assertEquals("This is not a test buffer", testBuffer.toString());
541
542        StringBuffer buf1 = new StringBuffer("abcd");
543        try {
544            buf1.insert(-1, (char[]) null, 0, 0);
545            fail("Should throw StringIndexOutOfBoundsException");
546        } catch (StringIndexOutOfBoundsException e) {
547            //expected
548        }
549
550        try {
551            testBuffer.insert(testBuffer.length() - 1, c, -1, 1);
552        } catch (StringIndexOutOfBoundsException e) {
553            //expected
554        }
555
556    }
557
558    /**
559     * @tests java.lang.StringBuffer#insert(int, char)
560     */
561    @TestTargetNew(
562        level = TestLevel.PARTIAL_COMPLETE,
563        notes = "IndexOutOfBoundsException is not verified.",
564        method = "insert",
565        args = {int.class, char.class}
566    )
567    public void test_insertIC() {
568        // Test for method java.lang.StringBuffer
569        // java.lang.StringBuffer.insert(int, char)
570        testBuffer.insert(15, 'T');
571        assertEquals("Insert test failed",
572                "This is a test Tbuffer", testBuffer.toString());
573    }
574
575    /**
576     * @tests java.lang.StringBuffer#insert(int, double)
577     */
578    @TestTargetNew(
579        level = TestLevel.COMPLETE,
580        notes = "",
581        method = "insert",
582        args = {int.class, double.class}
583    )
584    public void test_insertID() {
585        // Test for method java.lang.StringBuffer
586        // java.lang.StringBuffer.insert(int, double)
587        testBuffer.insert(15, Double.MAX_VALUE);
588        assertTrue("Insert test failed", testBuffer.toString().equals(
589                "This is a test " + Double.MAX_VALUE + "buffer"));
590        try {
591            testBuffer.insert(-1, Double.MAX_VALUE);
592            fail("StringIndexOutOfBoundsException is not thrown.");
593        } catch(StringIndexOutOfBoundsException sioobe) {
594            //expected
595        }
596
597        try {
598            testBuffer.insert(testBuffer.length() + 1, Double.MAX_VALUE);
599            fail("StringIndexOutOfBoundsException is not thrown.");
600        } catch(StringIndexOutOfBoundsException sioobe) {
601            //expected
602        }
603    }
604
605    /**
606     * @tests java.lang.StringBuffer#insert(int, float)
607     */
608    @TestTargetNew(
609        level = TestLevel.COMPLETE,
610        notes = "",
611        method = "insert",
612        args = {int.class, float.class}
613    )
614    public void test_insertIF() {
615        // Test for method java.lang.StringBuffer
616        // java.lang.StringBuffer.insert(int, float)
617        testBuffer.insert(15, Float.MAX_VALUE);
618        String testBufferString = testBuffer.toString();
619        String expectedResult = "This is a test "
620                + String.valueOf(Float.MAX_VALUE) + "buffer";
621        assertTrue("Insert test failed, got: " + "\'" + testBufferString + "\'"
622                + " but wanted: " + "\'" + expectedResult + "\'",
623                testBufferString.equals(expectedResult));
624
625        try {
626            testBuffer.insert(-1, Float.MAX_VALUE);
627            fail("StringIndexOutOfBoundsException is not thrown.");
628        } catch(StringIndexOutOfBoundsException sioobe) {
629            //expected
630        }
631
632        try {
633            testBuffer.insert(testBuffer.length() + 1, Float.MAX_VALUE);
634            fail("StringIndexOutOfBoundsException is not thrown.");
635        } catch(StringIndexOutOfBoundsException sioobe) {
636            //expected
637        }
638    }
639
640    /**
641     * @tests java.lang.StringBuffer#insert(int, int)
642     */
643    @TestTargetNew(
644        level = TestLevel.COMPLETE,
645        notes = "",
646        method = "insert",
647        args = {int.class, int.class}
648    )
649    public void test_insertII() {
650        // Test for method java.lang.StringBuffer
651        // java.lang.StringBuffer.insert(int, int)
652        testBuffer.insert(15, 100);
653        assertEquals("Insert test failed",
654                "This is a test 100buffer", testBuffer.toString());
655
656        try {
657            testBuffer.insert(-1, Integer.MAX_VALUE);
658            fail("StringIndexOutOfBoundsException is not thrown.");
659        } catch(StringIndexOutOfBoundsException sioobe) {
660            //expected
661        }
662
663        try {
664            testBuffer.insert(testBuffer.length() + 1, Integer.MAX_VALUE);
665            fail("StringIndexOutOfBoundsException is not thrown.");
666        } catch(StringIndexOutOfBoundsException sioobe) {
667            //expected
668        }
669    }
670
671    /**
672     * @tests java.lang.StringBuffer#insert(int, long)
673     */
674    @TestTargetNew(
675        level = TestLevel.COMPLETE,
676        notes = "",
677        method = "insert",
678        args = {int.class, long.class}
679    )
680    public void test_insertIJ() {
681        // Test for method java.lang.StringBuffer
682        // java.lang.StringBuffer.insert(int, long)
683        testBuffer.insert(15, 88888888888888888L);
684        assertEquals("Insert test failed",
685                "This is a test 88888888888888888buffer", testBuffer.toString());
686
687        try {
688            testBuffer.insert(-1, Long.MAX_VALUE);
689            fail("StringIndexOutOfBoundsException is not thrown.");
690        } catch(StringIndexOutOfBoundsException sioobe) {
691            //expected
692        }
693
694        try {
695            testBuffer.insert(testBuffer.length() + 1, Long.MAX_VALUE);
696            fail("StringIndexOutOfBoundsException is not thrown.");
697        } catch(StringIndexOutOfBoundsException sioobe) {
698            //expected
699        }
700    }
701
702    /**
703     * @tests java.lang.StringBuffer#insert(int, java.lang.Object)
704     */
705    @TestTargetNew(
706        level = TestLevel.COMPLETE,
707        notes = "",
708        method = "insert",
709        args = {int.class, java.lang.Object.class}
710    )
711    public void test_insertILjava_lang_Object() {
712        // Test for method java.lang.StringBuffer
713        // java.lang.StringBuffer.insert(int, java.lang.Object)
714        Object obj1 = new Object();
715        testBuffer.insert(15, obj1);
716        assertTrue("Insert test failed", testBuffer.toString().equals(
717                "This is a test " + obj1.toString() + "buffer"));
718
719        try {
720            testBuffer.insert(-1, obj1);
721            fail("StringIndexOutOfBoundsException is not thrown.");
722        } catch(StringIndexOutOfBoundsException sioobe) {
723            //expected
724        }
725
726        try {
727            testBuffer.insert(testBuffer.length() + 1, obj1);
728            fail("StringIndexOutOfBoundsException is not thrown.");
729        } catch(StringIndexOutOfBoundsException sioobe) {
730            //expected
731        }
732    }
733
734    /**
735     * @tests java.lang.StringBuffer#insert(int, java.lang.String)
736     */
737    @TestTargetNew(
738        level = TestLevel.COMPLETE,
739        notes = "",
740        method = "insert",
741        args = {int.class, java.lang.String.class}
742    )
743    public void test_insertILjava_lang_String() {
744        // Test for method java.lang.StringBuffer
745        // java.lang.StringBuffer.insert(int, java.lang.String)
746
747        testBuffer.insert(15, "STRING ");
748        assertEquals("Insert test failed",
749                "This is a test STRING buffer", testBuffer.toString());
750
751        try {
752            testBuffer.insert(-1, "");
753            fail("StringIndexOutOfBoundsException is not thrown.");
754        } catch(StringIndexOutOfBoundsException sioobe) {
755            //expected
756        }
757
758        try {
759            testBuffer.insert(testBuffer.length() + 1, "");
760            fail("StringIndexOutOfBoundsException is not thrown.");
761        } catch(StringIndexOutOfBoundsException sioobe) {
762            //expected
763        }
764    }
765
766    /**
767     * @tests java.lang.StringBuffer#insert(int, boolean)
768     */
769    @TestTargetNew(
770        level = TestLevel.COMPLETE,
771        notes = "",
772        method = "insert",
773        args = {int.class, boolean.class}
774    )
775    public void test_insertIZ() {
776        // Test for method java.lang.StringBuffer
777        // java.lang.StringBuffer.insert(int, boolean)
778        testBuffer.insert(15, true);
779        assertEquals("Insert test failed",
780                "This is a test truebuffer", testBuffer.toString());
781        try {
782            testBuffer.insert(testBuffer.length() + 1, true);
783            fail("StringIndexOutOfBoundsException is not thrown.");
784        } catch(StringIndexOutOfBoundsException sioobe) {
785            //expected
786        }
787
788        try {
789            testBuffer.insert(-1, true);
790            fail("StringIndexOutOfBoundsException is not thrown.");
791        } catch(StringIndexOutOfBoundsException sioobe) {
792            //expected
793        }
794    }
795
796    /**
797     * @tests java.lang.StringBuffer#length()
798     */
799    @TestTargetNew(
800        level = TestLevel.COMPLETE,
801        notes = "",
802        method = "length",
803        args = {}
804    )
805    public void test_length() {
806        // Test for method int java.lang.StringBuffer.length()
807        assertEquals("Incorrect length returned", 21, testBuffer.length());
808    }
809
810    /**
811     * @tests java.lang.StringBuffer#replace(int, int, java.lang.String)
812     */
813    @TestTargetNew(
814        level = TestLevel.COMPLETE,
815        notes = "",
816        method = "replace",
817        args = {int.class, int.class, java.lang.String.class}
818    )
819    public void test_replaceIILjava_lang_String() {
820        // Test for method java.lang.StringBuffer
821        // java.lang.StringBuffer.replace(int, int, java.lang.String)
822        testBuffer.replace(5, 9, "is a replaced");
823        assertTrue("Replace failed, wanted: " + "\'"
824                + "This is a replaced test buffer" + "\'" + " but got: " + "\'"
825                + testBuffer.toString() + "\'", testBuffer.toString().equals(
826                "This is a replaced test buffer"));
827        assertEquals("insert1", "text", new StringBuffer().replace(0, 0, "text")
828                .toString());
829        assertEquals("insert2", "123text", new StringBuffer("123").replace(3, 3, "text")
830                .toString());
831        assertEquals("insert2", "1text23", new StringBuffer("123").replace(1, 1, "text")
832                .toString());
833
834        try {
835            testBuffer.replace(-1, 0, "text");
836            fail("StringIndexOutOfBoundsException is not thrown.");
837        } catch(StringIndexOutOfBoundsException sioobe) {
838            //expected
839        }
840
841        try {
842            testBuffer.replace(0, -1, "text");
843            fail("StringIndexOutOfBoundsException is not thrown.");
844        } catch(StringIndexOutOfBoundsException sioobe) {
845            //expected
846        }
847
848        try {
849            testBuffer.replace(2, 1, "text");
850            fail("StringIndexOutOfBoundsException is not thrown.");
851        } catch(StringIndexOutOfBoundsException sioobe) {
852            //expected
853        }
854
855        try {
856            testBuffer.replace(testBuffer.length() + 1, testBuffer.length() + 1,
857                    "text");
858            fail("StringIndexOutOfBoundsException is not thrown.");
859        } catch(StringIndexOutOfBoundsException sioobe) {
860            //expected
861        }
862    }
863
864    private String writeString(String in) {
865        StringBuffer result = new StringBuffer();
866        result.append("\"");
867        for (int i = 0; i < in.length(); i++) {
868            result.append(" 0x" + Integer.toHexString(in.charAt(i)));
869        }
870        result.append("\"");
871        return result.toString();
872    }
873
874    private void reverseTest(String id, String org, String rev, String back) {
875        // create non-shared StringBuffer
876        StringBuffer sb = new StringBuffer(org);
877        sb.reverse();
878        String reversed = sb.toString();
879        assertTrue("reversed surrogate " + id + ": " + writeString(reversed),
880                reversed.equals(rev));
881        // create non-shared StringBuffer
882        sb = new StringBuffer(reversed);
883        sb.reverse();
884        reversed = sb.toString();
885        assertTrue("reversed surrogate " + id + "a: " + writeString(reversed),
886                reversed.equals(back));
887
888        // test algorithm when StringBuffer is shared
889        sb = new StringBuffer(org);
890        String copy = sb.toString();
891        assertEquals(org, copy);
892        sb.reverse();
893        reversed = sb.toString();
894        assertTrue("reversed surrogate " + id + ": " + writeString(reversed),
895                reversed.equals(rev));
896        sb = new StringBuffer(reversed);
897        copy = sb.toString();
898        assertEquals(rev, copy);
899        sb.reverse();
900        reversed = sb.toString();
901        assertTrue("reversed surrogate " + id + "a: " + writeString(reversed),
902                reversed.equals(back));
903
904    }
905
906    /**
907     * @tests java.lang.StringBuffer#reverse()
908     */
909    @TestTargetNew(
910        level = TestLevel.COMPLETE,
911        notes = "",
912        method = "reverse",
913        args = {}
914    )
915    public void test_reverse() {
916        // Test for method java.lang.StringBuffer
917        // java.lang.StringBuffer.reverse()
918        String org;
919        org = "a";
920        reverseTest("0", org, org, org);
921
922        org = "ab";
923        reverseTest("1", org, "ba", org);
924
925        org = "abcdef";
926        reverseTest("2", org, "fedcba", org);
927
928        org = "abcdefg";
929        reverseTest("3", org, "gfedcba", org);
930
931    }
932
933    /**
934     * @tests java.lang.StringBuffer#setCharAt(int, char)
935     */
936    @TestTargetNew(
937        level = TestLevel.COMPLETE,
938        notes = "",
939        method = "setCharAt",
940        args = {int.class, char.class}
941    )
942    public void test_setCharAtIC() {
943        // Test for method void java.lang.StringBuffer.setCharAt(int, char)
944        StringBuffer s = new StringBuffer("HelloWorld");
945        s.setCharAt(4, 'Z');
946        assertEquals("Returned incorrect char", 'Z', s.charAt(4));
947
948        try {
949            s.setCharAt(-1, 'Z');
950            fail("IndexOutOfBoundsException is not thrown.");
951        } catch(IndexOutOfBoundsException ioobe) {
952            //expected
953        }
954        try {
955            s.setCharAt(s.length() + 1, 'Z');
956            fail("IndexOutOfBoundsException is not thrown.");
957        } catch(IndexOutOfBoundsException ioobe) {
958            //expected
959        }
960    }
961
962    /**
963     * @tests java.lang.StringBuffer#setLength(int)
964     */
965    @TestTargetNew(
966        level = TestLevel.PARTIAL_COMPLETE,
967        notes = "IndexOutOfBoundsException is not verified.",
968        method = "setLength",
969        args = {int.class}
970    )
971    public void test_setLengthI() {
972        // Test for method void java.lang.StringBuffer.setLength(int)
973        testBuffer.setLength(1000);
974        assertEquals("Failed to increase length", 1000, testBuffer.length());
975        assertTrue("Increase in length trashed buffer", testBuffer.toString()
976                .startsWith("This is a test buffer"));
977        testBuffer.setLength(2);
978        assertEquals("Failed to decrease length", 2, testBuffer.length());
979        assertEquals("Decrease in length failed",
980                "Th", testBuffer.toString());
981    }
982
983    /**
984     * @tests java.lang.StringBuffer#substring(int)
985     */
986    @TestTargetNew(
987        level = TestLevel.COMPLETE,
988        notes = "",
989        method = "substring",
990        args = {int.class}
991    )
992    public void test_substringI() {
993        // Test for method java.lang.String
994        // java.lang.StringBuffer.substring(int)
995        assertEquals("Returned incorrect substring", "is a test buffer",
996                testBuffer.substring(5));
997
998        try {
999            testBuffer.substring(testBuffer.length() + 1);
1000            fail("StringIndexOutOfBoundsException is not thrown.");
1001        } catch(StringIndexOutOfBoundsException oobe) {
1002            //expected
1003        }
1004
1005        try {
1006            testBuffer.substring(-1);
1007            fail("StringIndexOutOfBoundsException is not thrown.");
1008        } catch(StringIndexOutOfBoundsException oobe) {
1009            //expected
1010        }
1011    }
1012
1013    /**
1014     * @tests java.lang.StringBuffer#substring(int, int)
1015     */
1016    @TestTargetNew(
1017        level = TestLevel.COMPLETE,
1018        notes = "",
1019        method = "substring",
1020        args = {int.class, int.class}
1021    )
1022    public void test_substringII() {
1023        // Test for method java.lang.String
1024        // java.lang.StringBuffer.substring(int, int)
1025        assertEquals("Returned incorrect substring", "is",
1026                testBuffer.substring(5, 7));
1027
1028        try {
1029            testBuffer.substring(-1, testBuffer.length());
1030            fail("StringIndexOutOfBoundsException is not thrown.");
1031        } catch(StringIndexOutOfBoundsException oobe) {
1032            //expected
1033        }
1034
1035        try {
1036            testBuffer.substring(0, -1);
1037            fail("StringIndexOutOfBoundsException is not thrown.");
1038        } catch(StringIndexOutOfBoundsException oobe) {
1039            //expected
1040        }
1041
1042        try {
1043            testBuffer.substring(2, 1);
1044            fail("StringIndexOutOfBoundsException is not thrown.");
1045        } catch(StringIndexOutOfBoundsException oobe) {
1046            //expected
1047        }
1048    }
1049
1050    /**
1051     * @tests java.lang.StringBuffer#toString()
1052     */
1053    @TestTargetNew(
1054        level = TestLevel.COMPLETE,
1055        notes = "",
1056        method = "toString",
1057        args = {}
1058    )
1059    public void test_toString() {
1060        // Test for method java.lang.String java.lang.StringBuffer.toString()
1061        assertEquals("Incorrect string value returned", "This is a test buffer", testBuffer.toString()
1062                );
1063    }
1064
1065    @TestTargetNew(
1066        level = TestLevel.COMPLETE,
1067        notes = "",
1068        method = "subSequence",
1069        args = {int.class, int.class}
1070    )
1071          public void test_subSequence() {
1072
1073              assertEquals("Incorrect substring returned", " is",
1074                      testBuffer.subSequence(4, 7));
1075              assertEquals("Incorrect substring returned", "test buffer",
1076                      testBuffer.subSequence(10, 21));
1077              assertEquals("not identical", "This is a test buffer",
1078                      testBuffer.subSequence(0, testBuffer.length()));
1079
1080              try {
1081                  testBuffer.subSequence(0, Integer.MAX_VALUE);
1082                  fail("IndexOutOfBoundsException was not thrown.");
1083              } catch(IndexOutOfBoundsException ioobe) {
1084                  //expected
1085              }
1086
1087              try {
1088                  testBuffer.subSequence(Integer.MAX_VALUE, testBuffer.length());
1089                  fail("IndexOutOfBoundsException was not thrown.");
1090              } catch(IndexOutOfBoundsException ioobe) {
1091                  //expected
1092              }
1093
1094              try {
1095                  testBuffer.subSequence(-1, testBuffer.length());
1096                  fail("IndexOutOfBoundsException was not thrown.");
1097              } catch(IndexOutOfBoundsException ioobe) {
1098                  //expected
1099              }
1100          }
1101
1102    @Override
1103    protected void setUp() {
1104        testBuffer = new StringBuffer("This is a test buffer");
1105    }
1106}
1107