1+ package com .baeldung .array .nullorempty ;
2+
3+ import static org .junit .jupiter .api .Assertions .assertFalse ;
4+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5+
6+ import java .math .BigDecimal ;
7+
8+ import org .apache .commons .lang3 .ArrayUtils ;
9+ import org .apache .commons .lang3 .ObjectUtils ;
10+ import org .junit .jupiter .api .Test ;
11+
12+ public class CheckArrayNullOrEmptyUnitTest {
13+
14+ private final static String [] STR_ARRAY = new String [] { "a" , "b" , "c" , "d" };
15+ private final static BigDecimal [] EMPTY_ARRAY = new BigDecimal [] {};
16+ private final static String [] NULL_ARRAY = null ;
17+ // primitive array
18+ private final static int [] INT_ARRAY = new int [] { 1 , 2 , 3 , 4 };
19+
20+ public static <T > boolean isArrayNullOrEmpty (T [] theArray ) {
21+ return theArray == null || theArray .length == 0 ;
22+ }
23+
24+ public static boolean isArrayNullOrEmpty (int [] theArray ) {
25+ return theArray == null || theArray .length == 0 ;
26+ }
27+
28+ @ Test
29+ void whenUsingIsArrayNullOrEmpty_thenCorrect () {
30+ assertTrue (isArrayNullOrEmpty (NULL_ARRAY ));
31+ assertTrue (isArrayNullOrEmpty (EMPTY_ARRAY ));
32+ assertFalse (isArrayNullOrEmpty (STR_ARRAY ));
33+
34+ //int array uses isArrayNullOrEmpty(int[] theArray)
35+ assertFalse (isArrayNullOrEmpty (INT_ARRAY ));
36+ }
37+
38+ @ Test
39+ void whenUsingArrayUtils_thenCorrect () {
40+ assertTrue (ArrayUtils .isEmpty (NULL_ARRAY ));
41+ assertTrue (ArrayUtils .isEmpty (EMPTY_ARRAY ));
42+ assertFalse (ArrayUtils .isEmpty (STR_ARRAY ));
43+ //primitive array
44+ assertFalse (ArrayUtils .isEmpty (INT_ARRAY ));
45+ }
46+
47+ @ Test
48+ void whenUsingObjectUtils_thenCorrect () {
49+ assertTrue (ObjectUtils .isEmpty (NULL_ARRAY ));
50+ assertTrue (ObjectUtils .isEmpty (EMPTY_ARRAY ));
51+ assertFalse (ObjectUtils .isEmpty (STR_ARRAY ));
52+ //primitive array
53+ assertFalse (ObjectUtils .isEmpty (INT_ARRAY ));
54+ }
55+ }
0 commit comments