001package org.andromda.core.common;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import junit.framework.TestCase;
006
007/**
008 * JUnit tests for {@link org.andromda.core.common.Introspector}
009 *
010 * @author Chad Brandon
011 */
012public class IntrospectorTest
013    extends TestCase
014{
015    /**
016     *
017     */
018    public void testGetProperty()
019    {
020        TestBean testBean = new TestBean();
021        assertEquals(testBean.getStringProperty(), Introspector.instance().getProperty(testBean, "stringProperty"));
022        assertEquals(testBean.getNestedBean().getStringProperty(), Introspector.instance().getProperty(testBean, "nestedBean.stringProperty"));
023        try
024        {
025            Introspector.instance().getProperty(testBean, "intProperty");
026            fail();
027        }
028        catch (IntrospectorException exception) {}
029    }
030
031    /**
032     *
033     */
034    public void testSetProperty()
035    {
036        TestBean testBean = new TestBean();
037        Introspector.instance().setProperty(testBean, "stringProperty", "A New String Value");
038        assertEquals(testBean.getStringProperty(), "A New String Value");
039        assertTrue(testBean.getBooleanProperty());
040        Introspector.instance().setProperty(testBean, "booleanProperty", "false");
041        assertFalse(testBean.getBooleanProperty());
042        assertEquals(testBean.getNestedBean().getLongProperty(), 222222);
043        Introspector.instance().setProperty(testBean, "nestedBean.longProperty", "9999");
044        assertEquals(testBean.getNestedBean().getLongProperty(), 9999);
045        assertEquals(testBean.getNestedBean().getNestedNestedBean().getIntProperty(), 5);
046        Introspector.instance().setProperty(testBean, "nestedBean.nestedNestedBean.intProperty", "10");
047        assertEquals(testBean.getNestedBean().getNestedNestedBean().getIntProperty(), 10);
048        Introspector.instance().setProperty(testBean, "aPropertyName", "SomeValue");
049        assertEquals(testBean.getAPropertyName(), "SomeValue");
050        try
051        {
052            Introspector.instance().getProperty(testBean, "intProperty");
053            fail();
054        }
055        catch (IntrospectorException exception) {}
056    }
057
058    /**
059     *
060     */
061    public void testIsReadable()
062    {
063        TestBean testBean = new TestBean();
064        assertTrue(Introspector.instance().isReadable(testBean, "stringProperty"));
065        assertFalse(Introspector.instance().isWritable(testBean, "byteProperty"));
066        assertTrue(Introspector.instance().isReadable(testBean, "nestedBean.stringProperty"));
067        assertTrue(Introspector.instance().isWritable(testBean, "nestedBean.stringProperty"));
068        assertFalse(Introspector.instance().isReadable(testBean, "nestedBean.intProperty"));
069        assertFalse(Introspector.instance().isWritable(testBean, "nestedBean.intProperty"));
070    }
071
072    /**
073     *
074     */
075    public void testConstainsValidProperty()
076    {
077        TestBean testBean = new TestBean();
078        assertTrue(Introspector.instance().containsValidProperty(testBean, "booleanProperty", null));
079        assertTrue(Introspector.instance().containsValidProperty(testBean, "nestedBean.booleanProperty", "false"));
080        assertFalse(Introspector.instance().containsValidProperty(testBean, "nestedBean.emptyCollectionProperty", null));
081        assertTrue(Introspector.instance().containsValidProperty(testBean, "nestedBean.nonEmptyCollectionProperty", null));
082        assertTrue(Introspector.instance().containsValidProperty(testBean, "aBCProperty", "true"));
083    }
084
085    private static final class TestBean
086    {
087        private boolean abcProperty = true;
088        private boolean booleanProperty = true;
089        private Integer integerProperty = 1;
090        private String stringProperty = "TestBean";
091        private int intProperty = 5;
092        private long longProperty = 1111111111;
093        private byte byteProperty = 1;
094        private NestedBean nestedBean = new NestedBean();
095        private String aPropertyName;
096        @SuppressWarnings("unused")
097        public boolean isABCProperty()
098        {
099            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}