|
14 | 14 |
|
15 | 15 | public class EmployeeSortingByDateUnitTest { |
16 | 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 | | - } |
| 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", DateUtils.addMonths(new Date(), -2)), |
| 26 | + new Employee("Frank", DateUtils.addDays(new Date(), -20)), |
| 27 | + new Employee("Steve", DateUtils.addDays(new Date(), -10)), |
| 28 | + new Employee("Jessica", DateUtils.addMonths(new Date(), -6)), |
| 29 | + new Employee("Pearl", DateUtils.addYears(new Date(), -1)), |
| 30 | + new Employee("John", new Date()) |
| 31 | + ); |
| 32 | + |
| 33 | + Collections.addAll(employeesSortedByDateDesc, |
| 34 | + new Employee("John", new Date()), |
| 35 | + new Employee("Steve", DateUtils.addDays(new Date(), -10)), |
| 36 | + new Employee("Frank", DateUtils.addDays(new Date(), -20)), |
| 37 | + new Employee("Earl", DateUtils.addMonths(new Date(), -2)), |
| 38 | + new Employee("Jessica", DateUtils.addMonths(new Date(), -6)), |
| 39 | + new Employee("Pearl", DateUtils.addYears(new Date(), -1)) |
| 40 | + ); |
| 41 | + |
| 42 | + Collections.addAll(employeesSortedByDateAsc, |
| 43 | + new Employee("Pearl", DateUtils.addYears(new Date(), -1)), |
| 44 | + new Employee("Jessica", DateUtils.addMonths(new Date(), -6)), |
| 45 | + new Employee("Earl", DateUtils.addMonths(new Date(), -2)), |
| 46 | + new Employee("Frank", DateUtils.addDays(new Date(), -20)), |
| 47 | + new Employee("Steve", DateUtils.addDays(new Date(), -10)), |
| 48 | + new Employee("John", new Date()) |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() { |
| 54 | + Collections.sort(employees); |
| 55 | + System.out.println(employees); |
| 56 | + |
| 57 | + assertEquals(employees, employeesSortedByDateAsc); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void givenEmpList_SortEmpList_thenCheckSortedList() { |
| 62 | + |
| 63 | + Collections.sort(employees, new Comparator<Employee>() { |
| 64 | + public int compare(Employee o1, Employee o2) { |
| 65 | + return o1.getJoiningDate().compareTo(o2.getJoiningDate()); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + assertEquals(employees, employeesSortedByDateAsc); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void givenEmpList_SortEmpList_thenCheckSortedListV1() { |
| 74 | + |
| 75 | + Collections.sort(employees, new Comparator<Employee>() { |
| 76 | + public int compare(Employee emp1, Employee emp2) { |
| 77 | + if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null) |
| 78 | + return 0; |
| 79 | + return emp1.getJoiningDate().compareTo(emp2.getJoiningDate()); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + assertEquals(employees, employeesSortedByDateAsc); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void givenEmpList_SortEmpList_thenSortedListinDescOrder() { |
| 88 | + Collections.sort(employees, Collections.reverseOrder()); |
| 89 | + |
| 90 | + assertEquals(employees, employeesSortedByDateDesc); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void givenEmpList_SortEmpList_thenCheckSortedListAsc() { |
| 95 | + |
| 96 | + Collections.sort(employees, new Comparator<Employee>() { |
| 97 | + public int compare(Employee emp1, Employee emp2) { |
| 98 | + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); |
| 99 | + } |
| 100 | + }); |
| 101 | + |
| 102 | + assertEquals(employees, employeesSortedByDateDesc); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() { |
| 107 | + |
| 108 | + Collections.sort(employees, new Comparator<Employee>() { |
| 109 | + public int compare(Employee emp1, Employee emp2) { |
| 110 | + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + assertEquals(employees, employeesSortedByDateDesc); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void givenEmpList_SortEmpList_thenCheckSortedListDescLambda() { |
| 119 | + |
| 120 | + Collections.sort(employees, |
| 121 | + (emp1, emp2) -> emp2.getJoiningDate().compareTo(emp1.getJoiningDate())); |
| 122 | + |
| 123 | + assertEquals(employees, employeesSortedByDateDesc); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void givenEmpList_SortEmpList_thenCheckSortedListDescLambdaV1() { |
| 128 | + |
| 129 | + Collections.sort(employees, (emp1, emp2) -> { |
| 130 | + if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null) |
| 131 | + return 0; |
| 132 | + return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); |
| 133 | + }); |
| 134 | + |
| 135 | + assertEquals(employees, employeesSortedByDateDesc); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void givenEmpList_SortEmpList_thenCheckSortedListAscLambda() { |
| 140 | + Collections.sort(employees, |
| 141 | + Comparator.comparing(Employee::getJoiningDate)); |
| 142 | + assertEquals(employees, employeesSortedByDateAsc); |
| 143 | + } |
145 | 144 |
|
146 | 145 | } |