1 package org.andromda.core.common;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5
6 import junit.framework.TestCase;
7
8
9 /***
10 * JUnit tests for {@link org.andromda.common.Introspector}
11 *
12 * @author Chad Brandon
13 */
14 public class IntrospectorTest
15 extends TestCase
16 {
17
18 public void testGetProperty()
19 {
20 TestBean testBean = new TestBean();
21 assertEquals(testBean.getStringProperty(), Introspector.instance().getProperty(testBean, "stringProperty"));
22 assertEquals(testBean.getNestedBean().getStringProperty(), Introspector.instance().getProperty(testBean, "nestedBean.stringProperty"));
23 try
24 {
25 Introspector.instance().getProperty(testBean, "intProperty");
26 fail();
27 }
28 catch (IntrospectorException exception) {}
29 }
30
31 public void testSetProperty()
32 {
33 TestBean testBean = new TestBean();
34 Introspector.instance().setProperty(testBean, "stringProperty", "A New String Value");
35 assertEquals(testBean.getStringProperty(), "A New String Value");
36 assertTrue(testBean.getBooleanProperty());
37 Introspector.instance().setProperty(testBean, "booleanProperty", "false");
38 assertFalse(testBean.getBooleanProperty());
39 assertEquals(testBean.getNestedBean().getLongProperty(), 222222);
40 Introspector.instance().setProperty(testBean, "nestedBean.longProperty", "9999");
41 assertEquals(testBean.getNestedBean().getLongProperty(), 9999);
42 assertEquals(testBean.getNestedBean().getNestedNestedBean().getIntProperty(), 5);
43 Introspector.instance().setProperty(testBean, "nestedBean.nestedNestedBean.intProperty", "10");
44 assertEquals(testBean.getNestedBean().getNestedNestedBean().getIntProperty(), 10);
45 Introspector.instance().setProperty(testBean, "aPropertyName", "SomeValue");
46 assertEquals(testBean.getAPropertyName(), "SomeValue");
47 try
48 {
49 Introspector.instance().getProperty(testBean, "intProperty");
50 fail();
51 }
52 catch (IntrospectorException exception) {}
53 }
54
55 public void testIsReadable()
56 {
57 TestBean testBean = new TestBean();
58 assertTrue(Introspector.instance().isReadable(testBean, "stringProperty"));
59 assertFalse(Introspector.instance().isWritable(testBean, "byteProperty"));
60 assertTrue(Introspector.instance().isReadable(testBean, "nestedBean.stringProperty"));
61 assertTrue(Introspector.instance().isWritable(testBean, "nestedBean.stringProperty"));
62 assertFalse(Introspector.instance().isReadable(testBean, "nestedBean.intProperty"));
63 assertFalse(Introspector.instance().isWritable(testBean, "nestedBean.intProperty"));
64 }
65
66 public void testConstainsValidProperty()
67 {
68 TestBean testBean = new TestBean();
69 assertTrue(Introspector.instance().containsValidProperty(testBean, "booleanProperty", null));
70 assertTrue(Introspector.instance().containsValidProperty(testBean, "nestedBean.booleanProperty", "false"));
71 assertFalse(Introspector.instance().containsValidProperty(testBean, "nestedBean.emptyCollectionProperty", null));
72 assertTrue(Introspector.instance().containsValidProperty(testBean, "nestedBean.nonEmptyCollectionProperty", null));
73 assertTrue(Introspector.instance().containsValidProperty(testBean, "aBCProperty", "true"));
74 }
75
76 private static final class TestBean
77 {
78 private boolean abcProperty = true;
79 private boolean booleanProperty = true;
80 private Integer integerProperty = new Integer(1);
81 private String stringProperty = "TestBean";
82 private int intProperty = 5;
83 private long longProperty = 1111111111;
84 private byte byteProperty = 01;
85 private NestedBean nestedBean = new NestedBean();
86 private String aPropertyName;
87 public boolean isABCProperty()
88 {
89 return abcProperty;
90 }
91 public void setABCProperty(boolean abcProperty)
92 {
93 this.abcProperty = abcProperty;
94 }
95 public boolean isBooleanProperty()
96 {
97 return booleanProperty;
98 }
99 public void setBooleanProperty(boolean booleanProperty)
100 {
101 this.booleanProperty = booleanProperty;
102 }
103 protected boolean getBooleanProperty()
104 {
105 return this.booleanProperty;
106 }
107 public byte getByteProperty()
108 {
109 return byteProperty;
110 }
111 protected void setByteProperty(byte byteProperty)
112 {
113 this.byteProperty = byteProperty;
114 }
115 protected Integer getIntegerProperty()
116 {
117 return integerProperty;
118 }
119 protected void setIntegerProperty(Integer integerProperty)
120 {
121 this.integerProperty = integerProperty;
122 }
123 protected int getIntProperty()
124 {
125 return intProperty;
126 }
127 protected void setIntProperty(int intProperty)
128 {
129 this.intProperty = intProperty;
130 }
131 protected long getLongProperty()
132 {
133 return longProperty;
134 }
135 protected void setLongProperty(long longProperty)
136 {
137 this.longProperty = longProperty;
138 }
139 public String getStringProperty()
140 {
141 return stringProperty;
142 }
143 public void setStringProperty(String stringProperty)
144 {
145 this.stringProperty = stringProperty;
146 }
147 public NestedBean getNestedBean()
148 {
149 return nestedBean;
150 }
151 public String getAPropertyName()
152 {
153 return aPropertyName;
154 }
155 public void setAPropertyName(String propertyName)
156 {
157 aPropertyName = propertyName;
158 }
159 }
160
161 private static final class NestedBean
162 {
163 private boolean booleanProperty = false;
164 private Integer integerProperty = new Integer(10);
165 private String stringProperty = "NestedBean";
166 private int intProperty = 54;
167 private long longProperty = 222222;
168 private byte byteProperty = 02;
169 private NestedNestedBean nestedNestedBean = new NestedNestedBean();
170 private Collection emptyCollectionProperty = new ArrayList();
171 private Collection nonEmptyCollectionProperty = new ArrayList();
172 public boolean isBooleanProperty()
173 {
174 return booleanProperty;
175 }
176 protected void setBooleanProperty(boolean booleanProperty)
177 {
178 this.booleanProperty = booleanProperty;
179 }
180 protected byte getByteProperty()
181 {
182 return byteProperty;
183 }
184 protected void setByteProperty(byte byteProperty)
185 {
186 this.byteProperty = byteProperty;
187 }
188 protected Integer getIntegerProperty()
189 {
190 return integerProperty;
191 }
192 protected void setIntegerProperty(Integer integerProperty)
193 {
194 this.integerProperty = integerProperty;
195 }
196 protected int getIntProperty()
197 {
198 return intProperty;
199 }
200 protected void setIntProperty(int intProperty)
201 {
202 this.intProperty = intProperty;
203 }
204 public long getLongProperty()
205 {
206 return longProperty;
207 }
208 public void setLongProperty(long longProperty)
209 {
210 this.longProperty = longProperty;
211 }
212 public String getStringProperty()
213 {
214 return stringProperty;
215 }
216 public void setStringProperty(String stringProperty)
217 {
218 this.stringProperty = stringProperty;
219 }
220 public NestedNestedBean getNestedNestedBean()
221 {
222 return nestedNestedBean;
223 }
224 public Collection getEmptyCollectionProperty()
225 {
226 return emptyCollectionProperty;
227 }
228 public Collection getNonEmptyCollectionProperty()
229 {
230 this.nonEmptyCollectionProperty.add("A String");
231 return nonEmptyCollectionProperty;
232 }
233 }
234
235 private static final class NestedNestedBean
236 {
237 private int intProperty = 5;
238 public int getIntProperty()
239 {
240 return this.intProperty;
241 }
242 public void setIntProperty(int intProperty)
243 {
244 this.intProperty = intProperty;
245 }
246 }
247 }