|
8 | 8 |
|
9 | 9 | import java.nio.charset.Charset; |
10 | 10 | import java.nio.charset.CharsetEncoder; |
11 | | -import java.util.ArrayList; |
12 | | -import java.util.List; |
13 | | -import java.util.Map; |
| 11 | +import java.util.*; |
14 | 12 |
|
| 13 | +import com.google.common.collect.*; |
15 | 14 | import org.junit.Test; |
16 | 15 |
|
17 | 16 | import com.google.common.base.CharMatcher; |
18 | 17 | import com.google.common.base.Function; |
19 | 18 | import com.google.common.base.Joiner; |
20 | 19 | import com.google.common.base.Predicate; |
21 | 20 | import com.google.common.base.Splitter; |
22 | | -import com.google.common.collect.Iterables; |
23 | | -import com.google.common.collect.Lists; |
24 | | -import com.google.common.collect.Maps; |
25 | 21 |
|
26 | 22 | public class GuavaStringTest { |
27 | 23 |
|
@@ -215,4 +211,53 @@ public boolean apply(final Character c) { |
215 | 211 | assertEquals("hello", result); |
216 | 212 | } |
217 | 213 |
|
| 214 | + public static class MultimapTest { |
| 215 | + |
| 216 | + @Test |
| 217 | + public void givenMap_whenAddTwoValuesForSameKey_shouldOverridePreviousKey() { |
| 218 | + //given |
| 219 | + String key = "a-key"; |
| 220 | + Map<String, String> map = new LinkedHashMap<>(); |
| 221 | + |
| 222 | + //when |
| 223 | + map.put(key, "firstValue"); |
| 224 | + map.put(key, "secondValue"); |
| 225 | + |
| 226 | + //then |
| 227 | + assertEquals(1, map.size()); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void givenMultiMap_whenAddTwoValuesForSameKey_shouldHaveTwoEntriesInMap() { |
| 232 | + //given |
| 233 | + String key = "a-key"; |
| 234 | + Multimap<String, String> map = ArrayListMultimap.create(); |
| 235 | + |
| 236 | + //when |
| 237 | + map.put(key, "firstValue"); |
| 238 | + map.put(key, "secondValue"); |
| 239 | + |
| 240 | + //then |
| 241 | + assertEquals(2, map.size()); |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + public void givenMapOfListValues_whenAddTwoValuesForSameKey_shouldHaveTwoElementsInList() { |
| 246 | + //given |
| 247 | + String key = "a-key"; |
| 248 | + Map<String, List<String>> map = new LinkedHashMap<>(); |
| 249 | + |
| 250 | + //when |
| 251 | + List<String> values = map.get(key); |
| 252 | + if(values == null){ |
| 253 | + values = new LinkedList<>(); |
| 254 | + values.add("firstValue"); |
| 255 | + values.add("secondValue"); |
| 256 | + } |
| 257 | + map.put(key, values); |
| 258 | + |
| 259 | + //then |
| 260 | + assertEquals(1, map.size()); |
| 261 | + } |
| 262 | + } |
218 | 263 | } |
0 commit comments