|
1 | 1 | package com.baeldung.list.flattennestedlist; |
2 | 2 |
|
3 | | -import static org.junit.Assert.assertEquals; |
| 3 | +import static java.util.Arrays.asList; |
4 | 4 | import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertThat; |
5 | 6 | import static org.junit.Assert.assertTrue; |
6 | 7 |
|
7 | 8 | import java.util.ArrayList; |
8 | | -import java.util.Arrays; |
9 | 9 | import java.util.Collection; |
10 | 10 | import java.util.List; |
11 | 11 | import java.util.stream.Collectors; |
12 | 12 |
|
13 | | -import org.junit.After; |
14 | | -import org.junit.Before; |
| 13 | +import org.hamcrest.collection.IsIterableContainingInOrder; |
15 | 14 | import org.junit.Test; |
16 | 15 |
|
17 | 16 | public class FlattenNestedListTest { |
18 | | - private List<List<String>> lol = new ArrayList<>(); |
19 | | - List<String> ls1 = Arrays.asList("one:one", "one:two", "one:three"); |
20 | | - List<String> ls2 = Arrays.asList("two:one", "two:two", "two:three"); |
21 | | - List<String> ls3 = Arrays.asList("three:one", "three:two", "three:three"); |
22 | | - |
23 | | - @Before |
24 | | - public void setup() { |
25 | | - lol.addAll(Arrays.asList(ls1, ls2, ls3)); |
26 | | - } |
27 | | - |
28 | | - @After |
29 | | - public void tearDown() { |
30 | | - lol = null; |
31 | | - } |
| 17 | + List<List<String>> lol = asList(asList("one:one"), asList("two:one", "two:two", "two:three"), asList("three:one", "three:two", "three:three", "three:four")); |
32 | 18 |
|
33 | 19 | @Test |
34 | 20 | public void givenString_flattenNestedList1() { |
35 | 21 | List<String> ls = flattenListOfListsImperatively(lol); |
36 | 22 |
|
37 | 23 | assertNotNull(ls); |
38 | | - assertTrue(ls.size() == 9); |
| 24 | + assertTrue(ls.size() == 8); |
39 | 25 | // assert content |
40 | | - assertEquals(ls.get(0), "one:one"); |
41 | | - assertEquals(ls.get(1), "one:two"); |
42 | | - assertEquals(ls.get(2), "one:three"); |
43 | | - assertEquals(ls.get(3), "two:one"); |
44 | | - assertEquals(ls.get(4), "two:two"); |
45 | | - assertEquals(ls.get(5), "two:three"); |
46 | | - assertEquals(ls.get(6), "three:one"); |
47 | | - assertEquals(ls.get(7), "three:two"); |
48 | | - assertEquals(ls.get(8), "three:three"); |
| 26 | + assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four")); |
49 | 27 | } |
50 | 28 |
|
51 | 29 | @Test |
52 | 30 | public void givenString_flattenNestedList2() { |
53 | 31 | List<String> ls = flattenListOfListsStream(lol); |
54 | 32 |
|
55 | 33 | assertNotNull(ls); |
56 | | - assertTrue(ls.size() == 9); |
| 34 | + assertTrue(ls.size() == 8); |
57 | 35 | // assert content |
58 | | - assertEquals(ls.get(0), "one:one"); |
59 | | - assertEquals(ls.get(1), "one:two"); |
60 | | - assertEquals(ls.get(2), "one:three"); |
61 | | - assertEquals(ls.get(3), "two:one"); |
62 | | - assertEquals(ls.get(4), "two:two"); |
63 | | - assertEquals(ls.get(5), "two:three"); |
64 | | - assertEquals(ls.get(6), "three:one"); |
65 | | - assertEquals(ls.get(7), "three:two"); |
66 | | - assertEquals(ls.get(8), "three:three"); |
| 36 | + assertThat(ls, IsIterableContainingInOrder.contains("one:one", "two:one", "two:two", "two:three", "three:one", "three:two", "three:three", "three:four")); |
67 | 37 | } |
68 | 38 |
|
69 | 39 | public <T> List<T> flattenListOfListsImperatively(List<List<T>> list) { |
|
0 commit comments