View Javadoc

1   /*
2    * Copyright 2013 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.myfaces.application.viewstate;
18  
19  import java.util.Map;
20  import javax.faces.context.FacesContext;
21  import org.apache.myfaces.spi.ViewScopeProvider;
22  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
23  import org.junit.Test;
24  import org.testng.Assert;
25  
26  /**
27   *
28   */
29  public class SerializedViewCollectionTestCase extends AbstractJsfTestCase
30  {
31      
32      @Test
33      public void testSerializedViewCollection1()
34      {
35          servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_VIEWS_IN_SESSION_PARAM, "1");
36          
37          SerializedViewCollection collection = new SerializedViewCollection();
38          String viewId = "/test.xhtml";
39          SerializedViewKey key1 = new IntIntSerializedViewKey(viewId.hashCode(), 1);
40          SerializedViewKey key2 = new IntIntSerializedViewKey(viewId.hashCode(), 2);
41          
42          TestViewScopeProvider provider = new TestViewScopeProvider();
43          
44          collection.put(facesContext, new Object[]{null,null,2}, key1, null, provider, "1");
45          Assert.assertNotNull(collection.get(key1));
46          collection.put(facesContext, new Object[]{null,null,2}, key2, null, provider, "2");
47          Assert.assertNotNull(collection.get(key2));
48          Assert.assertNull(collection.get(key1));
49          Assert.assertEquals(provider.getDestroyCount(), 1);
50          
51      }
52      
53      @Test
54      public void testSerializedViewCollection2()
55      {
56          servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_VIEWS_IN_SESSION_PARAM, "2");
57          servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION_PARAM, "1");
58          
59          SerializedViewCollection collection = new SerializedViewCollection();
60          String viewId = "/test.xhtml";
61          SerializedViewKey key1 = new IntIntSerializedViewKey(viewId.hashCode(), 1);
62          SerializedViewKey key2 = new IntIntSerializedViewKey(viewId.hashCode(), 2);
63          SerializedViewKey key3 = new IntIntSerializedViewKey(viewId.hashCode(), 3);
64          
65          TestViewScopeProvider provider = new TestViewScopeProvider();
66          
67          collection.put(facesContext, new Object[]{null,null,2}, key1, null, provider, "1");
68          Assert.assertNotNull(collection.get(key1));
69          collection.put(facesContext, new Object[]{null,null,2}, key3, null, provider, "3");
70          Assert.assertNotNull(collection.get(key1));
71          Assert.assertNotNull(collection.get(key3));
72          collection.put(facesContext, new Object[]{null,null,2}, key2, key1, provider, "2");
73          Assert.assertNotNull(collection.get(key2));
74          Assert.assertNotNull(collection.get(key3));
75          Assert.assertNull(collection.get(key1));
76          Assert.assertEquals(provider.getDestroyCount(), 1);
77          
78      }    
79      
80      @Test
81      public void testSerializedViewCollection3()
82      {
83          servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_VIEWS_IN_SESSION_PARAM, "1");
84          
85          SerializedViewCollection collection = new SerializedViewCollection();
86          String viewId = "/test.xhtml";
87          SerializedViewKey key1 = new IntIntSerializedViewKey(viewId.hashCode(), 1);
88          SerializedViewKey key2 = new IntIntSerializedViewKey(viewId.hashCode(), 2);
89          SerializedViewKey key3 = new IntIntSerializedViewKey(viewId.hashCode(), 3);
90          
91          TestViewScopeProvider provider = new TestViewScopeProvider();
92          
93          collection.put(facesContext, new Object[]{null,null,2}, key1, null, provider, "1");
94          Assert.assertNotNull(collection.get(key1));
95          collection.put(facesContext, new Object[]{null,null,2}, key2, null, provider, "1");
96          Assert.assertNotNull(collection.get(key2));
97          Assert.assertNull(collection.get(key1));
98          // Destroy should not happen, because there is still one view holding the viewScopeId.
99          Assert.assertEquals(provider.getDestroyCount(), 0);
100         collection.put(facesContext, new Object[]{null,null,2}, key3, null, provider, "2");
101         Assert.assertNotNull(collection.get(key3));
102         Assert.assertNull(collection.get(key2));
103         // Now it should be destroyed the view 1
104         Assert.assertEquals(provider.getDestroyCount(), 1);
105     }
106     
107     @Test
108     public void testSerializedViewCollection4()
109     {
110         servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_VIEWS_IN_SESSION_PARAM, "2");
111         servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION_PARAM, "1");
112         
113         SerializedViewCollection collection = new SerializedViewCollection();
114         String viewId = "/test.xhtml";
115         SerializedViewKey key1 = new IntIntSerializedViewKey(viewId.hashCode(), 1);
116         SerializedViewKey key2 = new IntIntSerializedViewKey(viewId.hashCode(), 2);
117         SerializedViewKey key3 = new IntIntSerializedViewKey(viewId.hashCode(), 3);
118         
119         TestViewScopeProvider provider = new TestViewScopeProvider();
120         
121         collection.put(facesContext, new Object[]{null,null,2}, key1, null, provider, "1");
122         Assert.assertNotNull(collection.get(key1));
123         collection.put(facesContext, new Object[]{null,null,2}, key3, null, provider, "3");
124         Assert.assertNotNull(collection.get(key1));
125         Assert.assertNotNull(collection.get(key3));
126         collection.put(facesContext, new Object[]{null,null,2}, key2, key1, provider, "1");
127         Assert.assertNotNull(collection.get(key2));
128         Assert.assertNotNull(collection.get(key3));
129         Assert.assertNull(collection.get(key1));
130         Assert.assertEquals(provider.getDestroyCount(), 0);
131         
132     }   
133     
134     @Test
135     public void testSerializedViewCollection5()
136     {
137         servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_VIEWS_IN_SESSION_PARAM, "3");
138         servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION_PARAM, "1");
139         
140         SerializedViewCollection collection = new SerializedViewCollection();
141         String viewId = "/test.xhtml";
142         SerializedViewKey key1 = new IntIntSerializedViewKey(viewId.hashCode(), 1);
143         SerializedViewKey key2 = new IntIntSerializedViewKey(viewId.hashCode(), 2);
144         SerializedViewKey key3 = new IntIntSerializedViewKey(viewId.hashCode(), 3);
145         SerializedViewKey key4 = new IntIntSerializedViewKey(viewId.hashCode(), 4);
146         
147         TestViewScopeProvider provider = new TestViewScopeProvider();
148         
149         collection.put(facesContext, new Object[]{null,null,2}, key1, null, provider, "1");
150         Assert.assertNotNull(collection.get(key1));
151         collection.put(facesContext, new Object[]{null,null,2}, key2, null, provider, "2");
152         Assert.assertNotNull(collection.get(key1));
153         Assert.assertNotNull(collection.get(key2));
154         collection.put(facesContext, new Object[]{null,null,2}, key3, null, provider, "3");
155         Assert.assertNotNull(collection.get(key1));
156         Assert.assertNotNull(collection.get(key2));
157         Assert.assertNotNull(collection.get(key3));
158         collection.put(facesContext, new Object[]{null,null,2}, key1, null, provider, "1");
159         Assert.assertNotNull(collection.get(key1));
160         Assert.assertNotNull(collection.get(key2));
161         Assert.assertNotNull(collection.get(key3));
162         
163         // The are 3 slots, and when enters key4 the algorithm should not discard the most
164         // recently used, so key1 and key3 should be preserved and key2 discarded.
165         collection.put(facesContext, new Object[]{null,null,2}, key4, null, provider, "4");
166         Assert.assertNotNull(collection.get(key1));
167         Assert.assertNull(collection.get(key2));
168         Assert.assertNotNull(collection.get(key3));
169         Assert.assertNotNull(collection.get(key4));
170         
171 
172         Assert.assertEquals(provider.getDestroyCount(), 1);
173     }
174     
175     @Test
176     public void testSerializedViewCollection6()
177     {
178         servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_VIEWS_IN_SESSION_PARAM, "4");
179         servletContext.addInitParameter(ServerSideStateCacheImpl.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION_PARAM, "2");
180         
181         SerializedViewCollection collection = new SerializedViewCollection();
182         String viewId = "/test.xhtml";
183         SerializedViewKey key1 = new IntIntSerializedViewKey(viewId.hashCode(), 1);
184         SerializedViewKey key2 = new IntIntSerializedViewKey(viewId.hashCode(), 2);
185         SerializedViewKey key3 = new IntIntSerializedViewKey(viewId.hashCode(), 3);
186         SerializedViewKey key4 = new IntIntSerializedViewKey(viewId.hashCode(), 4);
187         SerializedViewKey key5 = new IntIntSerializedViewKey(viewId.hashCode(), 5);
188         SerializedViewKey key6 = new IntIntSerializedViewKey(viewId.hashCode(), 6);
189         SerializedViewKey key7 = new IntIntSerializedViewKey(viewId.hashCode(), 7);
190         SerializedViewKey key8 = new IntIntSerializedViewKey(viewId.hashCode(), 8);
191         SerializedViewKey key9 = new IntIntSerializedViewKey(viewId.hashCode(), 9);
192         
193         TestViewScopeProvider provider = new TestViewScopeProvider();
194         
195         collection.put(facesContext, new Object[]{null,null,2}, key1, null, provider, "1");
196         Assert.assertNotNull(collection.get(key1));
197         collection.put(facesContext, new Object[]{null,null,2}, key2, key1, provider, "2");
198         Assert.assertNotNull(collection.get(key1));
199         Assert.assertNotNull(collection.get(key2));
200         collection.put(facesContext, new Object[]{null,null,2}, key3, null, provider, "3");
201         Assert.assertNotNull(collection.get(key1));
202         Assert.assertNotNull(collection.get(key2));
203         Assert.assertNotNull(collection.get(key3));
204         collection.put(facesContext, new Object[]{null,null,2}, key4, key3, provider, "4");
205         Assert.assertNotNull(collection.get(key1));
206         Assert.assertNotNull(collection.get(key2));
207         Assert.assertNotNull(collection.get(key3));
208         Assert.assertNotNull(collection.get(key4));
209         collection.put(facesContext, new Object[]{null,null,2}, key2, null, provider, "2");
210         Assert.assertNotNull(collection.get(key1));
211         Assert.assertNotNull(collection.get(key2));
212         Assert.assertNotNull(collection.get(key3));
213         Assert.assertNotNull(collection.get(key4));
214         
215         // The collection is full, but under a new key should remove key1
216         collection.put(facesContext, new Object[]{null,null,2}, key5, null, provider, "5");
217         Assert.assertNull(collection.get(key1));
218         Assert.assertNotNull(collection.get(key2));
219         Assert.assertNotNull(collection.get(key3));
220         Assert.assertNotNull(collection.get(key4));
221         Assert.assertNotNull(collection.get(key5));
222         
223         // The next oldest is key2, but it was refreshed, so the next one in age is key3
224         collection.put(facesContext, new Object[]{null,null,2}, key6, null, provider, "6");
225         Assert.assertNull(collection.get(key1));
226         Assert.assertNotNull(collection.get(key2));
227         Assert.assertNull(collection.get(key3));
228         Assert.assertNotNull(collection.get(key4));
229         Assert.assertNotNull(collection.get(key5));
230         Assert.assertNotNull(collection.get(key6));
231         
232         // There is a sequential view for key6, destroy the oldest one, which is key4
233         collection.put(facesContext, new Object[]{null,null,2}, key7, key6, provider, "7");
234         Assert.assertNull(collection.get(key1));
235         Assert.assertNotNull(collection.get(key2));
236         Assert.assertNull(collection.get(key3));
237         Assert.assertNull(collection.get(key4));
238         Assert.assertNotNull(collection.get(key5));
239         Assert.assertNotNull(collection.get(key6));
240         Assert.assertNotNull(collection.get(key7));
241         
242         // Since org.apache.myfaces.NUMBER_OF_SEQUENTIAL_VIEWS_IN_SESSION is 2, and we have
243         // the sequence [key6, key7, key8] , the one to destroy is key6. 
244         collection.put(facesContext, new Object[]{null,null,2}, key8, key7, provider, "8");
245         Assert.assertNull(collection.get(key1));
246         Assert.assertNotNull(collection.get(key2));
247         Assert.assertNull(collection.get(key3));
248         Assert.assertNull(collection.get(key4));
249         Assert.assertNotNull(collection.get(key5));
250         Assert.assertNull(collection.get(key6));
251         Assert.assertNotNull(collection.get(key7));
252         Assert.assertNotNull(collection.get(key8));
253         
254         // This is a sequence [key2, key9], but the oldest one is key2, so in this case
255         // key2 should be removed.
256         collection.put(facesContext, new Object[]{null,null,2}, key9, key2, provider, "9");
257         Assert.assertNull(collection.get(key1));
258         Assert.assertNull(collection.get(key2));
259         Assert.assertNull(collection.get(key3));
260         Assert.assertNull(collection.get(key4));
261         Assert.assertNotNull(collection.get(key5));
262         Assert.assertNull(collection.get(key6));
263         Assert.assertNotNull(collection.get(key7));
264         Assert.assertNotNull(collection.get(key8));
265         Assert.assertNotNull(collection.get(key9));
266         
267         Assert.assertEquals(provider.getDestroyCount(), 5);
268     }
269     
270     private static class TestViewScopeProvider extends ViewScopeProvider
271     {
272         private int destroyCount = 0;
273 
274         @Override
275         public void onSessionDestroyed()
276         {
277             throw new UnsupportedOperationException("Not supported yet.");
278         }
279 
280         @Override
281         public String generateViewScopeId(FacesContext facesContext)
282         {
283             throw new UnsupportedOperationException("Not supported yet.");
284         }
285 
286         @Override
287         public Map<String, Object> createViewScopeMap(FacesContext facesContext, String viewScopeId)
288         {
289             throw new UnsupportedOperationException("Not supported yet.");
290         }
291 
292         @Override
293         public Map<String, Object> restoreViewScopeMap(FacesContext facesContext, String viewScopeId)
294         {
295             throw new UnsupportedOperationException("Not supported yet.");
296         }
297 
298         @Override
299         public void destroyViewScopeMap(FacesContext facesContext, String viewScopeId)
300         {
301             destroyCount++;
302         }
303 
304         /**
305          * @return the destroyCount
306          */
307         public int getDestroyCount()
308         {
309             return destroyCount;
310         }
311 
312         /**
313          * @param destroyCount the destroyCount to set
314          */
315         public void setDestroyCount(int destroyCount)
316         {
317             this.destroyCount = destroyCount;
318         }
319         
320     }
321 }