001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.core.util;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.List;
021:
022: import org.kuali.core.exceptions.BeanComparisonException;
023: import org.kuali.kfs.context.KualiTestBase;
024:
025: /**
026: * This class tests the BeanPropertyComparator methods.
027: */
028: public class BeanPropertyComparatorTest extends KualiTestBase {
029:
030: public final void testConstructor_nullList() {
031: boolean failedAsExpected = false;
032:
033: try {
034: new BeanPropertyComparator(null);
035: } catch (IllegalArgumentException e) {
036: failedAsExpected = true;
037: }
038:
039: assertTrue(failedAsExpected);
040: }
041:
042: public final void testConstructor_emptyList() {
043: boolean failedAsExpected = false;
044: try {
045: new BeanPropertyComparator(new ArrayList());
046: } catch (IllegalArgumentException e) {
047: failedAsExpected = true;
048: }
049: assertTrue(failedAsExpected);
050: }
051:
052: public final void testCompare_unknownPropertyNames() {
053: List unknownProperties = Arrays.asList(new String[] { "one",
054: "two", "three" });
055:
056: BeanPropertyComparator bpc = new BeanPropertyComparator(
057: unknownProperties);
058: A a = new A("something", new Integer(0), Boolean.valueOf(false));
059: B b = new B("something else", new Integer(1), Boolean
060: .valueOf(true));
061:
062: boolean failedAsExpected = false;
063: try {
064: bpc.compare(a, b);
065: } catch (BeanComparisonException e) {
066: if (e.getCause() instanceof NullPointerException) {
067: failedAsExpected = true;
068: }
069: }
070: assertTrue(failedAsExpected);
071: }
072:
073: public final void testCompare_propertyTypeMismatch() {
074: List mismatchedProperties = Arrays.asList(new String[] { "i",
075: "b" });
076:
077: BeanPropertyComparator bpc = new BeanPropertyComparator(
078: mismatchedProperties);
079: A a = new A("something", new Integer(0), Boolean.valueOf(false));
080: C c = new C("something else", 1, true);
081:
082: boolean failedAsExpected = false;
083: try {
084: bpc.compare(a, c);
085: } catch (ClassCastException e) {
086: failedAsExpected = true;
087: }
088: assertTrue(failedAsExpected);
089: }
090:
091: public final void testCompare_privateProperty() {
092: List privateProperty = Arrays.asList(new String[] { "s" });
093:
094: BeanPropertyComparator bpc = new BeanPropertyComparator(
095: privateProperty);
096: C c = new C("something else", 1, true);
097: A a = new A("something", new Integer(0), Boolean.valueOf(false));
098:
099: boolean failedAsExpected = false;
100: try {
101: bpc.compare(c, a);
102: } catch (BeanComparisonException e) {
103: if (e.getCause() instanceof NullPointerException) {
104: failedAsExpected = true;
105: }
106: }
107: assertTrue(failedAsExpected);
108: }
109:
110: public final void testCompare_oneProperty_string() {
111: List properties = Arrays.asList(new String[] { "s" });
112:
113: BeanPropertyComparator bpc = new BeanPropertyComparator(
114: properties);
115: A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
116: B greater = new B("Two", new Integer(0), Boolean.valueOf(false));
117:
118: int lessThan = bpc.compare(lesser, greater);
119: assertTrue(lessThan < 0);
120:
121: int greaterThan = bpc.compare(greater, lesser);
122: assertTrue(greaterThan > 0);
123:
124: int equal = bpc.compare(greater, greater);
125: assertTrue(equal == 0);
126: }
127:
128: public final void testCompare_oneProperty_integer() {
129: List properties = Arrays.asList(new String[] { "i" });
130:
131: BeanPropertyComparator bpc = new BeanPropertyComparator(
132: properties);
133: A lesser = new A("One", new Integer(-1), Boolean.valueOf(false));
134: B greater = new B("One", new Integer(1), Boolean.valueOf(false));
135:
136: int lessThan = bpc.compare(lesser, greater);
137: assertTrue(lessThan < 0);
138:
139: int greaterThan = bpc.compare(greater, lesser);
140: assertTrue(greaterThan > 0);
141:
142: int equal = bpc.compare(greater, greater);
143: assertTrue(equal == 0);
144: }
145:
146: public final void testCompare_oneProperty_boolean() {
147: List properties = Arrays.asList(new String[] { "b" });
148:
149: BeanPropertyComparator bpc = new BeanPropertyComparator(
150: properties);
151: A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
152: B greater = new B("One", new Integer(0), Boolean.valueOf(true));
153:
154: int lessThan = bpc.compare(lesser, greater);
155: assertTrue(lessThan < 0);
156:
157: int greaterThan = bpc.compare(greater, lesser);
158: assertTrue(greaterThan > 0);
159:
160: int equal = bpc.compare(greater, greater);
161: assertTrue(equal == 0);
162: }
163:
164: public final void testCompare_oneLevel() {
165: List propertiesSIB = Arrays
166: .asList(new String[] { "s", "i", "b" });
167:
168: BeanPropertyComparator bpcSIB = new BeanPropertyComparator(
169: propertiesSIB);
170: A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
171: B greater = new B("Two", new Integer(0), Boolean.valueOf(false));
172:
173: int lessThan = bpcSIB.compare(lesser, greater);
174: assertTrue(lessThan < 0);
175:
176: int greaterThan = bpcSIB.compare(greater, lesser);
177: assertTrue(greaterThan > 0);
178:
179: int equal = bpcSIB.compare(greater, greater);
180: assertTrue(equal == 0);
181: }
182:
183: public final void testCompare_twoLevels() {
184: List propertiesSIB = Arrays
185: .asList(new String[] { "s", "i", "b" });
186:
187: BeanPropertyComparator bpc = new BeanPropertyComparator(
188: propertiesSIB);
189: A lesser = new A("Same", new Integer(-1), Boolean
190: .valueOf(false));
191: B greater = new B("Same", new Integer(1), Boolean
192: .valueOf(false));
193:
194: int lessThan = bpc.compare(lesser, greater);
195: assertTrue(lessThan < 0);
196:
197: int greaterThan = bpc.compare(greater, lesser);
198: assertTrue(greaterThan > 0);
199:
200: int equal = bpc.compare(greater, greater);
201: assertTrue(equal == 0);
202: }
203:
204: public final void testCompare_threeLevels() {
205: List propertiesSIB = Arrays
206: .asList(new String[] { "s", "i", "b" });
207:
208: BeanPropertyComparator bpc = new BeanPropertyComparator(
209: propertiesSIB);
210: A lesser = new A("Same", new Integer(1), Boolean.valueOf(false));
211: B greater = new B("Same", new Integer(1), Boolean.valueOf(true));
212:
213: int lessThan = bpc.compare(lesser, greater);
214: assertTrue(lessThan < 0);
215:
216: int greaterThan = bpc.compare(greater, lesser);
217: assertTrue(greaterThan > 0);
218:
219: int equal = bpc.compare(greater, greater);
220: assertTrue(equal == 0);
221: }
222:
223: public final void testCompare_differentCases() {
224: List propertiesSIB = Arrays
225: .asList(new String[] { "s", "i", "b" });
226:
227: BeanPropertyComparator sensitive = new BeanPropertyComparator(
228: propertiesSIB, false);
229: BeanPropertyComparator insensitive = new BeanPropertyComparator(
230: propertiesSIB, true);
231:
232: A lesser = new A("SomeThing", new Integer(1), Boolean
233: .valueOf(false));
234: B greater = new B("something", new Integer(1), Boolean
235: .valueOf(false));
236:
237: int equal = insensitive.compare(greater, lesser);
238: assertTrue(equal == 0);
239:
240: int inequal = sensitive.compare(greater, lesser);
241: assertTrue(inequal != 0);
242: }
243:
244: public static class A {
245: private String s;
246: private Integer i;
247: private Boolean b;
248:
249: public A(String s, Integer i, Boolean b) {
250: this .s = s;
251: this .i = i;
252: this .b = b;
253: }
254:
255: public String getS() {
256: return s;
257: }
258:
259: public Integer getI() {
260: return i;
261: }
262:
263: public Boolean getB() {
264: return b;
265: }
266: }
267:
268: public static class B {
269: private String s;
270: private Integer i;
271: private Boolean b;
272: private Long l;
273:
274: public B(String s, Integer i, Boolean b) {
275: this .s = s;
276: this .i = i;
277: this .b = b;
278: this .l = new Long(23);
279: }
280:
281: public String getS() {
282: return s;
283: }
284:
285: public Integer getI() {
286: return i;
287: }
288:
289: public Boolean getB() {
290: return b;
291: }
292:
293: public Long getL() {
294: return l;
295: }
296: }
297:
298: public static class C {
299: private boolean s;
300: private String i;
301: private float b;
302:
303: public C(String i, float b, boolean s) {
304: this .s = s;
305: this .i = i;
306: this .b = b;
307: }
308:
309: private boolean getS() {
310: return s;
311: }
312:
313: public String getI() {
314: return i;
315: }
316:
317: public float getB() {
318: return b;
319: }
320: }
321: }
|