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.view;
19  
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import javax.faces.application.FacesMessage;
24  import javax.faces.application.FacesMessage.Severity;
25  import javax.faces.component.UIViewRoot;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.shale.test.base.AbstractJsfTestCase;
31  
32  /***
33   * <p>Test case for <code>AbstractFacesBean</code>.</p>
34   */
35  public class ConcreteFacesBeanTestCase extends AbstractJsfTestCase {
36      
37      // ------------------------------------------------------------ Constructors
38  
39  
40      // Construct a new instance of this test case.
41      public ConcreteFacesBeanTestCase(String name) {
42          super(name);
43      }
44  
45  
46      // ---------------------------------------------------- Overall Test Methods
47  
48  
49      // Set up instance variables required by this test case.
50      protected void setUp() throws Exception {
51  
52          super.setUp();
53  
54          // Set up the instance we will be testing
55          bean = new ConcreteFacesBean();
56  
57      }
58  
59  
60      // Return the tests included in this test case.
61      public static Test suite() {
62  
63          return (new TestSuite(ConcreteFacesBeanTestCase.class));
64  
65      }
66  
67  
68      // Tear down instance variables required by this test case.
69      protected void tearDown() throws Exception {
70  
71          bean = null;
72          super.tearDown();
73  
74      }
75  
76  
77      // ------------------------------------------------------ Instance Variables
78  
79  
80      // The instance to be tested
81      AbstractFacesBean bean = null;
82  
83      
84      // ------------------------------------------------------------ Test Methods
85  
86  
87      // Test access to application scope attributes
88      public void testApplicationMap() {
89  
90          servletContext.setAttribute("foo", "bar");
91          Map map = bean.getApplicationMap();
92          assertTrue(map.containsKey("foo"));
93          assertTrue(map.containsValue("bar"));
94          assertEquals("bar", map.get("foo"));
95          map.put("baz", "bop");
96          assertEquals("bop", servletContext.getAttribute("baz"));
97          map.remove("foo");
98          assertNull(servletContext.getAttribute("foo"));
99  
100     }
101 
102 
103     // Test the getBean() method
104     public void testGetBean() {
105 
106         servletContext.setAttribute("foo1", "bar1");
107         request.setAttribute("foo2", "bar2");
108         session.setAttribute("foo3", "bar3");
109         assertEquals("bar1", bean.getBean("foo1"));
110         assertEquals("bar2", bean.getBean("foo2"));
111         assertEquals("bar3", bean.getBean("foo3"));
112 
113     }
114 
115 
116     // Test the getValue() method
117     public void testGetValue() {
118 
119         servletContext.setAttribute("foo", new ConcreteFacesBean("bar"));
120         assertEquals("bar", bean.getValue("#{foo.id}"));
121 
122     }
123 
124 
125     private Severity messagesAttachedSeverity[] = {
126         FacesMessage.SEVERITY_ERROR,
127         FacesMessage.SEVERITY_ERROR,
128         FacesMessage.SEVERITY_ERROR,
129         FacesMessage.SEVERITY_ERROR,
130         FacesMessage.SEVERITY_FATAL,
131         FacesMessage.SEVERITY_FATAL,
132         FacesMessage.SEVERITY_FATAL,
133         FacesMessage.SEVERITY_INFO,
134         FacesMessage.SEVERITY_INFO,
135         FacesMessage.SEVERITY_WARN,
136     };
137 
138     private String messagesAttachedSummary[] = {
139         "Error 0", "Error 1", "Error 2", "Error 3",
140         "Fatal 0", "Fatal 1", "Fatal 2",
141         "Info 0", "Info 1",
142         "Warn 0",
143     };
144 
145 
146     // Test messages attached to a particular component
147     public void testMessagesAttached() {
148 
149         UIViewRoot root = new UIViewRoot();
150         root.setId("root");
151         facesContext.setViewRoot(root);
152         bean.error(root, "Error 0");
153         bean.error(root, "Error 1");
154         bean.error(root, "Error 2");
155         bean.error(root, "Error 3");
156         bean.fatal(root, "Fatal 0");
157         bean.fatal(root, "Fatal 1");
158         bean.fatal(root, "Fatal 2");
159         bean.info(root, "Info 0");
160         bean.info(root, "Info 1");
161         bean.warn(root, "Warn 0");
162         checkMessages(facesContext.getMessages(root.getClientId(facesContext)),
163                       messagesAttachedSeverity,
164                       messagesAttachedSummary);
165 
166     }
167 
168 
169     private Severity messagesUnattachedSeverity[] = {
170         FacesMessage.SEVERITY_ERROR,
171         FacesMessage.SEVERITY_FATAL,
172         FacesMessage.SEVERITY_FATAL,
173         FacesMessage.SEVERITY_INFO,
174         FacesMessage.SEVERITY_INFO,
175         FacesMessage.SEVERITY_INFO,
176         FacesMessage.SEVERITY_WARN,
177         FacesMessage.SEVERITY_WARN,
178         FacesMessage.SEVERITY_WARN,
179         FacesMessage.SEVERITY_WARN,
180     };
181 
182     private String messagesUnattachedSummary[] = {
183         "Error 0",
184         "Fatal 0", "Fatal 1",
185         "Info 0", "Info 1", "Info 2",
186         "Warn 0", "Warn 1", "Warn 2", "Warn 3",
187     };
188 
189 
190     // Test messages not attached to any particular component
191     public void testMessagesUnattached() {
192 
193         bean.error("Error 0");
194         bean.fatal("Fatal 0");
195         bean.fatal("Fatal 1");
196         bean.info("Info 0");
197         bean.info("Info 1");
198         bean.info("Info 2");
199         bean.warn("Warn 0");
200         bean.warn("Warn 1");
201         bean.warn("Warn 2");
202         bean.warn("Warn 3");
203         checkMessages(facesContext.getMessages(null),
204                       messagesUnattachedSeverity,
205                       messagesUnattachedSummary);
206 
207     }
208 
209 
210     // Test pristine instance
211     public void testPristine() {
212 
213         assertNotNull(bean.getApplication());
214         assertNotNull(bean.getApplicationMap());
215         assertNotNull(bean.getExternalContext());
216         assertNotNull(bean.getFacesContext());
217         assertNotNull(bean.getLifecycle());
218         assertNotNull(bean.getRequestMap());
219         assertNotNull(bean.getSessionMap());
220 
221     }
222 
223 
224     // Test access to request scope attributes
225     public void testRequestMap() {
226 
227         request.setAttribute("foo", "bar");
228         Map map = bean.getRequestMap();
229         assertTrue(map.containsKey("foo"));
230         assertTrue(map.containsValue("bar"));
231         assertEquals("bar", map.get("foo"));
232         map.put("baz", "bop");
233         assertEquals("bop", request.getAttribute("baz"));
234         map.remove("foo");
235         assertNull(request.getAttribute("foo"));
236 
237     }
238 
239 
240     // Test access to session scope attributes
241     public void testSessionMap() {
242 
243         session.setAttribute("foo", "bar");
244         Map map = bean.getSessionMap();
245         assertTrue(map.containsKey("foo"));
246         assertTrue(map.containsValue("bar"));
247         assertEquals("bar", map.get("foo"));
248         map.put("baz", "bop");
249         assertEquals("bop", session.getAttribute("baz"));
250         map.remove("foo");
251         assertNull(session.getAttribute("foo"));
252 
253     }
254 
255 
256     // Test the setBean()method
257     public void testSetBean() {
258 
259         bean.setBean("foo", "bar");
260         assertEquals("bar", bean.getBean("foo"));
261         assertEquals("bar", request.getAttribute("foo"));
262 
263     }
264 
265 
266     // Test the setValue() method
267     public void testSetValue() {
268 
269         servletContext.setAttribute("foo", new ConcreteFacesBean("bar"));
270         bean.setValue("#{foo.id}", "baz");
271         assertEquals("baz", bean.getValue("#{foo.id}"));
272         assertEquals("baz", ((ConcreteFacesBean) servletContext.getAttribute("foo")).getId());
273 
274     }
275 
276 
277     // --------------------------------------------------------- Support Methods
278 
279 
280     // Check the queued messages for correct severity and summary messages
281     private void checkMessages(Iterator messages, Severity severity[], String summary[]) {
282         int n = 0;
283         while (messages.hasNext()) {
284             FacesMessage message = (FacesMessage) messages.next();
285             assertEquals(summary[n] + " severity", severity[n], message.getSeverity());
286             assertEquals(summary[n] + " summary", summary[n], message.getSummary());
287             n++;
288         }
289     }
290 
291 
292 }