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.application;
20  
21  import java.io.BufferedWriter;
22  import java.io.CharArrayWriter;
23  
24  import javax.faces.application.StateManager;
25  import javax.faces.component.UIOutput;
26  import javax.faces.component.UIViewRoot;
27  import javax.faces.render.RenderKitFactory;
28  import javax.faces.render.ResponseStateManager;
29  
30  import org.apache.myfaces.renderkit.html.HtmlResponseStateManager;
31  import org.apache.myfaces.shared.util.StateUtils;
32  import org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory;
33  import org.apache.myfaces.test.base.junit4.AbstractJsfConfigurableMultipleRequestsTestCase;
34  import org.apache.myfaces.test.mock.MockRenderKit;
35  import org.apache.myfaces.test.mock.MockResponseWriter;
36  import org.junit.Test;
37  import org.junit.runner.RunWith;
38  import org.junit.runners.JUnit4;
39  import org.testng.Assert;
40  
41  @RunWith(JUnit4.class)
42  public class StateManagerImplTest extends AbstractJsfConfigurableMultipleRequestsTestCase
43  {
44  
45      public StateManagerImplTest()
46      {
47          super();
48      }
49  
50      @Test
51      public void testWriteAndRestoreState() throws Exception
52      {
53          StateManager stateManager = null;
54          String viewStateParam = null;
55          
56          //renderKit.setResponseStateManager(new HtmlResponseStateManager());
57          //StateUtils.initSecret(servletContext);
58          //servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
59          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
60          
61          try
62          {
63              setupRequest();
64              
65              facesContext.setResponseWriter(new MockResponseWriter(new BufferedWriter(new CharArrayWriter()), null, null));
66      
67              UIViewRoot viewRoot = facesContext.getViewRoot();
68              viewRoot.setViewId("/root");
69              stateManager = new StateManagerImpl();
70      
71              UIOutput output = new UIOutput();
72              output.setValue("foo");
73              output.setId("foo");
74      
75              stateManager.writeState(facesContext, stateManager.saveView(facesContext));
76              
77              viewStateParam = stateManager.getViewState(facesContext);
78          }
79          finally
80          {
81              tearDownRequest();
82          }
83  
84          try
85          {
86              setupRequest();
87              
88              request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
89      
90              UIViewRoot restoredViewRoot = stateManager.restoreView(facesContext, "/root", RenderKitFactory.HTML_BASIC_RENDER_KIT);
91              
92              Assert.assertNotNull(restoredViewRoot);
93          }
94          finally
95          {
96              tearDownRequest();
97          }
98      }
99  
100     @Test
101     public void testWriteAndRestoreStateWithMyFacesRSM() throws Exception
102     {
103         StateManager stateManager = null;
104         String viewStateParam = null;
105 
106         setupRequest();
107         
108         ((MockRenderKit)renderKit).setResponseStateManager(new HtmlResponseStateManager());
109         StateUtils.initSecret(servletContext);
110         servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
111         
112         tearDownRequest();
113         
114         try
115         {
116             setupRequest();
117             
118             facesContext.setResponseWriter(new MockResponseWriter(new BufferedWriter(new CharArrayWriter()), null, null));
119     
120             UIViewRoot viewRoot = facesContext.getViewRoot();
121             viewRoot.setViewId("/root");
122             stateManager = new StateManagerImpl();
123     
124             UIOutput output = new UIOutput();
125             output.setValue("foo");
126             output.setId("foo");
127     
128             stateManager.writeState(facesContext, stateManager.saveView(facesContext));
129             
130             viewStateParam = stateManager.getViewState(facesContext);
131         }
132         finally
133         {
134             tearDownRequest();
135         }
136 
137         try
138         {
139             setupRequest();
140             
141             request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
142     
143             UIViewRoot restoredViewRoot = stateManager.restoreView(facesContext, "/root", RenderKitFactory.HTML_BASIC_RENDER_KIT);
144             
145             Assert.assertNotNull(restoredViewRoot);
146         }
147         finally
148         {
149             tearDownRequest();
150         }
151     }
152 }