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.apache.myfaces.Assert.*;
22 import static org.easymock.EasyMock.*;
23
24 import java.util.Date;
25
26 import javax.el.ELContext;
27 import javax.el.ELException;
28 import javax.el.PropertyNotFoundException;
29 import javax.el.ValueExpression;
30 import javax.faces.context.FacesContext;
31 import javax.faces.el.EvaluationException;
32
33 import junit.framework.TestCase;
34
35 import org.apache.myfaces.TestRunner;
36 import org.apache.myfaces.mock.ExceptionMockRunner;
37 import org.easymock.classextension.EasyMock;
38 import org.easymock.classextension.IMocksControl;
39
40
41
42
43
44
45
46 @SuppressWarnings("deprecation")
47 public class _ValueExpressionToValueBindingTest extends TestCase
48 {
49 private _ValueExpressionToValueBinding testimpl;
50 private ValueExpression expression;
51 private IMocksControl mockControl;
52 private FacesContext facesContext;
53 private ELContext elContext;
54
55 @Override
56 protected void setUp() throws Exception
57 {
58 mockControl = EasyMock.createControl();
59 expression = mockControl.createMock(ValueExpression.class);
60 facesContext = mockControl.createMock(FacesContext.class);
61 elContext = mockControl.createMock(ELContext.class);
62 testimpl = new _ValueExpressionToValueBinding(expression);
63 }
64
65 public void testHashCode() throws Exception
66 {
67 assertEquals(testimpl.hashCode(), testimpl.hashCode());
68 _ValueExpressionToValueBinding other = new _ValueExpressionToValueBinding(expression);
69 assertEquals(testimpl.hashCode(), other.hashCode());
70 other.setTransient(true);
71 assertFalse(testimpl.hashCode() == other.hashCode());
72 assertFalse(testimpl.hashCode() == mockControl.createMock(ValueExpression.class).hashCode());
73 }
74
75 public void testEquals() throws Exception
76 {
77 assertEquals(testimpl, testimpl);
78 _ValueExpressionToValueBinding other = new _ValueExpressionToValueBinding(expression);
79 assertEquals(testimpl, other);
80 other.setTransient(true);
81 assertFalse(testimpl.equals(other));
82 assertFalse(testimpl.equals(mockControl.createMock(ValueExpression.class)));
83 }
84
85
86
87
88
89 public void testGetType()
90 {
91 expect(facesContext.getELContext()).andReturn(elContext);
92 expect(expression.getType(eq(elContext))).andStubReturn(Date.class);
93 mockControl.replay();
94 assertEquals(Date.class, testimpl.getType(facesContext));
95 mockControl.verify();
96 }
97
98
99
100
101
102 public void testGetTypeExceptions() throws Exception
103 {
104 class GetTypeExceptionMockRunner extends ExceptionMockRunner
105 {
106 GetTypeExceptionMockRunner(Throwable exception)
107 {
108 super(mockControl, exception);
109 }
110
111 @Override
112 protected void setUp(IMocksControl mockControl, Throwable exceptionToThrow)
113 {
114 expect(facesContext.getELContext()).andReturn(elContext);
115 expect(expression.getType(eq(elContext))).andThrow(exceptionToThrow);
116 }
117
118 @Override
119 protected void run(IMocksControl mockControl) throws Exception
120 {
121 testimpl.getType(facesContext);
122 }
123 }
124 assertException(EvaluationException.class, new GetTypeExceptionMockRunner(new ELException()));
125 mockControl.reset();
126 assertException(javax.faces.el.PropertyNotFoundException.class, new GetTypeExceptionMockRunner(
127 new PropertyNotFoundException()));
128 }
129
130
131
132
133
134 public void testGetValue()
135 {
136 expect(facesContext.getELContext()).andReturn(elContext);
137 Object expectedValue = new StringBuffer();
138 expect(expression.getValue(eq(elContext))).andReturn(expectedValue);
139 mockControl.replay();
140 assertEquals(expectedValue, testimpl.getValue(facesContext));
141 mockControl.verify();
142 }
143
144
145
146
147
148 public void testGetValueExceptions()
149 {
150 class GetValueExceptionMockRunner extends ExceptionMockRunner
151 {
152 GetValueExceptionMockRunner(Throwable exception)
153 {
154 super(mockControl, exception);
155 }
156
157 @Override
158 protected void setUp(IMocksControl mockControl, Throwable exceptionToThrow)
159 {
160 expect(facesContext.getELContext()).andReturn(elContext);
161 expect(expression.getValue(eq(elContext))).andThrow(exceptionToThrow);
162 }
163
164 @Override
165 protected void run(IMocksControl mockControl) throws Exception
166 {
167 testimpl.getValue(facesContext);
168 }
169 }
170 assertException(EvaluationException.class, new GetValueExceptionMockRunner(new ELException()));
171 mockControl.reset();
172 assertException(javax.faces.el.PropertyNotFoundException.class, new GetValueExceptionMockRunner(
173 new PropertyNotFoundException()));
174 }
175
176
177
178
179
180 public void testIsReadOnly()
181 {
182 expect(facesContext.getELContext()).andReturn(elContext);
183 expect(expression.isReadOnly(eq(elContext))).andReturn(true);
184 mockControl.replay();
185 assertEquals(true, testimpl.isReadOnly(facesContext));
186 mockControl.verify();
187 mockControl.reset();
188 expect(facesContext.getELContext()).andReturn(elContext);
189 expect(expression.isReadOnly(eq(elContext))).andReturn(false);
190 mockControl.replay();
191 assertEquals(false, testimpl.isReadOnly(facesContext));
192 mockControl.verify();
193 }
194
195
196
197
198
199 public void testIsReadOnlyExceptions()
200 {
201 class IsReadOnlyExceptionMockRunner extends ExceptionMockRunner
202 {
203 IsReadOnlyExceptionMockRunner(Throwable exception)
204 {
205 super(mockControl, exception);
206 }
207
208 @Override
209 protected void setUp(IMocksControl mockControl, Throwable exceptionToThrow)
210 {
211 expect(facesContext.getELContext()).andReturn(elContext);
212 expect(expression.isReadOnly(eq(elContext))).andThrow(exceptionToThrow);
213 }
214
215 @Override
216 protected void run(IMocksControl mockControl) throws Exception
217 {
218 testimpl.isReadOnly(facesContext);
219 }
220 }
221 assertException(EvaluationException.class, new IsReadOnlyExceptionMockRunner(new ELException()));
222 mockControl.reset();
223 assertException(javax.faces.el.PropertyNotFoundException.class, new IsReadOnlyExceptionMockRunner(
224 new PropertyNotFoundException()));
225 }
226
227
228
229
230
231 public void testSetValue()
232 {
233 expect(facesContext.getELContext()).andReturn(elContext);
234 Object valueToSet = new StringBuffer();
235 expression.setValue(eq(elContext), eq(valueToSet));
236 mockControl.replay();
237 testimpl.setValue(facesContext, valueToSet);
238 mockControl.verify();
239 }
240
241
242
243
244
245 public void testSetValueExceptions()
246 {
247 final Object valueToSet = new StringBuffer();
248 class SetValueExceptionMockRunner extends ExceptionMockRunner
249 {
250 SetValueExceptionMockRunner(Throwable exception)
251 {
252 super(mockControl, exception);
253 }
254
255 @Override
256 protected void setUp(IMocksControl mockControl, Throwable exceptionToThrow)
257 {
258 expect(facesContext.getELContext()).andReturn(elContext);
259 expression.setValue(eq(elContext), eq(valueToSet));
260 expectLastCall().andThrow(exceptionToThrow);
261 }
262
263 @Override
264 protected void run(IMocksControl mockControl) throws Exception
265 {
266 testimpl.setValue(facesContext, valueToSet);
267 }
268 }
269 assertException(EvaluationException.class, new SetValueExceptionMockRunner(new ELException()));
270 mockControl.reset();
271 assertException(javax.faces.el.PropertyNotFoundException.class, new SetValueExceptionMockRunner(
272 new PropertyNotFoundException()));
273 }
274
275
276
277
278
279 public void testValueExpressionToValueBindingValueExpression()
280 {
281 assertException(IllegalArgumentException.class, new TestRunner()
282 {
283 public void run() throws Throwable
284 {
285 new _ValueExpressionToValueBinding(null);
286 }
287 });
288 }
289
290
291
292
293 public void testGetExpressionString()
294 {
295 expect(expression.getExpressionString()).andReturn("expressionString");
296 mockControl.replay();
297 assertEquals("expressionString", testimpl.getExpressionString());
298 mockControl.verify();
299 }
300 }