1 4 package org.oddjob.arooa.reflect; 5 6 import java.io.File ; 7 8 import junit.framework.TestCase; 9 10 import org.apache.commons.beanutils.BeanUtilsBean; 11 import org.apache.commons.beanutils.DynaBean; 12 import org.apache.commons.beanutils.LazyDynaBean; 13 import org.oddjob.arooa.ArooaException; 14 15 18 public class BeanUtilsBeanHelperTest extends TestCase { 19 20 public void testAttributeSetters() throws ArooaException { 21 22 BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper( 23 BeanUtilsBean.getInstance()); 24 Object subject = new ThingWithAttributes(); 25 try { 26 bubh.setProperty(subject, "one", "test"); 27 fail("setOne doesn't exist"); 28 } catch (ArooaException be) { 29 } 30 try { 31 bubh.setProperty(subject, "two", "test"); 32 fail("setTwo returns non void"); 33 } catch (ArooaException be) { 34 } 35 try { 36 bubh.setProperty(subject, "three", "test"); 37 fail("setThree takes no args"); 38 } catch (ArooaException be) { 39 } 40 41 try { 42 bubh.setProperty(subject, "four", "test"); 43 fail("setFour takes two args"); 44 } catch (ArooaException be) { 45 } 46 47 bubh.setProperty(subject, "five", null); 49 50 bubh.setProperty(subject, "five", "test"); 52 53 bubh.setProperty(subject, "five", new String [] {"test" }); 55 56 bubh.setProperty(subject, "five", new File ("hello.txt")); 58 59 try { 61 bubh.setProperty(subject, "six", null); 62 fail("can't set an int null"); 63 } catch (ArooaException e) { 64 } 66 67 bubh.setProperty(subject, "six", new Integer (2)); 69 70 bubh.setProperty(subject, "seven", new Integer (2)); 72 73 bubh.setProperty(subject, "eight", "hello.txt"); 75 76 bubh.setProperty(subject, "nine", "hello.txt"); 78 79 bubh.setProperty(subject, "nine", new File ("hello.txt")); 81 82 bubh.setProperty(subject, "nine", new Object [] { "hello.txt" } ); 84 85 bubh.setProperty(subject, "nine", new String [] { "hello.txt" } ); 87 88 bubh.setProperty(subject, "nine2", "hello.txt"); 90 91 bubh.setProperty(subject, "nine2", new File ("hello.txt")); 93 94 bubh.setProperty(subject, "nine2", new Object [] { "hello.txt" } ); 96 97 bubh.setProperty(subject, "nine2", new File [] { new File ("hello.txt") } ); 99 100 bubh.setProperty(subject, "ten", new String [] { "1", "2", "3" }); 102 103 } 104 105 109 public void testAttributeSetters2() { 110 BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper( 111 BeanUtilsBean.getInstance()); 112 Object subject = new ThingWithAttributes(); 113 114 bubh.setProperty(subject, "nine", new Object [] { 116 new File ("hello.txt"), new File ("goodbye.txt") } ); 117 118 } 119 120 121 public static class ThingWithAttributes { 122 123 public int setTwo(String s) { 124 return 0; 125 } 126 127 public void setThree() {} 128 129 public void setFour(String s1, String s2) {} 130 131 public void setFive(String s) {} 132 133 public void setSix(int i) {} 134 135 public void setSeven(Integer i) {} 136 137 public void setEight(File f) { } 138 139 public void setNine(File [] f) { 140 assertEquals( new File ("hello.txt"), f[0]); 141 } 142 143 public void setNine2(String [] f) { 145 assertEquals( "hello.txt", f[0]); 146 } 147 148 public void setTen(int[] i) { } 149 150 public void set(Object o) { } 151 152 } 153 154 public void testMappedProperties() throws ArooaException { 155 156 BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper( 157 BeanUtilsBean.getInstance()); 158 Object subject = new ThingWithMappedProperties(); 159 try { 160 bubh.setMappedProperty(subject, "one", "x", "test"); 161 fail("setOne doesn't exist"); 162 } catch (ArooaException be) { 163 } 164 try { 171 bubh.setMappedProperty(subject, "three", "x", "test"); 172 fail("setThree takes no args"); 173 } catch (ArooaException be) { 174 } 175 176 try { 177 bubh.setMappedProperty(subject, "four", "x", "test"); 178 fail("setFour takes three args"); 179 } catch (ArooaException be) { 180 } 181 182 bubh.setMappedProperty(subject, "five", "x", null); 184 185 bubh.setMappedProperty(subject, "five", "x", "test"); 187 188 bubh.setMappedProperty(subject, "five", "x", new String [] {"test" }); 190 191 bubh.setMappedProperty(subject, "five", "x", new File ("hello.txt")); 193 194 bubh.setMappedProperty(subject, "six", "x", new Integer (2)); 196 197 } 198 199 public static class ThingWithMappedProperties { 200 201 public int setTwo(String s, String s2) { 202 return 0; 203 } 204 205 public void setThree() {} 206 207 public void setFour(String s1, String s2, String s3) {} 208 209 public void setFive(String s, String v) {} 210 211 public void setSix(String s, int i) {} 212 213 } 214 215 public void testPropertyType() { 216 BeanUtilsBeanHelper bubh = new BeanUtilsBeanHelper( 217 BeanUtilsBean.getInstance()); 218 219 ThingWithAttributes t = new ThingWithAttributes(); 220 221 assertEquals(Object .class, bubh.getPropertyType(t, "one")); 223 224 assertEquals(File [].class, bubh.getPropertyType(t, "nine")); 226 227 DynaBean db = new LazyDynaBean(); 229 assertEquals(Object .class, bubh.getPropertyType(db, "foo")); 230 231 } 232 } 233 | Popular Tags |