11package org .baeldung .guava ;
22
3+ import static org .hamcrest .Matchers .contains ;
4+ import static org .hamcrest .Matchers .containsInAnyOrder ;
35import static org .junit .Assert .assertEquals ;
6+ import static org .junit .Assert .assertFalse ;
7+ import static org .junit .Assert .assertThat ;
8+ import static org .junit .Assert .assertTrue ;
49
510import java .util .Collection ;
6- import java .util .Collections ;
711import java .util .List ;
812
913import org .junit .Test ;
1418import com .google .common .base .Predicates ;
1519import com .google .common .collect .Collections2 ;
1620import com .google .common .collect .FluentIterable ;
17- import com .google .common .collect .ListMultimap ;
21+ import com .google .common .collect .Iterables ;
1822import com .google .common .collect .Lists ;
19- import com .google .common .collect .Multimaps ;
20- import com .google .common .collect .Ordering ;
2123
22- public class GuavaFilterCollectionsTest {
24+
25+ public class GuavaFilterTransformCollectionsTest {
2326
2427 @ Test
25- public void whenFilterCollection_thenFiltered () {
28+ public void whenFilterWithCollections2_thenFiltered () {
2629 final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
2730 final Collection <String > result = Collections2 .filter (names , Predicates .containsPattern ("a" ));
2831
2932 assertEquals (2 , result .size ());
30- assertEquals ("[Jane, Adam]" , result .toString ());
33+ assertThat (result , containsInAnyOrder ("Jane" , "Adam" ));
34+
35+ result .add ("anna" );
36+ assertEquals (5 , names .size ());
37+ result .remove ("Adam" );
38+ assertEquals (4 , names .size ());
39+ }
40+
41+ @ Test
42+ public void whenFilterWithIterables_thenFiltered () {
43+ final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
44+ final Iterable <String > result = Iterables .filter (names , Predicates .containsPattern ("a" ));
45+
46+ assertThat (result , containsInAnyOrder ("Jane" , "Adam" ));
3147 }
3248
3349 @ Test
@@ -43,7 +59,7 @@ public final boolean apply(final String input) {
4359 final Collection <String > result = Collections2 .filter (names , predicate );
4460
4561 assertEquals (3 , result .size ());
46- assertEquals ( "[ John, Jane, Adam]" , result . toString ( ));
62+ assertThat ( result , containsInAnyOrder ( " John" , " Jane" , " Adam" ));
4763 }
4864
4965 @ Test
@@ -52,11 +68,46 @@ public void whenFilterUsingMultiplePredicates_thenFiltered() {
5268 final Collection <String > result = Collections2 .filter (names , Predicates .or (Predicates .containsPattern ("J" ), Predicates .not (Predicates .containsPattern ("a" ))));
5369
5470 assertEquals (3 , result .size ());
55- assertEquals ( "[ John, Jane, Tom]" , result . toString ( ));
71+ assertThat ( result , containsInAnyOrder ( " John" , " Jane" , " Tom" ));
5672 }
5773
5874 @ Test
59- public void whenTransformCollection_thenTransformed () {
75+ public void whenRemoveNullFromCollection_thenRemoved () {
76+ final List <String > names = Lists .newArrayList ("John" , null , "Jane" , null , "Adam" , "Tom" );
77+ final Collection <String > result = Collections2 .filter (names , Predicates .notNull ());
78+
79+ assertEquals (4 , result .size ());
80+ assertThat (result , containsInAnyOrder ("John" , "Jane" , "Adam" , "Tom" ));
81+ }
82+
83+ @ Test
84+ public void whenCheckAllElementsForCondition_thenChecked () {
85+ final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
86+
87+ boolean result = Iterables .all (names , Predicates .containsPattern ("n|m" ));
88+ assertTrue (result );
89+
90+ result = Iterables .all (names , Predicates .containsPattern ("a" ));
91+ assertFalse (result );
92+ }
93+
94+ @ Test
95+ public void whenTransformWithIterables_thenTransformed () {
96+ final Function <String , Integer > function = new Function <String , Integer >() {
97+ @ Override
98+ public final Integer apply (final String input ) {
99+ return input .length ();
100+ }
101+ };
102+
103+ final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
104+ final Iterable <Integer > result = Iterables .transform (names , function );
105+
106+ assertThat (result , contains (4 , 4 , 4 , 3 ));
107+ }
108+
109+ @ Test
110+ public void whenTransformWithCollections2_thenTransformed () {
60111 final Function <String , Integer > function = new Function <String , Integer >() {
61112 @ Override
62113 public final Integer apply (final String input ) {
@@ -68,7 +119,10 @@ public final Integer apply(final String input) {
68119 final Collection <Integer > result = Collections2 .transform (names , function );
69120
70121 assertEquals (4 , result .size ());
71- assertEquals ("[4, 4, 4, 3]" , result .toString ());
122+ assertThat (result , contains (4 , 4 , 4 , 3 ));
123+
124+ result .remove (3 );
125+ assertEquals (3 , names .size ());
72126 }
73127
74128 @ Test
@@ -91,7 +145,16 @@ public final Boolean apply(final Integer input) {
91145 final Collection <Boolean > result = Collections2 .transform (names , Functions .compose (f2 , f1 ));
92146
93147 assertEquals (4 , result .size ());
94- assertEquals ("[true, true, true, false]" , result .toString ());
148+ assertThat (result , contains (true , true , true , false ));
149+ }
150+
151+ @ Test
152+ public void whenCreateFunctionFromPredicate_thenCreated () {
153+ final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
154+ final Collection <Boolean > result = Collections2 .transform (names , Functions .forPredicate (Predicates .containsPattern ("m" )));
155+
156+ assertEquals (4 , result .size ());
157+ assertThat (result , contains (false , false , true , true ));
95158 }
96159
97160 @ Test
@@ -114,44 +177,10 @@ public final Integer apply(final String input) {
114177 final Collection <Integer > result = FluentIterable .from (names ).filter (predicate ).transform (func ).toList ();
115178
116179 assertEquals (2 , result .size ());
117- assertEquals ("[4, 3]" , result .toString ());
118- }
119-
120- @ Test
121- public void whenSortCollection_thenSorted () {
122- final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
123- Collections .sort (names , Ordering .natural ());
124- assertEquals ("[Adam, Jane, John, Tom]" , names .toString ());
125- }
126-
127- @ Test
128- public void whenSortCollectionUsingCustomOrdering_thenSorted () {
129- final Ordering <String > ordering = Ordering .natural ().onResultOf (new Function <String , Integer >() {
130- @ Override
131- public final Integer apply (final String input ) {
132- return input .length ();
133- }
134- });
135-
136- final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
137- Collections .sort (names , ordering );
138- assertEquals ("[Tom, John, Jane, Adam]" , names .toString ());
180+ assertThat (result , containsInAnyOrder (4 , 3 ));
139181 }
140182
141- @ Test
142- public void whenGroupCollection_thenGrouped () {
143- final List <String > names = Lists .newArrayList ("John" , "Jane" , "Adam" , "Tom" );
144- final ListMultimap <Integer , String > groups = Multimaps .index (names , new Function <String , Integer >() {
145- @ Override
146- public final Integer apply (final String input ) {
147- return input .length ();
148- }
149- });
150183
151- assertEquals (2 , groups .keySet ().size ());
152- assertEquals (3 , groups .get (4 ).size ());
153- assertEquals ("[Tom]" , groups .get (3 ).toString ());
154- }
155184
156185}
157186
0 commit comments