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