BAEL-2018 Moved to core-java-collections (#4796)
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsInAnyOrder;
|
||||
|
||||
public class ConvertListToMapServiceUnitTest {
|
||||
List<Animal> list;
|
||||
|
||||
private ConvertListToMapService convertListService;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
this.convertListService = new ConvertListToMapService();
|
||||
this.list = new ArrayList<>();
|
||||
|
||||
Animal cat = new Animal(1, "Cat");
|
||||
list.add(cat);
|
||||
Animal dog = new Animal(2, "Dog");
|
||||
list.add(dog);
|
||||
Animal pig = new Animal(3, "Pig");
|
||||
list.add(pig);
|
||||
Animal cow = new Animal(4, "Cow");
|
||||
list.add(cow);
|
||||
Animal goat = new Animal(5, "Goat");
|
||||
list.add(goat);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertBeforeJava8_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListBeforeJava8(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertAfterJava8_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListAfterJava8(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertWithGuava_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithGuava(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAList_whenConvertWithApacheCommons_thenReturnMapWithTheSameElements() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(list);
|
||||
|
||||
assertThat(map.values(), containsInAnyOrder(list.toArray()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.convertlisttomap;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
|
||||
public class ConvertListWithDiplicatedIdToMapServiceUnitTest {
|
||||
List<Animal> duplicatedIdList;
|
||||
|
||||
private ConvertListToMapService convertListService = new ConvertListToMapService();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
|
||||
this.duplicatedIdList = new ArrayList<>();
|
||||
|
||||
Animal cat = new Animal(1, "Cat");
|
||||
duplicatedIdList.add(cat);
|
||||
Animal dog = new Animal(2, "Dog");
|
||||
duplicatedIdList.add(dog);
|
||||
Animal pig = new Animal(3, "Pig");
|
||||
duplicatedIdList.add(pig);
|
||||
Animal cow = new Animal(4, "Cow");
|
||||
duplicatedIdList.add(cow);
|
||||
Animal goat = new Animal(4, "Goat");
|
||||
duplicatedIdList.add(goat);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenADupIdList_whenConvertBeforeJava8_thenReturnMapWithRewrittenElement() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListBeforeJava8(duplicatedIdList);
|
||||
|
||||
assertThat(map.values(), hasSize(4));
|
||||
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenADupIdList_whenConvertWithApacheCommons_thenReturnMapWithRewrittenElement() {
|
||||
|
||||
Map<Integer, Animal> map = convertListService.convertListWithApacheCommons(duplicatedIdList);
|
||||
|
||||
assertThat(map.values(), hasSize(4));
|
||||
assertThat(map.values(), hasItem(duplicatedIdList.get(4)));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void givenADupIdList_whenConvertAfterJava8_thenException() {
|
||||
|
||||
convertListService.convertListAfterJava8(duplicatedIdList);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void givenADupIdList_whenConvertWithGuava_thenException() {
|
||||
|
||||
convertListService.convertListWithGuava(duplicatedIdList);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user