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.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.myfaces.test.mock.MockFacesContext12;
37  import org.easymock.classextension.EasyMock;
38  import org.easymock.classextension.IMocksControl;
39  
40  /**
41   * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
42   * @version $Revision: 883907 $ $Date: 2009-11-24 17:37:53 -0500 (Tue, 24 Nov 2009) $
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       * Test method for {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#hashCode()}.
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       * Test method for {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#equals(java.lang.Object)}.
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       * Test method for {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#isLiteralText()}.
92       */
93      public void testIsLiteralText()
94      {
95          mockControl.replay();
96          assertFalse(testimpl.isLiteralText());
97          mockControl.verify();
98      }
99  
100     /**
101      * Test method for
102      * {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#ValueBindingToValueExpression()}.
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      * Test method for
122      * {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#ValueBindingToValueExpression(javax.faces.el.ValueBinding)}.
123      */
124     public void testValueBindingToValueExpressionValueBinding()
125     {
126         assertEquals(binding, testimpl.getValueBinding());
127     }
128 
129     /**
130      * Test method for
131      * {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#isReadOnly(javax.el.ELContext)}.
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      * Test method for {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#getValue(javax.el.ELContext)}.
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      * Test method for {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#getType(javax.el.ELContext)}.
163      */
164     public void testGetTypeELContext()
165     {
166         expect(elContext.getContext(eq(FacesContext.class))).andReturn(facesContext);
167         Class<Date> 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      * Test method for {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#getType(javax.el.ELContext)}.
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      * Test method for
207      * {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#setValue(javax.el.ELContext, java.lang.Object)}.
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      * Test method for
221      * {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#setValue(javax.el.ELContext, java.lang.Object)}.
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      * Test method for {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#getExpressionString()}.
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      * Test method for {@link org.apache.myfaces.el.convert.ValueBindingToValueExpression#getExpectedType()}.
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 }