This commit is contained in:
akeshri
2020-09-09 09:34:10 +05:30
parent 64b7dec1b0
commit ece4d65065
4 changed files with 65 additions and 88 deletions
@@ -11,30 +11,22 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
public class MapFirstPairServiceUnitTest {
public class MapFirstPairUnitTest {
private MapFirstPairService mapFirstPairService;
private MapFirstPairExample mapFirstPairExample;
@Before
public void Setup() {
mapFirstPairService = new MapFirstPairServiceImpl();
}
private void populateMapValues(Map<Integer, String> map) {
if (map != null) {
map.put(5, "A");
map.put(1, "B");
map.put(2, "C");
}
mapFirstPairExample = new MapFirstPairExample();
}
@Test
public void whenUsingIteratorForHashMap() {
public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
Map<Integer, String> hashMap = new HashMap<>();
populateMapValues(hashMap);
hashMap = mapFirstPairExample.populateMapValues(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
@@ -43,10 +35,10 @@ public class MapFirstPairServiceUnitTest {
}
@Test
public void whenUsingStreamForHashMap() {
public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() {
Map<Integer, String> hashMap = new HashMap<>();
populateMapValues(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap);
hashMap = mapFirstPairExample.populateMapValues(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");
Map.Entry<Integer, String> pairInsertedFirst = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
@@ -55,21 +47,21 @@ public class MapFirstPairServiceUnitTest {
}
@Test
public void whenUsingIteratorForLinkedHashMap() {
public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
populateMapValues(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap);
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
}
@Test
public void whenUsingStreamForLinkedHashMap() {
public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
populateMapValues(linkedHashMap);
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
@@ -78,10 +70,10 @@ public class MapFirstPairServiceUnitTest {
@Test
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() {
Map<Integer, String> hashMap = new HashMap<>();
populateMapValues(hashMap);
hashMap = mapFirstPairExample.populateMapValues(hashMap);
hashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
assertEquals(expectedValue, actualValue);
@@ -90,10 +82,10 @@ public class MapFirstPairServiceUnitTest {
@Test
public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() {
Map<Integer, String> hashMap = new HashMap<>();
populateMapValues(hashMap);
hashMap = mapFirstPairExample.populateMapValues(hashMap);
hashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");
assertEquals(expectedValue, actualValue);
@@ -102,10 +94,10 @@ public class MapFirstPairServiceUnitTest {
@Test
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
populateMapValues(linkedHashMap);
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
linkedHashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);
@@ -114,10 +106,10 @@ public class MapFirstPairServiceUnitTest {
@Test
public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() {
Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
populateMapValues(linkedHashMap);
linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap);
linkedHashMap.put(0, "D");
Map.Entry<Integer, String> actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap);
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");
assertEquals(expectedValue, actualValue);