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