From ea345fa246b290b7ee98fcc5e5016877b2bde7d2 Mon Sep 17 00:00:00 2001 From: gitterjim-I Date: Wed, 29 Mar 2017 22:18:20 +0100 Subject: [PATCH] Integrate forEach and stream changes to test, removing non-test class. (#1529) * article Bael-667 initial commit. * Switch to use logging framework for output. --- core-java/0.8260098203820962 | 0 .../FlattenNestedListTest.java | 82 +++++++++++-------- 2 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 core-java/0.8260098203820962 diff --git a/core-java/0.8260098203820962 b/core-java/0.8260098203820962 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java b/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java index cf9334954b..fdf4934cb7 100644 --- a/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java +++ b/core-java/src/test/java/com/baeldung/list/flattennestedlist/FlattenNestedListTest.java @@ -1,6 +1,8 @@ package com.baeldung.list.flattennestedlist; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.Arrays; @@ -8,45 +10,60 @@ import java.util.Collection; import java.util.List; import java.util.stream.Collectors; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; public class FlattenNestedListTest { + private List> lol = new ArrayList<>(); + List ls1 = Arrays.asList("one:one", "one:two", "one:three"); + List ls2 = Arrays.asList("two:one", "two:two", "two:three"); + List ls3 = Arrays.asList("three:one", "three:two", "three:three"); - @Test - public void givenListOfListOfString_flattenNestedList1() { - // given - List ls1 = Arrays.asList("one:one", "one:two", "one:three"); - List ls2 = Arrays.asList("two:one", "two:two", "two:three"); - List ls3 = Arrays.asList("three:one", "three:two", "three:three"); + @Before + public void setup() { + lol.addAll(Arrays.asList(ls1, ls2, ls3)); + } - List> list = Arrays.asList(ls1, ls2, ls3); - - // when - List ls = flattenListOfListsImperatively(list); - - // then - assertNotNull(ls); - assertTrue(ls.size() == 9); - //TODO content assertion + @After + public void tearDown() { + lol = null; } @Test - public void givenListOfListOfString_flattenNestedList2() { - // given - List ls1 = Arrays.asList("one:one", "one:two", "one:three"); - List ls2 = Arrays.asList("two:one", "two:two", "two:three"); - List ls3 = Arrays.asList("three:one", "three:two", "three:three"); + public void givenString_flattenNestedList1() { + List ls = flattenListOfListsImperatively(lol); - List> list = Arrays.asList(ls1, ls2, ls3); - - // when - List ls = flattenListOfListsStream(list); - - // then assertNotNull(ls); assertTrue(ls.size() == 9); - //TODO content assertion + // assert content + assertEquals(ls.get(0), "one:one"); + assertEquals(ls.get(1), "one:two"); + assertEquals(ls.get(2), "one:three"); + assertEquals(ls.get(3), "two:one"); + assertEquals(ls.get(4), "two:two"); + assertEquals(ls.get(5), "two:three"); + assertEquals(ls.get(6), "three:one"); + assertEquals(ls.get(7), "three:two"); + assertEquals(ls.get(8), "three:three"); + } + + @Test + public void givenString_flattenNestedList2() { + List ls = flattenListOfListsStream(lol); + + assertNotNull(ls); + assertTrue(ls.size() == 9); + // assert content + assertEquals(ls.get(0), "one:one"); + assertEquals(ls.get(1), "one:two"); + assertEquals(ls.get(2), "one:three"); + assertEquals(ls.get(3), "two:one"); + assertEquals(ls.get(4), "two:two"); + assertEquals(ls.get(5), "two:three"); + assertEquals(ls.get(6), "three:one"); + assertEquals(ls.get(7), "three:two"); + assertEquals(ls.get(8), "three:three"); } public List flattenListOfListsImperatively(List> list) { @@ -56,9 +73,6 @@ public class FlattenNestedListTest { } public List flattenListOfListsStream(List> list) { - return list.stream() - .flatMap(Collection::stream) - .collect(Collectors.toList()); + return list.stream().flatMap(Collection::stream).collect(Collectors.toList()); } - }