View Javadoc

1   package org.apache.myfaces.custom.savestate;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  import java.util.LinkedList;
22  
23  import javax.faces.el.ValueBinding;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
29  
30  /**
31   * @author cagatay
32   * Tests for UISaveState
33   */
34  public class UISaveStateTest extends AbstractTomahawkViewControllerTestCase{
35  
36      private UISaveState saveState;
37      private SaveStateTestBean saveStateTestBean;
38  
39      public UISaveStateTest(String name) {
40          super(name);
41      }
42  
43      public static Test suite() {
44          return new TestSuite(UISaveStateTest.class);
45      }
46  
47      public void setUp() throws Exception{
48          super.setUp();
49          saveState = new UISaveState();
50          saveStateTestBean = new SaveStateTestBean();
51          facesContext.getExternalContext().getRequestMap().put("testBean", saveStateTestBean);
52      }
53  
54      public void tearDown() throws Exception{
55          saveState = null;
56          saveStateTestBean = null;
57          super.tearDown();
58      }
59  
60      public void testWorksWithLinkedList() {
61          ValueBinding valueBinding = facesContext.getApplication().createValueBinding("#{testBean.linkedList}");
62          saveState.setValueBinding("value", valueBinding);
63  
64          LinkedList linkedList = (LinkedList) valueBinding.getValue(facesContext);
65  
66          try {
67              Object state = saveState.saveState(facesContext);
68              Object values[] = (Object[])state;
69              assertEquals(values[2], linkedList);
70  
71              saveState.restoreState(facesContext, state);
72              
73          }catch(Exception exception) {
74              fail();
75          }
76      }
77  
78      public void testWorksWithStateHolder() {
79          ValueBinding valueBinding = facesContext.getApplication().createValueBinding("#{testBean}");
80          saveState.setValueBinding("value", valueBinding);
81  
82          SaveStateTestBean testBean = (SaveStateTestBean) valueBinding.getValue(facesContext);
83  
84          try {
85              Object state = saveState.saveState(facesContext);
86              Object values[] = (Object[])state;
87              assertNotSame(values[2], testBean); //values[2] should be a type of "javax.faces.component._AttachedStateWrapper"
88  
89              saveState.restoreState(facesContext, state);
90          }catch(Exception exception) {
91              fail();
92          }
93      }
94  
95  }