1 /* 2 * Replica is published under the terms 3 * of the Apache Software License. 4 */ 5 package replica.utils; 6 7 import junit.framework.TestCase; 8 9 /*** 10 * 11 * @author Pedro Costa 12 * @author Helder Silva 13 * @since 17/Jan/2004 14 */ 15 public class BeanUtilsTest extends TestCase { 16 17 /*** 18 * Constructor for BeanUtilsTest. 19 * @param name 20 */ 21 public BeanUtilsTest(String name) { 22 super(name); 23 } 24 25 public void testWriteStringProperty() { 26 27 final class Bean{ 28 String prop; 29 public Bean(){ super(); } 30 public String getProp(){ return prop; } 31 public void setProp( String value ){ prop = value; } 32 } 33 34 Bean bean = new Bean(); 35 36 BeanUtils.writeProperty(bean, "prop", "value"); 37 38 assertEquals("value", bean.getProp()); 39 } 40 41 public void testWriteBooleanProperty() { 42 43 final class Bean{ 44 boolean prop; 45 public Bean(){ super(); } 46 public boolean isProp(){ return prop; } 47 public void setProp( boolean value ){ prop = value; } 48 } 49 50 Bean bean = new Bean(); 51 52 BeanUtils.writeProperty(bean, "prop", "true"); 53 assertEquals(true, bean.isProp()); 54 55 BeanUtils.writeProperty(bean, "prop", "false"); 56 assertEquals(false, bean.isProp()); 57 } 58 59 public void testWriteIntProperty() { 60 61 final class Bean{ 62 int prop; 63 public Bean(){ super(); } 64 public int getProp(){ return prop; } 65 public void setProp( int value ){ prop = value; } 66 } 67 68 Bean bean = new Bean(); 69 70 BeanUtils.writeProperty(bean, "prop", "567"); 71 assertEquals(567, bean.getProp()); 72 73 BeanUtils.writeProperty(bean, "prop", "-567"); 74 assertEquals(-567, bean.getProp()); 75 } 76 77 public void testWriteLongProperty() { 78 79 final class Bean{ 80 long prop; 81 public Bean(){ super(); } 82 public long getProp(){ return prop; } 83 public void setProp( long value ){ prop = value; } 84 } 85 86 Bean bean = new Bean(); 87 88 BeanUtils.writeProperty(bean, "prop", Long.toString( Long.MAX_VALUE ) ); 89 assertEquals(Long.MAX_VALUE, bean.getProp()); 90 91 BeanUtils.writeProperty(bean, "prop", "456" ); 92 assertEquals(456, bean.getProp()); 93 94 BeanUtils.writeProperty(bean, "prop", Long.toString( Long.MIN_VALUE ) ); 95 assertEquals(Long.MIN_VALUE, bean.getProp()); 96 } 97 98 public void testWriteFloatProperty() { 99 100 final class Bean{ 101 float prop; 102 public Bean(){ super(); } 103 public float getProp(){ return prop; } 104 public void setProp( float value ){ prop = value; } 105 } 106 107 Bean bean = new Bean(); 108 109 BeanUtils.writeProperty(bean, "prop", Float.toString( Float.MAX_VALUE ) ); 110 assertEquals(Float.MAX_VALUE, bean.getProp(), 0); 111 BeanUtils.writeProperty(bean, "prop", Float.toString( Float.MIN_VALUE ) ); 112 assertEquals(Float.MIN_VALUE, bean.getProp(), 0); 113 BeanUtils.writeProperty(bean, "prop", "-345.43" ); 114 assertEquals(-345.43, bean.getProp(), 0.1); 115 BeanUtils.writeProperty(bean, "prop", "0" ); 116 assertEquals(0, bean.getProp(), 0); 117 } 118 119 public void testConvertNegativeFloatFromString(){ 120 121 assertEquals( -344.4, ((Float)BeanUtils.convertStringToType("-344.4", float.class)).floatValue(), 0.1); 122 } 123 124 public void testWriteDoubleProperty() { 125 126 final class Bean{ 127 double prop; 128 public Bean(){ super(); } 129 public double getProp(){ return prop; } 130 public void setProp( double value ){ prop = value; } 131 } 132 133 Bean bean = new Bean(); 134 135 BeanUtils.writeProperty(bean, "prop", Double.toString(Double.MAX_VALUE)); 136 assertEquals(Double.MAX_VALUE, bean.getProp(), 0); 137 BeanUtils.writeProperty(bean, "prop", Double.toString(Double.MIN_VALUE)); 138 assertEquals(Double.MIN_VALUE, bean.getProp(), 0); 139 BeanUtils.writeProperty(bean, "prop", "0"); 140 assertEquals(0, bean.getProp(), 0); 141 BeanUtils.writeProperty(bean, "prop", "3456.637"); 142 assertEquals(3456.637, bean.getProp(), 0); 143 } 144 }

This page was automatically generated by Maven