1+ package com .baeldung .commons .collections ;
2+
3+
4+ import com .baeldung .commons .collectionutil .Address ;
5+ import com .baeldung .commons .collectionutil .Customer ;
6+ import org .apache .commons .collections4 .Closure ;
7+ import org .apache .commons .collections4 .CollectionUtils ;
8+ import org .apache .commons .collections4 .Predicate ;
9+ import org .apache .commons .collections4 .Transformer ;
10+ import org .junit .Before ;
11+ import org .junit .Test ;
12+
13+ import java .util .*;
14+
15+ import static org .junit .Assert .assertFalse ;
16+ import static org .junit .Assert .assertTrue ;
17+
18+ public class CollectionUtilsGuideTest {
19+
20+
21+ Customer customer1 = new Customer (1 , "Daniel" , 123456l , "locality1" , "city1" , "1234" );
22+ Customer customer4 = new Customer (4 , "Bob" , 456789l , "locality4" , "city4" , "4567" );
23+ List <Customer > list1 , list2 , list3 , linkedList1 ;
24+
25+ @ Before
26+ public void setup () {
27+ Customer customer2 = new Customer (2 , "Fredrik" , 234567l , "locality2" , "city2" , "2345" );
28+ Customer customer3 = new Customer (3 , "Kyle" , 345678l , "locality3" , "city3" , "3456" );
29+ Customer customer5 = new Customer (5 , "Cat" , 567890l , "locality5" , "city5" , "5678" );
30+ Customer customer6 = new Customer (6 , "John" , 678901l , "locality6" , "city6" , "6789" );
31+
32+ list1 = Arrays .asList (customer1 , customer2 , customer3 );
33+ list2 = Arrays .asList (customer4 , customer5 , customer6 );
34+ list3 = Arrays .asList (customer1 , customer2 );
35+
36+ linkedList1 = new LinkedList <>(list1 );
37+ }
38+
39+ @ Test
40+ public void givenList_whenAddIgnoreNull_thenNoNullAdded () {
41+ CollectionUtils .addIgnoreNull (list1 , null );
42+ assertFalse (list1 .contains (null ));
43+ }
44+
45+ @ Test
46+ public void givenTwoSortedLists_whenCollated_thenSorted () {
47+ List <Customer > sortedList = CollectionUtils .collate (list1 , list2 );
48+ assertTrue (sortedList .get (0 ).getName ().equals ("Bob" ));
49+ assertTrue (sortedList .get (2 ).getName ().equals ("Daniel" ));
50+ }
51+
52+ @ Test
53+ public void givenListOfCustomers_whenTransformed_thenListOfAddress () {
54+ Collection <Address > addressCol = CollectionUtils .collect (list1 , new Transformer <Customer , Address >() {
55+ public Address transform (Customer customer ) {
56+ return new Address (customer .getLocality (), customer .getCity (), customer .getZip ());
57+ }
58+ });
59+
60+ List <Address > addressList = new ArrayList <Address >(addressCol );
61+ assertTrue (addressList .size () == 3 );
62+ assertTrue (addressList .get (0 ).getLocality ().equals ("locality1" ));
63+ }
64+
65+ @ Test
66+ public void givenCustomerList_whenCountMatches_thenCorrect () {
67+ int result = CollectionUtils .countMatches (list1 , customer -> Arrays .asList ("Daniel" ,"Kyle" ).contains (customer .getName ()));
68+ assertTrue (result == 2 );
69+ }
70+
71+ @ Test
72+ public void givenCustomerList_WhenFiltered_thenCorrectSize () {
73+
74+ boolean isModified = CollectionUtils .filter (linkedList1 , customer -> Arrays .asList ("Daniel" ,"Kyle" ).contains (customer .getName ()));
75+
76+ //filterInverse does the opposite. It removes the element from the list if the Predicate returns true
77+ //select and selectRejected work the same way except that they do not remove elements from the given collection and return a new collection
78+
79+ assertTrue (isModified && linkedList1 .size () == 2 );
80+ }
81+
82+ @ Test
83+ public void givenCustomerList_whenForAllDoSetNameNull_thenNameNull () {
84+ CollectionUtils .forAllDo (list1 , customer -> {
85+ customer .setName (null );
86+ });
87+
88+ // forAllButLast does the same except for the last element in the collection
89+ assertTrue (list1 .get (0 ).getName () == null );
90+ }
91+
92+ @ Test
93+ public void givenNonEmptyList_whenCheckedIsNotEmpty_thenTrue () {
94+ List <Customer > emptyList = new ArrayList <Customer >();
95+ List <Customer > nullList = null ;
96+
97+ //Very handy at times where we want to check if a collection is not null and not empty too.
98+ //isNotEmpty does the opposite. Handy because using ! operator on isEmpty makes it missable while reading
99+ assertTrue (CollectionUtils .isNotEmpty (list1 ));
100+ assertTrue (CollectionUtils .isEmpty (nullList ));
101+ assertTrue (CollectionUtils .isEmpty (emptyList ));
102+ }
103+
104+ @ Test
105+ public void givenCustomerListAndASubcollection_whenChecked_thenTrue () {
106+ assertTrue (CollectionUtils .isSubCollection (list3 , list1 ));
107+ }
108+
109+ @ Test
110+ public void givenTwoLists_whenIntersected_thenCheckSize () {
111+ Collection <Customer > intersection = CollectionUtils .intersection (list1 , list3 );
112+ assertTrue (intersection .size () == 2 );
113+ }
114+
115+ @ Test
116+ public void givenTwoLists_whenSubtracted_thenCheckElementNotPresentInA () {
117+ Collection <Customer > result = CollectionUtils .subtract (list1 , list3 );
118+ assertFalse (result .contains (customer1 ));
119+ }
120+
121+ @ Test
122+ public void givenTwoLists_whenUnioned_thenCheckElementPresentInResult () {
123+ Collection <Customer > union = CollectionUtils .union (list1 , list2 );
124+ assertTrue (union .contains (customer1 ));
125+ assertTrue (union .contains (customer4 ));
126+ }
127+
128+ }
0 commit comments