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.webapp;
20  
21  import java.lang.reflect.Field;
22  import java.util.Locale;
23  
24  import javax.faces.application.ProjectStage;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.context.FacesContext;
27  import javax.servlet.ServletContext;
28  import javax.servlet.ServletContextEvent;
29  
30  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
31  import org.easymock.EasyMock;
32  import org.easymock.IAnswer;
33  import org.junit.Assert;
34  import org.junit.Test;
35  import org.junit.runner.RunWith;
36  import org.junit.runners.JUnit4;
37  
38  /**
39   * Test cases for StartupServletContextListener
40   * 
41   * @author Jakob Korherr (latest modification by $Author: lu4242 $)
42   * @version $Revision: 983737 $ $Date: 2010-08-09 12:08:38 -0500 (Mon, 09 Aug 2010) $
43   */
44  @RunWith(JUnit4.class)
45  public class StartupServletContextListenerTest extends AbstractJsfTestCase
46  {
47      
48      private StartupServletContextListener _listener;
49  
50      @Override
51      public void setUp() throws Exception
52      {
53          super.setUp();
54          
55          _listener = new StartupServletContextListener();
56      }
57  
58      @Override
59      public void tearDown() throws Exception
60      {
61          _listener = null;
62          
63          super.tearDown();
64      }
65  
66      /**
67       * Verifies the calls to FacesInitializer on contextInitialized()
68       */
69      @Test
70      public void testContextInitializedInitializerCalled()
71      {
72          FacesInitializer initializer = EasyMock.createMock(FacesInitializer.class);
73          EasyMock.expect(initializer.initStartupFacesContext(servletContext)).andReturn(facesContext).once();
74          initializer.initFaces(servletContext);
75          EasyMock.expectLastCall().once();
76          initializer.destroyStartupFacesContext(facesContext);
77          EasyMock.expectLastCall().once();
78          EasyMock.replay(initializer);
79          _setFacesInitializer(initializer);
80          
81          _listener.contextInitialized(new ServletContextEvent(servletContext));
82          EasyMock.verify(initializer);
83      }
84      
85      /**
86       * Verifies the calls to FacesInitializer on contextDestroyed()
87       */
88      @Test
89      public void testContextDestroyedInitializerCalled()
90      {
91          _setServletContext(servletContext);
92          
93          FacesInitializer initializer = EasyMock.createMock(FacesInitializer.class);
94          EasyMock.expect(initializer.initShutdownFacesContext(servletContext)).andReturn(facesContext).once();
95          initializer.destroyFaces(servletContext);
96          EasyMock.expectLastCall().once();
97          initializer.destroyShutdownFacesContext(facesContext);
98          EasyMock.expectLastCall().once();
99          EasyMock.replay(initializer);
100         _setFacesInitializer(initializer);
101         
102         _listener.contextDestroyed(new ServletContextEvent(servletContext));
103         EasyMock.verify(initializer);
104     }
105     
106     /**
107      * Verifies the FacesContext handling at application startup.
108      */
109     @Test
110     public void testContextInitializedFacesContextAvailable()
111     {
112         // release the current FacesContext to enforce setCurrentInstance(null)
113         facesContext.release();
114         
115         final FacesInitializer realInitializer = FacesInitializerFactory.getFacesInitializer(servletContext);
116         final AssertFacesContextAnswer assertAnswer = new AssertFacesContextAnswer();
117         
118         FacesInitializer mockInitializer = EasyMock.createMock(FacesInitializer.class);
119         
120         // initStartupFacesContext pass through to realInitializer
121         EasyMock.expect(mockInitializer.initStartupFacesContext(servletContext))
122                 .andAnswer(new IAnswer<FacesContext>()
123         {
124 
125             public FacesContext answer() throws Throwable
126             {
127                 assertAnswer.facesContext = realInitializer.initStartupFacesContext(servletContext);
128                 return assertAnswer.facesContext;
129             }
130             
131         });
132         
133         // initFaces with assert answer
134         mockInitializer.initFaces(servletContext);
135         EasyMock.expectLastCall().andStubAnswer(assertAnswer);
136         
137         // destroyStartupFacesContext pass through to realInitializer
138         mockInitializer.destroyStartupFacesContext(EasyMock.isA(FacesContext.class));
139         EasyMock.expectLastCall().andStubAnswer(new IAnswer<Object>()
140         {
141 
142             public Object answer() throws Throwable
143             {
144                 FacesContext argCtx = (FacesContext) EasyMock.getCurrentArguments()[0];
145                 Assert.assertNotNull(argCtx);
146                 Assert.assertEquals(assertAnswer.facesContext, argCtx); // must be the same
147                 
148                 realInitializer.destroyStartupFacesContext(argCtx);
149                 
150                 return null;
151             }
152                     
153                 
154         });
155         
156         EasyMock.replay(mockInitializer);
157         _setFacesInitializer(mockInitializer);
158         
159         _listener.contextInitialized(new ServletContextEvent(servletContext));
160         EasyMock.verify(mockInitializer);
161         
162         Assert.assertNull(FacesContext.getCurrentInstance()); // must be null by now
163     }
164     
165     /**
166      * Verifies the FacesContext handling at application shutdown.
167      */
168     @Test
169     public void testContextDestroyedFacesContextAvailable()
170     {
171         _setServletContext(servletContext);
172         
173         // release the current FacesContext to enforce setCurrentInstance(null)
174         facesContext.release();
175         
176         final FacesInitializer realInitializer = FacesInitializerFactory.getFacesInitializer(servletContext);
177         final AssertFacesContextAnswer assertAnswer = new AssertFacesContextAnswer();
178         
179         FacesInitializer mockInitializer = EasyMock.createMock(FacesInitializer.class);
180         
181         // initShutdownFacesContext pass through to realInitializer
182         EasyMock.expect(mockInitializer.initShutdownFacesContext(servletContext))
183                 .andAnswer(new IAnswer<FacesContext>()
184         {
185 
186             public FacesContext answer() throws Throwable
187             {
188                 assertAnswer.facesContext = realInitializer.initShutdownFacesContext(servletContext);
189                 return assertAnswer.facesContext;
190             }
191             
192         });
193         
194         // destroyFaces with assert answer
195         mockInitializer.destroyFaces(servletContext);
196         EasyMock.expectLastCall().andStubAnswer(assertAnswer);
197         
198         // destroyShutdownFacesContext pass through to realInitializer
199         mockInitializer.destroyShutdownFacesContext(EasyMock.isA(FacesContext.class));
200         EasyMock.expectLastCall().andStubAnswer(new IAnswer<Object>()
201         {
202 
203             public Object answer() throws Throwable
204             {
205                 FacesContext argCtx = (FacesContext) EasyMock.getCurrentArguments()[0];
206                 Assert.assertNotNull(argCtx);
207                 Assert.assertEquals(assertAnswer.facesContext, argCtx); // must be the same
208                 
209                 realInitializer.destroyShutdownFacesContext(argCtx);
210                 
211                 return null;
212             }
213                     
214                 
215         });
216         
217         EasyMock.replay(mockInitializer);
218         _setFacesInitializer(mockInitializer);
219         
220         _listener.contextDestroyed(new ServletContextEvent(servletContext));
221         EasyMock.verify(mockInitializer);
222         
223         Assert.assertNull(FacesContext.getCurrentInstance());  // must be null by now
224     }
225     
226     /**
227      * Sets the ServletContext on the StartupServletContextListener
228      * @param servletContext
229      */
230     private void _setServletContext(ServletContext servletContext)
231     {
232         try
233         {
234             Field field = StartupServletContextListener.class.getDeclaredField("_servletContext");
235             field.setAccessible(true);
236             field.set(_listener, servletContext);
237         }
238         catch (Exception e)
239         {
240             throw new IllegalStateException("Could not set ServletContext for test", e);
241         }
242         
243     }
244     
245     /**
246      * Sets the FacesInitializer on the StartupServletContextListener
247      * @param facesInitializer
248      */
249     private void _setFacesInitializer(FacesInitializer facesInitializer)
250     {
251         try
252         {
253             Field field = StartupServletContextListener.class.getDeclaredField("_facesInitializer");
254             field.setAccessible(true);
255             field.set(_listener, facesInitializer);
256         }
257         catch (Exception e)
258         {
259             throw new IllegalStateException("Could not set FacesInitializer for test", e);
260         }
261         
262     }
263     
264     /**
265      * Helper class to do assertions on the StartupFacesContextImpl.
266      * @author Jakob Korherr
267      */
268     private final class AssertFacesContextAnswer implements IAnswer<Object>
269     {
270 
271         private FacesContext facesContext; // set by the first answer in the test case
272         
273         public Object answer() throws Throwable
274         {
275             Assert.assertEquals(facesContext, FacesContext.getCurrentInstance());
276             Assert.assertNotNull(facesContext);
277             Assert.assertNotNull(facesContext.getApplication());
278             Assert.assertNotNull(facesContext.getExternalContext());
279             Assert.assertNotNull(facesContext.getExceptionHandler());
280             Assert.assertEquals(facesContext.getApplication()
281                     .getProjectStage().equals(ProjectStage.Production),
282                     facesContext.isProjectStage(ProjectStage.Production));
283             UIViewRoot viewRoot = facesContext.getViewRoot();
284             Assert.assertNotNull(viewRoot);
285             Assert.assertEquals(Locale.getDefault(), viewRoot.getLocale());
286             
287             return null;
288         }
289 
290     }
291     
292 }