001: package org.andromda.core.common;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005:
006: import junit.framework.TestCase;
007:
008: /**
009: * JUnit tests for {@link org.andromda.common.Introspector}
010: *
011: * @author Chad Brandon
012: */
013: public class IntrospectorTest extends TestCase {
014:
015: public void testGetProperty() {
016: TestBean testBean = new TestBean();
017: assertEquals(testBean.getStringProperty(), Introspector
018: .instance().getProperty(testBean, "stringProperty"));
019: assertEquals(testBean.getNestedBean().getStringProperty(),
020: Introspector.instance().getProperty(testBean,
021: "nestedBean.stringProperty"));
022: try {
023: Introspector.instance()
024: .getProperty(testBean, "intProperty");
025: fail();
026: } catch (IntrospectorException exception) {
027: }
028: }
029:
030: public void testSetProperty() {
031: TestBean testBean = new TestBean();
032: Introspector.instance().setProperty(testBean, "stringProperty",
033: "A New String Value");
034: assertEquals(testBean.getStringProperty(), "A New String Value");
035: assertTrue(testBean.getBooleanProperty());
036: Introspector.instance().setProperty(testBean,
037: "booleanProperty", "false");
038: assertFalse(testBean.getBooleanProperty());
039: assertEquals(testBean.getNestedBean().getLongProperty(), 222222);
040: Introspector.instance().setProperty(testBean,
041: "nestedBean.longProperty", "9999");
042: assertEquals(testBean.getNestedBean().getLongProperty(), 9999);
043: assertEquals(testBean.getNestedBean().getNestedNestedBean()
044: .getIntProperty(), 5);
045: Introspector.instance().setProperty(testBean,
046: "nestedBean.nestedNestedBean.intProperty", "10");
047: assertEquals(testBean.getNestedBean().getNestedNestedBean()
048: .getIntProperty(), 10);
049: Introspector.instance().setProperty(testBean, "aPropertyName",
050: "SomeValue");
051: assertEquals(testBean.getAPropertyName(), "SomeValue");
052: try {
053: Introspector.instance()
054: .getProperty(testBean, "intProperty");
055: fail();
056: } catch (IntrospectorException exception) {
057: }
058: }
059:
060: public void testIsReadable() {
061: TestBean testBean = new TestBean();
062: assertTrue(Introspector.instance().isReadable(testBean,
063: "stringProperty"));
064: assertFalse(Introspector.instance().isWritable(testBean,
065: "byteProperty"));
066: assertTrue(Introspector.instance().isReadable(testBean,
067: "nestedBean.stringProperty"));
068: assertTrue(Introspector.instance().isWritable(testBean,
069: "nestedBean.stringProperty"));
070: assertFalse(Introspector.instance().isReadable(testBean,
071: "nestedBean.intProperty"));
072: assertFalse(Introspector.instance().isWritable(testBean,
073: "nestedBean.intProperty"));
074: }
075:
076: public void testConstainsValidProperty() {
077: TestBean testBean = new TestBean();
078: assertTrue(Introspector.instance().containsValidProperty(
079: testBean, "booleanProperty", null));
080: assertTrue(Introspector.instance().containsValidProperty(
081: testBean, "nestedBean.booleanProperty", "false"));
082: assertFalse(Introspector.instance().containsValidProperty(
083: testBean, "nestedBean.emptyCollectionProperty", null));
084: assertTrue(Introspector.instance()
085: .containsValidProperty(testBean,
086: "nestedBean.nonEmptyCollectionProperty", null));
087: assertTrue(Introspector.instance().containsValidProperty(
088: testBean, "aBCProperty", "true"));
089: }
090:
091: private static final class TestBean {
092: private boolean abcProperty = true;
093: private boolean booleanProperty = true;
094: private Integer integerProperty = new Integer(1);
095: private String stringProperty = "TestBean";
096: private int intProperty = 5;
097: private long longProperty = 1111111111;
098: private byte byteProperty = 01;
099: private NestedBean nestedBean = new NestedBean();
100: private String aPropertyName;
101:
102: public boolean isABCProperty() {
103: return abcProperty;
104: }
105:
106: public void setABCProperty(boolean abcProperty) {
107: this .abcProperty = abcProperty;
108: }
109:
110: public boolean isBooleanProperty() {
111: return booleanProperty;
112: }
113:
114: public void setBooleanProperty(boolean booleanProperty) {
115: this .booleanProperty = booleanProperty;
116: }
117:
118: protected boolean getBooleanProperty() {
119: return this .booleanProperty;
120: }
121:
122: public byte getByteProperty() {
123: return byteProperty;
124: }
125:
126: protected void setByteProperty(byte byteProperty) {
127: this .byteProperty = byteProperty;
128: }
129:
130: protected Integer getIntegerProperty() {
131: return integerProperty;
132: }
133:
134: protected void setIntegerProperty(Integer integerProperty) {
135: this .integerProperty = integerProperty;
136: }
137:
138: protected int getIntProperty() {
139: return intProperty;
140: }
141:
142: protected void setIntProperty(int intProperty) {
143: this .intProperty = intProperty;
144: }
145:
146: protected long getLongProperty() {
147: return longProperty;
148: }
149:
150: protected void setLongProperty(long longProperty) {
151: this .longProperty = longProperty;
152: }
153:
154: public String getStringProperty() {
155: return stringProperty;
156: }
157:
158: public void setStringProperty(String stringProperty) {
159: this .stringProperty = stringProperty;
160: }
161:
162: public NestedBean getNestedBean() {
163: return nestedBean;
164: }
165:
166: public String getAPropertyName() {
167: return aPropertyName;
168: }
169:
170: public void setAPropertyName(String propertyName) {
171: aPropertyName = propertyName;
172: }
173: }
174:
175: private static final class NestedBean {
176: private boolean booleanProperty = false;
177: private Integer integerProperty = new Integer(10);
178: private String stringProperty = "NestedBean";
179: private int intProperty = 54;
180: private long longProperty = 222222;
181: private byte byteProperty = 02;
182: private NestedNestedBean nestedNestedBean = new NestedNestedBean();
183: private Collection emptyCollectionProperty = new ArrayList();
184: private Collection nonEmptyCollectionProperty = new ArrayList();
185:
186: public boolean isBooleanProperty() {
187: return booleanProperty;
188: }
189:
190: protected void setBooleanProperty(boolean booleanProperty) {
191: this .booleanProperty = booleanProperty;
192: }
193:
194: protected byte getByteProperty() {
195: return byteProperty;
196: }
197:
198: protected void setByteProperty(byte byteProperty) {
199: this .byteProperty = byteProperty;
200: }
201:
202: protected Integer getIntegerProperty() {
203: return integerProperty;
204: }
205:
206: protected void setIntegerProperty(Integer integerProperty) {
207: this .integerProperty = integerProperty;
208: }
209:
210: protected int getIntProperty() {
211: return intProperty;
212: }
213:
214: protected void setIntProperty(int intProperty) {
215: this .intProperty = intProperty;
216: }
217:
218: public long getLongProperty() {
219: return longProperty;
220: }
221:
222: public void setLongProperty(long longProperty) {
223: this .longProperty = longProperty;
224: }
225:
226: public String getStringProperty() {
227: return stringProperty;
228: }
229:
230: public void setStringProperty(String stringProperty) {
231: this .stringProperty = stringProperty;
232: }
233:
234: public NestedNestedBean getNestedNestedBean() {
235: return nestedNestedBean;
236: }
237:
238: public Collection getEmptyCollectionProperty() {
239: return emptyCollectionProperty;
240: }
241:
242: public Collection getNonEmptyCollectionProperty() {
243: this .nonEmptyCollectionProperty.add("A String");
244: return nonEmptyCollectionProperty;
245: }
246: }
247:
248: private static final class NestedNestedBean {
249: private int intProperty = 5;
250:
251: public int getIntProperty() {
252: return this .intProperty;
253: }
254:
255: public void setIntProperty(int intProperty) {
256: this.intProperty = intProperty;
257: }
258: }
259: }
|