|
| 1 | +package com.baeldung.collections.sorting; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.Date; |
| 10 | +import java.util.List; |
| 11 | +import org.apache.commons.lang.time.DateUtils; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +public class EmployeeSortingByDateUnitTest { |
| 16 | + |
| 17 | + private List<Employee> employees = new ArrayList<>(); |
| 18 | + private List<Employee> employeesSortedByDateAsc = new ArrayList<>(); |
| 19 | + private List<Employee> employeesSortedByDateDesc = new ArrayList<>(); |
| 20 | + |
| 21 | + @Before |
| 22 | + public void initVariables() { |
| 23 | + |
| 24 | + Collections.addAll(employees, |
| 25 | + new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), |
| 26 | + new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), |
| 27 | + new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), |
| 28 | + new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), |
| 29 | + new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)), |
| 30 | + new Employee("John", 23, 5000, new Date()) |
| 31 | + ); |
| 32 | + |
| 33 | + Collections.addAll(employeesSortedByDateDesc, |
| 34 | + new Employee("John", 23, 5000, new Date()), |
| 35 | + new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), |
| 36 | + new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), |
| 37 | + new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), |
| 38 | + new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), |
| 39 | + new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)) |
| 40 | + ); |
| 41 | + |
| 42 | + Collections.addAll(employeesSortedByDateAsc, |
| 43 | + new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)), |
| 44 | + new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), |
| 45 | + new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), |
| 46 | + new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), |
| 47 | + new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), |
| 48 | + new Employee("John", 23, 5000, new Date()) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() { |
| 54 | + Collections.sort(employees); |
| 55 | + |
| 56 | + assertEquals(employees, employeesSortedByDateAsc); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void givenEmpList_SortEmpList_thenCheckSortedList() { |
| 61 | + |
| 62 | + Collections.sort(employees, new Comparator<Employee>() { |
| 63 | + public int compare(Employee o1, Employee o2) { |
| 64 | + return o1.getJoiningDate().compareTo(o2.getJoiningDate()); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + assertEquals(employees, employeesSortedByDateAsc); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void givenEmpList_SortEmpList_thenCheckSortedListV1() { |
| 73 | + |
| 74 | + Collections.sort(employees, new Comparator<Employee>() { |
| 75 | + public int compare(Employee emp1, Employee emp2) { |
| 76 | + if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null) |
| 77 | + return 0; |
| 78 | + return emp1.getJoiningDate().compareTo(emp2.getJoiningDate()); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + assertEquals(employees, employeesSortedByDateAsc); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void givenEmpList_SortEmpList_thenSortedListinAscOrder() { |
| 87 | + Collections.sort(employees, Collections.reverseOrder()); |
| 88 | + |
| 89 | + assertEquals(employees, employeesSortedByDateDesc); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void givenEmpList_SortEmpList_thenCheckSortedListAsc() { |
| 94 | + |
| 95 | + Collections.sort(employees, new Comparator<Employee>() { |
| 96 | + public int compare(Employee emp1, Employee emp2) { |
| 97 | + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + assertEquals(employees, employeesSortedByDateDesc); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() { |
| 106 | + |
| 107 | + Collections.sort(employees, new Comparator<Employee>() { |
| 108 | + public int compare(Employee emp1, Employee emp2) { |
| 109 | + if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null) |
| 110 | + return 0; |
| 111 | + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + assertEquals(employees, employeesSortedByDateDesc); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() { |
| 120 | + |
| 121 | + Collections.sort(employees, |
| 122 | + (emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate())); |
| 123 | + |
| 124 | + assertEquals(employees, employeesSortedByDateDesc); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() { |
| 129 | + |
| 130 | + Collections.sort(employees, (emp1, emp2) -> { |
| 131 | + if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null) |
| 132 | + return 0; |
| 133 | + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); |
| 134 | + }); |
| 135 | + |
| 136 | + assertEquals(employees, employeesSortedByDateDesc); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() { |
| 141 | + Collections.sort(employees, |
| 142 | + Comparator.comparing(Employee::getJoiningDate)); |
| 143 | + assertEquals(employees, employeesSortedByDateAsc); |
| 144 | + } |
| 145 | + |
| 146 | +} |