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