StringBuffer2Test.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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    @KnownFailure("Google TODO 1481226")
418    public void test_ensureCapacityI() {
419        // Test for method void java.lang.StringBuffer.ensureCapacity(int)
420        StringBuffer sb = new StringBuffer(10);
421
422        sb.ensureCapacity(-2);
423        assertEquals("Failed to increase capacity.", 10, sb.capacity());
424
425        sb.ensureCapacity(100);
426        assertTrue("Failed to increase capacity", sb.capacity() >= 100);
427
428        try {
429            sb.ensureCapacity(Integer.MAX_VALUE);
430            fail("OutOfMemoryError should be thrown.");
431        } catch(java.lang.OutOfMemoryError oome) {
432            //expected
433        }
434    }
435
436    /**
437     * @tests java.lang.StringBuffer#getChars(int, int, char[], int)
438     */
439    @TestTargetNew(
440        level = TestLevel.PARTIAL_COMPLETE,
441        notes = "Doesn't check exceptions.",
442        method = "getChars",
443        args = {int.class, int.class, char[].class, int.class}
444    )
445    public void test_getCharsII$CI() {
446        // Test for method void java.lang.StringBuffer.getChars(int, int, char
447        // [], int)
448
449        char[] buf = new char[10];
450        testBuffer.getChars(4, 8, buf, 2);
451        assertTrue("Returned incorrect chars", new String(buf, 2, 4)
452                .equals(testBuffer.toString().substring(4, 8)));
453
454        StringBuffer buf2 = new StringBuffer("");
455        try {
456            buf2.getChars(-1, 0, new char[5], 2);
457            fail("IndexOutOfBoundsException is not thrown.");
458        } catch (IndexOutOfBoundsException e) {
459            //expected
460        }
461
462        try {
463            buf2.getChars(0, -1, new char[5], 2);
464            fail("IndexOutOfBoundsException is not thrown.");
465        } catch (IndexOutOfBoundsException e) {
466            //expected
467        }
468
469        try {
470            buf2.getChars(0, -1, new char[5], 2);
471            fail("IndexOutOfBoundsException is not thrown.");
472        } catch (IndexOutOfBoundsException e) {
473            //expected
474        }
475
476        try {
477            buf2.getChars(2, 1, new char[5], 2);
478            fail("IndexOutOfBoundsException is not thrown.");
479        } catch (IndexOutOfBoundsException e) {
480            //expected
481        }
482
483        try {
484            buf2.getChars(0, 6, new char[5], 2);
485            fail("IndexOutOfBoundsException is not thrown.");
486        } catch (IndexOutOfBoundsException e) {
487            //expected
488        }
489
490        try {
491            buf2.getChars(0, 6, new char[10], 5);
492            fail("IndexOutOfBoundsException is not thrown.");
493        } catch (IndexOutOfBoundsException e) {
494            //expected
495        }
496    }
497
498    /**
499     * @tests java.lang.StringBuffer#insert(int, char[])
500     */
501    @TestTargetNew(
502        level = TestLevel.COMPLETE,
503        notes = "",
504        method = "insert",
505        args = {int.class, char[].class}
506    )
507    public void test_insertI$C() {
508        // Test for method java.lang.StringBuffer
509        // java.lang.StringBuffer.insert(int, char [])
510        char buf[] = new char[4];
511        "char".getChars(0, 4, buf, 0);
512        testBuffer.insert(15, buf);
513        assertEquals("Insert test failed",
514                "This is a test charbuffer", testBuffer.toString());
515
516        boolean exception = false;
517        StringBuffer buf1 = new StringBuffer("abcd");
518        try {
519            buf1.insert(-1, (char[]) null);
520        } catch (StringIndexOutOfBoundsException e) {
521            exception = true;
522        } catch (NullPointerException e) {
523        }
524        assertTrue("Should throw StringIndexOutOfBoundsException", exception);
525    }
526
527    /**
528     * @tests java.lang.StringBuffer#insert(int, char[], int, int)
529     */
530    @TestTargetNew(
531        level = TestLevel.COMPLETE,
532        notes = "",
533        method = "insert",
534        args = {int.class, char[].class, int.class, int.class}
535    )
536    public void test_insertI$CII() {
537        // Test for method java.lang.StringBuffer
538        // java.lang.StringBuffer.insert(int, char [], int, int)
539        char[] c = new char[] { 'n', 'o', 't', ' ' };
540        testBuffer.insert(8, c, 0, 4);
541        assertEquals("This is not a test buffer", testBuffer.toString());
542
543        StringBuffer buf1 = new StringBuffer("abcd");
544        try {
545            buf1.insert(-1, (char[]) null, 0, 0);
546            fail("Should throw StringIndexOutOfBoundsException");
547        } catch (StringIndexOutOfBoundsException e) {
548            //expected
549        }
550
551        try {
552            testBuffer.insert(testBuffer.length() - 1, c, -1, 1);
553        } catch (StringIndexOutOfBoundsException e) {
554            //expected
555        }
556
557    }
558
559    /**
560     * @tests java.lang.StringBuffer#insert(int, char)
561     */
562    @TestTargetNew(
563        level = TestLevel.PARTIAL_COMPLETE,
564        notes = "IndexOutOfBoundsException is not verified.",
565        method = "insert",
566        args = {int.class, char.class}
567    )
568    public void test_insertIC() {
569        // Test for method java.lang.StringBuffer
570        // java.lang.StringBuffer.insert(int, char)
571        testBuffer.insert(15, 'T');
572        assertEquals("Insert test failed",
573                "This is a test Tbuffer", testBuffer.toString());
574    }
575
576    /**
577     * @tests java.lang.StringBuffer#insert(int, double)
578     */
579    @TestTargetNew(
580        level = TestLevel.COMPLETE,
581        notes = "",
582        method = "insert",
583        args = {int.class, double.class}
584    )
585    public void test_insertID() {
586        // Test for method java.lang.StringBuffer
587        // java.lang.StringBuffer.insert(int, double)
588        testBuffer.insert(15, Double.MAX_VALUE);
589        assertTrue("Insert test failed", testBuffer.toString().equals(
590                "This is a test " + Double.MAX_VALUE + "buffer"));
591        try {
592            testBuffer.insert(-1, Double.MAX_VALUE);
593            fail("StringIndexOutOfBoundsException is not thrown.");
594        } catch(StringIndexOutOfBoundsException sioobe) {
595            //expected
596        }
597
598        try {
599            testBuffer.insert(testBuffer.length() + 1, Double.MAX_VALUE);
600            fail("StringIndexOutOfBoundsException is not thrown.");
601        } catch(StringIndexOutOfBoundsException sioobe) {
602            //expected
603        }
604    }
605
606    /**
607     * @tests java.lang.StringBuffer#insert(int, float)
608     */
609    @TestTargetNew(
610        level = TestLevel.COMPLETE,
611        notes = "",
612        method = "insert",
613        args = {int.class, float.class}
614    )
615    public void test_insertIF() {
616        // Test for method java.lang.StringBuffer
617        // java.lang.StringBuffer.insert(int, float)
618        testBuffer.insert(15, Float.MAX_VALUE);
619        String testBufferString = testBuffer.toString();
620        String expectedResult = "This is a test "
621                + String.valueOf(Float.MAX_VALUE) + "buffer";
622        assertTrue("Insert test failed, got: " + "\'" + testBufferString + "\'"
623                + " but wanted: " + "\'" + expectedResult + "\'",
624                testBufferString.equals(expectedResult));
625
626        try {
627            testBuffer.insert(-1, Float.MAX_VALUE);
628            fail("StringIndexOutOfBoundsException is not thrown.");
629        } catch(StringIndexOutOfBoundsException sioobe) {
630            //expected
631        }
632
633        try {
634            testBuffer.insert(testBuffer.length() + 1, Float.MAX_VALUE);
635            fail("StringIndexOutOfBoundsException is not thrown.");
636        } catch(StringIndexOutOfBoundsException sioobe) {
637            //expected
638        }
639    }
640
641    /**
642     * @tests java.lang.StringBuffer#insert(int, int)
643     */
644    @TestTargetNew(
645        level = TestLevel.COMPLETE,
646        notes = "",
647        method = "insert",
648        args = {int.class, int.class}
649    )
650    public void test_insertII() {
651        // Test for method java.lang.StringBuffer
652        // java.lang.StringBuffer.insert(int, int)
653        testBuffer.insert(15, 100);
654        assertEquals("Insert test failed",
655                "This is a test 100buffer", testBuffer.toString());
656
657        try {
658            testBuffer.insert(-1, Integer.MAX_VALUE);
659            fail("StringIndexOutOfBoundsException is not thrown.");
660        } catch(StringIndexOutOfBoundsException sioobe) {
661            //expected
662        }
663
664        try {
665            testBuffer.insert(testBuffer.length() + 1, Integer.MAX_VALUE);
666            fail("StringIndexOutOfBoundsException is not thrown.");
667        } catch(StringIndexOutOfBoundsException sioobe) {
668            //expected
669        }
670    }
671
672    /**
673     * @tests java.lang.StringBuffer#insert(int, long)
674     */
675    @TestTargetNew(
676        level = TestLevel.COMPLETE,
677        notes = "",
678        method = "insert",
679        args = {int.class, long.class}
680    )
681    public void test_insertIJ() {
682        // Test for method java.lang.StringBuffer
683        // java.lang.StringBuffer.insert(int, long)
684        testBuffer.insert(15, 88888888888888888L);
685        assertEquals("Insert test failed",
686                "This is a test 88888888888888888buffer", testBuffer.toString());
687
688        try {
689            testBuffer.insert(-1, Long.MAX_VALUE);
690            fail("StringIndexOutOfBoundsException is not thrown.");
691        } catch(StringIndexOutOfBoundsException sioobe) {
692            //expected
693        }
694
695        try {
696            testBuffer.insert(testBuffer.length() + 1, Long.MAX_VALUE);
697            fail("StringIndexOutOfBoundsException is not thrown.");
698        } catch(StringIndexOutOfBoundsException sioobe) {
699            //expected
700        }
701    }
702
703    /**
704     * @tests java.lang.StringBuffer#insert(int, java.lang.Object)
705     */
706    @TestTargetNew(
707        level = TestLevel.COMPLETE,
708        notes = "",
709        method = "insert",
710        args = {int.class, java.lang.Object.class}
711    )
712    public void test_insertILjava_lang_Object() {
713        // Test for method java.lang.StringBuffer
714        // java.lang.StringBuffer.insert(int, java.lang.Object)
715        Object obj1 = new Object();
716        testBuffer.insert(15, obj1);
717        assertTrue("Insert test failed", testBuffer.toString().equals(
718                "This is a test " + obj1.toString() + "buffer"));
719
720        try {
721            testBuffer.insert(-1, obj1);
722            fail("StringIndexOutOfBoundsException is not thrown.");
723        } catch(StringIndexOutOfBoundsException sioobe) {
724            //expected
725        }
726
727        try {
728            testBuffer.insert(testBuffer.length() + 1, obj1);
729            fail("StringIndexOutOfBoundsException is not thrown.");
730        } catch(StringIndexOutOfBoundsException sioobe) {
731            //expected
732        }
733    }
734
735    /**
736     * @tests java.lang.StringBuffer#insert(int, java.lang.String)
737     */
738    @TestTargetNew(
739        level = TestLevel.COMPLETE,
740        notes = "",
741        method = "insert",
742        args = {int.class, java.lang.String.class}
743    )
744    public void test_insertILjava_lang_String() {
745        // Test for method java.lang.StringBuffer
746        // java.lang.StringBuffer.insert(int, java.lang.String)
747
748        testBuffer.insert(15, "STRING ");
749        assertEquals("Insert test failed",
750                "This is a test STRING buffer", testBuffer.toString());
751
752        try {
753            testBuffer.insert(-1, "");
754            fail("StringIndexOutOfBoundsException is not thrown.");
755        } catch(StringIndexOutOfBoundsException sioobe) {
756            //expected
757        }
758
759        try {
760            testBuffer.insert(testBuffer.length() + 1, "");
761            fail("StringIndexOutOfBoundsException is not thrown.");
762        } catch(StringIndexOutOfBoundsException sioobe) {
763            //expected
764        }
765    }
766
767    /**
768     * @tests java.lang.StringBuffer#insert(int, boolean)
769     */
770    @TestTargetNew(
771        level = TestLevel.COMPLETE,
772        notes = "",
773        method = "insert",
774        args = {int.class, boolean.class}
775    )
776    public void test_insertIZ() {
777        // Test for method java.lang.StringBuffer
778        // java.lang.StringBuffer.insert(int, boolean)
779        testBuffer.insert(15, true);
780        assertEquals("Insert test failed",
781                "This is a test truebuffer", testBuffer.toString());
782        try {
783            testBuffer.insert(testBuffer.length() + 1, true);
784            fail("StringIndexOutOfBoundsException is not thrown.");
785        } catch(StringIndexOutOfBoundsException sioobe) {
786            //expected
787        }
788
789        try {
790            testBuffer.insert(-1, true);
791            fail("StringIndexOutOfBoundsException is not thrown.");
792        } catch(StringIndexOutOfBoundsException sioobe) {
793            //expected
794        }
795    }
796
797    /**
798     * @tests java.lang.StringBuffer#length()
799     */
800    @TestTargetNew(
801        level = TestLevel.COMPLETE,
802        notes = "",
803        method = "length",
804        args = {}
805    )
806    public void test_length() {
807        // Test for method int java.lang.StringBuffer.length()
808        assertEquals("Incorrect length returned", 21, testBuffer.length());
809    }
810
811    /**
812     * @tests java.lang.StringBuffer#replace(int, int, java.lang.String)
813     */
814    @TestTargetNew(
815        level = TestLevel.COMPLETE,
816        notes = "",
817        method = "replace",
818        args = {int.class, int.class, java.lang.String.class}
819    )
820    public void test_replaceIILjava_lang_String() {
821        // Test for method java.lang.StringBuffer
822        // java.lang.StringBuffer.replace(int, int, java.lang.String)
823        testBuffer.replace(5, 9, "is a replaced");
824        assertTrue("Replace failed, wanted: " + "\'"
825                + "This is a replaced test buffer" + "\'" + " but got: " + "\'"
826                + testBuffer.toString() + "\'", testBuffer.toString().equals(
827                "This is a replaced test buffer"));
828        assertEquals("insert1", "text", new StringBuffer().replace(0, 0, "text")
829                .toString());
830        assertEquals("insert2", "123text", new StringBuffer("123").replace(3, 3, "text")
831                .toString());
832        assertEquals("insert2", "1text23", new StringBuffer("123").replace(1, 1, "text")
833                .toString());
834
835        try {
836            testBuffer.replace(-1, 0, "text");
837            fail("StringIndexOutOfBoundsException is not thrown.");
838        } catch(StringIndexOutOfBoundsException sioobe) {
839            //expected
840        }
841
842        try {
843            testBuffer.replace(0, -1, "text");
844            fail("StringIndexOutOfBoundsException is not thrown.");
845        } catch(StringIndexOutOfBoundsException sioobe) {
846            //expected
847        }
848
849        try {
850            testBuffer.replace(2, 1, "text");
851            fail("StringIndexOutOfBoundsException is not thrown.");
852        } catch(StringIndexOutOfBoundsException sioobe) {
853            //expected
854        }
855
856        try {
857            testBuffer.replace(testBuffer.length() + 1, testBuffer.length() + 1,
858                    "text");
859            fail("StringIndexOutOfBoundsException is not thrown.");
860        } catch(StringIndexOutOfBoundsException sioobe) {
861            //expected
862        }
863    }
864
865    private String writeString(String in) {
866        StringBuffer result = new StringBuffer();
867        result.append("\"");
868        for (int i = 0; i < in.length(); i++) {
869            result.append(" 0x" + Integer.toHexString(in.charAt(i)));
870        }
871        result.append("\"");
872        return result.toString();
873    }
874
875    private void reverseTest(String id, String org, String rev, String back) {
876        // create non-shared StringBuffer
877        StringBuffer sb = new StringBuffer(org);
878        sb.reverse();
879        String reversed = sb.toString();
880        assertTrue("reversed surrogate " + id + ": " + writeString(reversed),
881                reversed.equals(rev));
882        // create non-shared StringBuffer
883        sb = new StringBuffer(reversed);
884        sb.reverse();
885        reversed = sb.toString();
886        assertTrue("reversed surrogate " + id + "a: " + writeString(reversed),
887                reversed.equals(back));
888
889        // test algorithm when StringBuffer is shared
890        sb = new StringBuffer(org);
891        String copy = sb.toString();
892        assertEquals(org, copy);
893        sb.reverse();
894        reversed = sb.toString();
895        assertTrue("reversed surrogate " + id + ": " + writeString(reversed),
896                reversed.equals(rev));
897        sb = new StringBuffer(reversed);
898        copy = sb.toString();
899        assertEquals(rev, copy);
900        sb.reverse();
901        reversed = sb.toString();
902        assertTrue("reversed surrogate " + id + "a: " + writeString(reversed),
903                reversed.equals(back));
904
905    }
906
907    /**
908     * @tests java.lang.StringBuffer#reverse()
909     */
910    @TestTargetNew(
911        level = TestLevel.COMPLETE,
912        notes = "",
913        method = "reverse",
914        args = {}
915    )
916    public void test_reverse() {
917        // Test for method java.lang.StringBuffer
918        // java.lang.StringBuffer.reverse()
919        String org;
920        org = "a";
921        reverseTest("0", org, org, org);
922
923        org = "ab";
924        reverseTest("1", org, "ba", org);
925
926        org = "abcdef";
927        reverseTest("2", org, "fedcba", org);
928
929        org = "abcdefg";
930        reverseTest("3", org, "gfedcba", org);
931
932    }
933
934    /**
935     * @tests java.lang.StringBuffer#setCharAt(int, char)
936     */
937    @TestTargetNew(
938        level = TestLevel.COMPLETE,
939        notes = "",
940        method = "setCharAt",
941        args = {int.class, char.class}
942    )
943    public void test_setCharAtIC() {
944        // Test for method void java.lang.StringBuffer.setCharAt(int, char)
945        StringBuffer s = new StringBuffer("HelloWorld");
946        s.setCharAt(4, 'Z');
947        assertEquals("Returned incorrect char", 'Z', s.charAt(4));
948
949        try {
950            s.setCharAt(-1, 'Z');
951            fail("IndexOutOfBoundsException is not thrown.");
952        } catch(IndexOutOfBoundsException ioobe) {
953            //expected
954        }
955        try {
956            s.setCharAt(s.length() + 1, 'Z');
957            fail("IndexOutOfBoundsException is not thrown.");
958        } catch(IndexOutOfBoundsException ioobe) {
959            //expected
960        }
961    }
962
963    /**
964     * @tests java.lang.StringBuffer#setLength(int)
965     */
966    @TestTargetNew(
967        level = TestLevel.PARTIAL_COMPLETE,
968        notes = "IndexOutOfBoundsException is not verified.",
969        method = "setLength",
970        args = {int.class}
971    )
972    public void test_setLengthI() {
973        // Test for method void java.lang.StringBuffer.setLength(int)
974        testBuffer.setLength(1000);
975        assertEquals("Failed to increase length", 1000, testBuffer.length());
976        assertTrue("Increase in length trashed buffer", testBuffer.toString()
977                .startsWith("This is a test buffer"));
978        testBuffer.setLength(2);
979        assertEquals("Failed to decrease length", 2, testBuffer.length());
980        assertEquals("Decrease in length failed",
981                "Th", testBuffer.toString());
982    }
983
984    /**
985     * @tests java.lang.StringBuffer#substring(int)
986     */
987    @TestTargetNew(
988        level = TestLevel.COMPLETE,
989        notes = "",
990        method = "substring",
991        args = {int.class}
992    )
993    public void test_substringI() {
994        // Test for method java.lang.String
995        // java.lang.StringBuffer.substring(int)
996        assertEquals("Returned incorrect substring", "is a test buffer",
997                testBuffer.substring(5));
998
999        try {
1000            testBuffer.substring(testBuffer.length() + 1);
1001            fail("StringIndexOutOfBoundsException is not thrown.");
1002        } catch(StringIndexOutOfBoundsException oobe) {
1003            //expected
1004        }
1005
1006        try {
1007            testBuffer.substring(-1);
1008            fail("StringIndexOutOfBoundsException is not thrown.");
1009        } catch(StringIndexOutOfBoundsException oobe) {
1010            //expected
1011        }
1012    }
1013
1014    /**
1015     * @tests java.lang.StringBuffer#substring(int, int)
1016     */
1017    @TestTargetNew(
1018        level = TestLevel.COMPLETE,
1019        notes = "",
1020        method = "substring",
1021        args = {int.class, int.class}
1022    )
1023    public void test_substringII() {
1024        // Test for method java.lang.String
1025        // java.lang.StringBuffer.substring(int, int)
1026        assertEquals("Returned incorrect substring", "is",
1027                testBuffer.substring(5, 7));
1028
1029        try {
1030            testBuffer.substring(-1, testBuffer.length());
1031            fail("StringIndexOutOfBoundsException is not thrown.");
1032        } catch(StringIndexOutOfBoundsException oobe) {
1033            //expected
1034        }
1035
1036        try {
1037            testBuffer.substring(0, -1);
1038            fail("StringIndexOutOfBoundsException is not thrown.");
1039        } catch(StringIndexOutOfBoundsException oobe) {
1040            //expected
1041        }
1042
1043        try {
1044            testBuffer.substring(2, 1);
1045            fail("StringIndexOutOfBoundsException is not thrown.");
1046        } catch(StringIndexOutOfBoundsException oobe) {
1047            //expected
1048        }
1049    }
1050
1051    /**
1052     * @tests java.lang.StringBuffer#toString()
1053     */
1054    @TestTargetNew(
1055        level = TestLevel.COMPLETE,
1056        notes = "",
1057        method = "toString",
1058        args = {}
1059    )
1060    public void test_toString() {
1061        // Test for method java.lang.String java.lang.StringBuffer.toString()
1062        assertEquals("Incorrect string value returned", "This is a test buffer", testBuffer.toString()
1063                );
1064    }
1065
1066    @TestTargetNew(
1067        level = TestLevel.COMPLETE,
1068        notes = "",
1069        method = "subSequence",
1070        args = {int.class, int.class}
1071    )
1072          public void test_subSequence() {
1073
1074              assertEquals("Incorrect substring returned", " is",
1075                      testBuffer.subSequence(4, 7));
1076              assertEquals("Incorrect substring returned", "test buffer",
1077                      testBuffer.subSequence(10, 21));
1078              assertEquals("not identical", "This is a test buffer",
1079                      testBuffer.subSequence(0, testBuffer.length()));
1080
1081              try {
1082                  testBuffer.subSequence(0, Integer.MAX_VALUE);
1083                  fail("IndexOutOfBoundsException was not thrown.");
1084              } catch(IndexOutOfBoundsException ioobe) {
1085                  //expected
1086              }
1087
1088              try {
1089                  testBuffer.subSequence(Integer.MAX_VALUE, testBuffer.length());
1090                  fail("IndexOutOfBoundsException was not thrown.");
1091              } catch(IndexOutOfBoundsException ioobe) {
1092                  //expected
1093              }
1094
1095              try {
1096                  testBuffer.subSequence(-1, testBuffer.length());
1097                  fail("IndexOutOfBoundsException was not thrown.");
1098              } catch(IndexOutOfBoundsException ioobe) {
1099                  //expected
1100              }
1101          }
1102
1103    @Override
1104    protected void setUp() {
1105        testBuffer = new StringBuffer("This is a test buffer");
1106    }
1107}
1108