1 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 35 36 public class BeanComparatorTestCase extends TestCase { 37 38 40 43 protected TestBean bean = null; 44 protected AlphaBean alphaBean1 = null; 45 protected AlphaBean alphaBean2 = null; 46 47 protected BeanComparator beanComparator = null; 49 50 51 52 53 54 56 61 public BeanComparatorTestCase(String name) { 62 super(name); 63 } 64 65 66 68 69 72 public void setUp() { 73 bean = new TestBean(); 74 alphaBean1 = new AlphaBean("alphaBean1"); 75 alphaBean2 = new AlphaBean("alphaBean2"); 76 77 78 } 79 80 81 84 public static Test suite() { 85 return (new TestSuite(BeanComparatorTestCase.class)); 86 } 87 88 91 public void tearDown() { 92 bean = null; 93 alphaBean1 = null; 94 alphaBean2 = null; 95 beanComparator = null; 96 } 97 98 99 101 102 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 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 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 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 168 public void testCompareWithNulls() { 169 try { 170 beanComparator = new BeanComparator("name"); 171 beanComparator.compare(alphaBean2, null); 172 173 fail("Should not be able to compare a null value."); 175 176 } 177 catch (Exception e) { 178 179 } 180 } 181 182 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 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 ; } 221 catch (Exception e) { 222 fail("Exception" + e); 223 } 224 } 225 226 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 262 | Popular Tags |