|
1 | 1 | package com.hellokoding.java.collections; |
2 | 2 |
|
| 3 | +import org.assertj.core.api.ThrowableAssert; |
3 | 4 | import org.junit.Test; |
4 | 5 |
|
5 | | -import java.util.*; |
6 | | -import java.util.stream.Collectors; |
| 6 | +import java.time.LocalDate; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Comparator; |
| 10 | +import java.util.List; |
7 | 11 |
|
8 | 12 | import static org.assertj.core.api.Assertions.assertThat; |
| 13 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
9 | 14 |
|
10 | 15 | public class ArrayListSortingTest { |
11 | 16 | @Test |
12 | | - public void givenNonComparableAndComparator_whenSortAsc_thenSuccess(){ |
13 | | - // given |
14 | | - List<NonComparableBook> bookList = new ArrayList<>(); |
15 | | - bookList.add(new NonComparableBook("C")); |
16 | | - bookList.add(new NonComparableBook("A")); |
17 | | - bookList.add(new NonComparableBook("D")); |
18 | | - |
19 | | - // when sorting in natural / ascending order |
20 | | - bookList.sort(Comparator.comparing(e -> e.title)); |
21 | | - |
22 | | - // then |
23 | | - String actual = bookList.stream().map(e -> e.title).collect(Collectors.joining()); |
24 | | - assertThat(actual).isEqualTo("ACD"); |
| 17 | + public void sortStrings(){ |
| 18 | + List<String> strings = new ArrayList<>(List.of("c", "a", "b")); |
| 19 | + |
| 20 | + strings.sort(Comparator.naturalOrder()); |
| 21 | + assertThat(strings).containsExactly("a", "b", "c"); |
| 22 | + |
| 23 | + strings.sort(Comparator.reverseOrder()); |
| 24 | + assertThat(strings).containsExactly("c", "b", "a"); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void sortNumbers() { |
| 29 | + List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2)); |
| 30 | + |
| 31 | + numbers.sort(Comparator.naturalOrder()); |
| 32 | + assertThat(numbers).containsExactly(1, 2, 3); |
| 33 | + |
| 34 | + numbers.sort(Comparator.reverseOrder()); |
| 35 | + assertThat(numbers).containsExactly(3, 2, 1); |
25 | 36 | } |
26 | 37 |
|
27 | 38 | @Test |
28 | | - public void givenComparable_whenSortAsc_thenSuccess(){ |
29 | | - // given |
30 | | - List<ComparableBook> bookList = new ArrayList<>(); |
31 | | - bookList.add(new ComparableBook("C")); |
32 | | - bookList.add(new ComparableBook("A")); |
33 | | - bookList.add(new ComparableBook("D")); |
34 | | - |
35 | | - // when sorting in natural / ascending order |
36 | | - bookList.sort(Comparator.naturalOrder()); |
37 | | - |
38 | | - // then |
39 | | - String actual = bookList.stream().map(e -> e.title).collect(Collectors.joining()); |
40 | | - assertThat(actual).isEqualTo("ACD"); |
| 39 | + public void sortDates() { |
| 40 | + LocalDate ld1 = LocalDate.of(2019, 3, 1); |
| 41 | + LocalDate ld2 = LocalDate.of(2019, 1, 1); |
| 42 | + LocalDate ld3 = LocalDate.of(2019, 2, 1); |
| 43 | + List<LocalDate> dates = new ArrayList<>(List.of(ld1, ld2, ld3)); |
| 44 | + |
| 45 | + dates.sort(Comparator.naturalOrder()); |
| 46 | + assertThat(dates).containsExactly(ld2, ld3, ld1); |
| 47 | + |
| 48 | + dates.sort(Comparator.reverseOrder()); |
| 49 | + assertThat(dates).containsExactly(ld1, ld3, ld2); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void sortNullObject() { |
| 54 | + List<Integer> lst = new ArrayList<>(Arrays.asList(3, 1, null)); |
| 55 | + |
| 56 | + ThrowableAssert.ThrowingCallable c = () -> lst.sort(Comparator.naturalOrder()); |
| 57 | + assertThatThrownBy(c).isInstanceOf(NullPointerException.class); |
41 | 58 | } |
42 | 59 |
|
43 | 60 | @Test |
44 | | - public void givenComparable_whenSortDesc_thenSuccess(){ |
45 | | - // given |
46 | | - List<ComparableBook> bookList = new ArrayList<>(); |
47 | | - bookList.add(new ComparableBook("C")); |
48 | | - bookList.add(new ComparableBook("A")); |
49 | | - bookList.add(new ComparableBook("D")); |
50 | | - |
51 | | - // when sorting in reverse / descending order |
52 | | - bookList.sort(Comparator.reverseOrder()); |
53 | | - |
54 | | - // then |
55 | | - String actual = bookList.stream().map(e -> e.title).collect(Collectors.joining()); |
56 | | - assertThat(actual).isEqualTo("DCA"); |
| 61 | + public void sortOneField() { |
| 62 | + Book book1 = new Book(1, "b"); |
| 63 | + Book book2 = new Book(2, "c"); |
| 64 | + Book book3 = new Book(3, "a"); |
| 65 | + |
| 66 | + List<Book> list = Arrays.asList(book1, book2, book3); |
| 67 | + list.sort(Comparator.comparing(Book::getTitle)); |
| 68 | + |
| 69 | + assertThat(list).containsExactly(book3, book1, book2); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void sortMultipleFields() { |
| 74 | + Book book1 = new Book(1, "b"); |
| 75 | + Book book2 = new Book(2, "c"); |
| 76 | + Book book3 = new Book(3, "c"); |
| 77 | + |
| 78 | + List<Book> list = Arrays.asList(book1, book2, book3); |
| 79 | + list.sort(Comparator |
| 80 | + .comparing(Book::getTitle, Comparator.reverseOrder()) |
| 81 | + .thenComparing(Book::getId, Comparator.reverseOrder())); |
| 82 | + |
| 83 | + assertThat(list).containsExactly(book3, book2, book1); |
| 84 | + } |
| 85 | + |
| 86 | + static class Book { |
| 87 | + int id; |
| 88 | + String title; |
| 89 | + |
| 90 | + Book(int id, String title) { |
| 91 | + this.id = id; |
| 92 | + this.title = title; |
| 93 | + } |
| 94 | + |
| 95 | + int getId() { |
| 96 | + return id; |
| 97 | + } |
| 98 | + |
| 99 | + String getTitle() { |
| 100 | + return title; |
| 101 | + } |
57 | 102 | } |
58 | 103 | } |
0 commit comments