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 static org.easymock.EasyMock.*;
22
23 import java.util.Date;
24
25 import javax.el.ELException;
26 import javax.el.ExpressionFactory;
27 import javax.el.MethodInfo;
28 import javax.el.MethodNotFoundException;
29 import javax.faces.application.Application;
30 import javax.faces.context.FacesContext;
31 import javax.faces.el.MethodBinding;
32
33 import junit.framework.TestCase;
34
35 import org.apache.myfaces.Assert;
36 import org.apache.myfaces.TestRunner;
37 import org.apache.shale.test.el.MockELContext;
38 import org.apache.shale.test.mock.MockFacesContext12;
39 import org.easymock.classextension.EasyMock;
40 import org.easymock.classextension.IMocksControl;
41
42
43
44
45
46 @SuppressWarnings("deprecation")
47 public class _MethodBindingToMethodExpressionTest extends TestCase
48 {
49
50 private IMocksControl _mocksControl;
51 private MethodBinding _methodBinding;
52 private MockELContext _elContext;
53 private MockFacesContext12 _facesContext;
54 private Application _application;
55 private ExpressionFactory _expressionFactory;
56
57 protected void setUp() throws Exception
58 {
59 _mocksControl = EasyMock.createControl();
60 _facesContext = new MockFacesContext12();
61 _application = _mocksControl.createMock(Application.class);
62 _facesContext.setApplication(_application);
63 _expressionFactory = _mocksControl.createMock(ExpressionFactory.class);
64 _elContext = new MockELContext();
65 _elContext.putContext(FacesContext.class, _facesContext);
66 _methodBinding = _mocksControl.createMock(MethodBinding.class);
67 }
68
69
70
71
72 public void test_MethodBindingToMethodExpression()
73 {
74 _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression();
75 assertNull(testimpl.getMethodBinding());
76 }
77
78
79
80
81
82 public void test_MethodBindingToMethodExpressionMethodBinding()
83 {
84 Assert.assertException(IllegalArgumentException.class, new TestRunner()
85 {
86 public void run() throws Throwable
87 {
88 new _MethodBindingToMethodExpression(null);
89 }
90 });
91 _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression(_methodBinding);
92 assertEquals(_methodBinding, testimpl.getMethodBinding());
93 }
94
95
96
97
98 public void testIsLiteralText()
99 {
100 assertIsLiteralText(true, "xxx");
101 assertIsLiteralText(false, "#{xxx}");
102 }
103
104 private void assertIsLiteralText(boolean expected, String expressionString)
105 {
106 _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression(_methodBinding);
107 expect(_methodBinding.getExpressionString()).andReturn(expressionString);
108 _mocksControl.replay();
109 assertEquals(expected, testimpl.isLiteralText());
110 _mocksControl.reset();
111 }
112
113
114
115
116 public void testGetMethodInfoELContext()
117 {
118 _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression(_methodBinding);
119 Class expectedReturnType = Date.class;
120 expect(_methodBinding.getType(same(_facesContext))).andReturn(expectedReturnType);
121 _mocksControl.replay();
122 MethodInfo methodInfo = testimpl.getMethodInfo(_elContext);
123 assertNotNull(methodInfo);
124
125 assertEquals(expectedReturnType, methodInfo.getReturnType());
126
127 _mocksControl.verify();
128 _mocksControl.reset();
129
130 assertGetMethodInfoException(MethodNotFoundException.class, new javax.faces.el.MethodNotFoundException());
131 assertGetMethodInfoException(ELException.class, new javax.faces.el.EvaluationException());
132 }
133
134 private void assertGetMethodInfoException(Class<? extends Throwable> expected, final Throwable firedFromMBGetType)
135 {
136 final _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression(_methodBinding);
137 expect(_methodBinding.getType(same(_facesContext))).andThrow(firedFromMBGetType);
138 _mocksControl.replay();
139 Assert.assertException(expected, new TestRunner()
140 {
141 public void run() throws Throwable
142 {
143 testimpl.getMethodInfo(_elContext);
144 }
145 });
146 _mocksControl.verify();
147 _mocksControl.reset();
148 }
149
150
151
152
153
154 public void testInvoke()
155 {
156 _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression(_methodBinding);
157 Object[] testParams = new Object[] { "test" };
158 Object expectedResult = new StringBuffer();
159 expect(_methodBinding.invoke(same(_facesContext), same(testParams))).andReturn(expectedResult);
160 _mocksControl.replay();
161 assertEquals(expectedResult, testimpl.invoke(_elContext, testParams));
162 _mocksControl.verify();
163 _mocksControl.reset();
164
165 assertInvokeException(MethodNotFoundException.class, new javax.faces.el.MethodNotFoundException());
166 assertInvokeException(ELException.class, new javax.faces.el.EvaluationException());
167 }
168
169 private void assertInvokeException(Class<? extends Throwable> expected, final Throwable firedFromMBInvoke)
170 {
171 final _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression(_methodBinding);
172 expect(_methodBinding.invoke(same(_facesContext), (Object[]) isNull())).andThrow(firedFromMBInvoke);
173 _mocksControl.replay();
174 Assert.assertException(expected, new TestRunner()
175 {
176 public void run() throws Throwable
177 {
178 testimpl.invoke(_elContext, null);
179 }
180 });
181 _mocksControl.verify();
182 _mocksControl.reset();
183 }
184
185
186
187
188 public void testGetExpressionString()
189 {
190 _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression(_methodBinding);
191 expect(_methodBinding.getExpressionString()).andReturn("xxx");
192 _mocksControl.replay();
193 assertEquals("xxx", testimpl.getExpressionString());
194 _mocksControl.verify();
195 }
196
197
198
199
200
201
202
203 public void testStateHolder() throws Exception
204 {
205 _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression(_methodBinding);
206 assertFalse(testimpl.isTransient());
207 testimpl.setTransient(true);
208 assertTrue(testimpl.isTransient());
209 assertNull(testimpl.saveState(_facesContext));
210 }
211 }