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.view.facelets;
20  
21  import javax.faces.application.StateManager;
22  import javax.faces.component.UIViewRoot;
23  import javax.faces.render.RenderKitFactory;
24  import javax.faces.render.ResponseStateManager;
25  
26  import org.apache.myfaces.application.StateManagerImpl;
27  import org.apache.myfaces.renderkit.html.HtmlResponseStateManager;
28  import org.apache.myfaces.shared.util.StateUtils;
29  import org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory;
30  import org.apache.myfaces.test.mock.MockRenderKit;
31  import org.junit.Test;
32  import org.testng.Assert;
33  
34  public class StateManagerWithFaceletsTest extends FaceletMultipleRequestsTestCase
35  {
36  
37      @Override
38      protected void setUpApplication() throws Exception
39      {
40          super.setUpApplication();
41          
42          application.setStateManager(new StateManagerImpl());
43      }
44  
45      @Override
46      protected void setUpServletContextAndSession() throws Exception
47      {
48          super.setUpServletContextAndSession();
49          
50          servletContext.addInitParameter(StateManager.PARTIAL_STATE_SAVING_PARAM_NAME, "true");
51          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
52      }
53  
54      @Test
55      public void testWriteAndRestoreState() throws Exception
56      {
57          String viewStateParam = null;
58          try
59          {
60              setupRequest();
61              
62              UIViewRoot root = facesContext.getViewRoot();
63              root.setViewId("/simpleTree.xhtml");
64              vdl.buildView(facesContext, root, "/simpleTree.xhtml");
65              
66              application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
67              
68              viewStateParam = application.getStateManager().getViewState(facesContext);
69              
70          }
71          finally
72          {
73              tearDownRequest();
74          }
75          
76          try
77          {
78              setupRequest();
79              
80              request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
81      
82              UIViewRoot restoredViewRoot = application.getStateManager().restoreView(facesContext, "/simpleTree.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
83              
84              Assert.assertNotNull(restoredViewRoot);
85          }
86          finally
87          {
88              tearDownRequest();
89          }
90      }
91      
92      @Test
93      public void testWriteAndRestoreStateWithMyFacesRSM() throws Exception
94      {
95          String viewStateParam = null;
96          
97          ((MockRenderKit)renderKit).setResponseStateManager(new HtmlResponseStateManager());
98          StateUtils.initSecret(servletContext);
99          servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
100 
101         try
102         {
103             setupRequest();
104             
105             UIViewRoot root = facesContext.getViewRoot();
106             root.setViewId("/simpleTree.xhtml");
107             vdl.buildView(facesContext, root, "/simpleTree.xhtml");
108             
109             application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
110             
111             viewStateParam = application.getStateManager().getViewState(facesContext);
112             
113         }
114         finally
115         {
116             tearDownRequest();
117         }
118         
119         try
120         {
121             setupRequest();
122             
123             request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
124     
125             UIViewRoot restoredViewRoot = application.getStateManager().restoreView(facesContext, "/simpleTree.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
126             
127             Assert.assertNotNull(restoredViewRoot);
128         }
129         finally
130         {
131             tearDownRequest();
132         }
133     }
134 
135 }