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