From 7df755545a310d46ab3c882ce6ca32fe13ed8a01 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 12 Jan 2017 11:16:34 +0100 Subject: [PATCH 1/2] BAEL-586 guava multimap examples --- .../java/com/baeldung/guava/MultimapTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 guava19/src/test/java/com/baeldung/guava/MultimapTest.java diff --git a/guava19/src/test/java/com/baeldung/guava/MultimapTest.java b/guava19/src/test/java/com/baeldung/guava/MultimapTest.java new file mode 100644 index 0000000000..dbe47923bb --- /dev/null +++ b/guava19/src/test/java/com/baeldung/guava/MultimapTest.java @@ -0,0 +1,62 @@ +package com.baeldung.guava; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import org.junit.Test; + +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class MultimapTest { + + @Test + public void givenMap_whenAddTwoValuesFroSameKey_shouldOverridePreviousKey() { + //given + String key = "a-key"; + Map map = new LinkedHashMap<>(); + + //when + map.put(key, "firstValue"); + map.put(key, "secondValue"); + + //then + assertEquals(1, map.size()); + } + + @Test + public void givenMultiMap_whenAddTwoValuesFroSameKey_shouldHaveTwoEntriesInMap() { + //given + String key = "a-key"; + Multimap map = ArrayListMultimap.create(); + + //when + map.put(key, "firstValue"); + map.put(key, "secondValue"); + + //then + assertEquals(2, map.size()); + } + + @Test + public void givenMapOfListValues_whenAddTwoValuesFroSameKey_shouldHaveTwoElementInList() { + //given + String key = "a-key"; + Map> map = new LinkedHashMap<>(); + + //when + List values = map.get(key); + if(values == null){ + values = new LinkedList<>(); + values.add("firstValue"); + values.add("secondValue"); + } + map.put(key, values); + + //then + assertEquals(1, map.size()); + } +} From 91fb87c06dc2fb406c8d8d0b14351c55095fdf96 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 12 Jan 2017 18:41:04 +0100 Subject: [PATCH 2/2] BAEL-586 rename tests according to pr review --- guava19/src/test/java/com/baeldung/guava/MultimapTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/guava19/src/test/java/com/baeldung/guava/MultimapTest.java b/guava19/src/test/java/com/baeldung/guava/MultimapTest.java index dbe47923bb..c984ab1e4b 100644 --- a/guava19/src/test/java/com/baeldung/guava/MultimapTest.java +++ b/guava19/src/test/java/com/baeldung/guava/MultimapTest.java @@ -14,7 +14,7 @@ import static org.junit.Assert.assertEquals; public class MultimapTest { @Test - public void givenMap_whenAddTwoValuesFroSameKey_shouldOverridePreviousKey() { + public void givenMap_whenAddTwoValuesForSameKey_shouldOverridePreviousKey() { //given String key = "a-key"; Map map = new LinkedHashMap<>(); @@ -28,7 +28,7 @@ public class MultimapTest { } @Test - public void givenMultiMap_whenAddTwoValuesFroSameKey_shouldHaveTwoEntriesInMap() { + public void givenMultiMap_whenAddTwoValuesForSameKey_shouldHaveTwoEntriesInMap() { //given String key = "a-key"; Multimap map = ArrayListMultimap.create(); @@ -42,7 +42,7 @@ public class MultimapTest { } @Test - public void givenMapOfListValues_whenAddTwoValuesFroSameKey_shouldHaveTwoElementInList() { + public void givenMapOfListValues_whenAddTwoValuesForSameKey_shouldHaveTwoElementsInList() { //given String key = "a-key"; Map> map = new LinkedHashMap<>();