1+ package com .baeldung .guava ;
2+
3+ import static org .junit .Assert .assertArrayEquals ;
4+ import static org .junit .Assert .assertEquals ;
5+ import static org .junit .Assert .assertTrue ;
6+
7+ import java .util .Arrays ;
8+ import java .util .List ;
9+
10+ import org .junit .Test ;
11+
12+ import com .google .common .collect .ArrayListMultimap ;
13+ import com .google .common .collect .BiMap ;
14+ import com .google .common .collect .HashBasedTable ;
15+ import com .google .common .collect .HashBiMap ;
16+ import com .google .common .collect .Multimap ;
17+ import com .google .common .collect .Table ;
18+
19+ public class GuavaUnitTest {
20+ private final static BiMap <Integer , String > daysOfWeek = HashBiMap .create ();
21+ private final static Multimap <String , String > groceryCart = ArrayListMultimap .create ();
22+ private final static Table <String , String , String > cityCoordinates = HashBasedTable .create ();
23+ private final static Table <String , String , String > movies = HashBasedTable .create ();
24+
25+ static {
26+ daysOfWeek .put (1 , "Monday" );
27+ daysOfWeek .put (2 , "Tuesday" );
28+ daysOfWeek .put (3 , "Wednesday" );
29+ daysOfWeek .put (4 , "Thursday" );
30+ daysOfWeek .put (5 , "Friday" );
31+ daysOfWeek .put (6 , "Saturday" );
32+ daysOfWeek .put (7 , "Sunday" );
33+
34+ groceryCart .put ("Fruits" , "Apple" );
35+ groceryCart .put ("Fruits" , "Grapes" );
36+ groceryCart .put ("Fruits" , "Strawberries" );
37+ groceryCart .put ("Vegetables" , "Spinach" );
38+ groceryCart .put ("Vegetables" , "Cabbage" );
39+
40+ cityCoordinates .put ("40.7128° N" , "74.0060° W" , "New York" );
41+ cityCoordinates .put ("48.8566° N" , "2.3522° E" , "Paris" );
42+ cityCoordinates .put ("19.0760° N" , "72.8777° E" , "Mumbai" );
43+
44+ movies .put ("Tom Hanks" , "Meg Ryan" , "You've Got Mail" );
45+ movies .put ("Tom Hanks" , "Catherine Zeta-Jones" , "The Terminal" );
46+ movies .put ("Bradley Cooper" , "Lady Gaga" , "A Star is Born" );
47+ movies .put ("Keenu Reaves" , "Sandra Bullock" , "Speed" );
48+ movies .put ("Tom Hanks" , "Sandra Bullock" , "Extremely Loud & Incredibly Close" );
49+ }
50+
51+ @ Test
52+ public void givenBiMap_whenValue_thenKeyReturned () {
53+ assertEquals (Integer .valueOf (7 ), daysOfWeek .inverse ()
54+ .get ("Sunday" ));
55+ }
56+
57+ @ Test
58+ public void givenBiMap_whenKey_thenValueReturned () {
59+ assertEquals ("Tuesday" , daysOfWeek .get (2 ));
60+ }
61+
62+ @ Test
63+ public void givenMultiValuedMap_whenFruitsFetched_thenFruitsReturned () {
64+
65+ List <String > fruits = Arrays .asList ("Apple" , "Grapes" , "Strawberries" );
66+ assertEquals (fruits , groceryCart .get ("Fruits" ));
67+ }
68+
69+ @ Test
70+ public void givenMultiValuedMap_whenVeggiesFetched_thenVeggiesReturned () {
71+ List <String > veggies = Arrays .asList ("Spinach" , "Cabbage" );
72+ assertEquals (veggies , groceryCart .get ("Vegetables" ));
73+ }
74+
75+ @ Test
76+ public void givenMultiValuedMap_whenFuitsRemoved_thenVeggiesPreserved () {
77+
78+ assertEquals (5 , groceryCart .size ());
79+
80+ groceryCart .remove ("Fruits" , "Apple" );
81+ assertEquals (4 , groceryCart .size ());
82+
83+ groceryCart .removeAll ("Fruits" );
84+ assertEquals (2 , groceryCart .size ());
85+ }
86+
87+ @ Test
88+ public void givenCoordinatesTable_whenFetched_thenOK () {
89+
90+ List <String > expectedLongitudes = Arrays .asList ("74.0060° W" , "2.3522° E" , "72.8777° E" );
91+
92+ assertArrayEquals (expectedLongitudes .toArray (), cityCoordinates .columnKeySet ()
93+ .toArray ());
94+
95+ List <String > expectedCities = Arrays .asList ("New York" , "Paris" , "Mumbai" );
96+
97+ assertArrayEquals (expectedCities .toArray (), cityCoordinates .values ()
98+ .toArray ());
99+
100+ assertTrue (cityCoordinates .rowKeySet ()
101+ .contains ("48.8566° N" ));
102+
103+ }
104+
105+ @ Test
106+ public void givenMoviesTable_whenFetched_thenOK () {
107+ assertEquals (3 , movies .row ("Tom Hanks" )
108+ .size ());
109+
110+ assertEquals (2 , movies .column ("Sandra Bullock" )
111+ .size ());
112+
113+ assertEquals ("A Star is Born" , movies .get ("Bradley Cooper" , "Lady Gaga" ));
114+
115+ assertTrue (movies .containsValue ("Speed" ));
116+
117+ }
118+ }
0 commit comments