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