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 javax.faces.application.StateManager;
22  import org.apache.myfaces.application.StateCache;
23  import org.apache.myfaces.test.base.junit4.AbstractJsfConfigurableMultipleRequestsTestCase;
24  import org.junit.Test;
25  import org.testng.Assert;
26  
27  /**
28   *
29   * @author lu4242
30   */
31  public class ClientSideStateCacheTest extends AbstractJsfConfigurableMultipleRequestsTestCase
32  {
33      
34      private static final int TIMESTAMP_PARAM = 2;
35      
36      @Test
37      public void testSaveRestoreState() throws Exception
38      {
39          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
40  
41          // Initialization
42          setupRequest();
43          StateCache stateCache = new ClientSideStateCacheImpl();
44          tearDownRequest();
45          
46          Object savedToken;
47          Object firstSavedToken;
48          
49          try
50          {
51              setupRequest();
52             
53              facesContext.getViewRoot().setViewId("view1.xhtml");
54              savedToken = stateCache.saveSerializedView(facesContext, 1);
55              firstSavedToken = savedToken;
56          }
57          finally
58          {
59              tearDownRequest();
60          }
61          
62          try
63          {
64              setupRequest();
65              
66              Object value = stateCache.restoreSerializedView(facesContext, "view1.xhtml", savedToken);
67              
68              Assert.assertEquals(1, value);
69              
70              facesContext.getViewRoot().setViewId("view2.xhtml");
71              savedToken = stateCache.saveSerializedView(facesContext, 2);
72          }
73          finally
74          {
75              tearDownRequest();
76          }
77  
78          try
79          {
80              setupRequest();
81              
82              Object value = stateCache.restoreSerializedView(facesContext, "view2.xhtml", savedToken);
83              
84              Assert.assertEquals(2, value);
85              
86              facesContext.getViewRoot().setViewId("view2.xhtml");
87              savedToken = stateCache.saveSerializedView(facesContext, 3);
88          }
89          finally
90          {
91              tearDownRequest();
92          }
93          
94          try
95          {
96              setupRequest();
97              
98              Object value = stateCache.restoreSerializedView(facesContext, "view1.xhtml", firstSavedToken);
99              
100             Assert.assertEquals(1, value);
101         }
102         finally
103         {
104             tearDownRequest();
105         }        
106     }
107     
108     @Test
109     public void testSaveRestoreStateWrongViewId() throws Exception
110     {
111         servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
112 
113         // Initialization
114         setupRequest();
115         StateCache stateCache = new ClientSideStateCacheImpl();
116         tearDownRequest();
117         
118         Object savedToken;
119         Object firstSavedToken;
120         
121         try
122         {
123             setupRequest();
124            
125             facesContext.getViewRoot().setViewId("/view1.xhtml");
126             savedToken = stateCache.saveSerializedView(facesContext, 1);
127             firstSavedToken = savedToken;
128         }
129         finally
130         {
131             tearDownRequest();
132         }
133         
134         try
135         {
136             setupRequest();
137             
138             // Note we are trying to restore restoring another different view with a token from the previous view.
139             // It should return null and later throw ViewExpiredException
140             // In client side state saving, the encoded viewId inside the state should be compared against the passed one.
141             // as parameter.
142             Object value = stateCache.restoreSerializedView(facesContext, "/view2.xhtml", firstSavedToken);
143             
144             Assert.assertNull(value);
145         }
146         finally
147         {
148             tearDownRequest();
149         }
150         
151         try
152         {
153             setupRequest();
154             
155             // It should restore this:
156             Object value = stateCache.restoreSerializedView(facesContext, "/view1.xhtml", firstSavedToken);
157             
158             Assert.assertEquals(1, value);
159         }
160         finally
161         {
162             tearDownRequest();
163         }
164         
165     }
166 
167     @Test
168     public void testSaveRestoreStateClientTimeout() throws Exception
169     {
170         servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
171         servletContext.addInitParameter(ClientSideStateCacheImpl.INIT_PARAM_CLIENT_VIEW_STATE_TIMEOUT, "2");
172 
173         // Initialization
174         setupRequest();
175         StateCache stateCache = new ClientSideStateCacheImpl();
176         tearDownRequest();
177         
178         Object savedToken;
179         Object firstSavedToken;
180         
181         try
182         {
183             setupRequest();
184            
185             facesContext.getViewRoot().setViewId("/view1.xhtml");
186             savedToken = stateCache.saveSerializedView(facesContext, 1);
187             firstSavedToken = savedToken;
188         }
189         finally
190         {
191             tearDownRequest();
192         }
193 
194         //Change timestamp to a previous date
195         Long timestamp = (Long)((Object[])firstSavedToken)[TIMESTAMP_PARAM];
196         ((Object[])firstSavedToken)[TIMESTAMP_PARAM] = timestamp.longValue() - 60000*3;
197         
198         try
199         {
200             setupRequest();
201             
202             // It should return null, because the timeStamp was changed to a previous date
203             Object value = stateCache.restoreSerializedView(facesContext, "/view1.xhtml", firstSavedToken);
204             
205             Assert.assertNull(value);
206         }
207         finally
208         {
209             tearDownRequest();
210         }
211     }
212 
213     
214 }