/krad/krad-app-framework/src/test/java/org/kuali/rice/krad/util/BeanPropertyComparatorTest.java

https://github.com/sbower/kuali-rice-1 · Java · 394 lines · 278 code · 98 blank · 18 comment · 10 complexity · 3353aede6b5766105b5909ae3f82fdb9 MD5 · raw file

  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. import org.junit.Assert;
  18. import org.junit.Test;
  19. import org.kuali.rice.krad.util.BeanPropertyComparator.BeanComparisonException;
  20. import java.text.DateFormat;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.ArrayList;
  24. import java.util.Arrays;
  25. import java.util.Date;
  26. import java.util.List;
  27. /**
  28. * This class tests the BeanPropertyComparator methods.
  29. */
  30. public class BeanPropertyComparatorTest {
  31. @Test public void testConstructor_nullList() {
  32. boolean failedAsExpected = false;
  33. try {
  34. new BeanPropertyComparator(null);
  35. }
  36. catch (IllegalArgumentException e) {
  37. failedAsExpected = true;
  38. }
  39. Assert.assertTrue(failedAsExpected);
  40. }
  41. @Test public void testConstructor_emptyList() {
  42. boolean failedAsExpected = false;
  43. try {
  44. new BeanPropertyComparator(new ArrayList());
  45. }
  46. catch (IllegalArgumentException e) {
  47. failedAsExpected = true;
  48. }
  49. Assert.assertTrue(failedAsExpected);
  50. }
  51. @Test public void testCompare_unknownPropertyNames() {
  52. List unknownProperties = Arrays.asList(new String[] { "one", "two", "three" });
  53. BeanPropertyComparator bpc = new BeanPropertyComparator(unknownProperties);
  54. A a = new A("something", new Integer(0), Boolean.valueOf(false));
  55. B b = new B("something else", new Integer(1), Boolean.valueOf(true));
  56. boolean failedAsExpected = false;
  57. try {
  58. bpc.compare(a, b);
  59. }
  60. catch (BeanComparisonException e) {
  61. if (e.getCause() instanceof NullPointerException) {
  62. failedAsExpected = true;
  63. }
  64. }
  65. Assert.assertTrue(failedAsExpected);
  66. }
  67. @Test public void testCompare_propertyTypeMismatch() {
  68. List mismatchedProperties = Arrays.asList(new String[] { "i", "b" });
  69. BeanPropertyComparator bpc = new BeanPropertyComparator(mismatchedProperties);
  70. A a = new A("something", new Integer(0), Boolean.valueOf(false));
  71. C c = new C("something else", 1, true);
  72. boolean failedAsExpected = false;
  73. try {
  74. bpc.compare(a, c);
  75. }
  76. catch (ClassCastException e) {
  77. failedAsExpected = true;
  78. }
  79. Assert.assertTrue(failedAsExpected);
  80. }
  81. @Test public void testCompare_privateProperty() {
  82. List privateProperty = Arrays.asList(new String[] { "s" });
  83. BeanPropertyComparator bpc = new BeanPropertyComparator(privateProperty);
  84. C c = new C("something else", 1, true);
  85. A a = new A("something", new Integer(0), Boolean.valueOf(false));
  86. boolean failedAsExpected = false;
  87. try {
  88. bpc.compare(c, a);
  89. }
  90. catch (BeanComparisonException e) {
  91. if (e.getCause() instanceof NullPointerException) {
  92. failedAsExpected = true;
  93. }
  94. }
  95. Assert.assertTrue(failedAsExpected);
  96. }
  97. @Test public void testCompare_oneProperty_string() {
  98. List properties = Arrays.asList(new String[] { "s" });
  99. BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
  100. A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
  101. B greater = new B("Two", new Integer(0), Boolean.valueOf(false));
  102. int lessThan = bpc.compare(lesser, greater);
  103. Assert.assertTrue(lessThan < 0);
  104. int greaterThan = bpc.compare(greater, lesser);
  105. Assert.assertTrue(greaterThan > 0);
  106. int equal = bpc.compare(greater, greater);
  107. Assert.assertTrue(equal == 0);
  108. }
  109. @Test public void testCompare_oneProperty_integer() {
  110. List properties = Arrays.asList(new String[] { "i" });
  111. BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
  112. A lesser = new A("One", new Integer(-1), Boolean.valueOf(false));
  113. B greater = new B("One", new Integer(1), Boolean.valueOf(false));
  114. int lessThan = bpc.compare(lesser, greater);
  115. Assert.assertTrue(lessThan < 0);
  116. int greaterThan = bpc.compare(greater, lesser);
  117. Assert.assertTrue(greaterThan > 0);
  118. int equal = bpc.compare(greater, greater);
  119. Assert.assertTrue(equal == 0);
  120. }
  121. @Test public void testCompare_oneProperty_boolean() {
  122. List properties = Arrays.asList(new String[] { "b" });
  123. BeanPropertyComparator bpc = new BeanPropertyComparator(properties);
  124. A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
  125. B greater = new B("One", new Integer(0), Boolean.valueOf(true));
  126. int lessThan = bpc.compare(lesser, greater);
  127. Assert.assertTrue(lessThan < 0);
  128. int greaterThan = bpc.compare(greater, lesser);
  129. Assert.assertTrue(greaterThan > 0);
  130. int equal = bpc.compare(greater, greater);
  131. Assert.assertTrue(equal == 0);
  132. }
  133. @Test public void testCompare_oneLevel() {
  134. List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
  135. BeanPropertyComparator bpcSIB = new BeanPropertyComparator(propertiesSIB);
  136. A lesser = new A("One", new Integer(0), Boolean.valueOf(false));
  137. B greater = new B("Two", new Integer(0), Boolean.valueOf(false));
  138. int lessThan = bpcSIB.compare(lesser, greater);
  139. Assert.assertTrue(lessThan < 0);
  140. int greaterThan = bpcSIB.compare(greater, lesser);
  141. Assert.assertTrue(greaterThan > 0);
  142. int equal = bpcSIB.compare(greater, greater);
  143. Assert.assertTrue(equal == 0);
  144. }
  145. @Test public void testCompare_twoLevels() {
  146. List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
  147. BeanPropertyComparator bpc = new BeanPropertyComparator(propertiesSIB);
  148. A lesser = new A("Same", new Integer(-1), Boolean.valueOf(false));
  149. B greater = new B("Same", new Integer(1), Boolean.valueOf(false));
  150. int lessThan = bpc.compare(lesser, greater);
  151. Assert.assertTrue(lessThan < 0);
  152. int greaterThan = bpc.compare(greater, lesser);
  153. Assert.assertTrue(greaterThan > 0);
  154. int equal = bpc.compare(greater, greater);
  155. Assert.assertTrue(equal == 0);
  156. }
  157. @Test public void testCompare_threeLevels() {
  158. List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
  159. BeanPropertyComparator bpc = new BeanPropertyComparator(propertiesSIB);
  160. A lesser = new A("Same", new Integer(1), Boolean.valueOf(false));
  161. B greater = new B("Same", new Integer(1), Boolean.valueOf(true));
  162. int lessThan = bpc.compare(lesser, greater);
  163. Assert.assertTrue(lessThan < 0);
  164. int greaterThan = bpc.compare(greater, lesser);
  165. Assert.assertTrue(greaterThan > 0);
  166. int equal = bpc.compare(greater, greater);
  167. Assert.assertTrue(equal == 0);
  168. }
  169. @Test public void testCompare_differentCases() {
  170. List propertiesSIB = Arrays.asList(new String[] { "s", "i", "b" });
  171. BeanPropertyComparator sensitive = new BeanPropertyComparator(propertiesSIB, false);
  172. BeanPropertyComparator insensitive = new BeanPropertyComparator(propertiesSIB, true);
  173. A lesser = new A("SomeThing", new Integer(1), Boolean.valueOf(false));
  174. B greater = new B("something", new Integer(1), Boolean.valueOf(false));
  175. int equal = insensitive.compare(greater, lesser);
  176. Assert.assertTrue(equal == 0);
  177. int inequal = sensitive.compare(greater, lesser);
  178. Assert.assertTrue(inequal != 0);
  179. }
  180. @Test public void testCompare_differentDates() throws ParseException {
  181. List propertiesD = Arrays.asList(new String[] { "d" });
  182. DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
  183. BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
  184. D lesser = new D(dateFormat.parse("01/01/1990"));
  185. D greater = new D(dateFormat.parse("01/02/1990"));
  186. int result = comparator.compare(greater, lesser);
  187. Assert.assertEquals(1, result);
  188. result = comparator.compare(lesser, greater);
  189. Assert.assertEquals(-1, result);
  190. result = comparator.compare(lesser, lesser);
  191. Assert.assertEquals(0, result);
  192. result = comparator.compare(greater, greater);
  193. Assert.assertEquals(0, result);
  194. }
  195. @Test public void testCompare_firstNullDates() throws ParseException {
  196. List propertiesD = Arrays.asList(new String[] { "d" });
  197. DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
  198. BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
  199. D lesser = new D(null);
  200. D greater = new D(dateFormat.parse("01/02/1990"));
  201. int result = comparator.compare(greater, lesser);
  202. Assert.assertEquals(1, result);
  203. result = comparator.compare(lesser, greater);
  204. Assert.assertEquals(-1, result);
  205. result = comparator.compare(lesser, lesser);
  206. Assert.assertEquals(0, result);
  207. result = comparator.compare(greater, greater);
  208. Assert.assertEquals(0, result);
  209. }
  210. @Test public void testCompare_secondNullDates() throws ParseException {
  211. List propertiesD = Arrays.asList(new String[] { "d" });
  212. DateFormat dateFormat = SimpleDateFormat.getDateInstance(SimpleDateFormat.SHORT);
  213. BeanPropertyComparator comparator = new BeanPropertyComparator(propertiesD);
  214. D lesser = new D(dateFormat.parse("01/02/1990"));
  215. D greater = new D(null);
  216. int result = comparator.compare(greater, lesser);
  217. Assert.assertEquals(-1, result);
  218. result = comparator.compare(lesser, greater);
  219. Assert.assertEquals(1, result);
  220. result = comparator.compare(lesser, lesser);
  221. Assert.assertEquals(0, result);
  222. result = comparator.compare(greater, greater);
  223. Assert.assertEquals(0, result);
  224. }
  225. public static class A {
  226. private String s;
  227. private Integer i;
  228. private Boolean b;
  229. public A(String s, Integer i, Boolean b) {
  230. this.s = s;
  231. this.i = i;
  232. this.b = b;
  233. }
  234. public String getS() {
  235. return s;
  236. }
  237. public Integer getI() {
  238. return i;
  239. }
  240. public Boolean getB() {
  241. return b;
  242. }
  243. }
  244. public static class B {
  245. private String s;
  246. private Integer i;
  247. private Boolean b;
  248. private Long l;
  249. public B(String s, Integer i, Boolean b) {
  250. this.s = s;
  251. this.i = i;
  252. this.b = b;
  253. this.l = new Long(23);
  254. }
  255. public String getS() {
  256. return s;
  257. }
  258. public Integer getI() {
  259. return i;
  260. }
  261. public Boolean getB() {
  262. return b;
  263. }
  264. public Long getL() {
  265. return l;
  266. }
  267. }
  268. public static class C {
  269. private boolean s;
  270. private String i;
  271. private float b;
  272. public C(String i, float b, boolean s) {
  273. this.s = s;
  274. this.i = i;
  275. this.b = b;
  276. }
  277. private boolean getS() {
  278. return s;
  279. }
  280. public String getI() {
  281. return i;
  282. }
  283. public float getB() {
  284. return b;
  285. }
  286. }
  287. public static class D {
  288. private Date d;
  289. public D(Date d) {
  290. this.d = d;
  291. }
  292. public Date getD() {
  293. return d;
  294. }
  295. }
  296. }