1   package net.sf.msplice.property.impl;
2   
3   import junit.framework.TestCase;
4   import net.sf.msplice.property.PropertyGetException;
5   
6   public class BeanutilsPropertyGetterTest extends TestCase {
7   	
8   
9   	private BeanutilsPropertyGetter propertyGetter = null;
10  			
11  	private String propertyName = "stringProperty";
12  
13  	private String nestedPropertyName = "nestedProperty";
14  
15  	private String nestedStringProperty = "nestedString";
16  
17  	private String invalidPropertyName = "anInvalidProperty";
18  
19  	private String value = "a-value";
20  
21  	private String nestedValue = "a-nested-value";
22  
23  	private TestBean rootObject = null;
24  
25  	private TestNestedProperty nestedProperty = null;
26  
27  	@Override
28  	protected void setUp() throws Exception {
29  		super.setUp();
30  
31  		propertyGetter = new BeanutilsPropertyGetter();
32  		rootObject = new TestBean();
33  
34  		nestedProperty = new TestNestedProperty();
35  
36  	}
37  
38  	public void testGetProperty() throws Exception {
39  
40  		rootObject.setStringProperty(value);
41  		
42  		Object result = propertyGetter.getProperty(propertyName, rootObject);
43  
44  		assertEquals("Checking the newly set property", value, result);
45  	}
46  
47  	public void testNestedProperty() throws Exception {
48  
49  		String nestedPropertyPath = nestedPropertyName + "."
50  				+ nestedStringProperty;
51  
52  		nestedProperty.setNestedString(nestedValue);
53  		rootObject.setNestedProperty(nestedProperty);
54  		
55  		Object result = propertyGetter.getProperty(nestedPropertyPath, rootObject);
56  
57  		assertEquals("Checking nested property state", nestedValue, result);
58  	}
59  	
60  	public void testNestedNullProperty() throws Exception {
61  
62  		String nestedPropertyPath = nestedPropertyName + "."
63  				+ nestedStringProperty;
64  		try {
65  			Object result = propertyGetter.getProperty(nestedPropertyPath, rootObject);
66  			fail("Must throw an exception on null nested property element");
67  		} catch (PropertyGetException e) {
68  			// exception thrown, everything is ok
69  			// print stack trace to see the output
70  			e.printStackTrace();
71  		}
72  	}
73  
74  	public void testGetNullProperty() throws Exception {
75  
76  		rootObject.setStringProperty(null);
77  		
78  		Object result = propertyGetter.getProperty(propertyName, rootObject);
79  
80  		assertEquals("Checking the newly set property", null, result);
81  	}
82  
83  	public void testGetNestedNullProperty() throws Exception {
84  
85  		String nestedPropertyPath = nestedPropertyName + "."
86  		+ nestedStringProperty;
87  
88  		nestedProperty.setNestedString(null);
89  		rootObject.setNestedProperty(nestedProperty);
90  		
91  		Object result = propertyGetter.getProperty(nestedPropertyPath, rootObject);
92  
93  		assertEquals("Checking the newly set property", null, result);
94  	}
95  
96  	public void testEmptyPropertyAndNullRoot() throws Exception {
97  
98  		try {
99  			propertyGetter.getProperty("", null);
100 			fail("Must throw an exception on empty property and null root");
101 		} catch (PropertyGetException e) {
102 			// exception thrown, everything is ok
103 			// print stack trace to see the output
104 			e.printStackTrace();
105 		}
106 		
107 	}
108 	
109 	public void testSetInexistentProperty() throws Exception {
110 
111 		try {
112 			propertyGetter.getProperty(invalidPropertyName, rootObject);
113 			fail("Must throw an exception on inexsitent property");
114 		} catch (PropertyGetException e) {
115 			// exception thrown, everything is ok
116 			// print stack trace to see the output
117 			e.printStackTrace();
118 		}
119 	}
120 
121 	public void testNullRootObject() throws Exception {
122 
123 		try {
124 			propertyGetter.getProperty(propertyName, null);
125 			fail("Must throw an exception on null root element");
126 		} catch (PropertyGetException e) {
127 			// exception thrown, everything is ok
128 			// print stack trace to see the output
129 			e.printStackTrace();
130 		}
131 	}
132 
133 
134 }