1 16 17 package org.apache.commons.beanutils; 18 19 import junit.framework.TestCase; 20 21 22 27 public class BeanToPropertyValueTransformerTest extends TestCase { 28 29 private static final Integer expectedIntegerValue = new Integer (123); 30 private static final Long expectedLongValue = new Long (123); 31 private static final Float expectedFloatValue = new Float (123.123f); 32 private static final Double expectedDoubleValue = new Double (567879.12344d); 33 private static final Boolean expectedBooleanValue = Boolean.TRUE; 34 private static final Byte expectedByteValue = new Byte ("12"); 35 36 41 public BeanToPropertyValueTransformerTest(String name) { 42 super(name); 43 } 44 45 48 public void testTransformWithSimpleStringProperty() { 49 BeanToPropertyValueTransformer transformer = 50 new BeanToPropertyValueTransformer("stringProperty"); 51 TestBean testBean = new TestBean("foo"); 52 assertEquals("foo", transformer.transform(testBean)); 53 } 54 55 59 public void testTransformWithSimpleStringPropertyAndNullValue() { 60 BeanToPropertyValueTransformer transformer = 61 new BeanToPropertyValueTransformer("stringProperty"); 62 TestBean testBean = new TestBean((String ) null); 63 assertNull(transformer.transform(testBean)); 64 } 65 66 69 public void testTransformWithSimpleIntProperty() { 70 BeanToPropertyValueTransformer transformer = 71 new BeanToPropertyValueTransformer("intProperty"); 72 TestBean testBean = new TestBean(expectedIntegerValue.intValue()); 73 assertEquals(expectedIntegerValue, transformer.transform(testBean)); 74 } 75 76 79 public void testTransformWithSimpleLongProperty() { 80 BeanToPropertyValueTransformer transformer = 81 new BeanToPropertyValueTransformer("longProperty"); 82 TestBean testBean = new TestBean(); 83 testBean.setLongProperty(expectedLongValue.longValue()); 84 assertEquals(expectedLongValue, transformer.transform(testBean)); 85 } 86 87 90 public void testTransformWithSimpleFloatProperty() { 91 BeanToPropertyValueTransformer transformer = 92 new BeanToPropertyValueTransformer("floatProperty"); 93 TestBean testBean = new TestBean(expectedFloatValue.floatValue()); 94 assertEquals(expectedFloatValue, transformer.transform(testBean)); 95 } 96 97 100 public void testTransformWithSimpleDoubleProperty() { 101 BeanToPropertyValueTransformer transformer = 102 new BeanToPropertyValueTransformer("doubleProperty"); 103 TestBean testBean = new TestBean(expectedDoubleValue.doubleValue()); 104 assertEquals(expectedDoubleValue, transformer.transform(testBean)); 105 } 106 107 110 public void testTransformWithSimpleByteProperty() { 111 BeanToPropertyValueTransformer transformer = 112 new BeanToPropertyValueTransformer("byteProperty"); 113 TestBean testBean = new TestBean(); 114 testBean.setByteProperty(expectedByteValue.byteValue()); 115 assertEquals(expectedByteValue, transformer.transform(testBean)); 116 } 117 118 121 public void testTransformWithSimpleBooleanProperty() { 122 BeanToPropertyValueTransformer transformer = 123 new BeanToPropertyValueTransformer("booleanProperty"); 124 TestBean testBean = new TestBean(expectedBooleanValue.booleanValue()); 125 assertEquals(expectedBooleanValue, transformer.transform(testBean)); 126 } 127 128 131 public void testTransformWithWriteOnlyProperty() { 132 try { 133 new BeanToPropertyValueTransformer("writeOnlyProperty").transform(new TestBean()); 134 } catch (IllegalArgumentException e) { 135 136 } 137 } 138 139 142 public void testTransformWithReadOnlyProperty() { 143 BeanToPropertyValueTransformer transformer = 144 new BeanToPropertyValueTransformer("readOnlyProperty"); 145 TestBean testBean = new TestBean(); 146 assertEquals(testBean.getReadOnlyProperty(), transformer.transform(testBean)); 147 } 148 149 152 public void testTransformWithInvalidProperty() { 153 try { 154 new BeanToPropertyValueTransformer("bogusProperty").transform(new TestBean()); 155 } catch (IllegalArgumentException e) { 156 157 } 158 } 159 160 163 public void testTransformWithNestedProperty() { 164 BeanToPropertyValueTransformer transformer = 165 new BeanToPropertyValueTransformer("anotherNested.stringProperty"); 166 TestBean testBean = new TestBean(); 167 TestBean nestedBean = new TestBean("foo"); 168 testBean.setAnotherNested(nestedBean); 169 assertEquals("foo", transformer.transform(testBean)); 170 } 171 172 175 public void testTransformWithMappedProperty() { 176 BeanToPropertyValueTransformer transformer = 177 new BeanToPropertyValueTransformer("mappedProperty(test-key)"); 178 TestBean testBean = new TestBean(); 179 180 testBean.setMappedProperty("test-key", "test-value"); 182 assertEquals("test-value", transformer.transform(testBean)); 183 184 transformer = new BeanToPropertyValueTransformer("mappedProperty(bogus-key)"); 186 assertEquals(null, transformer.transform(testBean)); 187 } 188 189 192 public void testTransformWithIndexedProperty() { 193 BeanToPropertyValueTransformer transformer = 194 new BeanToPropertyValueTransformer("intIndexed[0]"); 195 TestBean testBean = new TestBean(); 196 testBean.setIntIndexed(0, expectedIntegerValue.intValue()); 197 assertEquals(expectedIntegerValue, transformer.transform(testBean)); 198 199 transformer = new BeanToPropertyValueTransformer("intIndexed[9999]"); 201 202 try { 203 transformer.transform(testBean); 204 fail("Should have thrown an ArrayIndexOutOfBoundsException"); 205 } catch (ArrayIndexOutOfBoundsException e) { 206 207 } 208 } 209 210 213 public void testTransformWithNestedIndexedProperty() { 214 BeanToPropertyValueTransformer transformer = 215 new BeanToPropertyValueTransformer("anotherNested.intIndexed[0]"); 216 TestBean testBean = new TestBean(); 217 TestBean nestedBean = new TestBean(); 218 nestedBean.setIntIndexed(0, expectedIntegerValue.intValue()); 219 testBean.setAnotherNested(nestedBean); 220 assertEquals(expectedIntegerValue, transformer.transform(testBean)); 221 } 222 223 226 public void testTransformWithNullInPath() { 227 BeanToPropertyValueTransformer transformer = 228 new BeanToPropertyValueTransformer("anotherNested.stringProperty"); 229 230 try { 231 transformer.transform(new TestBean()); 232 fail("Should have throw IllegalArgumentException"); 233 } catch (IllegalArgumentException e) { 234 235 } 236 } 237 238 241 public void testTransformWithNullInPathAndIgnoreTrue() { 242 BeanToPropertyValueTransformer transformer = 243 new BeanToPropertyValueTransformer("anotherNested.stringProperty",true); 244 assertEquals(null, transformer.transform(new TestBean())); 245 } 246 } 247 | Popular Tags |