-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathTestGenericComparator.java
More file actions
248 lines (204 loc) · 10.5 KB
/
TestGenericComparator.java
File metadata and controls
248 lines (204 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.edurekademo.utilities;
import java.util.ArrayList;
import java.util.Collections;
import com.edurekademo.utilities.GenericComparator;
import com.edurekademo.utilities.CaseInsensitiveComparator;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.*;
/**
* @author Seshagiri Sriram
*
*/
@SuppressWarnings({"rawtypes","unchecked"})
public class TestGenericComparator
extends TestCase
{
public void initialize(ArrayList myData) {
UnitDTO d1 = new UnitDTO();
d1.setDeptID(100);d1.setEmpID(200);d1.setEmpName("Sriram");d1.setSpare(new Double(18.0));d1.setSpare2(new Double(18.0));
UnitDTO d2 = new UnitDTO();
d2.setDeptID(100);d2.setEmpID(201);d2.setEmpName("Somebody");d2.setSpare(new Double(11.0));d2.setSpare2(new Double(11.0));
UnitDTO d3 = new UnitDTO();
d3.setDeptID(100);d3.setEmpID(100);d3.setEmpName("Rajan");d3.setSpare(new Double(12.0));d3.setSpare2(new Double(12.0));
UnitDTO d4 = new UnitDTO();
d4.setDeptID(100);d4.setEmpID(102);d4.setEmpName("Vellman");d4.setSpare(new Double(10.0));d4.setSpare2(new Double(10.0));
UnitDTO d5 = new UnitDTO();
d5.setDeptID(100);d5.setEmpID(110);d5.setEmpName("Asma");d5.setSpare(new Double(10.0));d5.setSpare2(new Double(10.0));
myData.add(d1); myData.add(d2); myData.add(d3); myData.add(d4); myData.add(d5);
}
public void initialize2(ArrayList myData) {
UnitDTO d1 = new UnitDTO();
d1.setDeptID(100);d1.setEmpID(200);d1.setEmpName("Sriram");d1.setSpare(new Double(18.0));d1.setSpare2("A");
UnitDTO d2 = new UnitDTO();
d2.setDeptID(100);d2.setEmpID(201);d2.setEmpName("Somebody");d2.setSpare(new Double(11.0));d2.setSpare2("B");
UnitDTO d3 = new UnitDTO();
d3.setDeptID(100);d3.setEmpID(100);d3.setEmpName("Rajan");d3.setSpare(new Double(12.0));d3.setSpare2("C");
UnitDTO d4 = new UnitDTO();
d4.setDeptID(100);d4.setEmpID(102);d4.setEmpName("Vellman");d4.setSpare(new Double(10.0));d4.setSpare2("D");
UnitDTO d5 = new UnitDTO();
d5.setDeptID(100);d5.setEmpID(110);d5.setEmpName("Asma");d5.setSpare(new Double(10.0));d5.setSpare2("Z");
myData.add(d1); myData.add(d2); myData.add(d3); myData.add(d4); myData.add(d5);
}
public void initialize3(ArrayList myData) {
UnitDTO d1 = new UnitDTO();
d1.setDeptID(100);d1.setEmpID(200);d1.setEmpName("Sriram");d1.setSpare(new Double(18.0));d1.setSpare2("A");
UnitDTO d2 = new UnitDTO();
d2.setDeptID(100);d2.setEmpID(201);d2.setEmpName("asma");d2.setSpare(new Double(11.0));d2.setSpare2("B");
UnitDTO d3 = new UnitDTO();
d3.setDeptID(100);d3.setEmpID(100);d3.setEmpName("Rajan");d3.setSpare(new Double(12.0));d3.setSpare2("C");
UnitDTO d4 = new UnitDTO();
d4.setDeptID(100);d4.setEmpID(102);d4.setEmpName("Vellman");d4.setSpare(new Double(10.0));d4.setSpare2("D");
UnitDTO d5 = new UnitDTO();
d5.setDeptID(100);d5.setEmpID(110);d5.setEmpName("Asma");d5.setSpare(new Double(10.0));d5.setSpare2("Z");
UnitDTO d6 = new UnitDTO();
d6.setDeptID(100);d6.setEmpID(110);d6.setEmpName("ASMA");d6.setSpare(new Double(10.0));d6.setSpare2("Z");
myData.add(d1); myData.add(d2); myData.add(d3); myData.add(d4); myData.add(d5); myData.add(d6);
}
/**
* Create the test case
*
* @param testName name of the test case
*/
public TestGenericComparator( String testName )
{
super( testName );
}
/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( TestGenericComparator.class );
}
/**
* Test for Sorting by Emp Name Ascending
*/
public void testSortEmpNameAsc() {
ArrayList myData = new ArrayList(); initialize(myData);
Collections.sort(myData, new GenericComparator("empName", true)); // sort ascending..
assertEquals("Asma", ((UnitDTO)myData.get(0)).getEmpName());
}
/**
* Test for Sorting by Emp Name Descending
*/
public void testSortEmpNameDesc() {
ArrayList myData = new ArrayList(); initialize(myData);
Collections.sort(myData, new GenericComparator("empName", false)); // sort ascending..
assertEquals("Vellman", ((UnitDTO)myData.get(0)).getEmpName());
}
/**
* Test for Sorting by Emp ID Ascending
*/
@Ignore("testSortIDAsc")
public void testSortEmpIDAsc() {
ArrayList myData = new ArrayList(); initialize(myData);
Collections.sort(myData, new GenericComparator("empID", true)); // sort ascending..
assertEquals("Rajan", ((UnitDTO)myData.get(0)).getEmpName());
}
/**
* Test for Sorting besy Emp ID Descending...
*/
public void testSortEmpIDDesc() {
ArrayList myData = new ArrayList(); initialize(myData);
Collections.sort(myData, new GenericComparator("empID", false)); // sort Descending
assertEquals("Somebody", ((UnitDTO)myData.get(0)).getEmpName());
}
/**
* Test for Sorting by spare Ascending
*/
public void testSortEmpSpareAsc() {
ArrayList myData = new ArrayList(); initialize(myData);
Collections.sort(myData, new GenericComparator("spare", true)); // sort ascending
assertEquals("Vellman", ((UnitDTO)myData.get(0)).getEmpName());
}
/**
* Test for Sorting by spare Descending
*/
public void testSortEmpSpareDesc() {
ArrayList myData = new ArrayList(); initialize(myData);
Collections.sort(myData, new GenericComparator("spare", false)); // sort Descending
assertEquals("Sriram", ((UnitDTO)myData.get(0)).getEmpName());
}
/**
* Test for Sorting by spare2 Descending
*/
public void testSortEmpSpareDesc2() {
ArrayList myData = new ArrayList(); initialize(myData);
Collections.sort(myData, new GenericComparator("spare2", false)); // sort Descending
assertEquals("Sriram", ((UnitDTO)myData.get(0)).getEmpName());
}
/**
* Test for Sorting by spare2 Ascending
*/
public void testSortEmpSpareAsc2() {
ArrayList myData = new ArrayList(); initialize(myData);
Collections.sort(myData, new GenericComparator("spare2", true)); // sort ascending
assertEquals("Vellman", ((UnitDTO)myData.get(0)).getEmpName());
}
/**
* Test for Sorting by spare2 Ascending
*/
public void testSortEmpSpare2StringAsc() {
ArrayList myData = new ArrayList(); initialize2(myData);
Collections.sort(myData, new GenericComparator("spare2", true)); // sort ascending
assertEquals("A", ((UnitDTO)myData.get(0)).getSpare2());
}
/**
* Test for Sorting by spare2 Desc
*/
public void testSortEmpSpare2StringDesc() {
ArrayList myData = new ArrayList(); initialize2(myData);
Collections.sort(myData, new GenericComparator("spare2", false)); // sort Descending
assertEquals("Z", ((UnitDTO)myData.get(0)).getSpare2());
}
/**
* Test for Sorting by caseInsensitive Emp Name Ascending
*/
public void testSortEmpNameAscNewComparator() {
ArrayList myData = new ArrayList(); initialize3(myData);
Collections.sort(myData, new CaseInsensitiveComparator("empName", true)); // sort ascending..
assertEquals("Asma".toUpperCase(), ((UnitDTO)myData.get(0)).getEmpName().toUpperCase());
assertEquals("Asma".toUpperCase(), ((UnitDTO)myData.get(1)).getEmpName().toUpperCase());
}
/**
* Test for Sorting caseInsensitive by Emp Name Ascending
*/
public void testSortEmpNameAscNewComparator3Element() {
ArrayList myData = new ArrayList(); initialize3(myData);
Collections.sort(myData, new CaseInsensitiveComparator("empName", true)); // sort ascending..
assertEquals("Asma".toUpperCase(), ((UnitDTO)myData.get(0)).getEmpName().toUpperCase());
assertEquals("Asma".toUpperCase(), ((UnitDTO)myData.get(1)).getEmpName().toUpperCase());
assertEquals("Asma".toUpperCase(), ((UnitDTO)myData.get(2)).getEmpName().toUpperCase());
}
/**
* Test for Sorting caseInsensitive by Emp Name Descending
*/
public void testSortEmpNameDescNewComparator3Element() {
ArrayList myData = new ArrayList(); initialize3(myData);
Collections.sort(myData, new CaseInsensitiveComparator("empName", false)); // sort ascending..
assertEquals("Asma".toUpperCase(), ((UnitDTO)myData.get(3)).getEmpName().toUpperCase());
assertEquals("Asma".toUpperCase(), ((UnitDTO)myData.get(4)).getEmpName().toUpperCase());
assertEquals("Asma".toUpperCase(), ((UnitDTO)myData.get(5)).getEmpName().toUpperCase());
}
public void testDoubleSort() {
ArrayList myData = new ArrayList();
UnitDTO d1 = new UnitDTO();
d1.setDeptID(100);d1.setEmpID(200);d1.setEmpName("A");d1.setSpare(new Double(18.0));d1.setSpare2(new Double(18.0));
UnitDTO d2 = new UnitDTO();
d2.setDeptID(100);d2.setEmpID(199);d2.setEmpName("D");d2.setSpare(new Double(11.0));d2.setSpare2(new Double(11.0));
UnitDTO d3 = new UnitDTO();
d3.setDeptID(100);d3.setEmpID(201);d3.setEmpName("C");d3.setSpare(new Double(12.0));d3.setSpare2(new Double(12.0));
myData.add(d1); myData.add(d2); myData.add(d3);
Collections.sort(myData, new GenericComparator("empID", true)); // sort Asc
assertEquals (Integer.valueOf(3), writeList(myData));
Collections.sort(myData, new GenericComparator("empName", true)); // sort Asc
assertEquals (Integer.valueOf(3), writeList(myData));
assertFalse( ((UnitDTO)myData.get(1)).getEmpID() < ((UnitDTO)myData.get(2)).getEmpID());
}
private Integer writeList(ArrayList s){
Integer c = new Integer(s.size());
return c;
}
}