1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.util;
17
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.kuali.rice.krad.util.BeanPropertyComparator.BeanComparisonException;
21
22 import java.text.DateFormat;
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Date;
28 import java.util.List;
29
30
31
32
33 public class BeanPropertyComparatorTest {
34
35 @Test
36
37
38
39 public void testConstructor_nullList() {
40 boolean failedAsExpected = false;
41
42 try {
43 new BeanPropertyComparator(null);
44 }
45 catch (IllegalArgumentException e) {
46 failedAsExpected = true;
47 }
48
49 Assert.assertTrue(failedAsExpected);
50 }
51
52
53
54
55 @Test public void testConstructor_emptyList() {
56 boolean failedAsExpected = false;
57 try {
58 new BeanPropertyComparator(new ArrayList());
59 }
60 catch (IllegalArgumentException e) {
61 failedAsExpected = true;
62 }
63 Assert.assertTrue(failedAsExpected);
64 }
65
66 @Test
67
68
69
70
71
72
73 public void testCompare_unknownPropertyNames() {
74 List unknownProperties = Arrays.asList(new String[] { "one", "two", "three" });
75
76 BeanPropertyComparator bpc = new BeanPropertyComparator(unknownProperties);
77 A a = new A("something", new Integer(0), Boolean.valueOf(false));
78 B b = new B("something else", new Integer(1), Boolean.valueOf(true));
79
80 boolean failedAsExpected = false;
81 try {
82 bpc.compare(a, b);
83 }
84 catch (BeanComparisonException e) {
85 if (e.getCause() instanceof NullPointerException) {
86 failedAsExpected = true;
87 }
88 }
89 Assert.assertTrue(failedAsExpected);
90 }
91
92 @Test
93
94
95
96 public void testCompare_propertyTypeMismatch() {
97 List mismatchedProperties = Arrays.asList(new String[] { "i", "b" });
98
99 BeanPropertyComparator bpc = new BeanPropertyComparator(mismatchedProperties);
100 A a = new A("something", new Integer(0), Boolean.valueOf(false));
101 C c = new C("something else", 1, true);
102
103
104 boolean failedAsExpected = false;
105 try {
106 bpc.compare(a, c);
107 }
108 catch (ClassCastException e) {
109 failedAsExpected = true;
110 }
111 Assert.assertTrue(failedAsExpected);
112 }
113
114 @Test
115
116
117
118
119
120
121
122 public void testCompare_privateProperty() {
123 List privateProperty = Arrays.asList(new String[] { "s" });
124
125 BeanPropertyComparator bpc = new BeanPropertyComparator(privateProperty);
126 C c = new C("something else", 1, true);
127 A a = new A("something", new Integer(0), Boolean.valueOf(false));
128
129
130 boolean failedAsExpected = false;
131 try {
132 bpc.compare(c, a);
133 }
134 catch (BeanComparisonException e) {
135 if (e.getCause() instanceof NullPointerException) {
136 failedAsExpected = true;
137 }
138 }
139 Assert.assertTrue(failedAsExpected);
140 }
141
142
143 @Test
144
145
146
147 public void testCompare_oneProperty_string() {
148 List properties = Arrays.asList(new String[] { "s" });
149
150 BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
151 A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
152 B greater = new B("Two", new Integer(0), Boolean.valueOf(false));
153
154 int lessThan = bpc.compare(lesser, greater);
155 Assert.assertTrue(lessThan < 0);
156
157 int greaterThan = bpc.compare(greater, lesser);
158 Assert.assertTrue(greaterThan > 0);
159
160 int equal = bpc.compare(greater, greater);
161 Assert.assertTrue(equal == 0);
162 }
163
164 @Test
165
166
167
168 public void testCompare_oneProperty_integer() {
169 List properties = Arrays.asList(new String[] { "i" });
170
171 BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
172 A lesser = new A("One", new Integer(-1), Boolean.valueOf(false));
173 B greater = new B("One", new Integer(1), Boolean.valueOf(false));
174
175 int lessThan = bpc.compare(lesser, greater);
176 Assert.assertTrue(lessThan < 0);
177
178 int greaterThan = bpc.compare(greater, lesser);
179 Assert.assertTrue(greaterThan > 0);
180
181 int equal = bpc.compare(greater, greater);
182 Assert.assertTrue(equal == 0);
183 }
184
185 @Test
186
187
188
189 public void testCompare_oneProperty_boolean() {
190 List properties = Arrays.asList(new String[] { "b" });
191
192 BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
193 A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
194 B greater = new B("One", new Integer(0), Boolean.valueOf(true));
195
196 int lessThan = bpc.compare(lesser, greater);
197 Assert.assertTrue(lessThan < 0);
198
199 int greaterThan = bpc.compare(greater, lesser);
200 Assert.assertTrue(greaterThan > 0);
201
202 int equal = bpc.compare(greater, greater);
203 Assert.assertTrue(equal == 0);
204 }
205
206 @Test
207
208
209
210
211
212
213 public void testCompare_oneLevel() {
214 List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
215
216 BeanPropertyComparator bpcSIB = new BeanPropertyComparator(propertiesSIB);
217 A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
218 B greater = new B("Two", new Integer(0), Boolean.valueOf(false));
219
220 int lessThan = bpcSIB.compare(lesser, greater);
221 Assert.assertTrue(lessThan < 0);
222
223 int greaterThan = bpcSIB.compare(greater, lesser);
224 Assert.assertTrue(greaterThan > 0);
225
226 int equal = bpcSIB.compare(greater, greater);
227 Assert.assertTrue(equal == 0);
228 }
229
230 @Test
231
232
233
234
235
236
237 public void testCompare_twoLevels() {
238 List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
239
240 BeanPropertyComparator bpc = new BeanPropertyComparator(propertiesSIB);
241 A lesser = new A("Same", new Integer(-1), Boolean.valueOf(false));
242 B greater = new B("Same", new Integer(1), Boolean.valueOf(false));
243
244 int lessThan = bpc.compare(lesser, greater);
245 Assert.assertTrue(lessThan < 0);
246
247 int greaterThan = bpc.compare(greater, lesser);
248 Assert.assertTrue(greaterThan > 0);
249
250 int equal = bpc.compare(greater, greater);
251 Assert.assertTrue(equal == 0);
252 }
253
254 @Test
255
256
257
258
259
260
261 public void testCompare_threeLevels() {
262 List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
263
264 BeanPropertyComparator bpc = new BeanPropertyComparator(propertiesSIB);
265 A lesser = new A("Same", new Integer(1), Boolean.valueOf(false));
266 B greater = new B("Same", new Integer(1), Boolean.valueOf(true));
267
268 int lessThan = bpc.compare(lesser, greater);
269 Assert.assertTrue(lessThan < 0);
270
271 int greaterThan = bpc.compare(greater, lesser);
272 Assert.assertTrue(greaterThan > 0);
273
274 int equal = bpc.compare(greater, greater);
275 Assert.assertTrue(equal == 0);
276 }
277
278 @Test
279
280
281
282 public void testCompare_differentCases() {
283 List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
284
285 BeanPropertyComparator sensitive = new BeanPropertyComparator(propertiesSIB, false);
286 BeanPropertyComparator insensitive = new BeanPropertyComparator(propertiesSIB, true);
287
288 A lesser = new A("SomeThing", new Integer(1), Boolean.valueOf(false));
289 B greater = new B("something", new Integer(1), Boolean.valueOf(false));
290
291 int equal = insensitive.compare(greater, lesser);
292 Assert.assertTrue(equal == 0);
293
294 int inequal = sensitive.compare(greater, lesser);
295 Assert.assertTrue(inequal != 0);
296 }
297
298 @Test
299
300
301
302 public void testCompare_differentDates() throws ParseException {
303 List propertiesD = Arrays.asList(new String[] { "d" });
304
305 DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
306
307 BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
308
309 D lesser = new D(dateFormat.parse("01/01/1990"));
310 D greater = new D(dateFormat.parse("01/02/1990"));
311
312 int result = comparator.compare(greater, lesser);
313 Assert.assertEquals(1, result);
314
315 result = comparator.compare(lesser, greater);
316 Assert.assertEquals(-1, result);
317
318 result = comparator.compare(lesser, lesser);
319 Assert.assertEquals(0, result);
320
321 result = comparator.compare(greater, greater);
322 Assert.assertEquals(0, result);
323 }
324
325 @Test
326
327
328
329 public void testCompare_firstNullDates() throws ParseException {
330 List propertiesD = Arrays.asList(new String[] { "d" });
331
332 DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
333
334 BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
335
336 D lesser = new D(null);
337 D greater = new D(dateFormat.parse("01/02/1990"));
338
339 int result = comparator.compare(greater, lesser);
340 Assert.assertEquals(1, result);
341
342 result = comparator.compare(lesser, greater);
343 Assert.assertEquals(-1, result);
344
345 result = comparator.compare(lesser, lesser);
346 Assert.assertEquals(0, result);
347
348 result = comparator.compare(greater, greater);
349 Assert.assertEquals(0, result);
350 }
351
352 @Test
353
354
355
356 public void testCompare_secondNullDates() throws ParseException {
357 List propertiesD = Arrays.asList(new String[] { "d" });
358
359 DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
360
361 BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
362
363 D lesser = new D(dateFormat.parse("01/02/1990"));
364 D greater = new D(null);
365
366 int result = comparator.compare(greater, lesser);
367 Assert.assertEquals(-1, result);
368
369 result = comparator.compare(lesser, greater);
370 Assert.assertEquals(1, result);
371
372 result = comparator.compare(lesser, lesser);
373 Assert.assertEquals(0, result);
374
375 result = comparator.compare(greater, greater);
376 Assert.assertEquals(0, result);
377 }
378
379 public static class A {
380 private String s;
381 private Integer i;
382 private Boolean b;
383
384 public A(String s, Integer i, Boolean b) {
385 this.s = s;
386 this.i = i;
387 this.b = b;
388 }
389
390 public String getS() {
391 return s;
392 }
393
394 public Integer getI() {
395 return i;
396 }
397
398 public Boolean getB() {
399 return b;
400 }
401 }
402
403 public static class B {
404 private String s;
405 private Integer i;
406 private Boolean b;
407 private Long l;
408
409 public B(String s, Integer i, Boolean b) {
410 this.s = s;
411 this.i = i;
412 this.b = b;
413 this.l = new Long(23);
414 }
415
416 public String getS() {
417 return s;
418 }
419
420 public Integer getI() {
421 return i;
422 }
423
424 public Boolean getB() {
425 return b;
426 }
427
428 public Long getL() {
429 return l;
430 }
431 }
432
433 public static class C {
434 private boolean s;
435 private String i;
436 private float b;
437
438 public C(String i, float b, boolean s) {
439 this.s = s;
440 this.i = i;
441 this.b = b;
442 }
443
444 private boolean getS() {
445 return s;
446 }
447
448 public String getI() {
449 return i;
450 }
451
452 public float getB() {
453 return b;
454 }
455 }
456
457 public static class D {
458 private Date d;
459
460 public D(Date d) {
461 this.d = d;
462 }
463
464 public Date getD() {
465 return d;
466 }
467 }
468 }