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