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.viewstate;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  import java.util.Map;
26  import javax.faces.application.ProjectStage;
27  import javax.faces.application.StateManager;
28  
29  import org.apache.myfaces.application.StateCache;
30  import org.apache.myfaces.test.base.junit4.AbstractJsfConfigurableMultipleRequestsTestCase;
31  import org.junit.Test;
32  import org.testng.Assert;
33  
34  public class ServerSideStateCacheTest extends AbstractJsfConfigurableMultipleRequestsTestCase
35  {
36  
37      @Test
38      public void testNumberOfSequentialViewsInSession() throws Exception
39      {
40          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
41          servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION", "5");
42          servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION", "2");
43  
44          // Initialization
45          setupRequest();
46          StateCache stateCache = new ServerSideStateCacheImpl();
47          tearDownRequest();
48          
49          Object savedToken;
50          Object firstSavedToken;
51          
52          try
53          {
54              setupRequest();
55             
56              facesContext.getViewRoot().setViewId("view1.xhtml");
57              savedToken = stateCache.saveSerializedView(facesContext, 1);
58              firstSavedToken = savedToken;
59          }
60          finally
61          {
62              tearDownRequest();
63          }
64          
65          try
66          {
67              setupRequest();
68              
69              Object value = stateCache.restoreSerializedView(facesContext, "view1.xhtml", savedToken);
70              
71              Assert.assertEquals(1, value);
72              
73              facesContext.getViewRoot().setViewId("view2.xhtml");
74              savedToken = stateCache.saveSerializedView(facesContext, 2);
75          }
76          finally
77          {
78              tearDownRequest();
79          }
80  
81          try
82          {
83              setupRequest();
84              
85              Object value = stateCache.restoreSerializedView(facesContext, "view2.xhtml", savedToken);
86              
87              Assert.assertEquals(2, value);
88              
89              facesContext.getViewRoot().setViewId("view2.xhtml");
90              savedToken = stateCache.saveSerializedView(facesContext, 3);
91          }
92          finally
93          {
94              tearDownRequest();
95          }
96          
97          try
98          {
99              setupRequest();
100             
101             Object value = stateCache.restoreSerializedView(facesContext, "view1.xhtml", firstSavedToken);
102             
103             // Since org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION is 2, the first one was already discarded
104             Assert.assertNull(value);
105         }
106         finally
107         {
108             tearDownRequest();
109         }
110     }
111     
112     @Test
113     public void testSaveRestoreStateWrongViewId() throws Exception
114     {
115         servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
116 
117         // Initialization
118         setupRequest();
119         StateCache stateCache = new ClientSideStateCacheImpl();
120         tearDownRequest();
121         
122         Object savedToken;
123         Object firstSavedToken;
124         
125         try
126         {
127             setupRequest();
128            
129             facesContext.getViewRoot().setViewId("/view1.xhtml");
130             savedToken = stateCache.saveSerializedView(facesContext, 1);
131             firstSavedToken = savedToken;
132         }
133         finally
134         {
135             tearDownRequest();
136         }
137         
138         try
139         {
140             setupRequest();
141             
142             // Note we are trying to restore restoring another different view with a token from the previous view.
143             // It should return null and later throw ViewExpiredException
144             // In server side state saving, the hashCode of the viewId should be part of the key used to restore
145             // the state, along with a counter.
146             Object value = stateCache.restoreSerializedView(facesContext, "/view2.xhtml", firstSavedToken);
147             
148             Assert.assertNull(value);
149         }
150         finally
151         {
152             tearDownRequest();
153         }
154         
155         try
156         {
157             setupRequest();
158             
159             // It should restore this:
160             Object value = stateCache.restoreSerializedView(facesContext, "/view1.xhtml", firstSavedToken);
161             
162             Assert.assertEquals(1, value);
163         }
164         finally
165         {
166             tearDownRequest();
167         }
168         
169     }
170 
171     @Test
172     public void testNonExistingViewId() throws Exception
173     {
174         servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
175         servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION", "5");
176         servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION", "2");
177 
178         // this issue only happens in projectstage Production
179         servletContext.addInitParameter("faces.PROJECT_STAGE", "Production");
180 
181         try
182         {
183             // Initialization
184             setupRequest();
185 
186             // we need to take a viewId which is null -> not existing.
187             facesContext.getViewRoot().setViewId(null);
188 
189             StateCache stateCache = new ServerSideStateCacheImpl();
190             Object savedToken = stateCache.saveSerializedView(facesContext, 1);
191 
192         }
193         finally
194         {
195             tearDownRequest();
196         }
197 
198     }
199 
200     public void tryStateKeySerialization() throws Exception
201     {
202         // Initialization
203         setupRequest();
204         StateCache stateCache = new ServerSideStateCacheImpl();
205         tearDownRequest();
206         
207         Object savedToken;
208         Object firstSavedToken;
209         
210         try
211         {
212             setupRequest();
213             facesContext.getViewRoot().setViewId("view1.xhtml");
214             savedToken = stateCache.saveSerializedView(facesContext, 1);
215             firstSavedToken = savedToken;
216             
217             for (Map.Entry<String, Object> entry : facesContext.getExternalContext().getSessionMap().entrySet())
218             {
219                 ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
220                 ObjectOutputStream oos = new ObjectOutputStream(baos);
221                 oos.writeObject(entry.getValue());
222                 oos.flush();
223                 baos.flush();
224                 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
225                 ObjectInputStream ois = new ObjectInputStream(bais);
226                 Object blorg = ois.readObject();
227             }
228         }
229         finally
230         {
231             tearDownRequest();
232         }
233     }
234 
235     @Test
236     public void testStateKeySerialization1() throws Exception
237     {
238         servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
239         servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION", "5");
240         servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION", "2");
241         servletContext.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, ProjectStage.Production.toString());
242         
243         tryStateKeySerialization();
244     }
245     
246     @Test
247     public void testStateKeySerialization2() throws Exception
248     {
249         servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
250         servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION", "5");
251         servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION", "2");
252         servletContext.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, ProjectStage.Production.toString());
253         servletContext.addInitParameter("org.apache.myfaces.RANDOM_KEY_IN_VIEW_STATE_SESSION_TOKEN", "random");
254         
255         tryStateKeySerialization();
256     }
257     
258     @Test
259     public void testStateKeySerialization3() throws Exception
260     {
261         servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
262         servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION", "5");
263         servletContext.addInitParameter("org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION", "2");
264         servletContext.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, ProjectStage.Development.toString());
265         
266         tryStateKeySerialization();
267     }
268     
269 }