Source code: org/apache/commons/beanutils/BeanComparatorTestCase.java
1 /*
2 * Copyright 2001-2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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
17 package org.apache.commons.beanutils;
18
19
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.HashMap;
22 import java.util.Map;
23 import junit.framework.TestCase;
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27
28 /**
29 * <p>
30 * Test Case for the BeanComparator class.
31 *
32 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
33 * @version $Revision: 1.1 $
34 */
35
36 public class BeanComparatorTestCase extends TestCase {
37
38 // ---------------------------------------------------- Instance Variables
39
40 /**
41 * The test beans for each test.
42 */
43 protected TestBean bean = null;
44 protected AlphaBean alphaBean1 = null;
45 protected AlphaBean alphaBean2 = null;
46
47 // The test BeanComparator
48 protected BeanComparator beanComparator = null;
49
50
51
52
53
54 // ---------------------------------------------------------- Constructors
55
56 /**
57 * Construct a new instance of this test case.
58 *
59 * @param name Name of the test case
60 */
61 public BeanComparatorTestCase(String name) {
62 super(name);
63 }
64
65
66 // -------------------------------------------------- Overall Test Methods
67
68
69 /**
70 * Set up instance variables required by this test case.
71 */
72 public void setUp() {
73 bean = new TestBean();
74 alphaBean1 = new AlphaBean("alphaBean1");
75 alphaBean2 = new AlphaBean("alphaBean2");
76
77
78 }
79
80
81 /**
82 * Return the tests included in this test suite.
83 */
84 public static Test suite() {
85 return (new TestSuite(BeanComparatorTestCase.class));
86 }
87
88 /**
89 * Tear down instance variables required by this test case.
90 */
91 public void tearDown() {
92 bean = null;
93 alphaBean1 = null;
94 alphaBean2 = null;
95 beanComparator = null;
96 }
97
98
99 // ------------------------------------------------ Individual Test Methods
100
101
102 /**
103 * tests comparing two beans via their name using the default Comparator
104 */
105 public void testSimpleCompare() {
106 try {
107 beanComparator = new BeanComparator("name");
108 int result = beanComparator.compare(alphaBean1, alphaBean2);
109 assertTrue("Comparator did not sort properly. Result:" + result,result==-1);
110
111 }
112 catch (Exception e) {
113 fail("Exception");
114 }
115 }
116
117 /**
118 * tests comparing two beans via their name using the default Comparator, but the inverse
119 */
120 public void testSimpleCompareInverse() {
121 try {
122 beanComparator = new BeanComparator("name");
123 int result = beanComparator.compare(alphaBean2, alphaBean1);
124 assertTrue("Comparator did not sort properly. Result:" + result,result==1);
125
126 }
127 catch (Exception e) {
128 fail("Exception" + e);
129 }
130 }
131
132 /**
133 * tests comparing two beans via their name using the default Comparator where they have the same value.
134 */
135 public void testCompareIdentical() {
136 try {
137 alphaBean1 = new AlphaBean("alphabean");
138 alphaBean2 = new AlphaBean("alphabean");
139 beanComparator = new BeanComparator("name");
140 int result = beanComparator.compare(alphaBean1, alphaBean2);
141 assertTrue("Comparator did not sort properly. Result:" + result,result==0);
142
143 }
144 catch (Exception e) {
145 fail("Exception");
146 }
147 }
148
149 /**
150 * tests comparing one bean against itself.
151 */
152 public void testCompareBeanAgainstSelf() {
153 try {
154 beanComparator = new BeanComparator("name");
155 int result = beanComparator.compare(alphaBean1, alphaBean1);
156 assertTrue("Comparator did not sort properly. Result:" + result,result==0);
157
158 }
159 catch (Exception e) {
160 fail("Exception");
161 }
162 }
163
164 /**
165 * tests comparing two beans via their name using the default Comparator, but with one of the beans
166 * being null.
167 */
168 public void testCompareWithNulls() {
169 try {
170 beanComparator = new BeanComparator("name");
171 beanComparator.compare(alphaBean2, null);
172
173 // DEP not sure if this is the best way to test an exception?
174 fail("Should not be able to compare a null value.");
175
176 }
177 catch (Exception e) {
178
179 }
180 }
181
182 /**
183 * tests comparing two beans who don't have a property
184 */
185 public void testCompareOnMissingProperty() {
186 try {
187 beanComparator = new BeanComparator("bogusName");
188 beanComparator.compare(alphaBean2, alphaBean1);
189 fail("should not be able to compare");
190
191
192 }
193 catch (ClassCastException cce){
194 assertTrue("Wrong exception was thrown.",cce.toString().indexOf("Unknown property") > -1);
195 }
196 catch (Exception e) {
197 fail("Exception" + e);
198 }
199 }
200
201 /**
202 * tests comparing two beans on a boolean property, which is not possible.
203 */
204 public void testCompareOnBooleanProperty() {
205 try {
206 TestBean testBeanA = new TestBean();
207 TestBean testBeanB = new TestBean();
208
209 testBeanA.setBooleanProperty(true);
210 testBeanB.setBooleanProperty(false);
211
212 beanComparator = new BeanComparator("booleanProperty");
213 beanComparator.compare(testBeanA, testBeanB);
214
215 fail("BeanComparator should throw an exception when comparing two booleans.");
216
217 }
218 catch (ClassCastException cce){
219 ; // Expected result
220 }
221 catch (Exception e) {
222 fail("Exception" + e);
223 }
224 }
225
226 /**
227 * tests comparing two beans on a boolean property, then changing the property and testing
228 */
229 public void testSetProperty() {
230 try {
231 TestBean testBeanA = new TestBean();
232 TestBean testBeanB = new TestBean();
233
234 testBeanA.setDoubleProperty(5.5);
235 testBeanB.setDoubleProperty(1.0);
236
237 beanComparator = new BeanComparator("doubleProperty");
238 int result = beanComparator.compare(testBeanA, testBeanB);
239
240 assertTrue("Comparator did not sort properly. Result:" + result,result==1);
241
242 testBeanA.setStringProperty("string 1");
243 testBeanB.setStringProperty("string 2");
244
245 beanComparator.setProperty("stringProperty");
246
247 result = beanComparator.compare(testBeanA, testBeanB);
248
249 assertTrue("Comparator did not sort properly. Result:" + result,result==-1);
250
251 }
252 catch (ClassCastException cce){
253 fail("ClassCaseException " + cce.toString());
254 }
255 catch (Exception e) {
256 fail("Exception" + e);
257 }
258 }
259 }
260
261