001    /**
002     * Copyright 2005-2011 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     * This class tests the BeanPropertyComparator methods.
032     */
033    public class BeanPropertyComparatorTest {
034    
035        @Test public void testConstructor_nullList() {
036            boolean failedAsExpected = false;
037    
038            try {
039                new BeanPropertyComparator(null);
040            }
041            catch (IllegalArgumentException e) {
042                failedAsExpected = true;
043            }
044    
045            Assert.assertTrue(failedAsExpected);
046        }
047    
048        @Test public void testConstructor_emptyList() {
049            boolean failedAsExpected = false;
050            try {
051                new BeanPropertyComparator(new ArrayList());
052            }
053            catch (IllegalArgumentException e) {
054                failedAsExpected = true;
055            }
056            Assert.assertTrue(failedAsExpected);
057        }
058    
059        @Test public void testCompare_unknownPropertyNames() {
060            List unknownProperties = Arrays.asList(new String[] { "one", "two", "three" });
061    
062            BeanPropertyComparator bpc = new BeanPropertyComparator(unknownProperties);
063            A a = new A("something", new Integer(0), Boolean.valueOf(false));
064            B b = new B("something else", new Integer(1), Boolean.valueOf(true));
065    
066    
067            boolean failedAsExpected = false;
068            try {
069                bpc.compare(a, b);
070            }
071            catch (BeanComparisonException e) {
072                if (e.getCause() instanceof NullPointerException) {
073                    failedAsExpected = true;
074                }
075            }
076            Assert.assertTrue(failedAsExpected);
077        }
078    
079        @Test public void testCompare_propertyTypeMismatch() {
080            List mismatchedProperties = Arrays.asList(new String[] { "i", "b" });
081    
082            BeanPropertyComparator bpc = new BeanPropertyComparator(mismatchedProperties);
083            A a = new A("something", new Integer(0), Boolean.valueOf(false));
084            C c = new C("something else", 1, true);
085    
086    
087            boolean failedAsExpected = false;
088            try {
089                bpc.compare(a, c);
090            }
091            catch (ClassCastException e) {
092                failedAsExpected = true;
093            }
094            Assert.assertTrue(failedAsExpected);
095        }
096    
097        @Test public void testCompare_privateProperty() {
098            List privateProperty = Arrays.asList(new String[] { "s" });
099    
100            BeanPropertyComparator bpc = new BeanPropertyComparator(privateProperty);
101            C c = new C("something else", 1, true);
102            A a = new A("something", new Integer(0), Boolean.valueOf(false));
103    
104    
105            boolean failedAsExpected = false;
106            try {
107                bpc.compare(c, a);
108            }
109            catch (BeanComparisonException e) {
110                if (e.getCause() instanceof NullPointerException) {
111                    failedAsExpected = true;
112                }
113            }
114            Assert.assertTrue(failedAsExpected);
115        }
116    
117    
118        @Test public void testCompare_oneProperty_string() {
119            List properties = Arrays.asList(new String[] { "s" });
120    
121            BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
122            A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
123            B greater = new B("Two", new Integer(0), Boolean.valueOf(false));
124    
125            int lessThan = bpc.compare(lesser, greater);
126            Assert.assertTrue(lessThan < 0);
127    
128            int greaterThan = bpc.compare(greater, lesser);
129            Assert.assertTrue(greaterThan > 0);
130    
131            int equal = bpc.compare(greater, greater);
132            Assert.assertTrue(equal == 0);
133        }
134    
135        @Test public void testCompare_oneProperty_integer() {
136            List properties = Arrays.asList(new String[] { "i" });
137    
138            BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
139            A lesser = new A("One", new Integer(-1), Boolean.valueOf(false));
140            B greater = new B("One", new Integer(1), Boolean.valueOf(false));
141    
142            int lessThan = bpc.compare(lesser, greater);
143            Assert.assertTrue(lessThan < 0);
144    
145            int greaterThan = bpc.compare(greater, lesser);
146            Assert.assertTrue(greaterThan > 0);
147    
148            int equal = bpc.compare(greater, greater);
149            Assert.assertTrue(equal == 0);
150        }
151    
152        @Test public void testCompare_oneProperty_boolean() {
153            List properties = Arrays.asList(new String[] { "b" });
154    
155            BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
156            A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
157            B greater = new B("One", new Integer(0), Boolean.valueOf(true));
158    
159            int lessThan = bpc.compare(lesser, greater);
160            Assert.assertTrue(lessThan < 0);
161    
162            int greaterThan = bpc.compare(greater, lesser);
163            Assert.assertTrue(greaterThan > 0);
164    
165            int equal = bpc.compare(greater, greater);
166            Assert.assertTrue(equal == 0);
167        }
168    
169        @Test public void testCompare_oneLevel() {
170            List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
171    
172            BeanPropertyComparator bpcSIB = new BeanPropertyComparator(propertiesSIB);
173            A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
174            B greater = new B("Two", new Integer(0), Boolean.valueOf(false));
175    
176            int lessThan = bpcSIB.compare(lesser, greater);
177            Assert.assertTrue(lessThan < 0);
178    
179            int greaterThan = bpcSIB.compare(greater, lesser);
180            Assert.assertTrue(greaterThan > 0);
181    
182            int equal = bpcSIB.compare(greater, greater);
183            Assert.assertTrue(equal == 0);
184        }
185    
186        @Test public void testCompare_twoLevels() {
187            List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
188    
189            BeanPropertyComparator bpc = new BeanPropertyComparator(propertiesSIB);
190            A lesser = new A("Same", new Integer(-1), Boolean.valueOf(false));
191            B greater = new B("Same", new Integer(1), Boolean.valueOf(false));
192    
193            int lessThan = bpc.compare(lesser, greater);
194            Assert.assertTrue(lessThan < 0);
195    
196            int greaterThan = bpc.compare(greater, lesser);
197            Assert.assertTrue(greaterThan > 0);
198    
199            int equal = bpc.compare(greater, greater);
200            Assert.assertTrue(equal == 0);
201        }
202    
203        @Test public void testCompare_threeLevels() {
204            List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
205    
206            BeanPropertyComparator bpc = new BeanPropertyComparator(propertiesSIB);
207            A lesser = new A("Same", new Integer(1), Boolean.valueOf(false));
208            B greater = new B("Same", new Integer(1), Boolean.valueOf(true));
209    
210            int lessThan = bpc.compare(lesser, greater);
211            Assert.assertTrue(lessThan < 0);
212    
213            int greaterThan = bpc.compare(greater, lesser);
214            Assert.assertTrue(greaterThan > 0);
215    
216            int equal = bpc.compare(greater, greater);
217            Assert.assertTrue(equal == 0);
218        }
219    
220        @Test public void testCompare_differentCases() {
221            List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
222    
223            BeanPropertyComparator sensitive = new BeanPropertyComparator(propertiesSIB, false);
224            BeanPropertyComparator insensitive = new BeanPropertyComparator(propertiesSIB, true);
225    
226            A lesser = new A("SomeThing", new Integer(1), Boolean.valueOf(false));
227            B greater = new B("something", new Integer(1), Boolean.valueOf(false));
228    
229            int equal = insensitive.compare(greater, lesser);
230            Assert.assertTrue(equal == 0);
231    
232            int inequal = sensitive.compare(greater, lesser);
233            Assert.assertTrue(inequal != 0);
234        }
235        
236        @Test public void testCompare_differentDates() throws ParseException {
237            List propertiesD = Arrays.asList(new String[] { "d" });
238            
239            DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
240            
241            BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
242            
243            D lesser = new D(dateFormat.parse("01/01/1990"));
244            D greater = new D(dateFormat.parse("01/02/1990"));
245            
246            int result = comparator.compare(greater, lesser);
247            Assert.assertEquals(1, result);
248            
249            result = comparator.compare(lesser, greater);
250            Assert.assertEquals(-1, result);
251            
252            result = comparator.compare(lesser, lesser);
253            Assert.assertEquals(0, result);
254            
255            result = comparator.compare(greater, greater);
256            Assert.assertEquals(0, result);
257        }
258        
259        @Test public void testCompare_firstNullDates() throws ParseException {
260            List propertiesD = Arrays.asList(new String[] { "d" });
261            
262            DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
263            
264            BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
265            
266            D lesser = new D(null);
267            D greater = new D(dateFormat.parse("01/02/1990"));
268            
269            int result = comparator.compare(greater, lesser);
270            Assert.assertEquals(1, result);
271            
272            result = comparator.compare(lesser, greater);
273            Assert.assertEquals(-1, result);
274            
275            result = comparator.compare(lesser, lesser);
276            Assert.assertEquals(0, result);
277            
278            result = comparator.compare(greater, greater);
279            Assert.assertEquals(0, result);
280        }
281        
282        @Test public void testCompare_secondNullDates() throws ParseException {
283            List propertiesD = Arrays.asList(new String[] { "d" });
284            
285            DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
286            
287            BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
288            
289            D lesser = new D(dateFormat.parse("01/02/1990"));
290            D greater = new D(null);
291            
292            int result = comparator.compare(greater, lesser);
293            Assert.assertEquals(-1, result);
294            
295            result = comparator.compare(lesser, greater);
296            Assert.assertEquals(1, result);
297            
298            result = comparator.compare(lesser, lesser);
299            Assert.assertEquals(0, result);
300            
301            result = comparator.compare(greater, greater);
302            Assert.assertEquals(0, result);
303        }
304    
305        public static class A {
306            private String s;
307            private Integer i;
308            private Boolean b;
309    
310            public A(String s, Integer i, Boolean b) {
311                this.s = s;
312                this.i = i;
313                this.b = b;
314            }
315    
316            public String getS() {
317                return s;
318            }
319    
320            public Integer getI() {
321                return i;
322            }
323    
324            public Boolean getB() {
325                return b;
326            }
327        }
328    
329        public static class B {
330            private String s;
331            private Integer i;
332            private Boolean b;
333            private Long l;
334    
335            public B(String s, Integer i, Boolean b) {
336                this.s = s;
337                this.i = i;
338                this.b = b;
339                this.l = new Long(23);
340            }
341    
342            public String getS() {
343                return s;
344            }
345    
346            public Integer getI() {
347                return i;
348            }
349    
350            public Boolean getB() {
351                return b;
352            }
353    
354            public Long getL() {
355                return l;
356            }
357        }
358    
359        public static class C {
360            private boolean s;
361            private String i;
362            private float b;
363    
364            public C(String i, float b, boolean s) {
365                this.s = s;
366                this.i = i;
367                this.b = b;
368            }
369    
370            private boolean getS() {
371                return s;
372            }
373    
374            public String getI() {
375                return i;
376            }
377    
378            public float getB() {
379                return b;
380            }
381        }
382        
383        public static class D {
384            private Date d;
385            
386            public D(Date d) {
387                    this.d = d;
388            }
389            
390            public Date getD() {
391                    return d;
392            }
393        }
394    }