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.lifecycle;
20  
21  import static org.apache.myfaces.Assert.assertException;
22  import static org.easymock.EasyMock.eq;
23  import static org.easymock.EasyMock.expect;
24  import static org.easymock.EasyMock.same;
25  
26  import java.util.HashMap;
27  import java.util.Locale;
28  
29  import javax.faces.application.ViewExpiredException;
30  import javax.faces.application.ViewHandler;
31  import javax.faces.component.UIViewRoot;
32  import javax.faces.event.PhaseId;
33  import javax.faces.event.PostAddToViewEvent;
34  import javax.faces.view.ViewDeclarationLanguage;
35  
36  import org.apache.myfaces.FacesTestCase;
37  import org.apache.myfaces.TestRunner;
38  
39  /**
40   * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
41   * @version $Revision: 1533114 $ $Date: 2013-10-17 10:25:52 -0500 (Thu, 17 Oct 2013) $
42   */
43  public class RestoreViewExecutorTest extends FacesTestCase
44  {
45      private RestoreViewExecutor _testimpl;
46      private ViewHandler _viewHandler;
47      private RestoreViewSupport _restoreViewSupport;
48  
49      @Override
50      protected void setUp() throws Exception
51      {
52          super.setUp();
53          _viewHandler = _mocksControl.createMock(ViewHandler.class);
54          _restoreViewSupport = _mocksControl.createMock(RestoreViewSupport.class);
55          _testimpl = new RestoreViewExecutor();
56          _testimpl.setRestoreViewSupport(_restoreViewSupport);
57      }
58  
59      /**
60       * Test method for
61       * {@link org.apache.myfaces.lifecycle.RestoreViewExecutor#execute(javax.faces.context.FacesContext)}.
62       */
63      public void testExecuteWithExistingViewRoot()
64      {
65          expect(_facesContext.getApplication()).andReturn(_application).anyTimes();
66          expect(_application.getViewHandler()).andReturn(_viewHandler).anyTimes();
67          _viewHandler.initView(eq(_facesContext));
68          UIViewRoot viewRoot = _mocksControl.createMock(UIViewRoot.class);
69          expect(_facesContext.getViewRoot()).andReturn(viewRoot).times(2);
70          Locale expectedLocale = new Locale("xxx");
71          expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
72          expect(_externalContext.getRequestLocale()).andReturn(expectedLocale);
73          viewRoot.setLocale(eq(expectedLocale));
74          expect(viewRoot.getAfterPhaseListener()).andReturn(null);
75          _restoreViewSupport.processComponentBinding(same(_facesContext), same(viewRoot));
76  
77          _mocksControl.replay();
78          _testimpl.doPrePhaseActions(_facesContext);
79          _testimpl.execute(_facesContext);
80          _mocksControl.verify();
81      }
82  
83      /**
84       * Test method for
85       * {@link org.apache.myfaces.lifecycle.RestoreViewExecutor#execute(javax.faces.context.FacesContext)}.
86       *//*
87      public void testExecuteWOExistingViewRootNoPostBack()
88      {
89          setupWOExistingViewRoot();
90          expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
91          expect(_externalContext.getRequestMap()).andReturn(new HashMap());
92          expect(_restoreViewSupport.isPostback(same(_facesContext))).andReturn(false);
93          _facesContext.renderResponse();
94  
95          UIViewRoot viewRoot = _mocksControl.createMock(UIViewRoot.class);
96          //viewRoot.subscribeToEvent(same(PostAddToViewEvent.class), same(viewRoot));
97  
98          ViewDeclarationLanguage vdl = _mocksControl.createMock(ViewDeclarationLanguage.class);
99          //expect(_restoreViewSupport.deriveViewId(same(_facesContext), eq("calculatedViewId"))).andReturn("calculatedViewId");
100         expect(_viewHandler.deriveLogicalViewId(same(_facesContext), eq("calculatedViewId"))).andReturn("calculatedViewId");
101         expect(_facesContext.getResponseComplete()).andReturn(false);
102         expect(_viewHandler.getViewDeclarationLanguage(same(_facesContext), eq("calculatedViewId")))
103             .andReturn(vdl);
104         expect(vdl.getViewMetadata(same(_facesContext), eq("calculatedViewId")))
105             .andReturn(null);
106         expect(_viewHandler.createView(same(_facesContext), eq("calculatedViewId"))).andReturn(viewRoot);
107 
108         _application.publishEvent(same(_facesContext), same(PostAddToViewEvent.class), same(viewRoot));
109         _facesContext.setViewRoot(same(viewRoot));
110         expect(_facesContext.getViewRoot()).andReturn(viewRoot);
111         expect(viewRoot.getAfterPhaseListener()).andReturn(null);
112 
113         _mocksControl.replay();
114         _testimpl.doPrePhaseActions(_facesContext);
115         _testimpl.execute(_facesContext);
116         _mocksControl.verify();
117     }*/
118 
119     /**
120      * Test method for
121      * {@link org.apache.myfaces.lifecycle.RestoreViewExecutor#execute(javax.faces.context.FacesContext)}.
122      *//*
123     public void testExecuteWOExistingViewRootPostBack()
124     {
125         setupWOExistingViewRoot();
126         expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
127         expect(_externalContext.getRequestMap()).andReturn(new HashMap());
128         expect(_restoreViewSupport.isPostback(same(_facesContext))).andReturn(true);
129         _facesContext.setProcessingEvents(eq(true));
130         UIViewRoot viewRoot = _mocksControl.createMock(UIViewRoot.class);
131         expect(_viewHandler.restoreView(same(_facesContext), eq("calculatedViewId"))).andReturn(viewRoot);
132         _restoreViewSupport.processComponentBinding(same(_facesContext), same(viewRoot));
133         _facesContext.setViewRoot(same(viewRoot));
134         _facesContext.setProcessingEvents(eq(false));
135         expect(_facesContext.getViewRoot()).andReturn(viewRoot);
136         expect(viewRoot.getAfterPhaseListener()).andReturn(null);
137 
138         _mocksControl.replay();
139         _testimpl.doPrePhaseActions(_facesContext);
140         _testimpl.execute(_facesContext);
141         _mocksControl.verify();
142     }*/
143 
144     /**
145      * Test method for
146      * {@link org.apache.myfaces.lifecycle.RestoreViewExecutor#execute(javax.faces.context.FacesContext)}.
147      *//*
148     public void testExecuteWOExistingViewRootPostBackAndViewExpired()
149     {
150         setupWOExistingViewRoot();
151         expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
152         expect(_externalContext.getRequestMap()).andReturn(new HashMap());
153         expect(_restoreViewSupport.isPostback(same(_facesContext))).andReturn(true);
154         _facesContext.setProcessingEvents(eq(true));
155         expect(_viewHandler.restoreView(same(_facesContext), eq("calculatedViewId"))).andReturn(null);
156         _facesContext.setProcessingEvents(eq(false));
157 
158         _mocksControl.replay();
159         assertException(ViewExpiredException.class, new TestRunner()
160         {
161             public void run() throws Throwable
162             {
163                 _testimpl.doPrePhaseActions(_facesContext);
164                 _testimpl.execute(_facesContext);
165             };
166         });
167         _mocksControl.verify();
168     }*/
169 
170     private void setupWOExistingViewRoot()
171     {
172         expect(_facesContext.getApplication()).andReturn(_application).anyTimes();
173         expect(_application.getViewHandler()).andReturn(_viewHandler).anyTimes();
174         _viewHandler.initView(eq(_facesContext));
175         expect(_facesContext.getViewRoot()).andReturn(null);
176         expect(_restoreViewSupport.calculateViewId(eq(_facesContext))).andReturn("calculatedViewId");
177     }
178 
179     /**
180      * Test method for {@link org.apache.myfaces.lifecycle.RestoreViewExecutor#getRestoreViewSupport()}.
181      */
182     public void testGetRestoreViewSupport() throws Exception
183     {
184         expect(_facesContext.getExternalContext()).andReturn(_externalContext).anyTimes();
185         expect(_externalContext.getInitParameter("javax.faces.FACELETS_VIEW_MAPPINGS")).andReturn(null).anyTimes();
186         expect(_externalContext.getInitParameter("facelets.VIEW_MAPPINGS")).andReturn(null).anyTimes();
187         expect(_externalContext.getInitParameter("javax.faces.FACELETS_SUFFIX")).andReturn(null).anyTimes();
188         expect(_externalContext.getInitParameter("javax.faces.DEFAULT_SUFFIX")).andReturn(null).anyTimes();
189         _mocksControl.replay();
190         assertTrue(DefaultRestoreViewSupport.class.equals(new RestoreViewExecutor().getRestoreViewSupport(_facesContext).getClass()));
191         _mocksControl.verify();
192     }
193 
194     /**
195      * Test method for {@link org.apache.myfaces.lifecycle.RestoreViewExecutor#getPhase()}.
196      */
197     public void testGetPhase()
198     {
199         assertEquals(PhaseId.RESTORE_VIEW, _testimpl.getPhase());
200     }
201 
202 }