1+ package com .baeldung .stringbuildernullorempty ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertFalse ;
4+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5+
6+ import org .junit .jupiter .api .Test ;
7+
8+ public class StringBuilderNullOrEmptyUnitTest {
9+
10+ private static final StringBuilder SB_NULL = null ;
11+ private static final StringBuilder SB_EMPTY = new StringBuilder ();
12+ private static final StringBuilder SB_EMPTY_STR = new StringBuilder ("" );
13+ private static final StringBuilder SB_BLANK_STR = new StringBuilder (" " );
14+ private static final StringBuilder SB_WITH_TEXT = new StringBuilder ("I am a magic string" );
15+
16+ public static boolean isNull (StringBuilder sb ) {
17+ return sb == null ;
18+ }
19+
20+ @ Test
21+ void whenCallingIsNull_thenCorrect () {
22+ assertTrue (isNull (SB_NULL ));
23+
24+ assertFalse (isNull (SB_EMPTY ));
25+ assertFalse (isNull (SB_EMPTY_STR ));
26+ assertFalse (isNull (SB_BLANK_STR ));
27+ assertFalse (isNull (SB_WITH_TEXT ));
28+ }
29+
30+ public static boolean isNullOrEmptyByStrEmpty (StringBuilder sb ) {
31+ return sb == null || sb .toString ()
32+ .isEmpty ();
33+ }
34+
35+ @ Test
36+ void whenCallingIsNullOrEmptyByStrEmpty_thenCorrect () {
37+ assertTrue (isNullOrEmptyByStrEmpty (SB_NULL ));
38+ assertTrue (isNullOrEmptyByStrEmpty (SB_EMPTY ));
39+ assertTrue (isNullOrEmptyByStrEmpty (SB_EMPTY_STR ));
40+
41+ assertFalse (isNullOrEmptyByStrEmpty (SB_BLANK_STR ));
42+ assertFalse (isNullOrEmptyByStrEmpty (SB_WITH_TEXT ));
43+ }
44+
45+ public static boolean isNullOrEmptyByLength (StringBuilder sb ) {
46+ return sb == null || sb .length () == 0 ;
47+ }
48+
49+ @ Test
50+ void whenCallingIsNullOrEmptyByLength_thenCorrect () {
51+ assertTrue (isNullOrEmptyByLength (SB_NULL ));
52+ assertTrue (isNullOrEmptyByLength (SB_EMPTY ));
53+ assertTrue (isNullOrEmptyByLength (SB_EMPTY_STR ));
54+
55+ assertFalse (isNullOrEmptyByLength (SB_BLANK_STR ));
56+ assertFalse (isNullOrEmptyByLength (SB_WITH_TEXT ));
57+ }
58+
59+ public static boolean isNullOrEmpty (StringBuilder sb ) {
60+ return sb == null || sb .isEmpty ();
61+ }
62+
63+ @ Test
64+ void whenCallingIsNullOrEmpty_thenCorrect () {
65+ assertTrue (isNullOrEmpty (SB_NULL ));
66+ assertTrue (isNullOrEmpty (SB_EMPTY ));
67+ assertTrue (isNullOrEmpty (SB_EMPTY_STR ));
68+
69+ assertFalse (isNullOrEmpty (SB_BLANK_STR ));
70+ assertFalse (isNullOrEmpty (SB_WITH_TEXT ));
71+ }
72+ }
0 commit comments