View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.el.convert;
20  
21  import static org.apache.myfaces.Assert.assertException;
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   * Tests for {@link ValueExpressionToValueBinding}. TODO: tests for StateHolder implementation
42   * 
43   * @author Mathias Broekelmann (latest modification by $Author: slessard $)
44   * @version $Revision: 690051 $ $Date: 2008-08-28 18:47:56 -0500 (Thu, 28 Aug 2008) $
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       * Test method for
87       * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#getType(javax.faces.context.FacesContext)}.
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       * Test method for
100      * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#getType(javax.faces.context.FacesContext)}.
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      * Test method for
132      * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#getValue(javax.faces.context.FacesContext)}.
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      * Test method for
146      * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#getValue(javax.faces.context.FacesContext)}.
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      * Test method for
178      * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#isReadOnly(javax.faces.context.FacesContext)}.
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      * Test method for
197      * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#isReadOnly(javax.faces.context.FacesContext)}.
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      * Test method for
229      * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#setValue(javax.faces.context.FacesContext, java.lang.Object)}.
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      * Test method for
243      * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#setValue(javax.faces.context.FacesContext, java.lang.Object)}.
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      * Test method for
277      * {@link org.apache.myfaces.el.convert.ValueExpressionToValueBinding#ValueExpressionToValueBinding(javax.el.ValueExpression)}.
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      * Test method for {@link javax.faces.el.ValueBinding#getExpressionString()}.
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 }