1
2 package org.yajul.util;
3
4 import junit.framework.TestCase;
5
6 import java.util.Map;
7
8 /***
9 * TODO: Add class javadoc
10 *
11 * @author josh Apr 4, 2004 9:30:45 AM
12 */
13 public class PropertyAccessorTest extends TestCase
14 {
15 public PropertyAccessorTest(String name)
16 {
17 super(name);
18 }
19
20 public void testGettersAndSetters() throws Exception
21 {
22 BeanProperties accessor = new BeanProperties(TestBean.class);
23 TestBean bean = new TestBean();
24 assertEquals(3,accessor.values(bean).size());
25 accessor.setProperty(bean,"intProp", 3);
26 assertEquals(3,bean.getIntProp());
27 assertEquals(3,accessor.getProperty(bean,"intProp"));
28 accessor.setProperty(bean,"stringProp","test string");
29 assertEquals("test string",bean.getStringProp());
30 assertEquals("test string",accessor.getProperty(bean,"stringProp"));
31 accessor.setProperty(bean,"doubleProp", 3.1415);
32 assertEquals(3.1415,bean.getDoubleProp(),0.001);
33 assertEquals(3.1415,accessor.getDoubleProperty(bean,"doubleProp"),0.001);
34 TestBean copy = new TestBean();
35 accessor.copy(bean,copy);
36 assertEquals(bean,copy);
37 }
38
39 public void testPropertyMap() throws Exception
40 {
41 TestBean bean = new TestBean();
42 bean.setIntProp(42);
43 bean.setStringProp("the answer");
44 Map map = new PropertyMap(bean);
45 assertEquals(3,map.size());
46 assertEquals("the answer",map.get("stringProp"));
47 TestBean copy = new TestBean();
48 Map map2 = new PropertyMap(copy);
49 assertNull(map2.get("stringProp"));
50
51 map2.putAll(map);
52 assertEquals(3,map2.size());
53 assertEquals("the answer",map2.get("stringProp"));
54 assertEquals(bean,copy);
55 }
56
57 public static class TestBean
58 {
59 private int intProp;
60 private String stringProp;
61 private double doubleProp;
62
63 public int getIntProp()
64 {
65 return intProp;
66 }
67
68 public void setIntProp(int intProp)
69 {
70 this.intProp = intProp;
71 }
72
73 public String getStringProp()
74 {
75 return stringProp;
76 }
77
78 public void setStringProp(String stringProp)
79 {
80 this.stringProp = stringProp;
81 }
82
83 public double getDoubleProp()
84 {
85 return doubleProp;
86 }
87
88 public void setDoubleProp(double doubleProp)
89 {
90 this.doubleProp = doubleProp;
91 }
92
93 public boolean equals(Object o)
94 {
95 if (this == o) return true;
96 if (!(o instanceof TestBean)) return false;
97
98 final TestBean testBean = (TestBean) o;
99
100 if (doubleProp != testBean.doubleProp) return false;
101
102 if (intProp != testBean.intProp) return false;
103 return !(stringProp != null ? !stringProp.equals(testBean.stringProp) : testBean.stringProp != null);
104
105 }
106
107 public int hashCode()
108 {
109 int result;
110 long temp;
111 result = intProp;
112 result = 29 * result + (stringProp != null ? stringProp.hashCode() : 0);
113 temp = doubleProp != +0.0d ? Double.doubleToLongBits(doubleProp) : 0l;
114 result = 29 * result + (int) (temp ^ (temp >>> 32));
115 return result;
116 }
117 }
118 }