2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.test.mock;
19  
20  import javax.faces.el.MethodBinding;
21  import javax.faces.el.ValueBinding;
22  
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  
26  import org.apache.shale.test.base.AbstractJsfTestCase;
27  
28  /***
29   * <p>Simple unit tests for Mock Objects that have behavior.</p>
30   */
31  
32  public class MockObjectsTestCase extends AbstractJsfTestCase {
33  
34  
35      // ------------------------------------------------------------ Constructors
36  
37  
38      // Construct a new instance of this test case.
39      public MockObjectsTestCase(String name) {
40          super(name);
41      }
42  
43  
44      // ---------------------------------------------------- Overall Test Methods
45  
46  
47      // Set up instance variables required by this test case.
48      protected void setUp() throws Exception {
49  
50          super.setUp();
51  
52          // Set up Servlet API Objects
53          servletContext.addInitParameter("appParamName", "appParamValue");
54          servletContext.setAttribute("appScopeName", "appScopeValue");
55          servletContext.setAttribute("sameKey", "sameKeyAppValue");
56          session.setAttribute("sesScopeName", "sesScopeValue");
57          session.setAttribute("sameKey", "sameKeySesValue");
58          request.setAttribute("reqScopeName", "reqScopeValue");
59          request.setAttribute("sameKey", "sameKeyReqValue");
60          request.setAttribute("test", new TestMockBean());
61  
62      }
63  
64  
65      // Return the tests included in this test case.
66      public static Test suite() {
67  
68          return (new TestSuite(MockObjectsTestCase.class));
69  
70      }
71  
72  
73      // Tear down instance variables required by this test case.
74      protected void tearDown() throws Exception {
75  
76  
77          super.tearDown();
78  
79      }
80  
81  
82      // ------------------------------------------------------ Instance Variables
83  
84  
85      // ------------------------------------------------- Individual Test Methods
86  
87  
88      public void testMethodBindingGetTypePositive() throws Exception {
89  
90          Class argsString[] = new Class[] { String.class };
91          Class argsNone[] = new Class[0];
92  
93          checkMethodBindingGetType("test.getCommand", argsNone, String.class);
94          checkMethodBindingGetType("test.setCommand", argsString,  null);
95          checkMethodBindingGetType("test.getInput", argsNone, String.class);
96          checkMethodBindingGetType("test.setInput", argsString, null);
97          checkMethodBindingGetType("test.getOutput", argsNone, String.class);
98          checkMethodBindingGetType("test.setOutput", argsString, null);
99          checkMethodBindingGetType("test.combine", argsNone, String.class);
100 
101     }
102 
103 
104     public void testMethodBindingInvokePositive() throws Exception {
105 
106         TestMockBean bean = (TestMockBean) request.getAttribute("test");
107         MethodBinding mb = null;
108         Class argsString[] = new Class[] { String.class };
109         Class argsNone[] = new Class[0];
110         assertEquals("::", bean.combine());
111 
112         mb = application.createMethodBinding("test.setCommand", argsString);
113         mb.invoke(facesContext, new String[] { "command" });
114         assertEquals("command", bean.getCommand());
115         mb = application.createMethodBinding("test.setInput", argsString);
116         mb.invoke(facesContext, new String[] { "input" });
117         assertEquals("input", bean.getInput());
118         mb = application.createMethodBinding("test.setOutput", argsString);
119         mb.invoke(facesContext, new String[] { "output" });
120         assertEquals("output", bean.getOutput());
121         mb = application.createMethodBinding("test.combine", null);
122         assertEquals("command:input:output", bean.combine());
123         assertEquals("command:input:output", mb.invoke(facesContext, null));
124 
125     }
126 
127 
128     // Positive tests for ValueBinding.getValue()
129     public void testValueBindingGetValuePositive() throws Exception {
130 
131         // Implicit search
132         checkValueBindingGetValue("appScopeName", "appScopeValue");
133         checkValueBindingGetValue("sesScopeName", "sesScopeValue");
134         checkValueBindingGetValue("reqScopeName", "reqScopeValue");
135         checkValueBindingGetValue("sameKey", "sameKeyReqValue"); // Req scope
136 
137         // Explicit scope search
138         checkValueBindingGetValue("applicationScope.appScopeName",
139                                   "appScopeValue");
140         checkValueBindingGetValue("applicationScope.sameKey",
141                                   "sameKeyAppValue");
142         checkValueBindingGetValue("sessionScope.sesScopeName",
143                                   "sesScopeValue");
144         checkValueBindingGetValue("sessionScope.sameKey",
145                                   "sameKeySesValue");
146         checkValueBindingGetValue("requestScope.reqScopeName",
147                                   "reqScopeValue");
148         checkValueBindingGetValue("requestScope.sameKey",
149                                   "sameKeyReqValue");
150 
151     }
152 
153 
154     // Positive tests for ValueBinding.putValue()
155     public void testValueBindingPutValuePositive() throws Exception {
156 
157         ValueBinding vb = null;
158 
159         // New top-level variable
160         assertNull(request.getAttribute("newSimpleName"));
161         assertNull(session.getAttribute("newSimpleName"));
162         assertNull(servletContext.getAttribute("newSimpleName"));
163         vb = application.createValueBinding("newSimpleName");
164         vb.setValue(facesContext, "newSimpleValue");
165         assertEquals("newSimpleValue", request.getAttribute("newSimpleName"));
166         assertNull(session.getAttribute("newSimpleName"));
167         assertNull(servletContext.getAttribute("newSimpleName"));
168 
169         // New request-scope variable
170         assertNull(request.getAttribute("newReqName"));
171         assertNull(session.getAttribute("newReqName"));
172         assertNull(servletContext.getAttribute("newReqName"));
173         vb = application.createValueBinding("requestScope.newReqName");
174         vb.setValue(facesContext, "newReqValue");
175         assertEquals("newReqValue", request.getAttribute("newReqName"));
176         assertNull(session.getAttribute("newReqName"));
177         assertNull(servletContext.getAttribute("newReqName"));
178 
179         // New session-scope variable
180         assertNull(request.getAttribute("newSesName"));
181         assertNull(session.getAttribute("newSesName"));
182         assertNull(servletContext.getAttribute("newSesName"));
183         vb = application.createValueBinding("sessionScope.newSesName");
184         vb.setValue(facesContext, "newSesValue");
185         assertNull(request.getAttribute("newSesName"));
186         assertEquals("newSesValue", session.getAttribute("newSesName"));
187         assertNull(servletContext.getAttribute("newSesName"));
188 
189         // New application-scope variable
190         assertNull(request.getAttribute("newAppName"));
191         assertNull(session.getAttribute("newAppName"));
192         assertNull(servletContext.getAttribute("newAppName"));
193         vb = application.createValueBinding("applicationScope.newAppName");
194         vb.setValue(facesContext, "newAppValue");
195         assertNull(request.getAttribute("newAppName"));
196         assertNull(session.getAttribute("newAppName"));
197         assertEquals("newAppValue", servletContext.getAttribute("newAppName"));
198 
199         // Old top-level variable (just created)
200         assertEquals("newSimpleValue", request.getAttribute("newSimpleName"));
201         assertNull(session.getAttribute("newSimpleName"));
202         assertNull(servletContext.getAttribute("newSimpleName"));
203         vb = application.createValueBinding("newSimpleName");
204         vb.setValue(facesContext, "newerSimpleValue");
205         assertEquals("newerSimpleValue", request.getAttribute("newSimpleName"));
206         assertNull(session.getAttribute("newSimpleName"));
207         assertNull(servletContext.getAttribute("newSimpleName"));
208 
209         // Old hierarchically found variable
210         assertEquals("sameKeyAppValue", servletContext.getAttribute("sameKey"));
211         assertEquals("sameKeySesValue", session.getAttribute("sameKey"));
212         assertEquals("sameKeyReqValue", request.getAttribute("sameKey"));
213         vb = application.createValueBinding("sameKey");
214         vb.setValue(facesContext, "sameKeyNewValue");
215         assertEquals("sameKeyAppValue", servletContext.getAttribute("sameKey"));
216         assertEquals("sameKeySesValue", session.getAttribute("sameKey"));
217         assertEquals("sameKeyNewValue", request.getAttribute("sameKey"));
218 
219 
220     }
221 
222 
223     // --------------------------------------------------------- Private Methods
224 
225 
226     private void checkMethodBindingGetType(String ref, Class params[],
227                                            Class expected) throws Exception {
228 
229         MethodBinding mb = application.createMethodBinding(ref, params);
230         assertNotNull("MethodBinding[" + ref + "] exists", mb);
231         assertEquals("MethodBinding[" + ref + "] type",
232                      expected,
233                      mb.getType(facesContext));
234 
235     }
236 
237 
238     private void checkValueBindingGetValue(String ref, Object expected) {
239 
240         ValueBinding vb = application.createValueBinding(ref);
241         assertNotNull("ValueBinding[" + ref + "] exists", vb);
242         assertEquals("ValueBinding[" + ref + "] value",
243                      expected,
244                      vb.getValue(facesContext));
245 
246     }
247 
248 
249 }