1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package javax.faces.component;
20
21 import javax.el.ELContext;
22 import javax.el.ValueExpression;
23 import javax.faces.el.ValueBinding;
24
25 import org.apache.commons.beanutils.PropertyUtils;
26 import org.apache.shale.test.mock.MockFacesContext12;
27 import static org.easymock.EasyMock.*;
28 import org.easymock.classextension.EasyMock;
29 import org.easymock.classextension.IMocksControl;
30 import static org.testng.Assert.*;
31 import org.testng.annotations.AfterMethod;
32 import org.testng.annotations.BeforeMethod;
33 import org.testng.annotations.BeforeTest;
34 import org.testng.annotations.Test;
35
36
37
38
39
40 public abstract class AbstractUIComponentPropertyTest<T extends Object>
41 {
42 private final String _property;
43 private final T _defaultValue;
44 private final T[] _testValues;
45
46 private IMocksControl _mocksControl;
47 private MockFacesContext12 _facesContext;
48 private ValueBinding _valueBinding;
49 private ValueExpression _valueExpression;
50 private ELContext _elContext;
51 private UIComponent _component;
52
53 public AbstractUIComponentPropertyTest(String property, T defaultValue, T... testValues)
54 {
55 _property = property;
56 _defaultValue = defaultValue;
57 _testValues = testValues;
58 }
59
60 @BeforeMethod
61 protected void setUp() throws Exception
62 {
63 _mocksControl = EasyMock.createControl();
64 _facesContext = new MockFacesContext12();
65 _elContext = _mocksControl.createMock(ELContext.class);
66 _facesContext.setELContext(_elContext);
67 _valueBinding = _mocksControl.createMock(ValueBinding.class);
68 _valueExpression = _mocksControl.createMock(ValueExpression.class);
69 _component = createComponent();
70 }
71
72 protected IMocksControl getMocksControl()
73 {
74 return _mocksControl;
75 }
76
77 protected abstract UIComponent createComponent();
78
79 @Test
80 public void testDefaultValue() throws Exception
81 {
82 assertEquals(_defaultValue, PropertyUtils.getProperty(_component, _property));
83 }
84
85 @Test
86 public void testExplicitValue() throws Exception
87 {
88 for (T testValue : _testValues)
89 {
90 PropertyUtils.setProperty(_component, _property, testValue);
91 assertEquals(testValue, PropertyUtils.getProperty(_component, _property));
92 }
93 }
94
95 @Test
96 public void testExpressionWithLiteralTextValue() throws Exception
97 {
98 for (T testValue : _testValues)
99 {
100 expect(_valueExpression.isLiteralText()).andReturn(true);
101 expect(_valueExpression.getValue(eq(_facesContext.getELContext()))).andReturn(testValue);
102 _mocksControl.replay();
103 _component.setValueExpression(_property, _valueExpression);
104 assertEquals(testValue, PropertyUtils.getProperty(_component, _property));
105 _mocksControl.reset();
106 }
107 }
108
109 @Test
110 public void testExpressionValue() throws Exception
111 {
112 for (T testValue : _testValues)
113 {
114 expect(_valueExpression.isLiteralText()).andReturn(false);
115 _mocksControl.replay();
116 _component.setValueExpression(_property, _valueExpression);
117 _mocksControl.reset();
118 expect(_valueExpression.getValue(eq(_facesContext.getELContext()))).andReturn(testValue);
119 _mocksControl.replay();
120 assertEquals(testValue, PropertyUtils.getProperty(_component, _property));
121 _mocksControl.reset();
122 }
123 }
124 }