View Javadoc
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  
18  package org.apache.commons.lang3.text;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNotSame;
26  import static org.junit.Assert.assertSame;
27  import static org.junit.Assert.assertTrue;
28  import static org.junit.Assert.fail;
29  
30  import java.io.IOException;
31  import java.io.Reader;
32  import java.io.StringReader;
33  import java.io.StringWriter;
34  import java.io.Writer;
35  import java.nio.CharBuffer;
36  import java.util.Arrays;
37  
38  import org.apache.commons.lang3.ArrayUtils;
39  
40  /**
41   * Unit tests for {@link org.apache.commons.lang3.text.StrBuilder}.
42   */
43  @Deprecated
44  public class StrBuilderTest {
45  
46      //-----------------------------------------------------------------------
47      @Test
48      public void testConstructors() {
49          final StrBuilder sb0 = new StrBuilder();
50          assertEquals(32, sb0.capacity());
51          assertEquals(0, sb0.length());
52          assertEquals(0, sb0.size());
53  
54          final StrBuilder sb1 = new StrBuilder(32);
55          assertEquals(32, sb1.capacity());
56          assertEquals(0, sb1.length());
57          assertEquals(0, sb1.size());
58  
59          final StrBuilder sb2 = new StrBuilder(0);
60          assertEquals(32, sb2.capacity());
61          assertEquals(0, sb2.length());
62          assertEquals(0, sb2.size());
63  
64          final StrBuilder sb3 = new StrBuilder(-1);
65          assertEquals(32, sb3.capacity());
66          assertEquals(0, sb3.length());
67          assertEquals(0, sb3.size());
68  
69          final StrBuilder sb4 = new StrBuilder(1);
70          assertEquals(1, sb4.capacity());
71          assertEquals(0, sb4.length());
72          assertEquals(0, sb4.size());
73  
74          final StrBuilder sb5 = new StrBuilder(null);
75          assertEquals(32, sb5.capacity());
76          assertEquals(0, sb5.length());
77          assertEquals(0, sb5.size());
78  
79          final StrBuilder sb6 = new StrBuilder("");
80          assertEquals(32, sb6.capacity());
81          assertEquals(0, sb6.length());
82          assertEquals(0, sb6.size());
83  
84          final StrBuilder sb7 = new StrBuilder("foo");
85          assertEquals(35, sb7.capacity());
86          assertEquals(3, sb7.length());
87          assertEquals(3, sb7.size());
88      }
89  
90      //-----------------------------------------------------------------------
91      @Test
92      public void testChaining() {
93          final StrBuilder sb = new StrBuilder();
94          assertSame(sb, sb.setNewLineText(null));
95          assertSame(sb, sb.setNullText(null));
96          assertSame(sb, sb.setLength(1));
97          assertSame(sb, sb.setCharAt(0, 'a'));
98          assertSame(sb, sb.ensureCapacity(0));
99          assertSame(sb, sb.minimizeCapacity());
100         assertSame(sb, sb.clear());
101         assertSame(sb, sb.reverse());
102         assertSame(sb, sb.trim());
103     }
104 
105     //-----------------------------------------------------------------------
106     @Test
107     public void testReadFromReader() throws Exception {
108         String s = "";
109         for (int i = 0; i < 100; ++i) {
110             final StrBuilder sb = new StrBuilder();
111             final int len = sb.readFrom(new StringReader(s));
112 
113             assertEquals(s.length(), len);
114             assertEquals(s, sb.toString());
115 
116             s += Integer.toString(i);
117         }
118     }
119 
120     @Test
121     public void testReadFromReaderAppendsToEnd() throws Exception {
122         final StrBuilder sb = new StrBuilder("Test");
123         sb.readFrom(new StringReader(" 123"));
124         assertEquals("Test 123", sb.toString());
125     }
126 
127     @Test
128     public void testReadFromCharBuffer() throws Exception {
129         String s = "";
130         for (int i = 0; i < 100; ++i) {
131             final StrBuilder sb = new StrBuilder();
132             final int len = sb.readFrom(CharBuffer.wrap(s));
133 
134             assertEquals(s.length(), len);
135             assertEquals(s, sb.toString());
136 
137             s += Integer.toString(i);
138         }
139     }
140 
141     @Test
142     public void testReadFromCharBufferAppendsToEnd() throws Exception {
143         final StrBuilder sb = new StrBuilder("Test");
144         sb.readFrom(CharBuffer.wrap(" 123"));
145         assertEquals("Test 123", sb.toString());
146     }
147 
148     @Test
149     public void testReadFromReadable() throws Exception {
150         String s = "";
151         for (int i = 0; i < 100; ++i) {
152             final StrBuilder sb = new StrBuilder();
153             final int len = sb.readFrom(new MockReadable(s));
154 
155             assertEquals(s.length(), len);
156             assertEquals(s, sb.toString());
157 
158             s += Integer.toString(i);
159         }
160     }
161 
162     @Test
163     public void testReadFromReadableAppendsToEnd() throws Exception {
164         final StrBuilder sb = new StrBuilder("Test");
165         sb.readFrom(new MockReadable(" 123"));
166         assertEquals("Test 123", sb.toString());
167     }
168 
169     private static class MockReadable implements Readable {
170 
171         private final CharBuffer src;
172 
173         MockReadable(final String src) {
174             this.src = CharBuffer.wrap(src);
175         }
176 
177         @Override
178         public int read(final CharBuffer cb) throws IOException {
179             return src.read(cb);
180         }
181     }
182 
183     //-----------------------------------------------------------------------
184     @Test
185     public void testGetSetNewLineText() {
186         final StrBuilder sb = new StrBuilder();
187         assertEquals(null, sb.getNewLineText());
188 
189         sb.setNewLineText("#");
190         assertEquals("#", sb.getNewLineText());
191 
192         sb.setNewLineText("");
193         assertEquals("", sb.getNewLineText());
194 
195         sb.setNewLineText(null);
196         assertEquals(null, sb.getNewLineText());
197     }
198 
199     //-----------------------------------------------------------------------
200     @Test
201     public void testGetSetNullText() {
202         final StrBuilder sb = new StrBuilder();
203         assertEquals(null, sb.getNullText());
204 
205         sb.setNullText("null");
206         assertEquals("null", sb.getNullText());
207 
208         sb.setNullText("");
209         assertEquals(null, sb.getNullText());
210 
211         sb.setNullText("NULL");
212         assertEquals("NULL", sb.getNullText());
213 
214         sb.setNullText(null);
215         assertEquals(null, sb.getNullText());
216     }
217 
218     //-----------------------------------------------------------------------
219     @Test
220     public void testCapacityAndLength() {
221         final StrBuilder sb = new StrBuilder();
222         assertEquals(32, sb.capacity());
223         assertEquals(0, sb.length());
224         assertEquals(0, sb.size());
225         assertTrue(sb.isEmpty());
226 
227         sb.minimizeCapacity();
228         assertEquals(0, sb.capacity());
229         assertEquals(0, sb.length());
230         assertEquals(0, sb.size());
231         assertTrue(sb.isEmpty());
232 
233         sb.ensureCapacity(32);
234         assertTrue(sb.capacity() >= 32);
235         assertEquals(0, sb.length());
236         assertEquals(0, sb.size());
237         assertTrue(sb.isEmpty());
238 
239         sb.append("foo");
240         assertTrue(sb.capacity() >= 32);
241         assertEquals(3, sb.length());
242         assertEquals(3, sb.size());
243         assertTrue(sb.isEmpty() == false);
244 
245         sb.clear();
246         assertTrue(sb.capacity() >= 32);
247         assertEquals(0, sb.length());
248         assertEquals(0, sb.size());
249         assertTrue(sb.isEmpty());
250 
251         sb.append("123456789012345678901234567890123");
252         assertTrue(sb.capacity() > 32);
253         assertEquals(33, sb.length());
254         assertEquals(33, sb.size());
255         assertTrue(sb.isEmpty() == false);
256 
257         sb.ensureCapacity(16);
258         assertTrue(sb.capacity() > 16);
259         assertEquals(33, sb.length());
260         assertEquals(33, sb.size());
261         assertTrue(sb.isEmpty() == false);
262 
263         sb.minimizeCapacity();
264         assertEquals(33, sb.capacity());
265         assertEquals(33, sb.length());
266         assertEquals(33, sb.size());
267         assertTrue(sb.isEmpty() == false);
268 
269         try {
270             sb.setLength(-1);
271             fail("setLength(-1) expected StringIndexOutOfBoundsException");
272         } catch (final IndexOutOfBoundsException e) {
273             // expected
274         }
275 
276         sb.setLength(33);
277         assertEquals(33, sb.capacity());
278         assertEquals(33, sb.length());
279         assertEquals(33, sb.size());
280         assertTrue(sb.isEmpty() == false);
281 
282         sb.setLength(16);
283         assertTrue(sb.capacity() >= 16);
284         assertEquals(16, sb.length());
285         assertEquals(16, sb.size());
286         assertEquals("1234567890123456", sb.toString());
287         assertTrue(sb.isEmpty() == false);
288 
289         sb.setLength(32);
290         assertTrue(sb.capacity() >= 32);
291         assertEquals(32, sb.length());
292         assertEquals(32, sb.size());
293         assertEquals("1234567890123456\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", sb.toString());
294         assertTrue(sb.isEmpty() == false);
295 
296         sb.setLength(0);
297         assertTrue(sb.capacity() >= 32);
298         assertEquals(0, sb.length());
299         assertEquals(0, sb.size());
300         assertTrue(sb.isEmpty());
301     }
302 
303     //-----------------------------------------------------------------------
304     @Test
305     public void testLength() {
306         final StrBuilder sb = new StrBuilder();
307         assertEquals(0, sb.length());
308 
309         sb.append("Hello");
310         assertEquals(5, sb.length());
311     }
312 
313     @Test
314     public void testSetLength() {
315         final StrBuilder sb = new StrBuilder();
316         sb.append("Hello");
317         sb.setLength(2);  // shorten
318         assertEquals("He", sb.toString());
319         sb.setLength(2);  // no change
320         assertEquals("He", sb.toString());
321         sb.setLength(3);  // lengthen
322         assertEquals("He\0", sb.toString());
323 
324         try {
325             sb.setLength(-1);
326             fail("setLength(-1) expected StringIndexOutOfBoundsException");
327         } catch (final IndexOutOfBoundsException e) {
328             // expected
329         }
330     }
331 
332     //-----------------------------------------------------------------------
333     @Test
334     public void testCapacity() {
335         final StrBuilder sb = new StrBuilder();
336         assertEquals(sb.buffer.length, sb.capacity());
337 
338         sb.append("HelloWorldHelloWorldHelloWorldHelloWorld");
339         assertEquals(sb.buffer.length, sb.capacity());
340     }
341 
342     @Test
343     public void testEnsureCapacity() {
344         final StrBuilder sb = new StrBuilder();
345         sb.ensureCapacity(2);
346         assertTrue(sb.capacity() >= 2);
347 
348         sb.ensureCapacity(-1);
349         assertTrue(sb.capacity() >= 0);
350 
351         sb.append("HelloWorld");
352         sb.ensureCapacity(40);
353         assertTrue(sb.capacity() >= 40);
354     }
355 
356     @Test
357     public void testMinimizeCapacity() {
358         final StrBuilder sb = new StrBuilder();
359         sb.minimizeCapacity();
360         assertEquals(0, sb.capacity());
361 
362         sb.append("HelloWorld");
363         sb.minimizeCapacity();
364         assertEquals(10, sb.capacity());
365     }
366 
367     //-----------------------------------------------------------------------
368     @Test
369     public void testSize() {
370         final StrBuilder sb = new StrBuilder();
371         assertEquals(0, sb.size());
372 
373         sb.append("Hello");
374         assertEquals(5, sb.size());
375     }
376 
377     @Test
378     public void testIsEmpty() {
379         final StrBuilder sb = new StrBuilder();
380         assertTrue(sb.isEmpty());
381 
382         sb.append("Hello");
383         assertFalse(sb.isEmpty());
384 
385         sb.clear();
386         assertTrue(sb.isEmpty());
387     }
388 
389     @Test
390     public void testClear() {
391         final StrBuilder sb = new StrBuilder();
392         sb.append("Hello");
393         sb.clear();
394         assertEquals(0, sb.length());
395         assertTrue(sb.buffer.length >= 5);
396     }
397 
398     //-----------------------------------------------------------------------
399     @Test
400     public void testCharAt() {
401         final StrBuilder sb = new StrBuilder();
402         try {
403             sb.charAt(0);
404             fail("charAt(0) expected IndexOutOfBoundsException");
405         } catch (final IndexOutOfBoundsException e) {
406             // expected
407         }
408         try {
409             sb.charAt(-1);
410             fail("charAt(-1) expected IndexOutOfBoundsException");
411         } catch (final IndexOutOfBoundsException e) {
412             // expected
413         }
414         sb.append("foo");
415         assertEquals('f', sb.charAt(0));
416         assertEquals('o', sb.charAt(1));
417         assertEquals('o', sb.charAt(2));
418         try {
419             sb.charAt(-1);
420             fail("charAt(-1) expected IndexOutOfBoundsException");
421         } catch (final IndexOutOfBoundsException e) {
422             // expected
423         }
424         try {
425             sb.charAt(3);
426             fail("charAt(3) expected IndexOutOfBoundsException");
427         } catch (final IndexOutOfBoundsException e) {
428             // expected
429         }
430     }
431 
432     //-----------------------------------------------------------------------
433     @Test
434     public void testSetCharAt() {
435         final StrBuilder sb = new StrBuilder();
436         try {
437             sb.setCharAt(0, 'f');
438             fail("setCharAt(0,) expected IndexOutOfBoundsException");
439         } catch (final IndexOutOfBoundsException e) {
440             // expected
441         }
442         try {
443             sb.setCharAt(-1, 'f');
444             fail("setCharAt(-1,) expected IndexOutOfBoundsException");
445         } catch (final IndexOutOfBoundsException e) {
446             // expected
447         }
448         sb.append("foo");
449         sb.setCharAt(0, 'b');
450         sb.setCharAt(1, 'a');
451         sb.setCharAt(2, 'r');
452         try {
453             sb.setCharAt(3, '!');
454             fail("setCharAt(3,) expected IndexOutOfBoundsException");
455         } catch (final IndexOutOfBoundsException e) {
456             // expected
457         }
458         assertEquals("bar", sb.toString());
459     }
460 
461     //-----------------------------------------------------------------------
462     @Test
463     public void testDeleteCharAt() {
464         final StrBuilder sb = new StrBuilder("abc");
465         sb.deleteCharAt(0);
466         assertEquals("bc", sb.toString());
467 
468         try {
469             sb.deleteCharAt(1000);
470             fail("Expected IndexOutOfBoundsException");
471         } catch (final IndexOutOfBoundsException e) {}
472     }
473 
474     //-----------------------------------------------------------------------
475     @Test
476     public void testToCharArray() {
477         final StrBuilder sb = new StrBuilder();
478         assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, sb.toCharArray());
479 
480         char[] a = sb.toCharArray();
481         assertNotNull("toCharArray() result is null", a);
482         assertEquals("toCharArray() result is too large", 0, a.length);
483 
484         sb.append("junit");
485         a = sb.toCharArray();
486         assertEquals("toCharArray() result incorrect length", 5, a.length);
487         assertTrue("toCharArray() result does not match", Arrays.equals("junit".toCharArray(), a));
488     }
489 
490     @Test
491     public void testToCharArrayIntInt() {
492         final StrBuilder sb = new StrBuilder();
493         assertEquals(ArrayUtils.EMPTY_CHAR_ARRAY, sb.toCharArray(0, 0));
494 
495         sb.append("junit");
496         char[] a = sb.toCharArray(0, 20); // too large test
497         assertEquals("toCharArray(int,int) result incorrect length", 5, a.length);
498         assertTrue("toCharArray(int,int) result does not match", Arrays.equals("junit".toCharArray(), a));
499 
500         a = sb.toCharArray(0, 4);
501         assertEquals("toCharArray(int,int) result incorrect length", 4, a.length);
502         assertTrue("toCharArray(int,int) result does not match", Arrays.equals("juni".toCharArray(), a));
503 
504         a = sb.toCharArray(0, 4);
505         assertEquals("toCharArray(int,int) result incorrect length", 4, a.length);
506         assertTrue("toCharArray(int,int) result does not match", Arrays.equals("juni".toCharArray(), a));
507 
508         a = sb.toCharArray(0, 1);
509         assertNotNull("toCharArray(int,int) result is null", a);
510 
511         try {
512             sb.toCharArray(-1, 5);
513             fail("no string index out of bound on -1");
514         } catch (final IndexOutOfBoundsException e) {
515         }
516 
517         try {
518             sb.toCharArray(6, 5);
519             fail("no string index out of bound on -1");
520         } catch (final IndexOutOfBoundsException e) {
521         }
522     }
523 
524     @Test
525     public void testGetChars ( ) {
526         final StrBuilder sb = new StrBuilder();
527 
528         char[] input = new char[10];
529         char[] a = sb.getChars(input);
530         assertSame (input, a);
531         assertTrue(Arrays.equals(new char[10], a));
532 
533         sb.append("junit");
534         a = sb.getChars(input);
535         assertSame(input, a);
536         assertTrue(Arrays.equals(new char[] {'j','u','n','i','t',0,0,0,0,0},a));
537 
538         a = sb.getChars(null);
539         assertNotSame(input,a);
540         assertEquals(5,a.length);
541         assertTrue(Arrays.equals("junit".toCharArray(),a));
542 
543         input = new char[5];
544         a = sb.getChars(input);
545         assertSame(input, a);
546 
547         input = new char[4];
548         a = sb.getChars(input);
549         assertNotSame(input, a);
550     }
551 
552     @Test
553     public void testGetCharsIntIntCharArrayInt( ) {
554         final StrBuilder sb = new StrBuilder();
555 
556         sb.append("junit");
557         char[] a = new char[5];
558         sb.getChars(0,5,a,0);
559         assertTrue(Arrays.equals(new char[] {'j','u','n','i','t'},a));
560 
561         a = new char[5];
562         sb.getChars(0,2,a,3);
563         assertTrue(Arrays.equals(new char[] {0,0,0,'j','u'},a));
564 
565         try {
566             sb.getChars(-1,0,a,0);
567             fail("no exception");
568         } catch (final IndexOutOfBoundsException e) {
569         }
570 
571         try {
572             sb.getChars(0,-1,a,0);
573             fail("no exception");
574         } catch (final IndexOutOfBoundsException e) {
575         }
576 
577         try {
578             sb.getChars(0,20,a,0);
579             fail("no exception");
580         } catch (final IndexOutOfBoundsException e) {
581         }
582 
583         try {
584             sb.getChars(4,2,a,0);
585             fail("no exception");
586         } catch (final IndexOutOfBoundsException e) {
587         }
588     }
589 
590     //-----------------------------------------------------------------------
591     @Test
592     public void testDeleteIntInt() {
593         StrBuilder sb = new StrBuilder("abc");
594         sb.delete(0, 1);
595         assertEquals("bc", sb.toString());
596         sb.delete(1, 2);
597         assertEquals("b", sb.toString());
598         sb.delete(0, 1);
599         assertEquals("", sb.toString());
600         sb.delete(0, 1000);
601         assertEquals("", sb.toString());
602 
603         try {
604             sb.delete(1, 2);
605             fail("Expected IndexOutOfBoundsException");
606         } catch (final IndexOutOfBoundsException e) {}
607         try {
608             sb.delete(-1, 1);
609             fail("Expected IndexOutOfBoundsException");
610         } catch (final IndexOutOfBoundsException e) {}
611 
612         sb = new StrBuilder("anything");
613         try {
614             sb.delete(2, 1);
615             fail("Expected IndexOutOfBoundsException");
616         } catch (final IndexOutOfBoundsException e) {}
617     }
618 
619     //-----------------------------------------------------------------------
620     @Test
621     public void testDeleteAll_char() {
622         StrBuilder sb = new StrBuilder("abcbccba");
623         sb.deleteAll('X');
624         assertEquals("abcbccba", sb.toString());
625         sb.deleteAll('a');
626         assertEquals("bcbccb", sb.toString());
627         sb.deleteAll('c');
628         assertEquals("bbb", sb.toString());
629         sb.deleteAll('b');
630         assertEquals("", sb.toString());
631 
632         sb = new StrBuilder("");
633         sb.deleteAll('b');
634         assertEquals("", sb.toString());
635     }
636 
637     @Test
638     public void testDeleteFirst_char() {
639         StrBuilder sb = new StrBuilder("abcba");
640         sb.deleteFirst('X');
641         assertEquals("abcba", sb.toString());
642         sb.deleteFirst('a');
643         assertEquals("bcba", sb.toString());
644         sb.deleteFirst('c');
645         assertEquals("bba", sb.toString());
646         sb.deleteFirst('b');
647         assertEquals("ba", sb.toString());
648 
649         sb = new StrBuilder("");
650         sb.deleteFirst('b');
651         assertEquals("", sb.toString());
652     }
653 
654     // -----------------------------------------------------------------------
655     @Test
656     public void testDeleteAll_String() {
657         StrBuilder sb = new StrBuilder("abcbccba");
658         sb.deleteAll((String) null);
659         assertEquals("abcbccba", sb.toString());
660         sb.deleteAll("");
661         assertEquals("abcbccba", sb.toString());
662 
663         sb.deleteAll("X");
664         assertEquals("abcbccba", sb.toString());
665         sb.deleteAll("a");
666         assertEquals("bcbccb", sb.toString());
667         sb.deleteAll("c");
668         assertEquals("bbb", sb.toString());
669         sb.deleteAll("b");
670         assertEquals("", sb.toString());
671 
672         sb = new StrBuilder("abcbccba");
673         sb.deleteAll("bc");
674         assertEquals("acba", sb.toString());
675 
676         sb = new StrBuilder("");
677         sb.deleteAll("bc");
678         assertEquals("", sb.toString());
679     }
680 
681     @Test
682     public void testDeleteFirst_String() {
683         StrBuilder sb = new StrBuilder("abcbccba");
684         sb.deleteFirst((String) null);
685         assertEquals("abcbccba", sb.toString());
686         sb.deleteFirst("");
687         assertEquals("abcbccba", sb.toString());
688 
689         sb.deleteFirst("X");
690         assertEquals("abcbccba", sb.toString());
691         sb.deleteFirst("a");
692         assertEquals("bcbccba", sb.toString());
693         sb.deleteFirst("c");
694         assertEquals("bbccba", sb.toString());
695         sb.deleteFirst("b");
696         assertEquals("bccba", sb.toString());
697 
698         sb = new StrBuilder("abcbccba");
699         sb.deleteFirst("bc");
700         assertEquals("abccba", sb.toString());
701 
702         sb = new StrBuilder("");
703         sb.deleteFirst("bc");
704         assertEquals("", sb.toString());
705     }
706 
707     // -----------------------------------------------------------------------
708     @Test
709     public void testDeleteAll_StrMatcher() {
710         StrBuilder sb = new StrBuilder("A0xA1A2yA3");
711         sb.deleteAll((StrMatcher) null);
712         assertEquals("A0xA1A2yA3", sb.toString());
713         sb.deleteAll(A_NUMBER_MATCHER);
714         assertEquals("xy", sb.toString());
715 
716         sb = new StrBuilder("Ax1");
717         sb.deleteAll(A_NUMBER_MATCHER);
718         assertEquals("Ax1", sb.toString());
719 
720         sb = new StrBuilder("");
721         sb.deleteAll(A_NUMBER_MATCHER);
722         assertEquals("", sb.toString());
723     }
724 
725     @Test
726     public void testDeleteFirst_StrMatcher() {
727         StrBuilder sb = new StrBuilder("A0xA1A2yA3");
728         sb.deleteFirst((StrMatcher) null);
729         assertEquals("A0xA1A2yA3", sb.toString());
730         sb.deleteFirst(A_NUMBER_MATCHER);
731         assertEquals("xA1A2yA3", sb.toString());
732 
733         sb = new StrBuilder("Ax1");
734         sb.deleteFirst(A_NUMBER_MATCHER);
735         assertEquals("Ax1", sb.toString());
736 
737         sb = new StrBuilder("");
738         sb.deleteFirst(A_NUMBER_MATCHER);
739         assertEquals("", sb.toString());
740     }
741 
742     // -----------------------------------------------------------------------
743     @Test
744     public void testReplace_int_int_String() {
745         StrBuilder sb = new StrBuilder("abc");
746         sb.replace(0, 1, "d");
747         assertEquals("dbc", sb.toString());
748         sb.replace(0, 1, "aaa");
749         assertEquals("aaabc", sb.toString());
750         sb.replace(0, 3, "");
751         assertEquals("bc", sb.toString());
752         sb.replace(1, 2, null);
753         assertEquals("b", sb.toString());
754         sb.replace(1, 1000, "text");
755         assertEquals("btext", sb.toString());
756         sb.replace(0, 1000, "text");
757         assertEquals("text", sb.toString());
758 
759         sb = new StrBuilder("atext");
760         sb.replace(1, 1, "ny");
761         assertEquals("anytext", sb.toString());
762         try {
763             sb.replace(2, 1, "anything");
764             fail("Expected IndexOutOfBoundsException");
765         } catch (final IndexOutOfBoundsException e) {}
766 
767         sb = new StrBuilder();
768         try {
769             sb.replace(1, 2, "anything");
770             fail("Expected IndexOutOfBoundsException");
771         } catch (final IndexOutOfBoundsException e) {}
772         try {
773             sb.replace(-1, 1, "anything");
774             fail("Expected IndexOutOfBoundsException");
775         } catch (final IndexOutOfBoundsException e) {}
776     }
777 
778     //-----------------------------------------------------------------------
779     @Test
780     public void testReplaceAll_char_char() {
781         final StrBuilder sb = new StrBuilder("abcbccba");
782         sb.replaceAll('x', 'y');
783         assertEquals("abcbccba", sb.toString());
784         sb.replaceAll('a', 'd');
785         assertEquals("dbcbccbd", sb.toString());
786         sb.replaceAll('b', 'e');
787         assertEquals("dececced", sb.toString());
788         sb.replaceAll('c', 'f');
789         assertEquals("defeffed", sb.toString());
790         sb.replaceAll('d', 'd');
791         assertEquals("defeffed", sb.toString());
792     }
793 
794     //-----------------------------------------------------------------------
795     @Test
796     public void testReplaceFirst_char_char() {
797         final StrBuilder sb = new StrBuilder("abcbccba");
798         sb.replaceFirst('x', 'y');
799         assertEquals("abcbccba", sb.toString());
800         sb.replaceFirst('a', 'd');
801         assertEquals("dbcbccba", sb.toString());
802         sb.replaceFirst('b', 'e');
803         assertEquals("decbccba", sb.toString());
804         sb.replaceFirst('c', 'f');
805         assertEquals("defbccba", sb.toString());
806         sb.replaceFirst('d', 'd');
807         assertEquals("defbccba", sb.toString());
808     }
809 
810     //-----------------------------------------------------------------------
811     @Test
812     public void testReplaceAll_String_String() {
813         StrBuilder sb = new StrBuilder("abcbccba");
814         sb.replaceAll((String) null, null);
815         assertEquals("abcbccba", sb.toString());
816         sb.replaceAll((String) null, "anything");
817         assertEquals("abcbccba", sb.toString());
818         sb.replaceAll("", null);
819         assertEquals("abcbccba", sb.toString());
820         sb.replaceAll("", "anything");
821         assertEquals("abcbccba", sb.toString());
822 
823         sb.replaceAll("x", "y");
824         assertEquals("abcbccba", sb.toString());
825         sb.replaceAll("a", "d");
826         assertEquals("dbcbccbd", sb.toString());
827         sb.replaceAll("d", null);
828         assertEquals("bcbccb", sb.toString());
829         sb.replaceAll("cb", "-");
830         assertEquals("b-c-", sb.toString());
831 
832         sb = new StrBuilder("abcba");
833         sb.replaceAll("b", "xbx");
834         assertEquals("axbxcxbxa", sb.toString());
835 
836         sb = new StrBuilder("bb");
837         sb.replaceAll("b", "xbx");
838         assertEquals("xbxxbx", sb.toString());
839     }
840 
841     @Test
842     public void testReplaceFirst_String_String() {
843         StrBuilder sb = new StrBuilder("abcbccba");
844         sb.replaceFirst((String) null, null);
845         assertEquals("abcbccba", sb.toString());
846         sb.replaceFirst((String) null, "anything");
847         assertEquals("abcbccba", sb.toString());
848         sb.replaceFirst("", null);
849         assertEquals("abcbccba", sb.toString());
850         sb.replaceFirst("", "anything");
851         assertEquals("abcbccba", sb.toString());
852 
853         sb.replaceFirst("x", "y");
854         assertEquals("abcbccba", sb.toString());
855         sb.replaceFirst("a", "d");
856         assertEquals("dbcbccba", sb.toString());
857         sb.replaceFirst("d", null);
858         assertEquals("bcbccba", sb.toString());
859         sb.replaceFirst("cb", "-");
860         assertEquals("b-ccba", sb.toString());
861 
862         sb = new StrBuilder("abcba");
863         sb.replaceFirst("b", "xbx");
864         assertEquals("axbxcba", sb.toString());
865 
866         sb = new StrBuilder("bb");
867         sb.replaceFirst("b", "xbx");
868         assertEquals("xbxb", sb.toString());
869     }
870 
871     //-----------------------------------------------------------------------
872     @Test
873     public void testReplaceAll_StrMatcher_String() {
874         StrBuilder sb = new StrBuilder("abcbccba");
875         sb.replaceAll((StrMatcher) null, null);
876         assertEquals("abcbccba", sb.toString());
877         sb.replaceAll((StrMatcher) null, "anything");
878         assertEquals("abcbccba", sb.toString());
879         sb.replaceAll(StrMatcher.noneMatcher(), null);
880         assertEquals("abcbccba", sb.toString());
881         sb.replaceAll(StrMatcher.noneMatcher(), "anything");
882         assertEquals("abcbccba", sb.toString());
883 
884         sb.replaceAll(StrMatcher.charMatcher('x'), "y");
885         assertEquals("abcbccba", sb.toString());
886         sb.replaceAll(StrMatcher.charMatcher('a'), "d");
887         assertEquals("dbcbccbd", sb.toString());
888         sb.replaceAll(StrMatcher.charMatcher('d'), null);
889         assertEquals("bcbccb", sb.toString());
890         sb.replaceAll(StrMatcher.stringMatcher("cb"), "-");
891         assertEquals("b-c-", sb.toString());
892 
893         sb = new StrBuilder("abcba");
894         sb.replaceAll(StrMatcher.charMatcher('b'), "xbx");
895         assertEquals("axbxcxbxa", sb.toString());
896 
897         sb = new StrBuilder("bb");
898         sb.replaceAll(StrMatcher.charMatcher('b'), "xbx");
899         assertEquals("xbxxbx", sb.toString());
900 
901         sb = new StrBuilder("A1-A2A3-A4");
902         sb.replaceAll(A_NUMBER_MATCHER, "***");
903         assertEquals("***-******-***", sb.toString());
904 
905         sb = new StrBuilder("Dear X, hello X.");
906         sb.replaceAll(StrMatcher.stringMatcher("X"), "012345678901234567");
907         assertEquals("Dear 012345678901234567, hello 012345678901234567.", sb.toString());
908     }
909 
910     @Test
911     public void testReplaceFirst_StrMatcher_String() {
912         StrBuilder sb = new StrBuilder("abcbccba");
913         sb.replaceFirst((StrMatcher) null, null);
914         assertEquals("abcbccba", sb.toString());
915         sb.replaceFirst((StrMatcher) null, "anything");
916         assertEquals("abcbccba", sb.toString());
917         sb.replaceFirst(StrMatcher.noneMatcher(), null);
918         assertEquals("abcbccba", sb.toString());
919         sb.replaceFirst(StrMatcher.noneMatcher(), "anything");
920         assertEquals("abcbccba", sb.toString());
921 
922         sb.replaceFirst(StrMatcher.charMatcher('x'), "y");
923         assertEquals("abcbccba", sb.toString());
924         sb.replaceFirst(StrMatcher.charMatcher('a'), "d");
925         assertEquals("dbcbccba", sb.toString());
926         sb.replaceFirst(StrMatcher.charMatcher('d'), null);
927         assertEquals("bcbccba", sb.toString());
928         sb.replaceFirst(StrMatcher.stringMatcher("cb"), "-");
929         assertEquals("b-ccba", sb.toString());
930 
931         sb = new StrBuilder("abcba");
932         sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx");
933         assertEquals("axbxcba", sb.toString());
934 
935         sb = new StrBuilder("bb");
936         sb.replaceFirst(StrMatcher.charMatcher('b'), "xbx");
937         assertEquals("xbxb", sb.toString());
938 
939         sb = new StrBuilder("A1-A2A3-A4");
940         sb.replaceFirst(A_NUMBER_MATCHER, "***");
941         assertEquals("***-A2A3-A4", sb.toString());
942     }
943 
944     //-----------------------------------------------------------------------
945     @Test
946     public void testReplace_StrMatcher_String_int_int_int_VaryMatcher() {
947         StrBuilder sb = new StrBuilder("abcbccba");
948         sb.replace(null, "x", 0, sb.length(), -1);
949         assertEquals("abcbccba", sb.toString());
950 
951         sb.replace(StrMatcher.charMatcher('a'), "x", 0, sb.length(), -1);
952         assertEquals("xbcbccbx", sb.toString());
953 
954         sb.replace(StrMatcher.stringMatcher("cb"), "x", 0, sb.length(), -1);
955         assertEquals("xbxcxx", sb.toString());
956 
957         sb = new StrBuilder("A1-A2A3-A4");
958         sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1);
959         assertEquals("***-******-***", sb.toString());
960 
961         sb = new StrBuilder();
962         sb.replace(A_NUMBER_MATCHER, "***", 0, sb.length(), -1);
963         assertEquals("", sb.toString());
964     }
965 
966     @Test
967     public void testReplace_StrMatcher_String_int_int_int_VaryReplace() {
968         StrBuilder sb = new StrBuilder("abcbccba");
969         sb.replace(StrMatcher.stringMatcher("cb"), "cb", 0, sb.length(), -1);
970         assertEquals("abcbccba", sb.toString());
971 
972         sb = new StrBuilder("abcbccba");
973         sb.replace(StrMatcher.stringMatcher("cb"), "-", 0, sb.length(), -1);
974         assertEquals("ab-c-a", sb.toString());
975 
976         sb = new StrBuilder("abcbccba");
977         sb.replace(StrMatcher.stringMatcher("cb"), "+++", 0, sb.length(), -1);
978         assertEquals("ab+++c+++a", sb.toString());
979 
980         sb = new StrBuilder("abcbccba");
981         sb.replace(StrMatcher.stringMatcher("cb"), "", 0, sb.length(), -1);
982         assertEquals("abca", sb.toString());
983 
984         sb = new StrBuilder("abcbccba");
985         sb.replace(StrMatcher.stringMatcher("cb"), null, 0, sb.length(), -1);
986         assertEquals("abca", sb.toString());
987     }
988 
989     @Test
990     public void testReplace_StrMatcher_String_int_int_int_VaryStartIndex() {
991         StrBuilder sb = new StrBuilder("aaxaaaayaa");
992         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, sb.length(), -1);
993         assertEquals("-x--y-", sb.toString());
994 
995         sb = new StrBuilder("aaxaaaayaa");
996         sb.replace(StrMatcher.stringMatcher("aa"), "-", 1, sb.length(), -1);
997         assertEquals("aax--y-", sb.toString());
998 
999         sb = new StrBuilder("aaxaaaayaa");
1000         sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, sb.length(), -1);
1001         assertEquals("aax--y-", sb.toString());
1002 
1003         sb = new StrBuilder("aaxaaaayaa");
1004         sb.replace(StrMatcher.stringMatcher("aa"), "-", 3, sb.length(), -1);
1005         assertEquals("aax--y-", sb.toString());
1006 
1007         sb = new StrBuilder("aaxaaaayaa");
1008         sb.replace(StrMatcher.stringMatcher("aa"), "-", 4, sb.length(), -1);
1009         assertEquals("aaxa-ay-", sb.toString());
1010 
1011         sb = new StrBuilder("aaxaaaayaa");
1012         sb.replace(StrMatcher.stringMatcher("aa"), "-", 5, sb.length(), -1);
1013         assertEquals("aaxaa-y-", sb.toString());
1014 
1015         sb = new StrBuilder("aaxaaaayaa");
1016         sb.replace(StrMatcher.stringMatcher("aa"), "-", 6, sb.length(), -1);
1017         assertEquals("aaxaaaay-", sb.toString());
1018 
1019         sb = new StrBuilder("aaxaaaayaa");
1020         sb.replace(StrMatcher.stringMatcher("aa"), "-", 7, sb.length(), -1);
1021         assertEquals("aaxaaaay-", sb.toString());
1022 
1023         sb = new StrBuilder("aaxaaaayaa");
1024         sb.replace(StrMatcher.stringMatcher("aa"), "-", 8, sb.length(), -1);
1025         assertEquals("aaxaaaay-", sb.toString());
1026 
1027         sb = new StrBuilder("aaxaaaayaa");
1028         sb.replace(StrMatcher.stringMatcher("aa"), "-", 9, sb.length(), -1);
1029         assertEquals("aaxaaaayaa", sb.toString());
1030 
1031         sb = new StrBuilder("aaxaaaayaa");
1032         sb.replace(StrMatcher.stringMatcher("aa"), "-", 10, sb.length(), -1);
1033         assertEquals("aaxaaaayaa", sb.toString());
1034 
1035         sb = new StrBuilder("aaxaaaayaa");
1036         try {
1037             sb.replace(StrMatcher.stringMatcher("aa"), "-", 11, sb.length(), -1);
1038             fail();
1039         } catch (final IndexOutOfBoundsException ex) {}
1040         assertEquals("aaxaaaayaa", sb.toString());
1041 
1042         sb = new StrBuilder("aaxaaaayaa");
1043         try {
1044             sb.replace(StrMatcher.stringMatcher("aa"), "-", -1, sb.length(), -1);
1045             fail();
1046         } catch (final IndexOutOfBoundsException ex) {}
1047         assertEquals("aaxaaaayaa", sb.toString());
1048     }
1049 
1050     @Test
1051     public void testReplace_StrMatcher_String_int_int_int_VaryEndIndex() {
1052         StrBuilder sb = new StrBuilder("aaxaaaayaa");
1053         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 0, -1);
1054         assertEquals("aaxaaaayaa", sb.toString());
1055 
1056         sb = new StrBuilder("aaxaaaayaa");
1057         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 2, -1);
1058         assertEquals("-xaaaayaa", sb.toString());
1059 
1060         sb = new StrBuilder("aaxaaaayaa");
1061         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 3, -1);
1062         assertEquals("-xaaaayaa", sb.toString());
1063 
1064         sb = new StrBuilder("aaxaaaayaa");
1065         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 4, -1);
1066         assertEquals("-xaaaayaa", sb.toString());
1067 
1068         sb = new StrBuilder("aaxaaaayaa");
1069         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 5, -1);
1070         assertEquals("-x-aayaa", sb.toString());
1071 
1072         sb = new StrBuilder("aaxaaaayaa");
1073         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 6, -1);
1074         assertEquals("-x-aayaa", sb.toString());
1075 
1076         sb = new StrBuilder("aaxaaaayaa");
1077         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 7, -1);
1078         assertEquals("-x--yaa", sb.toString());
1079 
1080         sb = new StrBuilder("aaxaaaayaa");
1081         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 8, -1);
1082         assertEquals("-x--yaa", sb.toString());
1083 
1084         sb = new StrBuilder("aaxaaaayaa");
1085         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 9, -1);
1086         assertEquals("-x--yaa", sb.toString());
1087 
1088         sb = new StrBuilder("aaxaaaayaa");
1089         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1);
1090         assertEquals("-x--y-", sb.toString());
1091 
1092         sb = new StrBuilder("aaxaaaayaa");
1093         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 1000, -1);
1094         assertEquals("-x--y-", sb.toString());
1095 
1096         sb = new StrBuilder("aaxaaaayaa");
1097         try {
1098             sb.replace(StrMatcher.stringMatcher("aa"), "-", 2, 1, -1);
1099             fail();
1100         } catch (final IndexOutOfBoundsException ex) {}
1101         assertEquals("aaxaaaayaa", sb.toString());
1102     }
1103 
1104     @Test
1105     public void testReplace_StrMatcher_String_int_int_int_VaryCount() {
1106         StrBuilder sb = new StrBuilder("aaxaaaayaa");
1107         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, -1);
1108         assertEquals("-x--y-", sb.toString());
1109 
1110         sb = new StrBuilder("aaxaaaayaa");
1111         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 0);
1112         assertEquals("aaxaaaayaa", sb.toString());
1113 
1114         sb = new StrBuilder("aaxaaaayaa");
1115         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 1);
1116         assertEquals("-xaaaayaa", sb.toString());
1117 
1118         sb = new StrBuilder("aaxaaaayaa");
1119         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 2);
1120         assertEquals("-x-aayaa", sb.toString());
1121 
1122         sb = new StrBuilder("aaxaaaayaa");
1123         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 3);
1124         assertEquals("-x--yaa", sb.toString());
1125 
1126         sb = new StrBuilder("aaxaaaayaa");
1127         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 4);
1128         assertEquals("-x--y-", sb.toString());
1129 
1130         sb = new StrBuilder("aaxaaaayaa");
1131         sb.replace(StrMatcher.stringMatcher("aa"), "-", 0, 10, 5);
1132         assertEquals("-x--y-", sb.toString());
1133     }
1134 
1135     //-----------------------------------------------------------------------
1136     @Test
1137     public void testReverse() {
1138         final StrBuilder sb = new StrBuilder();
1139         assertEquals("", sb.reverse().toString());
1140 
1141         sb.clear().append(true);
1142         assertEquals("eurt", sb.reverse().toString());
1143         assertEquals("true", sb.reverse().toString());
1144     }
1145 
1146     //-----------------------------------------------------------------------
1147     @Test
1148     public void testTrim() {
1149         final StrBuilder sb = new StrBuilder();
1150         assertEquals("", sb.reverse().toString());
1151 
1152         sb.clear().append(" \u0000 ");
1153         assertEquals("", sb.trim().toString());
1154 
1155         sb.clear().append(" \u0000 a b c");
1156         assertEquals("a b c", sb.trim().toString());
1157 
1158         sb.clear().append("a b c \u0000 ");
1159         assertEquals("a b c", sb.trim().toString());
1160 
1161         sb.clear().append(" \u0000 a b c \u0000 ");
1162         assertEquals("a b c", sb.trim().toString());
1163 
1164         sb.clear().append("a b c");
1165         assertEquals("a b c", sb.trim().toString());
1166     }
1167 
1168     //-----------------------------------------------------------------------
1169     @Test
1170     public void testStartsWith() {
1171         final StrBuilder sb = new StrBuilder();
1172         assertFalse(sb.startsWith("a"));
1173         assertFalse(sb.startsWith(null));
1174         assertTrue(sb.startsWith(""));
1175         sb.append("abc");
1176         assertTrue(sb.startsWith("a"));
1177         assertTrue(sb.startsWith("ab"));
1178         assertTrue(sb.startsWith("abc"));
1179         assertFalse(sb.startsWith("cba"));
1180     }
1181 
1182     @Test
1183     public void testEndsWith() {
1184         final StrBuilder sb = new StrBuilder();
1185         assertFalse(sb.endsWith("a"));
1186         assertFalse(sb.endsWith("c"));
1187         assertTrue(sb.endsWith(""));
1188         assertFalse(sb.endsWith(null));
1189         sb.append("abc");
1190         assertTrue(sb.endsWith("c"));
1191         assertTrue(sb.endsWith("bc"));
1192         assertTrue(sb.endsWith("abc"));
1193         assertFalse(sb.endsWith("cba"));
1194         assertFalse(sb.endsWith("abcd"));
1195         assertFalse(sb.endsWith(" abc"));
1196         assertFalse(sb.endsWith("abc "));
1197     }
1198 
1199     //-----------------------------------------------------------------------
1200     @Test
1201     public void testSubSequenceIntInt() {
1202        final StrBuilder sb = new StrBuilder ("hello goodbye");
1203        // Start index is negative
1204        try {
1205             sb.subSequence(-1, 5);
1206             fail();
1207         } catch (final IndexOutOfBoundsException e) {}
1208 
1209         // End index is negative
1210        try {
1211             sb.subSequence(2, -1);
1212             fail();
1213         } catch (final IndexOutOfBoundsException e) {}
1214 
1215         // End index greater than length()
1216         try {
1217             sb.subSequence(2, sb.length() + 1);
1218             fail();
1219         } catch (final IndexOutOfBoundsException e) {}
1220 
1221         // Start index greater then end index
1222         try {
1223             sb.subSequence(3, 2);
1224             fail();
1225         } catch (final IndexOutOfBoundsException e) {}
1226 
1227         // Normal cases
1228         assertEquals ("hello", sb.subSequence(0, 5));
1229         assertEquals ("hello goodbye".subSequence(0, 6), sb.subSequence(0, 6));
1230         assertEquals ("goodbye", sb.subSequence(6, 13));
1231         assertEquals ("hello goodbye".subSequence(6,13), sb.subSequence(6, 13));
1232     }
1233 
1234     @Test
1235     public void testSubstringInt() {
1236         final StrBuilder sb = new StrBuilder ("hello goodbye");
1237         assertEquals ("goodbye", sb.substring(6));
1238         assertEquals ("hello goodbye".substring(6), sb.substring(6));
1239         assertEquals ("hello goodbye", sb.substring(0));
1240         assertEquals ("hello goodbye".substring(0), sb.substring(0));
1241         try {
1242             sb.substring(-1);
1243             fail ();
1244         } catch (final IndexOutOfBoundsException e) {}
1245 
1246         try {
1247             sb.substring(15);
1248             fail ();
1249         } catch (final IndexOutOfBoundsException e) {}
1250 
1251     }
1252 
1253     @Test
1254     public void testSubstringIntInt() {
1255         final StrBuilder sb = new StrBuilder ("hello goodbye");
1256         assertEquals ("hello", sb.substring(0, 5));
1257         assertEquals ("hello goodbye".substring(0, 6), sb.substring(0, 6));
1258 
1259         assertEquals ("goodbye", sb.substring(6, 13));
1260         assertEquals ("hello goodbye".substring(6,13), sb.substring(6, 13));
1261 
1262         assertEquals ("goodbye", sb.substring(6, 20));
1263 
1264         try {
1265             sb.substring(-1, 5);
1266             fail();
1267         } catch (final IndexOutOfBoundsException e) {}
1268 
1269         try {
1270             sb.substring(15, 20);
1271             fail();
1272         } catch (final IndexOutOfBoundsException e) {}
1273     }
1274 
1275     // -----------------------------------------------------------------------
1276     @Test
1277     public void testMidString() {
1278         final StrBuilder sb = new StrBuilder("hello goodbye hello");
1279         assertEquals("goodbye", sb.midString(6, 7));
1280         assertEquals("hello", sb.midString(0, 5));
1281         assertEquals("hello", sb.midString(-5, 5));
1282         assertEquals("", sb.midString(0, -1));
1283         assertEquals("", sb.midString(20, 2));
1284         assertEquals("hello", sb.midString(14, 22));
1285     }
1286 
1287     @Test
1288     public void testRightString() {
1289         final StrBuilder sb = new StrBuilder("left right");
1290         assertEquals("right", sb.rightString(5));
1291         assertEquals("", sb.rightString(0));
1292         assertEquals("", sb.rightString(-5));
1293         assertEquals("left right", sb.rightString(15));
1294     }
1295 
1296     @Test
1297     public void testLeftString() {
1298         final StrBuilder sb = new StrBuilder("left right");
1299         assertEquals("left", sb.leftString(4));
1300         assertEquals("", sb.leftString(0));
1301         assertEquals("", sb.leftString(-5));
1302         assertEquals("left right", sb.leftString(15));
1303     }
1304 
1305     // -----------------------------------------------------------------------
1306     @Test
1307     public void testContains_char() {
1308         final StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1309         assertTrue(sb.contains('a'));
1310         assertTrue(sb.contains('o'));
1311         assertTrue(sb.contains('z'));
1312         assertFalse(sb.contains('1'));
1313     }
1314 
1315     @Test
1316     public void testContains_String() {
1317         final StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1318         assertTrue(sb.contains("a"));
1319         assertTrue(sb.contains("pq"));
1320         assertTrue(sb.contains("z"));
1321         assertFalse(sb.contains("zyx"));
1322         assertFalse(sb.contains((String) null));
1323     }
1324 
1325     @Test
1326     public void testContains_StrMatcher() {
1327         StrBuilder sb = new StrBuilder("abcdefghijklmnopqrstuvwxyz");
1328         assertTrue(sb.contains(StrMatcher.charMatcher('a')));
1329         assertTrue(sb.contains(StrMatcher.stringMatcher("pq")));
1330         assertTrue(sb.contains(StrMatcher.charMatcher('z')));
1331         assertFalse(sb.contains(StrMatcher.stringMatcher("zy")));
1332         assertFalse(sb.contains((StrMatcher) null));
1333 
1334         sb = new StrBuilder();
1335         assertFalse(sb.contains(A_NUMBER_MATCHER));
1336         sb.append("B A1 C");
1337         assertTrue(sb.contains(A_NUMBER_MATCHER));
1338     }
1339 
1340     // -----------------------------------------------------------------------
1341     @Test
1342     public void testIndexOf_char() {
1343         final StrBuilder sb = new StrBuilder("abab");
1344         assertEquals(0, sb.indexOf('a'));
1345 
1346         // should work like String#indexOf
1347         assertEquals("abab".indexOf('a'), sb.indexOf('a'));
1348 
1349         assertEquals(1, sb.indexOf('b'));
1350         assertEquals("abab".indexOf('b'), sb.indexOf('b'));
1351 
1352         assertEquals(-1, sb.indexOf('z'));
1353     }
1354 
1355     @Test
1356     public void testIndexOf_char_int() {
1357         StrBuilder sb = new StrBuilder("abab");
1358         assertEquals(0, sb.indexOf('a', -1));
1359         assertEquals(0, sb.indexOf('a', 0));
1360         assertEquals(2, sb.indexOf('a', 1));
1361         assertEquals(-1, sb.indexOf('a', 4));
1362         assertEquals(-1, sb.indexOf('a', 5));
1363 
1364         // should work like String#indexOf
1365         assertEquals("abab".indexOf('a', 1), sb.indexOf('a', 1));
1366 
1367         assertEquals(3, sb.indexOf('b', 2));
1368         assertEquals("abab".indexOf('b', 2), sb.indexOf('b', 2));
1369 
1370         assertEquals(-1, sb.indexOf('z', 2));
1371 
1372         sb = new StrBuilder("xyzabc");
1373         assertEquals(2, sb.indexOf('z', 0));
1374         assertEquals(-1, sb.indexOf('z', 3));
1375     }
1376 
1377     @Test
1378     public void testLastIndexOf_char() {
1379         final StrBuilder sb = new StrBuilder("abab");
1380 
1381         assertEquals (2, sb.lastIndexOf('a'));
1382         //should work like String#lastIndexOf
1383         assertEquals ("abab".lastIndexOf('a'), sb.lastIndexOf('a'));
1384 
1385         assertEquals(3, sb.lastIndexOf('b'));
1386         assertEquals ("abab".lastIndexOf('b'), sb.lastIndexOf('b'));
1387 
1388         assertEquals (-1, sb.lastIndexOf('z'));
1389     }
1390 
1391     @Test
1392     public void testLastIndexOf_char_int() {
1393         StrBuilder sb = new StrBuilder("abab");
1394         assertEquals(-1, sb.lastIndexOf('a', -1));
1395         assertEquals(0, sb.lastIndexOf('a', 0));
1396         assertEquals(0, sb.lastIndexOf('a', 1));
1397 
1398         // should work like String#lastIndexOf
1399         assertEquals("abab".lastIndexOf('a', 1), sb.lastIndexOf('a', 1));
1400 
1401         assertEquals(1, sb.lastIndexOf('b', 2));
1402         assertEquals("abab".lastIndexOf('b', 2), sb.lastIndexOf('b', 2));
1403 
1404         assertEquals(-1, sb.lastIndexOf('z', 2));
1405 
1406         sb = new StrBuilder("xyzabc");
1407         assertEquals(2, sb.lastIndexOf('z', sb.length()));
1408         assertEquals(-1, sb.lastIndexOf('z', 1));
1409     }
1410 
1411     // -----------------------------------------------------------------------
1412     @Test
1413     public void testIndexOf_String() {
1414         final StrBuilder sb = new StrBuilder("abab");
1415 
1416         assertEquals(0, sb.indexOf("a"));
1417         //should work like String#indexOf
1418         assertEquals("abab".indexOf("a"), sb.indexOf("a"));
1419 
1420         assertEquals(0, sb.indexOf("ab"));
1421         //should work like String#indexOf
1422         assertEquals("abab".indexOf("ab"), sb.indexOf("ab"));
1423 
1424         assertEquals(1, sb.indexOf("b"));
1425         assertEquals("abab".indexOf("b"), sb.indexOf("b"));
1426 
1427         assertEquals(1, sb.indexOf("ba"));
1428         assertEquals("abab".indexOf("ba"), sb.indexOf("ba"));
1429 
1430         assertEquals(-1, sb.indexOf("z"));
1431 
1432         assertEquals(-1, sb.indexOf((String) null));
1433     }
1434 
1435     @Test
1436     public void testIndexOf_String_int() {
1437         StrBuilder sb = new StrBuilder("abab");
1438         assertEquals(0, sb.indexOf("a", -1));
1439         assertEquals(0, sb.indexOf("a", 0));
1440         assertEquals(2, sb.indexOf("a", 1));
1441         assertEquals(2, sb.indexOf("a", 2));
1442         assertEquals(-1, sb.indexOf("a", 3));
1443         assertEquals(-1, sb.indexOf("a", 4));
1444         assertEquals(-1, sb.indexOf("a", 5));
1445 
1446         assertEquals(-1, sb.indexOf("abcdef", 0));
1447         assertEquals(0, sb.indexOf("", 0));
1448         assertEquals(1, sb.indexOf("", 1));
1449 
1450         //should work like String#indexOf
1451         assertEquals ("abab".indexOf("a", 1), sb.indexOf("a", 1));
1452 
1453         assertEquals(2, sb.indexOf("ab", 1));
1454         //should work like String#indexOf
1455         assertEquals("abab".indexOf("ab", 1), sb.indexOf("ab", 1));
1456 
1457         assertEquals(3, sb.indexOf("b", 2));
1458         assertEquals("abab".indexOf("b", 2), sb.indexOf("b", 2));
1459 
1460         assertEquals(1, sb.indexOf("ba", 1));
1461         assertEquals("abab".indexOf("ba", 2), sb.indexOf("ba", 2));
1462 
1463         assertEquals(-1, sb.indexOf("z", 2));
1464 
1465         sb = new StrBuilder("xyzabc");
1466         assertEquals(2, sb.indexOf("za", 0));
1467         assertEquals(-1, sb.indexOf("za", 3));
1468 
1469         assertEquals(-1, sb.indexOf((String) null, 2));
1470     }
1471 
1472     @Test
1473     public void testLastIndexOf_String() {
1474         final StrBuilder sb = new StrBuilder("abab");
1475 
1476         assertEquals(2, sb.lastIndexOf("a"));
1477         //should work like String#lastIndexOf
1478         assertEquals("abab".lastIndexOf("a"), sb.lastIndexOf("a"));
1479 
1480         assertEquals(2, sb.lastIndexOf("ab"));
1481         //should work like String#lastIndexOf
1482         assertEquals("abab".lastIndexOf("ab"), sb.lastIndexOf("ab"));
1483 
1484         assertEquals(3, sb.lastIndexOf("b"));
1485         assertEquals("abab".lastIndexOf("b"), sb.lastIndexOf("b"));
1486 
1487         assertEquals(1, sb.lastIndexOf("ba"));
1488         assertEquals("abab".lastIndexOf("ba"), sb.lastIndexOf("ba"));
1489 
1490         assertEquals(-1, sb.lastIndexOf("z"));
1491 
1492         assertEquals(-1, sb.lastIndexOf((String) null));
1493     }
1494 
1495     @Test
1496     public void testLastIndexOf_String_int() {
1497         StrBuilder sb = new StrBuilder("abab");
1498         assertEquals(-1, sb.lastIndexOf("a", -1));
1499         assertEquals(0, sb.lastIndexOf("a", 0));
1500         assertEquals(0, sb.lastIndexOf("a", 1));
1501         assertEquals(2, sb.lastIndexOf("a", 2));
1502         assertEquals(2, sb.lastIndexOf("a", 3));
1503         assertEquals(2, sb.lastIndexOf("a", 4));
1504         assertEquals(2, sb.lastIndexOf("a", 5));
1505 
1506         assertEquals(-1, sb.lastIndexOf("abcdef", 3));
1507         assertEquals("abab".lastIndexOf("", 3), sb.lastIndexOf("", 3));
1508         assertEquals("abab".lastIndexOf("", 1), sb.lastIndexOf("", 1));
1509 
1510         //should work like String#lastIndexOf
1511         assertEquals("abab".lastIndexOf("a", 1), sb.lastIndexOf("a", 1));
1512 
1513         assertEquals(0, sb.lastIndexOf("ab", 1));
1514         //should work like String#lastIndexOf
1515         assertEquals("abab".lastIndexOf("ab", 1), sb.lastIndexOf("ab", 1));
1516 
1517         assertEquals(1, sb.lastIndexOf("b", 2));
1518         assertEquals("abab".lastIndexOf("b", 2), sb.lastIndexOf("b", 2));
1519 
1520         assertEquals(1, sb.lastIndexOf("ba", 2));
1521         assertEquals("abab".lastIndexOf("ba", 2), sb.lastIndexOf("ba", 2));
1522 
1523         assertEquals(-1, sb.lastIndexOf("z", 2));
1524 
1525         sb = new StrBuilder("xyzabc");
1526         assertEquals(2, sb.lastIndexOf("za", sb.length()));
1527         assertEquals(-1, sb.lastIndexOf("za", 1));
1528 
1529         assertEquals(-1, sb.lastIndexOf((String) null, 2));
1530     }
1531 
1532     // -----------------------------------------------------------------------
1533     @Test
1534     public void testIndexOf_StrMatcher() {
1535         final StrBuilder sb = new StrBuilder();
1536         assertEquals(-1, sb.indexOf((StrMatcher) null));
1537         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a')));
1538 
1539         sb.append("ab bd");
1540         assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a')));
1541         assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b')));
1542         assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher()));
1543         assertEquals(4, sb.indexOf(StrMatcher.charMatcher('d')));
1544         assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher()));
1545         assertEquals(-1, sb.indexOf((StrMatcher) null));
1546 
1547         sb.append(" A1 junction");
1548         assertEquals(6, sb.indexOf(A_NUMBER_MATCHER));
1549     }
1550 
1551     @Test
1552     public void testIndexOf_StrMatcher_int() {
1553         final StrBuilder sb = new StrBuilder();
1554         assertEquals(-1, sb.indexOf((StrMatcher) null, 2));
1555         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2));
1556         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 0));
1557 
1558         sb.append("ab bd");
1559         assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), -2));
1560         assertEquals(0, sb.indexOf(StrMatcher.charMatcher('a'), 0));
1561         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 2));
1562         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('a'), 20));
1563 
1564         assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), -1));
1565         assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 0));
1566         assertEquals(1, sb.indexOf(StrMatcher.charMatcher('b'), 1));
1567         assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 2));
1568         assertEquals(3, sb.indexOf(StrMatcher.charMatcher('b'), 3));
1569         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 4));
1570         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 5));
1571         assertEquals(-1, sb.indexOf(StrMatcher.charMatcher('b'), 6));
1572 
1573         assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), -2));
1574         assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 0));
1575         assertEquals(2, sb.indexOf(StrMatcher.spaceMatcher(), 2));
1576         assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 4));
1577         assertEquals(-1, sb.indexOf(StrMatcher.spaceMatcher(), 20));
1578 
1579         assertEquals(-1, sb.indexOf(StrMatcher.noneMatcher(), 0));
1580         assertEquals(-1, sb.indexOf((StrMatcher) null, 0));
1581 
1582         sb.append(" A1 junction with A2");
1583         assertEquals(6, sb.indexOf(A_NUMBER_MATCHER, 5));
1584         assertEquals(6, sb.indexOf(A_NUMBER_MATCHER, 6));
1585         assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 7));
1586         assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 22));
1587         assertEquals(23, sb.indexOf(A_NUMBER_MATCHER, 23));
1588         assertEquals(-1, sb.indexOf(A_NUMBER_MATCHER, 24));
1589     }
1590 
1591     @Test
1592     public void testLastIndexOf_StrMatcher() {
1593         final StrBuilder sb = new StrBuilder();
1594         assertEquals(-1, sb.lastIndexOf((StrMatcher) null));
1595         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a')));
1596 
1597         sb.append("ab bd");
1598         assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a')));
1599         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b')));
1600         assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher()));
1601         assertEquals(4, sb.lastIndexOf(StrMatcher.charMatcher('d')));
1602         assertEquals(-1, sb.lastIndexOf(StrMatcher.noneMatcher()));
1603         assertEquals(-1, sb.lastIndexOf((StrMatcher) null));
1604 
1605         sb.append(" A1 junction");
1606         assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER));
1607     }
1608 
1609     @Test
1610     public void testLastIndexOf_StrMatcher_int() {
1611         final StrBuilder sb = new StrBuilder();
1612         assertEquals(-1, sb.lastIndexOf((StrMatcher) null, 2));
1613         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), 2));
1614         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), 0));
1615         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), -1));
1616 
1617         sb.append("ab bd");
1618         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('a'), -2));
1619         assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 0));
1620         assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 2));
1621         assertEquals(0, sb.lastIndexOf(StrMatcher.charMatcher('a'), 20));
1622 
1623         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('b'), -1));
1624         assertEquals(-1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 0));
1625         assertEquals(1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 1));
1626         assertEquals(1, sb.lastIndexOf(StrMatcher.charMatcher('b'), 2));
1627         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 3));
1628         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 4));
1629         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 5));
1630         assertEquals(3, sb.lastIndexOf(StrMatcher.charMatcher('b'), 6));
1631 
1632         assertEquals(-1, sb.lastIndexOf(StrMatcher.spaceMatcher(), -2));
1633         assertEquals(-1, sb.lastIndexOf(StrMatcher.spaceMatcher(), 0));
1634         assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 2));
1635         assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 4));
1636         assertEquals(2, sb.lastIndexOf(StrMatcher.spaceMatcher(), 20));
1637 
1638         assertEquals(-1, sb.lastIndexOf(StrMatcher.noneMatcher(), 0));
1639         assertEquals(-1, sb.lastIndexOf((StrMatcher) null, 0));
1640 
1641         sb.append(" A1 junction with A2");
1642         assertEquals(-1, sb.lastIndexOf(A_NUMBER_MATCHER, 5));
1643         assertEquals(-1, sb.lastIndexOf(A_NUMBER_MATCHER, 6)); // A matches, 1 is outside bounds
1644         assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 7));
1645         assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 22));
1646         assertEquals(6, sb.lastIndexOf(A_NUMBER_MATCHER, 23)); // A matches, 2 is outside bounds
1647         assertEquals(23, sb.lastIndexOf(A_NUMBER_MATCHER, 24));
1648     }
1649 
1650     static final StrMatcher A_NUMBER_MATCHER = new StrMatcher() {
1651         @Override
1652         public int isMatch(final char[] buffer, int pos, final int bufferStart, final int bufferEnd) {
1653             if (buffer[pos] == 'A') {
1654                 pos++;
1655                 if (pos < bufferEnd && buffer[pos] >= '0' && buffer[pos] <= '9') {
1656                     return 2;
1657                 }
1658             }
1659             return 0;
1660         }
1661     };
1662 
1663     //-----------------------------------------------------------------------
1664     @Test
1665     public void testAsTokenizer() throws Exception {
1666         // from Javadoc
1667         final StrBuilder b = new StrBuilder();
1668         b.append("a b ");
1669         final StrTokenizer t = b.asTokenizer();
1670 
1671         final String[] tokens1 = t.getTokenArray();
1672         assertEquals(2, tokens1.length);
1673         assertEquals("a", tokens1[0]);
1674         assertEquals("b", tokens1[1]);
1675         assertEquals(2, t.size());
1676 
1677         b.append("c d ");
1678         final String[] tokens2 = t.getTokenArray();
1679         assertEquals(2, tokens2.length);
1680         assertEquals("a", tokens2[0]);
1681         assertEquals("b", tokens2[1]);
1682         assertEquals(2, t.size());
1683         assertEquals("a", t.next());
1684         assertEquals("b", t.next());
1685 
1686         t.reset();
1687         final String[] tokens3 = t.getTokenArray();
1688         assertEquals(4, tokens3.length);
1689         assertEquals("a", tokens3[0]);
1690         assertEquals("b", tokens3[1]);
1691         assertEquals("c", tokens3[2]);
1692         assertEquals("d", tokens3[3]);
1693         assertEquals(4, t.size());
1694         assertEquals("a", t.next());
1695         assertEquals("b", t.next());
1696         assertEquals("c", t.next());
1697         assertEquals("d", t.next());
1698 
1699         assertEquals("a b c d ", t.getContent());
1700     }
1701 
1702     // -----------------------------------------------------------------------
1703     @Test
1704     public void testAsReader() throws Exception {
1705         final StrBuilder sb = new StrBuilder("some text");
1706         Reader reader = sb.asReader();
1707         assertTrue(reader.ready());
1708         final char[] buf = new char[40];
1709         assertEquals(9, reader.read(buf));
1710         assertEquals("some text", new String(buf, 0, 9));
1711 
1712         assertEquals(-1, reader.read());
1713         assertFalse(reader.ready());
1714         assertEquals(0, reader.skip(2));
1715         assertEquals(0, reader.skip(-1));
1716 
1717         assertTrue(reader.markSupported());
1718         reader = sb.asReader();
1719         assertEquals('s', reader.read());
1720         reader.mark(-1);
1721         char[] array = new char[3];
1722         assertEquals(3, reader.read(array, 0, 3));
1723         assertEquals('o', array[0]);
1724         assertEquals('m', array[1]);
1725         assertEquals('e', array[2]);
1726         reader.reset();
1727         assertEquals(1, reader.read(array, 1, 1));
1728         assertEquals('o', array[0]);
1729         assertEquals('o', array[1]);
1730         assertEquals('e', array[2]);
1731         assertEquals(2, reader.skip(2));
1732         assertEquals(' ', reader.read());
1733 
1734         assertTrue(reader.ready());
1735         reader.close();
1736         assertTrue(reader.ready());
1737 
1738         reader = sb.asReader();
1739         array = new char[3];
1740         try {
1741             reader.read(array, -1, 0);
1742             fail();
1743         } catch (final IndexOutOfBoundsException ex) {}
1744         try {
1745             reader.read(array, 0, -1);
1746             fail();
1747         } catch (final IndexOutOfBoundsException ex) {}
1748         try {
1749             reader.read(array, 100, 1);
1750             fail();
1751         } catch (final IndexOutOfBoundsException ex) {}
1752         try {
1753             reader.read(array, 0, 100);
1754             fail();
1755         } catch (final IndexOutOfBoundsException ex) {}
1756         try {
1757             reader.read(array, Integer.MAX_VALUE, Integer.MAX_VALUE);
1758             fail();
1759         } catch (final IndexOutOfBoundsException ex) {}
1760 
1761         assertEquals(0, reader.read(array, 0, 0));
1762         assertEquals(0, array[0]);
1763         assertEquals(0, array[1]);
1764         assertEquals(0, array[2]);
1765 
1766         reader.skip(9);
1767         assertEquals(-1, reader.read(array, 0, 1));
1768 
1769         reader.reset();
1770         array = new char[30];
1771         assertEquals(9, reader.read(array, 0, 30));
1772     }
1773 
1774     //-----------------------------------------------------------------------
1775     @Test
1776     public void testAsWriter() throws Exception {
1777         final StrBuilder sb = new StrBuilder("base");
1778         final Writer writer = sb.asWriter();
1779 
1780         writer.write('l');
1781         assertEquals("basel", sb.toString());
1782 
1783         writer.write(new char[] {'i', 'n'});
1784         assertEquals("baselin", sb.toString());
1785 
1786         writer.write(new char[] {'n', 'e', 'r'}, 1, 2);
1787         assertEquals("baseliner", sb.toString());
1788 
1789         writer.write(" rout");
1790         assertEquals("baseliner rout", sb.toString());
1791 
1792         writer.write("ping that server", 1, 3);
1793         assertEquals("baseliner routing", sb.toString());
1794 
1795         writer.flush();  // no effect
1796         assertEquals("baseliner routing", sb.toString());
1797 
1798         writer.close();  // no effect
1799         assertEquals("baseliner routing", sb.toString());
1800 
1801         writer.write(" hi");  // works after close
1802         assertEquals("baseliner routing hi", sb.toString());
1803 
1804         sb.setLength(4);  // mix and match
1805         writer.write('d');
1806         assertEquals("based", sb.toString());
1807     }
1808 
1809     //-----------------------------------------------------------------------
1810     @Test
1811     public void testEqualsIgnoreCase() {
1812         final StrBuilder sb1 = new StrBuilder();
1813         final StrBuilder sb2 = new StrBuilder();
1814         assertTrue(sb1.equalsIgnoreCase(sb1));
1815         assertTrue(sb1.equalsIgnoreCase(sb2));
1816         assertTrue(sb2.equalsIgnoreCase(sb2));
1817 
1818         sb1.append("abc");
1819         assertFalse(sb1.equalsIgnoreCase(sb2));
1820 
1821         sb2.append("ABC");
1822         assertTrue(sb1.equalsIgnoreCase(sb2));
1823 
1824         sb2.clear().append("abc");
1825         assertTrue(sb1.equalsIgnoreCase(sb2));
1826         assertTrue(sb1.equalsIgnoreCase(sb1));
1827         assertTrue(sb2.equalsIgnoreCase(sb2));
1828 
1829         sb2.clear().append("aBc");
1830         assertTrue(sb1.equalsIgnoreCase(sb2));
1831     }
1832 
1833     //-----------------------------------------------------------------------
1834     @Test
1835     public void testEquals() {
1836         final StrBuilder sb1 = new StrBuilder();
1837         final StrBuilder sb2 = new StrBuilder();
1838         assertTrue(sb1.equals(sb2));
1839         assertTrue(sb1.equals(sb1));
1840         assertTrue(sb2.equals(sb2));
1841         assertTrue(sb1.equals((Object) sb2));
1842 
1843         sb1.append("abc");
1844         assertFalse(sb1.equals(sb2));
1845         assertFalse(sb1.equals((Object) sb2));
1846 
1847         sb2.append("ABC");
1848         assertFalse(sb1.equals(sb2));
1849         assertFalse(sb1.equals((Object) sb2));
1850 
1851         sb2.clear().append("abc");
1852         assertTrue(sb1.equals(sb2));
1853         assertTrue(sb1.equals((Object) sb2));
1854 
1855         assertFalse(sb1.equals(Integer.valueOf(1)));
1856         assertFalse(sb1.equals("abc"));
1857     }
1858 
1859     @Test
1860     public void test_LANG_1131_EqualsWithNullStrBuilder() throws Exception {
1861         final StrBuilder sb = new StrBuilder();
1862         final StrBuilder other = null;
1863         assertFalse(sb.equals(other));
1864     }
1865 
1866     //-----------------------------------------------------------------------
1867     @Test
1868     public void testHashCode() {
1869         final StrBuilder sb = new StrBuilder();
1870         final int hc1a = sb.hashCode();
1871         final int hc1b = sb.hashCode();
1872         assertEquals(0, hc1a);
1873         assertEquals(hc1a, hc1b);
1874 
1875         sb.append("abc");
1876         final int hc2a = sb.hashCode();
1877         final int hc2b = sb.hashCode();
1878         assertTrue(hc2a != 0);
1879         assertEquals(hc2a, hc2b);
1880     }
1881 
1882     //-----------------------------------------------------------------------
1883     @Test
1884     public void testToString() {
1885         final StrBuilder sb = new StrBuilder("abc");
1886         assertEquals("abc", sb.toString());
1887     }
1888 
1889     //-----------------------------------------------------------------------
1890     @Test
1891     public void testToStringBuffer() {
1892         final StrBuilder sb = new StrBuilder();
1893         assertEquals(new StringBuffer().toString(), sb.toStringBuffer().toString());
1894 
1895         sb.append("junit");
1896         assertEquals(new StringBuffer("junit").toString(), sb.toStringBuffer().toString());
1897     }
1898 
1899     //-----------------------------------------------------------------------
1900     @Test
1901     public void testToStringBuilder() {
1902         final StrBuilder sb = new StrBuilder();
1903         assertEquals(new StringBuilder().toString(), sb.toStringBuilder().toString());
1904 
1905         sb.append("junit");
1906         assertEquals(new StringBuilder("junit").toString(), sb.toStringBuilder().toString());
1907     }
1908 
1909     //-----------------------------------------------------------------------
1910     @Test
1911     public void testLang294() {
1912         final StrBuilder sb = new StrBuilder("\n%BLAH%\nDo more stuff\neven more stuff\n%BLAH%\n");
1913         sb.deleteAll("\n%BLAH%");
1914         assertEquals("\nDo more stuff\neven more stuff\n", sb.toString());
1915     }
1916 
1917     @Test
1918     public void testIndexOfLang294() {
1919         final StrBuilder sb = new StrBuilder("onetwothree");
1920         sb.deleteFirst("three");
1921         assertEquals(-1, sb.indexOf("three"));
1922     }
1923 
1924     //-----------------------------------------------------------------------
1925     @Test
1926     public void testLang295() {
1927         final StrBuilder sb = new StrBuilder("onetwothree");
1928         sb.deleteFirst("three");
1929         assertFalse( "The contains(char) method is looking beyond the end of the string", sb.contains('h'));
1930         assertEquals( "The indexOf(char) method is looking beyond the end of the string", -1, sb.indexOf('h'));
1931     }
1932 
1933     //-----------------------------------------------------------------------
1934     @Test
1935     public void testLang412Right() {
1936         final StrBuilder sb = new StrBuilder();
1937         sb.appendFixedWidthPadRight(null, 10, '*');
1938         assertEquals( "Failed to invoke appendFixedWidthPadRight correctly", "**********", sb.toString());
1939     }
1940 
1941     @Test
1942     public void testLang412Left() {
1943         final StrBuilder sb = new StrBuilder();
1944         sb.appendFixedWidthPadLeft(null, 10, '*');
1945         assertEquals( "Failed to invoke appendFixedWidthPadLeft correctly", "**********", sb.toString());
1946     }
1947 
1948     @Test
1949     public void testAsBuilder() {
1950         final StrBuilder sb = new StrBuilder().appendAll("Lorem", " ", "ipsum", " ", "dolor");
1951         assertEquals(sb.toString(), sb.build());
1952     }
1953 
1954     //-----------------------------------------------------------------------
1955     @Test
1956     public void testAppendCharBuffer() {
1957         final StrBuilder sb1 = new StrBuilder();
1958         final CharBuffer buf = CharBuffer.allocate(10);
1959         buf.append("0123456789");
1960         buf.flip();
1961         sb1.append(buf);
1962         assertEquals("0123456789", sb1.toString());
1963 
1964         final StrBuilder sb2 = new StrBuilder();
1965         sb2.append(buf, 1, 8);
1966         assertEquals("12345678", sb2.toString());
1967     }
1968 
1969     //-----------------------------------------------------------------------
1970     @Test
1971     public void testAppendToWriter() throws Exception {
1972         final StrBuilder sb = new StrBuilder("1234567890");
1973         final StringWriter writer = new StringWriter();
1974         writer.append("Test ");
1975 
1976         sb.appendTo(writer);
1977 
1978         assertEquals("Test 1234567890", writer.toString());
1979     }
1980 
1981     @Test
1982     public void testAppendToStringBuilder() throws Exception {
1983         final StrBuilder sb = new StrBuilder("1234567890");
1984         final StringBuilder builder = new StringBuilder("Test ");
1985 
1986         sb.appendTo(builder);
1987 
1988         assertEquals("Test 1234567890", builder.toString());
1989     }
1990 
1991     @Test
1992     public void testAppendToStringBuffer() throws Exception {
1993         final StrBuilder sb = new StrBuilder("1234567890");
1994         final StringBuffer buffer = new StringBuffer("Test ");
1995 
1996         sb.appendTo(buffer);
1997 
1998         assertEquals("Test 1234567890", buffer.toString());
1999     }
2000 
2001     @Test
2002     public void testAppendToCharBuffer() throws Exception {
2003         final StrBuilder sb = new StrBuilder("1234567890");
2004         final String text = "Test ";
2005         final CharBuffer buffer = CharBuffer.allocate(sb.size() + text.length());
2006         buffer.put(text);
2007 
2008         sb.appendTo(buffer);
2009 
2010         buffer.flip();
2011         assertEquals("Test 1234567890", buffer.toString());
2012     }
2013 }