View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class tests the BeanPropertyComparator methods.
32   */
33  public class BeanPropertyComparatorTest {
34  
35      @Test public void testConstructor_nullList() {
36          boolean failedAsExpected = false;
37  
38          try {
39              new BeanPropertyComparator(null);
40          }
41          catch (IllegalArgumentException e) {
42              failedAsExpected = true;
43          }
44  
45          Assert.assertTrue(failedAsExpected);
46      }
47  
48      @Test public void testConstructor_emptyList() {
49          boolean failedAsExpected = false;
50          try {
51              new BeanPropertyComparator(new ArrayList());
52          }
53          catch (IllegalArgumentException e) {
54              failedAsExpected = true;
55          }
56          Assert.assertTrue(failedAsExpected);
57      }
58  
59      @Test public void testCompare_unknownPropertyNames() {
60          List unknownProperties = Arrays.asList(new String[] { "one", "two", "three" });
61  
62          BeanPropertyComparator bpc = new BeanPropertyComparator(unknownProperties);
63          A a = new A("something", new Integer(0), Boolean.valueOf(false));
64          B b = new B("something else", new Integer(1), Boolean.valueOf(true));
65  
66  
67          boolean failedAsExpected = false;
68          try {
69              bpc.compare(a, b);
70          }
71          catch (BeanComparisonException e) {
72              if (e.getCause() instanceof NullPointerException) {
73                  failedAsExpected = true;
74              }
75          }
76          Assert.assertTrue(failedAsExpected);
77      }
78  
79      @Test public void testCompare_propertyTypeMismatch() {
80          List mismatchedProperties = Arrays.asList(new String[] { "i", "b" });
81  
82          BeanPropertyComparator bpc = new BeanPropertyComparator(mismatchedProperties);
83          A a = new A("something", new Integer(0), Boolean.valueOf(false));
84          C c = new C("something else", 1, true);
85  
86  
87          boolean failedAsExpected = false;
88          try {
89              bpc.compare(a, c);
90          }
91          catch (ClassCastException e) {
92              failedAsExpected = true;
93          }
94          Assert.assertTrue(failedAsExpected);
95      }
96  
97      @Test public void testCompare_privateProperty() {
98          List privateProperty = Arrays.asList(new String[] { "s" });
99  
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 }