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