1+ package org .baeldung .guava ;
2+
3+ import com .google .common .collect .HashMultiset ;
4+ import com .google .common .collect .Multiset ;
5+ import org .junit .Test ;
6+
7+ import java .util .HashMap ;
8+ import java .util .Map ;
9+
10+ import static org .assertj .core .api .Assertions .assertThat ;
11+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
12+
13+ public class GuavaMultiSetUnitTest {
14+
15+ @ Test
16+ public void givenMultiSet_whenAddingValues_shouldReturnCorrectCount () {
17+ Multiset <String > bookStore = HashMultiset .create ();
18+ bookStore .add ("Potter" );
19+ bookStore .add ("Potter" );
20+ bookStore .add ("Potter" );
21+
22+ assertThat (bookStore .contains ("Potter" )).isTrue ();
23+ assertThat (bookStore .count ("Potter" )).isEqualTo (3 );
24+ }
25+
26+ @ Test
27+ public void givenMultiSet_whenRemovingValues_shouldReturnCorrectCount () {
28+ Multiset <String > bookStore = HashMultiset .create ();
29+ bookStore .add ("Potter" );
30+ bookStore .add ("Potter" );
31+
32+ bookStore .remove ("Potter" );
33+ assertThat (bookStore .contains ("Potter" )).isTrue ();
34+ assertThat (bookStore .count ("Potter" )).isEqualTo (1 );
35+ }
36+
37+ @ Test
38+ public void givenMultiSet_whenSetCount_shouldReturnCorrectCount () {
39+ Multiset <String > bookStore = HashMultiset .create ();
40+ bookStore .setCount ("Potter" , 50 );
41+ assertThat (bookStore .count ("Potter" )).isEqualTo (50 );
42+ }
43+
44+ @ Test
45+ public void givenMultiSet_whenSettingNegativeCount_shouldThrowException () {
46+ Multiset <String > bookStore = HashMultiset .create ();
47+ assertThatThrownBy (() -> bookStore .setCount ("Potter" , -1 ))
48+ .isInstanceOf (IllegalArgumentException .class );
49+ }
50+
51+ @ Test
52+ public void givenMultiSet_whenSettingCountWithEmptySet_shouldBeSuccessful () {
53+ Multiset <String > bookStore = HashMultiset .create ();
54+ assertThat (bookStore .setCount ("Potter" , 0 , 2 )).isTrue ();
55+ }
56+
57+ @ Test
58+ public void givenMultiSet_whenSettingCountWithCorrectValue_shouldBeSuccessful () {
59+ Multiset <String > bookStore = HashMultiset .create ();
60+ bookStore .add ("Potter" );
61+ bookStore .add ("Potter" );
62+
63+ assertThat (bookStore .setCount ("Potter" , 2 , 52 )).isTrue ();
64+ }
65+
66+ @ Test
67+ public void givenMultiSet_whenSettingCountWithIncorrectValue_shouldFail () {
68+ Multiset <String > bookStore = HashMultiset .create ();
69+ bookStore .add ("Potter" );
70+ bookStore .add ("Potter" );
71+
72+ assertThat (bookStore .setCount ("Potter" , 5 , 52 )).isFalse ();
73+ }
74+
75+ @ Test
76+ public void givenMap_compareMultiSetOperations () {
77+ Map <String , Integer > bookStore = new HashMap <>();
78+ bookStore .put ("Potter" , 3 );
79+
80+ assertThat (bookStore .containsKey ("Potter" )).isTrue ();
81+ assertThat (bookStore .get ("Potter" )).isEqualTo (3 );
82+
83+ bookStore .put ("Potter" , 2 );
84+ assertThat (bookStore .get ("Potter" )).isEqualTo (2 );
85+
86+ bookStore .put ("Potter" , null );
87+ assertThat (bookStore .containsKey ("Potter" )).isTrue ();
88+
89+ bookStore .put ("Potter" , -1 );
90+ assertThat (bookStore .containsKey ("Potter" )).isTrue ();
91+ }
92+ }
0 commit comments