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 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   * @author Mathias Broekelmann (latest modification by $Author: skitching $)
44   * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Sun, 13 Jul 2008) $
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       * Test method for {@link javax.faces.component._MethodBindingToMethodExpression#_MethodBindingToMethodExpression()}.
71       */
72      public void test_MethodBindingToMethodExpression()
73      {
74          _MethodBindingToMethodExpression testimpl = new _MethodBindingToMethodExpression();
75          assertNull(testimpl.getMethodBinding());
76      }
77  
78      /**
79       * Test method for
80       * {@link javax.faces.component._MethodBindingToMethodExpression#_MethodBindingToMethodExpression(javax.faces.el.MethodBinding)}.
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       * Test method for {@link javax.faces.component._MethodBindingToMethodExpression#isLiteralText()}.
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      * Test method for {@link javax.faces.component._MethodBindingToMethodExpression#getMethodInfo(javax.el.ELContext)}.
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         // assertNull(methodInfo.getName());
125         assertEquals(expectedReturnType, methodInfo.getReturnType());
126         // assertNull(methodInfo.getParamTypes());
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      * Test method for
152      * {@link javax.faces.component._MethodBindingToMethodExpression#invoke(javax.el.ELContext, java.lang.Object[])}.
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      * Test method for {@link javax.faces.component._MethodBindingToMethodExpression#getExpressionString()}.
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      * Test method for
199      * {@link javax.faces.component._MethodBindingToMethodExpression#restoreState(javax.faces.context.FacesContext, java.lang.Object)}.
200      * 
201      * @throws Exception
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 }