001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.lang.reflect.InvocationTargetException;
021: import java.util.HashMap;
022: import java.util.Map;
023: import junit.framework.TestCase;
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: /**
028: * <p>
029: * Test Case for the BeanComparator class.
030: *
031: * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
032: * @version $Revision: 557796 $
033: */
034:
035: public class BeanComparatorTestCase extends TestCase {
036:
037: // ---------------------------------------------------- Instance Variables
038:
039: /**
040: * The test beans for each test.
041: */
042: protected TestBean bean = null;
043: protected AlphaBean alphaBean1 = null;
044: protected AlphaBean alphaBean2 = null;
045:
046: // The test BeanComparator
047: protected BeanComparator beanComparator = null;
048:
049: // ---------------------------------------------------------- Constructors
050:
051: /**
052: * Construct a new instance of this test case.
053: *
054: * @param name Name of the test case
055: */
056: public BeanComparatorTestCase(String name) {
057: super (name);
058: }
059:
060: // -------------------------------------------------- Overall Test Methods
061:
062: /**
063: * Set up instance variables required by this test case.
064: */
065: public void setUp() {
066: bean = new TestBean();
067: alphaBean1 = new AlphaBean("alphaBean1");
068: alphaBean2 = new AlphaBean("alphaBean2");
069:
070: }
071:
072: /**
073: * Return the tests included in this test suite.
074: */
075: public static Test suite() {
076: return (new TestSuite(BeanComparatorTestCase.class));
077: }
078:
079: /**
080: * Tear down instance variables required by this test case.
081: */
082: public void tearDown() {
083: bean = null;
084: alphaBean1 = null;
085: alphaBean2 = null;
086: beanComparator = null;
087: }
088:
089: // ------------------------------------------------ Individual Test Methods
090:
091: /**
092: * tests comparing two beans via their name using the default Comparator
093: */
094: public void testSimpleCompare() {
095: try {
096: beanComparator = new BeanComparator("name");
097: int result = beanComparator.compare(alphaBean1, alphaBean2);
098: assertTrue("Comparator did not sort properly. Result:"
099: + result, result == -1);
100:
101: } catch (Exception e) {
102: fail("Exception");
103: }
104: }
105:
106: /**
107: * tests comparing two beans via their name using the default Comparator, but the inverse
108: */
109: public void testSimpleCompareInverse() {
110: try {
111: beanComparator = new BeanComparator("name");
112: int result = beanComparator.compare(alphaBean2, alphaBean1);
113: assertTrue("Comparator did not sort properly. Result:"
114: + result, result == 1);
115:
116: } catch (Exception e) {
117: fail("Exception" + e);
118: }
119: }
120:
121: /**
122: * tests comparing two beans via their name using the default Comparator where they have the same value.
123: */
124: public void testCompareIdentical() {
125: try {
126: alphaBean1 = new AlphaBean("alphabean");
127: alphaBean2 = new AlphaBean("alphabean");
128: beanComparator = new BeanComparator("name");
129: int result = beanComparator.compare(alphaBean1, alphaBean2);
130: assertTrue("Comparator did not sort properly. Result:"
131: + result, result == 0);
132:
133: } catch (Exception e) {
134: fail("Exception");
135: }
136: }
137:
138: /**
139: * tests comparing one bean against itself.
140: */
141: public void testCompareBeanAgainstSelf() {
142: try {
143: beanComparator = new BeanComparator("name");
144: int result = beanComparator.compare(alphaBean1, alphaBean1);
145: assertTrue("Comparator did not sort properly. Result:"
146: + result, result == 0);
147:
148: } catch (Exception e) {
149: fail("Exception");
150: }
151: }
152:
153: /**
154: * tests comparing two beans via their name using the default Comparator, but with one of the beans
155: * being null.
156: */
157: public void testCompareWithNulls() {
158: try {
159: beanComparator = new BeanComparator("name");
160: beanComparator.compare(alphaBean2, null);
161:
162: // DEP not sure if this is the best way to test an exception?
163: fail("Should not be able to compare a null value.");
164:
165: } catch (Exception e) {
166:
167: }
168: }
169:
170: /**
171: * tests comparing two beans who don't have a property
172: */
173: public void testCompareOnMissingProperty() {
174: try {
175: beanComparator = new BeanComparator("bogusName");
176: beanComparator.compare(alphaBean2, alphaBean1);
177: fail("should not be able to compare");
178:
179: } catch (Exception e) {
180: assertTrue("Wrong exception was thrown: " + e, e.toString()
181: .indexOf("Unknown property") > -1);
182: }
183: }
184:
185: /**
186: * tests comparing two beans on a boolean property, which is not possible.
187: */
188: public void testCompareOnBooleanProperty() {
189: try {
190: TestBean testBeanA = new TestBean();
191: TestBean testBeanB = new TestBean();
192:
193: testBeanA.setBooleanProperty(true);
194: testBeanB.setBooleanProperty(false);
195:
196: beanComparator = new BeanComparator("booleanProperty");
197: beanComparator.compare(testBeanA, testBeanB);
198:
199: // **** java.lang.Boolean implements Comparable from JDK 1.5 onwards
200: // so this test no longer fails
201: // fail("BeanComparator should throw an exception when comparing two booleans.");
202:
203: } catch (ClassCastException cce) {
204: ; // Expected result
205: } catch (Exception e) {
206: fail("Exception" + e);
207: }
208: }
209:
210: /**
211: * tests comparing two beans on a boolean property, then changing the property and testing
212: */
213: public void testSetProperty() {
214: try {
215: TestBean testBeanA = new TestBean();
216: TestBean testBeanB = new TestBean();
217:
218: testBeanA.setDoubleProperty(5.5);
219: testBeanB.setDoubleProperty(1.0);
220:
221: beanComparator = new BeanComparator("doubleProperty");
222: int result = beanComparator.compare(testBeanA, testBeanB);
223:
224: assertTrue("Comparator did not sort properly. Result:"
225: + result, result == 1);
226:
227: testBeanA.setStringProperty("string 1");
228: testBeanB.setStringProperty("string 2");
229:
230: beanComparator.setProperty("stringProperty");
231:
232: result = beanComparator.compare(testBeanA, testBeanB);
233:
234: assertTrue("Comparator did not sort properly. Result:"
235: + result, result == -1);
236:
237: } catch (ClassCastException cce) {
238: fail("ClassCaseException " + cce.toString());
239: } catch (Exception e) {
240: fail("Exception" + e);
241: }
242: }
243: }
|