001 /**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.rice.krad.util;
017
018 import org.junit.Assert;
019 import org.junit.Test;
020 import org.kuali.rice.krad.util.BeanPropertyComparator.BeanComparisonException;
021
022 import java.text.DateFormat;
023 import java.text.ParseException;
024 import java.text.SimpleDateFormat;
025 import java.util.ArrayList;
026 import java.util.Arrays;
027 import java.util.Date;
028 import java.util.List;
029
030 /**
031 * BeanPropertyComparatorTest tests the BeanPropertyComparator methods
032 */
033 public class BeanPropertyComparatorTest {
034
035 @Test
036 /**
037 * tests that an IllegalArgumentException is thrown when BeanPropertyComparator constructor is passed a null argument
038 */
039 public void testConstructor_nullList() {
040 boolean failedAsExpected = false;
041
042 try {
043 new BeanPropertyComparator(null);
044 }
045 catch (IllegalArgumentException e) {
046 failedAsExpected = true;
047 }
048
049 Assert.assertTrue(failedAsExpected);
050 }
051
052 /**
053 * tests that an IllegalArgumentException is thrown when BeanPropertyComparator constructor is passed a empty list
054 */
055 @Test public void testConstructor_emptyList() {
056 boolean failedAsExpected = false;
057 try {
058 new BeanPropertyComparator(new ArrayList());
059 }
060 catch (IllegalArgumentException e) {
061 failedAsExpected = true;
062 }
063 Assert.assertTrue(failedAsExpected);
064 }
065
066 @Test
067 /**
068 * tests comparison with an unknown property name
069 *
070 * <p>test that a <code>NullPointerException</code> is thrown when the list of property names contains a property name
071 * that does not exist in the first argument to @{link org.kuali.rice.krad.util.BeanPropertyComparator#compare(java.lang.Object, java.lang.Object)}</p>
072 */
073 public void testCompare_unknownPropertyNames() {
074 List unknownProperties = Arrays.asList(new String[] { "one", "two", "three" });
075
076 BeanPropertyComparator bpc = new BeanPropertyComparator(unknownProperties);
077 A a = new A("something", new Integer(0), Boolean.valueOf(false));
078 B b = new B("something else", new Integer(1), Boolean.valueOf(true));
079
080 boolean failedAsExpected = false;
081 try {
082 bpc.compare(a, b);
083 }
084 catch (BeanComparisonException e) {
085 if (e.getCause() instanceof NullPointerException) {
086 failedAsExpected = true;
087 }
088 }
089 Assert.assertTrue(failedAsExpected);
090 }
091
092 @Test
093 /**
094 * tests that a ClassCastException is thrown when comparing beans that each have a property with the same name but of different object type
095 */
096 public void testCompare_propertyTypeMismatch() {
097 List mismatchedProperties = Arrays.asList(new String[] { "i", "b" });
098
099 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 * tests comparison when a property has a getter with private scope
117 *
118 * <p>test that a NullPointerException exception is thrown when the first argument to
119 * @{link org.kuali.rice.krad.util.BeanPropertyComparator#compare(java.lang.Object, java.lang.Object)}
120 * has a private scoped getter</p>
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 * test the comparison result when specifying a property of type String
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 * test the comparison result when specifying a property of type Integer
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 * test the comparison result when specifying a property of type Boolean
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 * tests comparison of multiple properties
209 *
210 * <p>using 3 properties, compare two beans that have a different value for the first property
211 * and the same values for the other two properties</p>
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 * tests comparison of multiple properties
233 *
234 * <p>using 3 properties, compare two beans that have a different value for the second property
235 * and the same values for the other two properties</p>
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 * tests comparison of multiple properties
257 *
258 * <p>using 3 properties, compare two beans that have a different value for the third property
259 * and the same values for the other two properties</p>
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 * test that case is ignored during String comparisons when set to true the constructor
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 * test that the result of comparing two dates is as expected
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 * test the comparison of null objects
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 * test the comparison of null objects
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 }