1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24
25
26
27
28
29
30
31
32
33 public class BeanComparatorTestCase extends TestCase {
34
35
36
37
38
39
40 protected TestBean bean = null;
41 protected AlphaBean alphaBean1 = null;
42 protected AlphaBean alphaBean2 = null;
43
44
45
46
47
48
49
50
51
52 public BeanComparatorTestCase(final String name) {
53 super(name);
54 }
55
56
57
58
59
60
61
62
63 @Override
64 public void setUp() {
65 bean = new TestBean();
66 alphaBean1 = new AlphaBean("alphaBean1");
67 alphaBean2 = new AlphaBean("alphaBean2");
68
69
70 }
71
72
73
74
75
76 public static Test suite() {
77 return (new TestSuite(BeanComparatorTestCase.class));
78 }
79
80
81
82
83 @Override
84 public void tearDown() {
85 bean = null;
86 alphaBean1 = null;
87 alphaBean2 = null;
88 }
89
90
91
92
93
94
95
96
97 public void testSimpleCompare() {
98 final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>(
99 "name");
100 final int result = beanComparator.compare(alphaBean1, alphaBean2);
101 assertTrue("Comparator did not sort properly. Result:" + result,
102 result == -1);
103 }
104
105
106
107
108 public void testSimpleCompareInverse() {
109 final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>(
110 "name");
111 final int result = beanComparator.compare(alphaBean2, alphaBean1);
112 assertTrue("Comparator did not sort properly. Result:" + result,
113 result == 1);
114 }
115
116
117
118
119 public void testCompareIdentical() {
120 alphaBean1 = new AlphaBean("alphabean");
121 alphaBean2 = new AlphaBean("alphabean");
122 final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>(
123 "name");
124 final int result = beanComparator.compare(alphaBean1, alphaBean2);
125 assertTrue("Comparator did not sort properly. Result:" + result,
126 result == 0);
127 }
128
129
130
131
132 public void testCompareBeanAgainstSelf() {
133 final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>(
134 "name");
135 final int result = beanComparator.compare(alphaBean1, alphaBean1);
136 assertTrue("Comparator did not sort properly. Result:" + result,
137 result == 0);
138 }
139
140
141
142
143
144 public void testCompareWithNulls() {
145 try {
146 final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>("name");
147 beanComparator.compare(alphaBean2, null);
148
149 fail("Should not be able to compare a null value.");
150 }
151 catch (final Exception e) {
152
153 }
154 }
155
156
157
158
159 public void testCompareOnMissingProperty() {
160 try {
161 final BeanComparator<AlphaBean> beanComparator = new BeanComparator<AlphaBean>("bogusName");
162 beanComparator.compare(alphaBean2, alphaBean1);
163 fail("should not be able to compare");
164
165
166 }
167 catch (final Exception e) {
168 assertTrue("Wrong exception was thrown: " + e, e.toString().indexOf("Unknown property") > -1);
169 }
170 }
171
172
173
174
175 public void testCompareOnBooleanProperty() {
176 try {
177 final TestBean testBeanA = new TestBean();
178 final TestBean testBeanB = new TestBean();
179
180 testBeanA.setBooleanProperty(true);
181 testBeanB.setBooleanProperty(false);
182
183 final BeanComparator<TestBean> beanComparator = new BeanComparator<TestBean>("booleanProperty");
184 beanComparator.compare(testBeanA, testBeanB);
185
186
187
188
189
190 }
191 catch (final ClassCastException cce){
192
193 }
194 }
195
196
197
198
199 public void testSetProperty() {
200 final TestBean testBeanA = new TestBean();
201 final TestBean testBeanB = new TestBean();
202
203 testBeanA.setDoubleProperty(5.5);
204 testBeanB.setDoubleProperty(1.0);
205
206 final BeanComparator<TestBean> beanComparator = new BeanComparator<TestBean>(
207 "doubleProperty");
208 int result = beanComparator.compare(testBeanA, testBeanB);
209
210 assertTrue("Comparator did not sort properly. Result:" + result,
211 result == 1);
212
213 testBeanA.setStringProperty("string 1");
214 testBeanB.setStringProperty("string 2");
215
216 beanComparator.setProperty("stringProperty");
217
218 result = beanComparator.compare(testBeanA, testBeanB);
219
220 assertTrue("Comparator did not sort properly. Result:" + result,
221 result == -1);
222 }
223 }