|
| 1 | +package com.baeldung.list.listoflist; |
| 2 | + |
| 3 | +import com.baeldung.java.list.Flower; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | +import java.util.*; |
| 8 | +import static org.junit.Assert.*; |
| 9 | + |
| 10 | +public class AddElementsToListUnitTest { |
| 11 | + |
| 12 | + List<Flower> flowers; |
| 13 | + |
| 14 | + @Before |
| 15 | + public void init() { |
| 16 | + this.flowers = new ArrayList<>(Arrays.asList( |
| 17 | + new Flower("Poppy", 12), |
| 18 | + new Flower("Anemone", 8), |
| 19 | + new Flower("Catmint", 12))); |
| 20 | + } |
| 21 | + @Test |
| 22 | + public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithNewItems() { |
| 23 | + List<Flower> anotherList = new ArrayList<>(); |
| 24 | + anotherList.addAll(flowers); |
| 25 | + assertEquals(anotherList.size(), flowers.size()); |
| 26 | + Assert.assertTrue(anotherList.containsAll(flowers)); |
| 27 | + } |
| 28 | + @Test |
| 29 | + public void givenAList_whenTargetListIsEmpty_thenReturnTargetListWithOneModifiedElementByConstructor() { |
| 30 | + List<Flower> anotherList = new ArrayList<>(); |
| 31 | + anotherList.addAll(flowers); |
| 32 | + Flower flower = anotherList.get(0); |
| 33 | + flower.setPetals(flowers.get(0).getPetals() * 3); |
| 34 | + assertEquals(anotherList.size(), flowers.size()); |
| 35 | + Assert.assertTrue(anotherList.containsAll(flowers)); |
| 36 | + } |
| 37 | + @Test |
| 38 | + public void givenAListAndElements_whenUseCollectionsAddAll_thenAddElementsToTargetList() { |
| 39 | + List<Flower> target = new ArrayList<>(); |
| 40 | + Collections.addAll(target, flowers.get(0), flowers.get(1), flowers.get(2), flowers.get(0)); |
| 41 | + assertEquals(target.size(), 4); |
| 42 | + } |
| 43 | + @Test |
| 44 | + public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListSkipFirstElementByStreamProcess() { |
| 45 | + List<Flower> flowerVase = new ArrayList<>(); |
| 46 | + flowers.stream() |
| 47 | + .skip(1) |
| 48 | + .forEachOrdered(flowerVase::add); |
| 49 | + assertEquals(flowerVase.size() + 1, flowers.size()); |
| 50 | + assertFalse(flowerVase.containsAll(flowers)); |
| 51 | + } |
| 52 | + @Test |
| 53 | + public void givenTwoList_whenSourceListDoesNotHaveNullElements_thenAddElementsToTargetListFilteringElementsByStreamProcess() { |
| 54 | + List<Flower> flowerVase = new ArrayList<>(); |
| 55 | + flowers.stream() |
| 56 | + .filter(f -> f.getPetals() > 10) |
| 57 | + .forEachOrdered(flowerVase::add); |
| 58 | + assertEquals(flowerVase.size() + 1, flowers.size()); |
| 59 | + assertFalse(flowerVase.containsAll(flowers)); |
| 60 | + } |
| 61 | + @Test |
| 62 | + public void givenAList_whenListIsNotNull_thenAddElementsToListByStreamProcessWihtOptional() { |
| 63 | + List<Flower> target = new ArrayList<>(); |
| 64 | + Optional.ofNullable(flowers) |
| 65 | + .ifPresent(target::addAll); |
| 66 | + assertNotNull(target); |
| 67 | + assertEquals(target.size(), 3); |
| 68 | + } |
| 69 | +} |
0 commit comments