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 static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.text.DecimalFormatSymbols;
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Iterator;
28  
29  import org.apache.commons.lang3.AbstractLangTest;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Unit tests for {@link org.apache.commons.lang3.text.StrBuilder}.
34   */
35  @Deprecated
36  public class StrBuilderAppendInsertTest extends AbstractLangTest {
37  
38      /** The system line separator. */
39      private static final String SEP = System.lineSeparator();
40  
41      /** Test subclass of Object, with a toString method. */
42      private static final Object FOO = new Object() {
43          @Override
44          public String toString() {
45              return "foo";
46          }
47      };
48  
49      @Test
50      public void testAppend_Boolean() {
51          final StrBuilder sb = new StrBuilder();
52          sb.append(true);
53          assertEquals("true", sb.toString());
54  
55          sb.append(false);
56          assertEquals("truefalse", sb.toString());
57  
58          sb.append('!');
59          assertEquals("truefalse!", sb.toString());
60      }
61  
62      @Test
63      public void testAppend_CharArray() {
64          StrBuilder sb = new StrBuilder();
65          sb.setNullText("NULL").append((char[]) null);
66          assertEquals("NULL", sb.toString());
67  
68          sb = new StrBuilder();
69          sb.append(new char[0]);
70          assertEquals("", sb.toString());
71  
72          sb.append(new char[]{'f', 'o', 'o'});
73          assertEquals("foo", sb.toString());
74      }
75  
76      @Test
77      public void testAppend_CharArray_int_int() {
78          StrBuilder sb = new StrBuilder();
79          sb.setNullText("NULL").append((char[]) null, 0, 1);
80          assertEquals("NULL", sb.toString());
81  
82          sb = new StrBuilder();
83          sb.append(new char[]{'f', 'o', 'o'}, 0, 3);
84          assertEquals("foo", sb.toString());
85  
86          final StrBuilder sb1 = sb;
87          assertThrows(
88                  IndexOutOfBoundsException.class,
89                  () -> sb1.append(new char[]{'b', 'a', 'r'}, -1, 1),
90                  "append(char[], -1,) expected IndexOutOfBoundsException");
91  
92          assertThrows(
93                  IndexOutOfBoundsException.class,
94                  () -> sb1.append(new char[]{'b', 'a', 'r'}, 3, 1),
95                  "append(char[], 3,) expected IndexOutOfBoundsException");
96  
97          assertThrows(
98                  IndexOutOfBoundsException.class,
99                  () -> sb1.append(new char[]{'b', 'a', 'r'}, 1, -1),
100                 "append(char[],, -1) expected IndexOutOfBoundsException");
101 
102         assertThrows(
103                 IndexOutOfBoundsException.class,
104                 () -> sb1.append(new char[]{'b', 'a', 'r'}, 1, 3),
105                 "append(char[], 1, 3) expected IndexOutOfBoundsException");
106 
107         assertThrows(
108                 IndexOutOfBoundsException.class,
109                 () -> sb1.append(new char[]{'b', 'a', 'r'}, -1, 3),
110                 "append(char[], -1, 3) expected IndexOutOfBoundsException");
111 
112         assertThrows(
113                 IndexOutOfBoundsException.class,
114                 () -> sb1.append(new char[]{'b', 'a', 'r'}, 4, 0),
115                 "append(char[], 4, 0) expected IndexOutOfBoundsException");
116 
117         sb.append(new char[]{'b', 'a', 'r'}, 3, 0);
118         assertEquals("foo", sb.toString());
119 
120         sb.append(new char[]{'a', 'b', 'c', 'b', 'a', 'r', 'd', 'e', 'f'}, 3, 3);
121         assertEquals("foobar", sb.toString());
122     }
123 
124     @Test
125     public void testAppend_FormattedString() {
126         StrBuilder sb;
127 
128         sb = new StrBuilder();
129         sb.append("Hi", (Object[]) null);
130         assertEquals("Hi", sb.toString());
131 
132         sb = new StrBuilder();
133         sb.append("Hi", "Alice");
134         assertEquals("Hi", sb.toString());
135 
136         sb = new StrBuilder();
137         sb.append("Hi %s", "Alice");
138         assertEquals("Hi Alice", sb.toString());
139 
140         sb = new StrBuilder();
141         sb.append("Hi %s %,d", "Alice", 5000);
142         // group separator depends on system locale
143         final char groupingSeparator = DecimalFormatSymbols.getInstance().getGroupingSeparator();
144         final String expected = "Hi Alice 5" + groupingSeparator + "000";
145         assertEquals(expected, sb.toString());
146     }
147 
148     @Test
149     public void testAppend_Object() {
150         final StrBuilder sb = new StrBuilder();
151         sb.appendNull();
152         assertEquals("", sb.toString());
153 
154         sb.append((Object) null);
155         assertEquals("", sb.toString());
156 
157         sb.append(FOO);
158         assertEquals("foo", sb.toString());
159 
160         sb.append((StringBuffer) null);
161         assertEquals("foo", sb.toString());
162 
163         sb.append(new StringBuffer("baz"));
164         assertEquals("foobaz", sb.toString());
165 
166         sb.append(new StrBuilder("yes"));
167         assertEquals("foobazyes", sb.toString());
168 
169         sb.append((CharSequence) "Seq");
170         assertEquals("foobazyesSeq", sb.toString());
171 
172         sb.append(new StringBuilder("bld")); // Check it supports StringBuilder
173         assertEquals("foobazyesSeqbld", sb.toString());
174     }
175 
176     @Test
177     public void testAppend_PrimitiveNumber() {
178         final StrBuilder sb = new StrBuilder();
179         sb.append(0);
180         assertEquals("0", sb.toString());
181 
182         sb.append(1L);
183         assertEquals("01", sb.toString());
184 
185         sb.append(2.3f);
186         assertEquals("012.3", sb.toString());
187 
188         sb.append(4.5d);
189         assertEquals("012.34.5", sb.toString());
190     }
191 
192     @Test
193     public void testAppend_StrBuilder() {
194         StrBuilder sb = new StrBuilder();
195         sb.setNullText("NULL").append((StrBuilder) null);
196         assertEquals("NULL", sb.toString());
197 
198         sb = new StrBuilder();
199         sb.append(new StrBuilder("foo"));
200         assertEquals("foo", sb.toString());
201 
202         sb.append(new StrBuilder(""));
203         assertEquals("foo", sb.toString());
204 
205         sb.append(new StrBuilder("bar"));
206         assertEquals("foobar", sb.toString());
207     }
208 
209     @Test
210     public void testAppend_StrBuilder_int_int() {
211         StrBuilder sb = new StrBuilder();
212         sb.setNullText("NULL").append((StrBuilder) null, 0, 1);
213         assertEquals("NULL", sb.toString());
214 
215         sb = new StrBuilder();
216         sb.append(new StrBuilder("foo"), 0, 3);
217         assertEquals("foo", sb.toString());
218 
219         final StrBuilder sb1 = sb;
220         assertThrows(
221                 IndexOutOfBoundsException.class,
222                 () -> sb1.append(new StrBuilder("bar"), -1, 1),
223                 "append(char[], -1,) expected IndexOutOfBoundsException");
224 
225         assertThrows(
226                 IndexOutOfBoundsException.class,
227                 () -> sb1.append(new StrBuilder("bar"), 3, 1),
228                 "append(char[], 3,) expected IndexOutOfBoundsException");
229 
230         assertThrows(
231                 IndexOutOfBoundsException.class,
232                 () -> sb1.append(new StrBuilder("bar"), 1, -1),
233                 "append(char[],, -1) expected IndexOutOfBoundsException");
234 
235         assertThrows(
236                 IndexOutOfBoundsException.class,
237                 () -> sb1.append(new StrBuilder("bar"), 1, 3),
238                 "append(char[], 1, 3) expected IndexOutOfBoundsException");
239 
240         assertThrows(
241                 IndexOutOfBoundsException.class,
242                 () -> sb1.append(new StrBuilder("bar"), -1, 3),
243                 "append(char[], -1, 3) expected IndexOutOfBoundsException");
244 
245         assertThrows(
246                 IndexOutOfBoundsException.class,
247                 () -> sb1.append(new StrBuilder("bar"), 4, 0),
248                 "append(char[], 4, 0) expected IndexOutOfBoundsException");
249 
250         sb.append(new StrBuilder("bar"), 3, 0);
251         assertEquals("foo", sb.toString());
252 
253         sb.append(new StrBuilder("abcbardef"), 3, 3);
254         assertEquals("foobar", sb.toString());
255     }
256 
257     @Test
258     public void testAppend_String() {
259         StrBuilder sb = new StrBuilder();
260         sb.setNullText("NULL").append((String) null);
261         assertEquals("NULL", sb.toString());
262 
263         sb = new StrBuilder();
264         sb.append("foo");
265         assertEquals("foo", sb.toString());
266 
267         sb.append("");
268         assertEquals("foo", sb.toString());
269 
270         sb.append("bar");
271         assertEquals("foobar", sb.toString());
272     }
273 
274     @Test
275     public void testAppend_String_int_int() {
276         StrBuilder sb = new StrBuilder();
277         sb.setNullText("NULL").append((String) null, 0, 1);
278         assertEquals("NULL", sb.toString());
279 
280         sb = new StrBuilder();
281         sb.append("foo", 0, 3);
282         assertEquals("foo", sb.toString());
283 
284         final StrBuilder sb1 = sb;
285         assertThrows(
286                 IndexOutOfBoundsException.class,
287                 () -> sb1.append("bar", -1, 1),
288                 "append(char[], -1,) expected IndexOutOfBoundsException");
289 
290         assertThrows(
291                 IndexOutOfBoundsException.class,
292                 () -> sb1.append("bar", 3, 1),
293                 "append(char[], 3,) expected IndexOutOfBoundsException");
294 
295         assertThrows(
296                 IndexOutOfBoundsException.class,
297                 () -> sb1.append("bar", 1, -1),
298                 "append(char[],, -1) expected IndexOutOfBoundsException");
299 
300         assertThrows(
301                 IndexOutOfBoundsException.class,
302                 () -> sb1.append("bar", 1, 3),
303                 "append(char[], 1, 3) expected IndexOutOfBoundsException");
304 
305         assertThrows(
306                 IndexOutOfBoundsException.class,
307                 () -> sb1.append("bar", -1, 3),
308                 "append(char[], -1, 3) expected IndexOutOfBoundsException");
309 
310         assertThrows(
311                 IndexOutOfBoundsException.class,
312                 () -> sb1.append("bar", 4, 0),
313                 "append(char[], 4, 0) expected IndexOutOfBoundsException");
314 
315         sb.append("bar", 3, 0);
316         assertEquals("foo", sb.toString());
317 
318         sb.append("abcbardef", 3, 3);
319         assertEquals("foobar", sb.toString());
320 
321         sb.append((CharSequence) "abcbardef", 4, 3);
322         assertEquals("foobarard", sb.toString());
323     }
324 
325     @Test
326     public void testAppend_StringBuffer() {
327         StrBuilder sb = new StrBuilder();
328         sb.setNullText("NULL").append((StringBuffer) null);
329         assertEquals("NULL", sb.toString());
330 
331         sb = new StrBuilder();
332         sb.append(new StringBuffer("foo"));
333         assertEquals("foo", sb.toString());
334 
335         sb.append(new StringBuffer(""));
336         assertEquals("foo", sb.toString());
337 
338         sb.append(new StringBuffer("bar"));
339         assertEquals("foobar", sb.toString());
340     }
341 
342     @Test
343     public void testAppend_StringBuffer_int_int() {
344         StrBuilder sb = new StrBuilder();
345         sb.setNullText("NULL").append((StringBuffer) null, 0, 1);
346         assertEquals("NULL", sb.toString());
347 
348         sb = new StrBuilder();
349         sb.append(new StringBuffer("foo"), 0, 3);
350         assertEquals("foo", sb.toString());
351 
352         final StrBuilder sb1 = sb;
353         assertThrows(
354                 IndexOutOfBoundsException.class,
355                 () -> sb1.append(new StringBuffer("bar"), -1, 1),
356                 "append(char[], -1,) expected IndexOutOfBoundsException");
357 
358         assertThrows(
359                 IndexOutOfBoundsException.class,
360                 () -> sb1.append(new StringBuffer("bar"), 3, 1),
361                 "append(char[], 3,) expected IndexOutOfBoundsException");
362 
363         assertThrows(
364                 IndexOutOfBoundsException.class,
365                 () -> sb1.append(new StringBuffer("bar"), 1, -1),
366                 "append(char[],, -1) expected IndexOutOfBoundsException");
367 
368         assertThrows(
369                 IndexOutOfBoundsException.class,
370                 () -> sb1.append(new StringBuffer("bar"), 1, 3),
371                 "append(char[], 1, 3) expected IndexOutOfBoundsException");
372 
373         assertThrows(
374                 IndexOutOfBoundsException.class,
375                 () -> sb1.append(new StringBuffer("bar"), -1, 3),
376                 "append(char[], -1, 3) expected IndexOutOfBoundsException");
377 
378         assertThrows(
379                 IndexOutOfBoundsException.class,
380                 () -> sb1.append(new StringBuffer("bar"), 4, 0),
381                 "append(char[], 4, 0) expected IndexOutOfBoundsException");
382 
383         sb.append(new StringBuffer("bar"), 3, 0);
384         assertEquals("foo", sb.toString());
385 
386         sb.append(new StringBuffer("abcbardef"), 3, 3);
387         assertEquals("foobar", sb.toString());
388     }
389 
390     @Test
391     public void testAppend_StringBuilder() {
392         StrBuilder sb = new StrBuilder();
393         sb.setNullText("NULL").append((String) null);
394         assertEquals("NULL", sb.toString());
395 
396         sb = new StrBuilder();
397         sb.append(new StringBuilder("foo"));
398         assertEquals("foo", sb.toString());
399 
400         sb.append(new StringBuilder(""));
401         assertEquals("foo", sb.toString());
402 
403         sb.append(new StringBuilder("bar"));
404         assertEquals("foobar", sb.toString());
405     }
406 
407     @Test
408     public void testAppend_StringBuilder_int_int() {
409         StrBuilder sb = new StrBuilder();
410         sb.setNullText("NULL").append((String) null, 0, 1);
411         assertEquals("NULL", sb.toString());
412 
413         sb = new StrBuilder();
414         sb.append(new StringBuilder("foo"), 0, 3);
415         assertEquals("foo", sb.toString());
416 
417         final StrBuilder sb1 = sb;
418         assertThrows(
419                 IndexOutOfBoundsException.class,
420                 () -> sb1.append(new StringBuilder("bar"), -1, 1),
421                 "append(StringBuilder, -1,) expected IndexOutOfBoundsException");
422 
423         assertThrows(
424                 IndexOutOfBoundsException.class,
425                 () -> sb1.append(new StringBuilder("bar"), 3, 1),
426                 "append(StringBuilder, 3,) expected IndexOutOfBoundsException");
427 
428         assertThrows(
429                 IndexOutOfBoundsException.class,
430                 () -> sb1.append(new StringBuilder("bar"), 1, -1),
431                 "append(StringBuilder,, -1) expected IndexOutOfBoundsException");
432 
433         assertThrows(
434                 IndexOutOfBoundsException.class,
435                 () -> sb1.append(new StringBuilder("bar"), 1, 3),
436                 "append(StringBuilder, 1, 3) expected IndexOutOfBoundsException");
437 
438         assertThrows(
439                 IndexOutOfBoundsException.class,
440                 () -> sb1.append(new StringBuilder("bar"), -1, 3),
441                 "append(StringBuilder, -1, 3) expected IndexOutOfBoundsException");
442 
443         assertThrows(
444                 IndexOutOfBoundsException.class,
445                 () -> sb1.append(new StringBuilder("bar"), 4, 0),
446                 "append(StringBuilder, 4, 0) expected IndexOutOfBoundsException");
447 
448         sb.append(new StringBuilder("bar"), 3, 0);
449         assertEquals("foo", sb.toString());
450 
451         sb.append(new StringBuilder("abcbardef"), 3, 3);
452         assertEquals("foobar", sb.toString());
453 
454         sb.append( new StringBuilder("abcbardef"), 4, 3);
455         assertEquals("foobarard", sb.toString());
456     }
457 
458     @Test
459     public void testAppendAll_Array() {
460         final StrBuilder sb = new StrBuilder();
461         sb.appendAll((Object[]) null);
462         assertEquals("", sb.toString());
463 
464         sb.clear();
465         sb.appendAll();
466         assertEquals("", sb.toString());
467 
468         sb.clear();
469         sb.appendAll("foo", "bar", "baz");
470         assertEquals("foobarbaz", sb.toString());
471 
472         sb.clear();
473         sb.appendAll("foo", "bar", "baz");
474         assertEquals("foobarbaz", sb.toString());
475     }
476 
477     @Test
478     public void testAppendAll_Collection() {
479         final StrBuilder sb = new StrBuilder();
480         sb.appendAll((Collection<?>) null);
481         assertEquals("", sb.toString());
482 
483         sb.clear();
484         sb.appendAll(Collections.EMPTY_LIST);
485         assertEquals("", sb.toString());
486 
487         sb.clear();
488         sb.appendAll(Arrays.asList("foo", "bar", "baz"));
489         assertEquals("foobarbaz", sb.toString());
490     }
491 
492     @Test
493     public void testAppendAll_Iterator() {
494         final StrBuilder sb = new StrBuilder();
495         sb.appendAll((Iterator<?>) null);
496         assertEquals("", sb.toString());
497 
498         sb.clear();
499         sb.appendAll(Collections.EMPTY_LIST.iterator());
500         assertEquals("", sb.toString());
501 
502         sb.clear();
503         sb.appendAll(Arrays.asList("foo", "bar", "baz").iterator());
504         assertEquals("foobarbaz", sb.toString());
505     }
506 
507     @Test
508     public void testAppendFixedWidthPadLeft() {
509         final StrBuilder sb = new StrBuilder();
510         sb.appendFixedWidthPadLeft("foo", -1, '-');
511         assertEquals("", sb.toString());
512 
513         sb.clear();
514         sb.appendFixedWidthPadLeft("foo", 0, '-');
515         assertEquals("", sb.toString());
516 
517         sb.clear();
518         sb.appendFixedWidthPadLeft("foo", 1, '-');
519         assertEquals("o", sb.toString());
520 
521         sb.clear();
522         sb.appendFixedWidthPadLeft("foo", 2, '-');
523         assertEquals("oo", sb.toString());
524 
525         sb.clear();
526         sb.appendFixedWidthPadLeft("foo", 3, '-');
527         assertEquals("foo", sb.toString());
528 
529         sb.clear();
530         sb.appendFixedWidthPadLeft("foo", 4, '-');
531         assertEquals("-foo", sb.toString());
532 
533         sb.clear();
534         sb.appendFixedWidthPadLeft("foo", 10, '-');
535         assertEquals(10, sb.length());
536         //            1234567890
537         assertEquals("-------foo", sb.toString());
538 
539         sb.clear();
540         sb.setNullText("null");
541         sb.appendFixedWidthPadLeft(null, 5, '-');
542         assertEquals("-null", sb.toString());
543     }
544 
545     @Test
546     public void testAppendFixedWidthPadLeft_int() {
547         final StrBuilder sb = new StrBuilder();
548         sb.appendFixedWidthPadLeft(123, -1, '-');
549         assertEquals("", sb.toString());
550 
551         sb.clear();
552         sb.appendFixedWidthPadLeft(123, 0, '-');
553         assertEquals("", sb.toString());
554 
555         sb.clear();
556         sb.appendFixedWidthPadLeft(123, 1, '-');
557         assertEquals("3", sb.toString());
558 
559         sb.clear();
560         sb.appendFixedWidthPadLeft(123, 2, '-');
561         assertEquals("23", sb.toString());
562 
563         sb.clear();
564         sb.appendFixedWidthPadLeft(123, 3, '-');
565         assertEquals("123", sb.toString());
566 
567         sb.clear();
568         sb.appendFixedWidthPadLeft(123, 4, '-');
569         assertEquals("-123", sb.toString());
570 
571         sb.clear();
572         sb.appendFixedWidthPadLeft(123, 10, '-');
573         assertEquals(10, sb.length());
574         //            1234567890
575         assertEquals("-------123", sb.toString());
576     }
577 
578     @Test
579     public void testAppendFixedWidthPadRight() {
580         final StrBuilder sb = new StrBuilder();
581         sb.appendFixedWidthPadRight("foo", -1, '-');
582         assertEquals("", sb.toString());
583 
584         sb.clear();
585         sb.appendFixedWidthPadRight("foo", 0, '-');
586         assertEquals("", sb.toString());
587 
588         sb.clear();
589         sb.appendFixedWidthPadRight("foo", 1, '-');
590         assertEquals("f", sb.toString());
591 
592         sb.clear();
593         sb.appendFixedWidthPadRight("foo", 2, '-');
594         assertEquals("fo", sb.toString());
595 
596         sb.clear();
597         sb.appendFixedWidthPadRight("foo", 3, '-');
598         assertEquals("foo", sb.toString());
599 
600         sb.clear();
601         sb.appendFixedWidthPadRight("foo", 4, '-');
602         assertEquals("foo-", sb.toString());
603 
604         sb.clear();
605         sb.appendFixedWidthPadRight("foo", 10, '-');
606         assertEquals(10, sb.length());
607         //            1234567890
608         assertEquals("foo-------", sb.toString());
609 
610         sb.clear();
611         sb.setNullText("null");
612         sb.appendFixedWidthPadRight(null, 5, '-');
613         assertEquals("null-", sb.toString());
614     }
615 
616     @Test
617     public void testAppendFixedWidthPadRight_int() {
618         final StrBuilder sb = new StrBuilder();
619         sb.appendFixedWidthPadRight(123, -1, '-');
620         assertEquals("", sb.toString());
621 
622         sb.clear();
623         sb.appendFixedWidthPadRight(123, 0, '-');
624         assertEquals("", sb.toString());
625 
626         sb.clear();
627         sb.appendFixedWidthPadRight(123, 1, '-');
628         assertEquals("1", sb.toString());
629 
630         sb.clear();
631         sb.appendFixedWidthPadRight(123, 2, '-');
632         assertEquals("12", sb.toString());
633 
634         sb.clear();
635         sb.appendFixedWidthPadRight(123, 3, '-');
636         assertEquals("123", sb.toString());
637 
638         sb.clear();
639         sb.appendFixedWidthPadRight(123, 4, '-');
640         assertEquals("123-", sb.toString());
641 
642         sb.clear();
643         sb.appendFixedWidthPadRight(123, 10, '-');
644         assertEquals(10, sb.length());
645         //            1234567890
646         assertEquals("123-------", sb.toString());
647     }
648 
649     @Test
650     public void testAppendln_Boolean() {
651         final StrBuilder sb = new StrBuilder();
652         sb.appendln(true);
653         assertEquals("true" + SEP, sb.toString());
654 
655         sb.clear();
656         sb.appendln(false);
657         assertEquals("false" + SEP, sb.toString());
658     }
659 
660     @Test
661     public void testAppendln_CharArray() {
662         final int[] count = new int[2];
663         final StrBuilder sb = new StrBuilder() {
664             private static final long serialVersionUID = 1L;
665 
666             @Override
667             public StrBuilder append(final char[] str) {
668                 count[0]++;
669                 return super.append(str);
670             }
671             @Override
672             public StrBuilder appendNewLine() {
673                 count[1]++;
674                 return super.appendNewLine();
675             }
676         };
677         sb.appendln("foo".toCharArray());
678         assertEquals("foo" + SEP, sb.toString());
679         assertEquals(1, count[0]);
680         assertEquals(1, count[1]);
681     }
682 
683     @Test
684     public void testAppendln_CharArray_int_int() {
685         final int[] count = new int[2];
686         final StrBuilder sb = new StrBuilder() {
687             private static final long serialVersionUID = 1L;
688 
689             @Override
690             public StrBuilder append(final char[] str, final int startIndex, final int length) {
691                 count[0]++;
692                 return super.append(str, startIndex, length);
693             }
694             @Override
695             public StrBuilder appendNewLine() {
696                 count[1]++;
697                 return super.appendNewLine();
698             }
699         };
700         sb.appendln("foo".toCharArray(), 0, 3);
701         assertEquals("foo" + SEP, sb.toString());
702         assertEquals(1, count[0]);
703         assertEquals(1, count[1]);
704     }
705 
706     @Test
707     public void testAppendln_FormattedString() {
708         final int[] count = new int[2];
709         final StrBuilder sb = new StrBuilder() {
710             private static final long serialVersionUID = 1L;
711 
712             @Override
713             public StrBuilder append(final String str) {
714                 count[0]++;
715                 return super.append(str);
716             }
717             @Override
718             public StrBuilder appendNewLine() {
719                 count[1]++;
720                 return super.appendNewLine();
721             }
722         };
723         sb.appendln("Hello %s", "Alice");
724         assertEquals("Hello Alice" + SEP, sb.toString());
725         assertEquals(2, count[0]);  // appendNewLine() calls append(String)
726         assertEquals(1, count[1]);
727     }
728 
729     @Test
730     public void testAppendln_Object() {
731         final StrBuilder sb = new StrBuilder();
732         sb.appendln((Object) null);
733         assertEquals("" + SEP, sb.toString());
734 
735         sb.appendln(FOO);
736         assertEquals(SEP + "foo" + SEP, sb.toString());
737 
738         sb.appendln(Integer.valueOf(6));
739         assertEquals(SEP + "foo" + SEP + "6" + SEP, sb.toString());
740     }
741 
742     @Test
743     public void testAppendln_PrimitiveNumber() {
744         final StrBuilder sb = new StrBuilder();
745         sb.appendln(0);
746         assertEquals("0" + SEP, sb.toString());
747 
748         sb.clear();
749         sb.appendln(1L);
750         assertEquals("1" + SEP, sb.toString());
751 
752         sb.clear();
753         sb.appendln(2.3f);
754         assertEquals("2.3" + SEP, sb.toString());
755 
756         sb.clear();
757         sb.appendln(4.5d);
758         assertEquals("4.5" + SEP, sb.toString());
759     }
760 
761     @Test
762     public void testAppendln_StrBuilder() {
763         final int[] count = new int[2];
764         final StrBuilder sb = new StrBuilder() {
765             private static final long serialVersionUID = 1L;
766 
767             @Override
768             public StrBuilder append(final StrBuilder str) {
769                 count[0]++;
770                 return super.append(str);
771             }
772             @Override
773             public StrBuilder appendNewLine() {
774                 count[1]++;
775                 return super.appendNewLine();
776             }
777         };
778         sb.appendln(new StrBuilder("foo"));
779         assertEquals("foo" + SEP, sb.toString());
780         assertEquals(1, count[0]);
781         assertEquals(1, count[1]);
782     }
783 
784     @Test
785     public void testAppendln_StrBuilder_int_int() {
786         final int[] count = new int[2];
787         final StrBuilder sb = new StrBuilder() {
788             private static final long serialVersionUID = 1L;
789 
790             @Override
791             public StrBuilder append(final StrBuilder str, final int startIndex, final int length) {
792                 count[0]++;
793                 return super.append(str, startIndex, length);
794             }
795             @Override
796             public StrBuilder appendNewLine() {
797                 count[1]++;
798                 return super.appendNewLine();
799             }
800         };
801         sb.appendln(new StrBuilder("foo"), 0, 3);
802         assertEquals("foo" + SEP, sb.toString());
803         assertEquals(1, count[0]);
804         assertEquals(1, count[1]);
805     }
806 
807     @Test
808     public void testAppendln_String() {
809         final int[] count = new int[2];
810         final StrBuilder sb = new StrBuilder() {
811             private static final long serialVersionUID = 1L;
812 
813             @Override
814             public StrBuilder append(final String str) {
815                 count[0]++;
816                 return super.append(str);
817             }
818             @Override
819             public StrBuilder appendNewLine() {
820                 count[1]++;
821                 return super.appendNewLine();
822             }
823         };
824         sb.appendln("foo");
825         assertEquals("foo" + SEP, sb.toString());
826         assertEquals(2, count[0]);  // appendNewLine() calls append(String)
827         assertEquals(1, count[1]);
828     }
829 
830     @Test
831     public void testAppendln_String_int_int() {
832         final int[] count = new int[2];
833         final StrBuilder sb = new StrBuilder() {
834             private static final long serialVersionUID = 1L;
835 
836             @Override
837             public StrBuilder append(final String str, final int startIndex, final int length) {
838                 count[0]++;
839                 return super.append(str, startIndex, length);
840             }
841             @Override
842             public StrBuilder appendNewLine() {
843                 count[1]++;
844                 return super.appendNewLine();
845             }
846         };
847         sb.appendln("foo", 0, 3);
848         assertEquals("foo" + SEP, sb.toString());
849         assertEquals(1, count[0]);
850         assertEquals(1, count[1]);
851     }
852 
853     @Test
854     public void testAppendln_StringBuffer() {
855         final int[] count = new int[2];
856         final StrBuilder sb = new StrBuilder() {
857             private static final long serialVersionUID = 1L;
858 
859             @Override
860             public StrBuilder append(final StringBuffer str) {
861                 count[0]++;
862                 return super.append(str);
863             }
864             @Override
865             public StrBuilder appendNewLine() {
866                 count[1]++;
867                 return super.appendNewLine();
868             }
869         };
870         sb.appendln(new StringBuffer("foo"));
871         assertEquals("foo" + SEP, sb.toString());
872         assertEquals(1, count[0]);
873         assertEquals(1, count[1]);
874     }
875 
876     @Test
877     public void testAppendln_StringBuffer_int_int() {
878         final int[] count = new int[2];
879         final StrBuilder sb = new StrBuilder() {
880             private static final long serialVersionUID = 1L;
881 
882             @Override
883             public StrBuilder append(final StringBuffer str, final int startIndex, final int length) {
884                 count[0]++;
885                 return super.append(str, startIndex, length);
886             }
887             @Override
888             public StrBuilder appendNewLine() {
889                 count[1]++;
890                 return super.appendNewLine();
891             }
892         };
893         sb.appendln(new StringBuffer("foo"), 0, 3);
894         assertEquals("foo" + SEP, sb.toString());
895         assertEquals(1, count[0]);
896         assertEquals(1, count[1]);
897     }
898 
899     @Test
900     public void testAppendln_StringBuilder() {
901         final int[] count = new int[2];
902         final StrBuilder sb = new StrBuilder() {
903             private static final long serialVersionUID = 1L;
904 
905             @Override
906             public StrBuilder append(final StringBuilder str) {
907                 count[0]++;
908                 return super.append(str);
909             }
910             @Override
911             public StrBuilder appendNewLine() {
912                 count[1]++;
913                 return super.appendNewLine();
914             }
915         };
916         sb.appendln(new StringBuilder("foo"));
917         assertEquals("foo" + SEP, sb.toString());
918         assertEquals(1, count[0]);
919         assertEquals(1, count[1]);
920     }
921 
922     @Test
923     public void testAppendln_StringBuilder_int_int() {
924         final int[] count = new int[2];
925         final StrBuilder sb = new StrBuilder() {
926             private static final long serialVersionUID = 1L;
927 
928             @Override
929             public StrBuilder append(final StringBuilder str, final int startIndex, final int length) {
930                 count[0]++;
931                 return super.append(str, startIndex, length);
932             }
933             @Override
934             public StrBuilder appendNewLine() {
935                 count[1]++;
936                 return super.appendNewLine();
937             }
938         };
939         sb.appendln(new StringBuilder("foo"), 0, 3);
940         assertEquals("foo" + SEP, sb.toString());
941         assertEquals(1, count[0]);
942         assertEquals(1, count[1]);
943     }
944 
945     @Test
946     public void testAppendNewLine() {
947         StrBuilder sb = new StrBuilder("---");
948         sb.appendNewLine().append("+++");
949         assertEquals("---" + SEP + "+++", sb.toString());
950 
951         sb = new StrBuilder("---");
952         sb.setNewLineText("#").appendNewLine().setNewLineText(null).appendNewLine();
953         assertEquals("---#" + SEP, sb.toString());
954     }
955 
956     @Test
957     public void testAppendPadding() {
958         final StrBuilder sb = new StrBuilder();
959         sb.append("foo");
960         assertEquals("foo", sb.toString());
961 
962         sb.appendPadding(-1, '-');
963         assertEquals("foo", sb.toString());
964 
965         sb.appendPadding(0, '-');
966         assertEquals("foo", sb.toString());
967 
968         sb.appendPadding(1, '-');
969         assertEquals("foo-", sb.toString());
970 
971         sb.appendPadding(16, '-');
972         assertEquals(20, sb.length());
973         //            12345678901234567890
974         assertEquals("foo-----------------", sb.toString());
975     }
976 
977     @Test
978     public void testAppendSeparator_char() {
979         final StrBuilder sb = new StrBuilder();
980         sb.appendSeparator(',');  // no effect
981         assertEquals("", sb.toString());
982         sb.append("foo");
983         assertEquals("foo", sb.toString());
984         sb.appendSeparator(',');
985         assertEquals("foo,", sb.toString());
986     }
987 
988     @Test
989     public void testAppendSeparator_char_char() {
990         final StrBuilder sb = new StrBuilder();
991         final char startSeparator = ':';
992         final char standardSeparator = ',';
993         final String foo = "foo";
994         sb.appendSeparator(standardSeparator, startSeparator);  // no effect
995         assertEquals(String.valueOf(startSeparator), sb.toString());
996         sb.append(foo);
997         assertEquals(String.valueOf(startSeparator) + foo, sb.toString());
998         sb.appendSeparator(standardSeparator, startSeparator);
999         assertEquals(String.valueOf(startSeparator) + foo + standardSeparator, sb.toString());
1000     }
1001 
1002     @Test
1003     public void testAppendSeparator_char_int() {
1004         final StrBuilder sb = new StrBuilder();
1005         sb.appendSeparator(',', 0);  // no effect
1006         assertEquals("", sb.toString());
1007         sb.append("foo");
1008         assertEquals("foo", sb.toString());
1009         sb.appendSeparator(',', 1);
1010         assertEquals("foo,", sb.toString());
1011 
1012         sb.appendSeparator(',', -1);  // no effect
1013         assertEquals("foo,", sb.toString());
1014     }
1015 
1016     @Test
1017     public void testAppendSeparator_String() {
1018         final StrBuilder sb = new StrBuilder();
1019         sb.appendSeparator(",");  // no effect
1020         assertEquals("", sb.toString());
1021         sb.append("foo");
1022         assertEquals("foo", sb.toString());
1023         sb.appendSeparator(",");
1024         assertEquals("foo,", sb.toString());
1025     }
1026 
1027     @Test
1028     public void testAppendSeparator_String_int() {
1029         final StrBuilder sb = new StrBuilder();
1030         sb.appendSeparator(",", 0);  // no effect
1031         assertEquals("", sb.toString());
1032         sb.append("foo");
1033         assertEquals("foo", sb.toString());
1034         sb.appendSeparator(",", 1);
1035         assertEquals("foo,", sb.toString());
1036 
1037         sb.appendSeparator(",", -1);  // no effect
1038         assertEquals("foo,", sb.toString());
1039     }
1040 
1041     @Test
1042     public void testAppendSeparator_String_String() {
1043         final StrBuilder sb = new StrBuilder();
1044         final String startSeparator = "order by ";
1045         final String standardSeparator = ",";
1046         final String foo = "foo";
1047         sb.appendSeparator(null, null);
1048         assertEquals("", sb.toString());
1049         sb.appendSeparator(standardSeparator, null);
1050         assertEquals("", sb.toString());
1051         sb.appendSeparator(standardSeparator, startSeparator);
1052         assertEquals(startSeparator, sb.toString());
1053         sb.appendSeparator(null, null);
1054         assertEquals(startSeparator, sb.toString());
1055         sb.appendSeparator(null, startSeparator);
1056         assertEquals(startSeparator, sb.toString());
1057         sb.append(foo);
1058         assertEquals(startSeparator + foo, sb.toString());
1059         sb.appendSeparator(standardSeparator, startSeparator);
1060         assertEquals(startSeparator + foo + standardSeparator, sb.toString());
1061     }
1062 
1063     @Test
1064     public void testAppendWithNullText() {
1065         final StrBuilder sb = new StrBuilder();
1066         sb.setNullText("NULL");
1067         assertEquals("", sb.toString());
1068 
1069         sb.appendNull();
1070         assertEquals("NULL", sb.toString());
1071 
1072         sb.append((Object) null);
1073         assertEquals("NULLNULL", sb.toString());
1074 
1075         sb.append(FOO);
1076         assertEquals("NULLNULLfoo", sb.toString());
1077 
1078         sb.append((String) null);
1079         assertEquals("NULLNULLfooNULL", sb.toString());
1080 
1081         sb.append("");
1082         assertEquals("NULLNULLfooNULL", sb.toString());
1083 
1084         sb.append("bar");
1085         assertEquals("NULLNULLfooNULLbar", sb.toString());
1086 
1087         sb.append((StringBuffer) null);
1088         assertEquals("NULLNULLfooNULLbarNULL", sb.toString());
1089 
1090         sb.append(new StringBuffer("baz"));
1091         assertEquals("NULLNULLfooNULLbarNULLbaz", sb.toString());
1092     }
1093 
1094     @Test
1095     public void testAppendWithSeparators_Array() {
1096         final StrBuilder sb = new StrBuilder();
1097         sb.appendWithSeparators((Object[]) null, ",");
1098         assertEquals("", sb.toString());
1099 
1100         sb.clear();
1101         sb.appendWithSeparators(new Object[0], ",");
1102         assertEquals("", sb.toString());
1103 
1104         sb.clear();
1105         sb.appendWithSeparators(new Object[]{"foo", "bar", "baz"}, ",");
1106         assertEquals("foo,bar,baz", sb.toString());
1107 
1108         sb.clear();
1109         sb.appendWithSeparators(new Object[]{"foo", "bar", "baz"}, null);
1110         assertEquals("foobarbaz", sb.toString());
1111 
1112         sb.clear();
1113         sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ",");
1114         assertEquals("foo,,baz", sb.toString());
1115     }
1116 
1117     @Test
1118     public void testAppendWithSeparators_Collection() {
1119         final StrBuilder sb = new StrBuilder();
1120         sb.appendWithSeparators((Collection<?>) null, ",");
1121         assertEquals("", sb.toString());
1122 
1123         sb.clear();
1124         sb.appendWithSeparators(Collections.EMPTY_LIST, ",");
1125         assertEquals("", sb.toString());
1126 
1127         sb.clear();
1128         sb.appendWithSeparators(Arrays.asList("foo", "bar", "baz"), ",");
1129         assertEquals("foo,bar,baz", sb.toString());
1130 
1131         sb.clear();
1132         sb.appendWithSeparators(Arrays.asList("foo", "bar", "baz"), null);
1133         assertEquals("foobarbaz", sb.toString());
1134 
1135         sb.clear();
1136         sb.appendWithSeparators(Arrays.asList("foo", null, "baz"), ",");
1137         assertEquals("foo,,baz", sb.toString());
1138     }
1139     @Test
1140     public void testAppendWithSeparators_Iterator() {
1141         final StrBuilder sb = new StrBuilder();
1142         sb.appendWithSeparators((Iterator<?>) null, ",");
1143         assertEquals("", sb.toString());
1144 
1145         sb.clear();
1146         sb.appendWithSeparators(Collections.EMPTY_LIST.iterator(), ",");
1147         assertEquals("", sb.toString());
1148 
1149         sb.clear();
1150         sb.appendWithSeparators(Arrays.asList("foo", "bar", "baz").iterator(), ",");
1151         assertEquals("foo,bar,baz", sb.toString());
1152 
1153         sb.clear();
1154         sb.appendWithSeparators(Arrays.asList("foo", "bar", "baz").iterator(), null);
1155         assertEquals("foobarbaz", sb.toString());
1156 
1157         sb.clear();
1158         sb.appendWithSeparators(Arrays.asList("foo", null, "baz").iterator(), ",");
1159         assertEquals("foo,,baz", sb.toString());
1160     }
1161 
1162     @Test
1163     public void testAppendWithSeparatorsWithNullText() {
1164         final StrBuilder sb = new StrBuilder();
1165         sb.setNullText("null");
1166         sb.appendWithSeparators(new Object[]{"foo", null, "baz"}, ",");
1167         assertEquals("foo,null,baz", sb.toString());
1168 
1169         sb.clear();
1170         sb.appendWithSeparators(Arrays.asList("foo", null, "baz"), ",");
1171         assertEquals("foo,null,baz", sb.toString());
1172     }
1173 
1174     @Test
1175     public void testInsert() {
1176 
1177         final StrBuilder sb = new StrBuilder();
1178         sb.append("barbaz");
1179         assertEquals("barbaz", sb.toString());
1180 
1181         assertThrows(
1182                 IndexOutOfBoundsException.class,
1183                 () -> sb.insert(-1, FOO),
1184                 "insert(-1, Object) expected StringIndexOutOfBoundsException");
1185 
1186         assertThrows(
1187                 IndexOutOfBoundsException.class,
1188                 () -> sb.insert(7, FOO),
1189                 "insert(7, Object) expected StringIndexOutOfBoundsException");
1190 
1191         sb.insert(0, (Object) null);
1192         assertEquals("barbaz", sb.toString());
1193 
1194         sb.insert(0, FOO);
1195         assertEquals("foobarbaz", sb.toString());
1196 
1197         sb.clear();
1198         sb.append("barbaz");
1199         assertEquals("barbaz", sb.toString());
1200 
1201         assertThrows(
1202                 IndexOutOfBoundsException.class,
1203                 () -> sb.insert(-1, "foo"),
1204                 "insert(-1, String) expected StringIndexOutOfBoundsException");
1205 
1206         assertThrows(
1207                 IndexOutOfBoundsException.class,
1208                 () -> sb.insert(7, "foo"),
1209                 "insert(7, String) expected StringIndexOutOfBoundsException");
1210 
1211         sb.insert(0, (String) null);
1212         assertEquals("barbaz", sb.toString());
1213 
1214         sb.insert(0, "foo");
1215         assertEquals("foobarbaz", sb.toString());
1216 
1217         sb.clear();
1218         sb.append("barbaz");
1219         assertEquals("barbaz", sb.toString());
1220 
1221         assertThrows(
1222                 IndexOutOfBoundsException.class,
1223                 () -> sb.insert(-1, new char[]{'f', 'o', 'o'}),
1224                 "insert(-1, char[]) expected StringIndexOutOfBoundsException");
1225 
1226         assertThrows(
1227                 IndexOutOfBoundsException.class,
1228                 () -> sb.insert(7, new char[]{'f', 'o', 'o'}),
1229                 "insert(7, char[]) expected StringIndexOutOfBoundsException");
1230 
1231         sb.insert(0, (char[]) null);
1232         assertEquals("barbaz", sb.toString());
1233 
1234         sb.insert(0, new char[0]);
1235         assertEquals("barbaz", sb.toString());
1236 
1237         sb.insert(0, new char[]{'f', 'o', 'o'});
1238         assertEquals("foobarbaz", sb.toString());
1239 
1240         sb.clear();
1241         sb.append("barbaz");
1242         assertEquals("barbaz", sb.toString());
1243 
1244         assertThrows(
1245                 IndexOutOfBoundsException.class,
1246                 () -> sb.insert(-1, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3),
1247                 "insert(-1, char[], 3, 3) expected StringIndexOutOfBoundsException");
1248 
1249         assertThrows(
1250                 IndexOutOfBoundsException.class,
1251                 () -> sb.insert(7, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3),
1252                 "insert(7, char[], 3, 3) expected StringIndexOutOfBoundsException");
1253 
1254         sb.insert(0, null, 0, 0);
1255         assertEquals("barbaz", sb.toString());
1256 
1257         sb.insert(0, new char[0], 0, 0);
1258         assertEquals("barbaz", sb.toString());
1259 
1260         assertThrows(
1261                 IndexOutOfBoundsException.class,
1262                 () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, -1, 3),
1263                 "insert(0, char[], -1, 3) expected StringIndexOutOfBoundsException");
1264 
1265         assertThrows(
1266                 IndexOutOfBoundsException.class,
1267                 () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 10, 3),
1268                 "insert(0, char[], 10, 3) expected StringIndexOutOfBoundsException");
1269 
1270         assertThrows(
1271                 IndexOutOfBoundsException.class,
1272                 () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, -1),
1273                 "insert(0, char[], 0, -1) expected StringIndexOutOfBoundsException");
1274 
1275         assertThrows(
1276                 IndexOutOfBoundsException.class,
1277                 () -> sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 10),
1278                 "insert(0, char[], 0, 10) expected StringIndexOutOfBoundsException");
1279 
1280         sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 0, 0);
1281         assertEquals("barbaz", sb.toString());
1282 
1283         sb.insert(0, new char[]{'a', 'b', 'c', 'f', 'o', 'o', 'd', 'e', 'f'}, 3, 3);
1284         assertEquals("foobarbaz", sb.toString());
1285 
1286         sb.clear();
1287         sb.append("barbaz");
1288         assertEquals("barbaz", sb.toString());
1289 
1290         assertThrows(
1291                 IndexOutOfBoundsException.class,
1292                 () -> sb.insert(-1, true),
1293                 "insert(-1, boolean) expected StringIndexOutOfBoundsException");
1294 
1295         assertThrows(
1296                 IndexOutOfBoundsException.class,
1297                 () -> sb.insert(7, true),
1298                 "insert(7, boolean) expected StringIndexOutOfBoundsException");
1299 
1300         sb.insert(0, true);
1301         assertEquals("truebarbaz", sb.toString());
1302 
1303         sb.insert(0, false);
1304         assertEquals("falsetruebarbaz", sb.toString());
1305 
1306         sb.clear();
1307         sb.append("barbaz");
1308         assertEquals("barbaz", sb.toString());
1309 
1310         assertThrows(
1311                 IndexOutOfBoundsException.class,
1312                 () -> sb.insert(-1, '!'),
1313                 "insert(-1, char) expected StringIndexOutOfBoundsException");
1314 
1315         assertThrows(
1316                 IndexOutOfBoundsException.class,
1317                 () -> sb.insert(7, '!'),
1318                 "insert(7, char) expected StringIndexOutOfBoundsException");
1319 
1320         sb.insert(0, '!');
1321         assertEquals("!barbaz", sb.toString());
1322 
1323         sb.clear();
1324         sb.append("barbaz");
1325         assertEquals("barbaz", sb.toString());
1326 
1327         assertThrows(
1328                 IndexOutOfBoundsException.class,
1329                 () -> sb.insert(-1, 0),
1330                 "insert(-1, int) expected StringIndexOutOfBoundsException");
1331 
1332         assertThrows(
1333                 IndexOutOfBoundsException.class,
1334                 () -> sb.insert(7, 0),
1335                 "insert(7, int) expected StringIndexOutOfBoundsException");
1336 
1337         sb.insert(0, '0');
1338         assertEquals("0barbaz", sb.toString());
1339 
1340         sb.clear();
1341         sb.append("barbaz");
1342         assertEquals("barbaz", sb.toString());
1343 
1344         assertThrows(
1345                 IndexOutOfBoundsException.class,
1346                 () -> sb.insert(-1, 1L),
1347                 "insert(-1, long) expected StringIndexOutOfBoundsException");
1348 
1349         assertThrows(
1350                 IndexOutOfBoundsException.class,
1351                 () -> sb.insert(7, 1L),
1352                 "insert(7, long) expected StringIndexOutOfBoundsException");
1353 
1354         sb.insert(0, 1L);
1355         assertEquals("1barbaz", sb.toString());
1356 
1357         sb.clear();
1358         sb.append("barbaz");
1359         assertEquals("barbaz", sb.toString());
1360 
1361         assertThrows(
1362                 IndexOutOfBoundsException.class,
1363                 () -> sb.insert(-1, 2.3F),
1364                 "insert(-1, float) expected StringIndexOutOfBoundsException");
1365 
1366         assertThrows(
1367                 IndexOutOfBoundsException.class,
1368                 () -> sb.insert(7, 2.3F),
1369                 "insert(7, float) expected StringIndexOutOfBoundsException");
1370 
1371         sb.insert(0, 2.3F);
1372         assertEquals("2.3barbaz", sb.toString());
1373 
1374         sb.clear();
1375         sb.append("barbaz");
1376         assertEquals("barbaz", sb.toString());
1377 
1378         assertThrows(
1379                 IndexOutOfBoundsException.class,
1380                 () -> sb.insert(-1, 4.5D),
1381                 "insert(-1, double) expected StringIndexOutOfBoundsException");
1382 
1383         assertThrows(
1384                 IndexOutOfBoundsException.class,
1385                 () -> sb.insert(7, 4.5D),
1386                 "insert(7, double) expected StringIndexOutOfBoundsException");
1387 
1388         sb.insert(0, 4.5D);
1389         assertEquals("4.5barbaz", sb.toString());
1390     }
1391 
1392     @Test
1393     public void testInsertWithNullText() {
1394         final StrBuilder sb = new StrBuilder();
1395         sb.setNullText("null");
1396         sb.append("barbaz");
1397         assertEquals("barbaz", sb.toString());
1398 
1399         assertThrows(
1400                 IndexOutOfBoundsException.class,
1401                 () -> sb.insert(-1, FOO),
1402                 "insert(-1, Object) expected StringIndexOutOfBoundsException");
1403 
1404         assertThrows(
1405                 IndexOutOfBoundsException.class,
1406                 () -> sb.insert(7, FOO),
1407                 "insert(7, Object) expected StringIndexOutOfBoundsException");
1408 
1409         sb.insert(0, (Object) null);
1410         assertEquals("nullbarbaz", sb.toString());
1411 
1412         sb.insert(0, FOO);
1413         assertEquals("foonullbarbaz", sb.toString());
1414 
1415         sb.clear();
1416         sb.append("barbaz");
1417         assertEquals("barbaz", sb.toString());
1418 
1419         assertThrows(
1420                 IndexOutOfBoundsException.class,
1421                 () -> sb.insert(-1, "foo"),
1422                 "insert(-1, String) expected StringIndexOutOfBoundsException");
1423 
1424         assertThrows(
1425                 IndexOutOfBoundsException.class,
1426                 () -> sb.insert(7, "foo"),
1427                 "insert(7, String) expected StringIndexOutOfBoundsException");
1428 
1429         sb.insert(0, (String) null);
1430         assertEquals("nullbarbaz", sb.toString());
1431 
1432         sb.insert(0, "foo");
1433         assertEquals("foonullbarbaz", sb.toString());
1434 
1435         sb.insert(0, (char[]) null);
1436         assertEquals("nullfoonullbarbaz", sb.toString());
1437 
1438         sb.insert(0, null, 0, 0);
1439         assertEquals("nullnullfoonullbarbaz", sb.toString());
1440     }
1441 
1442     // See: https://issues.apache.org/jira/browse/LANG-299
1443     @Test
1444     public void testLang299() {
1445         final StrBuilder sb = new StrBuilder(1);
1446         sb.appendFixedWidthPadRight("foo", 1, '-');
1447         assertEquals("f", sb.toString());
1448     }
1449 }