View Javadoc

1   /*
2    * Copyright 2004-2006 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.jsp;
18  
19  //import org.apache.myfaces.shared_impl.config.MyfacesConfig;
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  
29  import org.apache.myfaces.test.base.AbstractJsfTestCase;
30  import org.apache.myfaces.test.mock.MockResponseWriter;
31  
32  public class JspStateManagerImplTest extends AbstractJsfTestCase {
33  
34      public static void main(String[] args) {
35          junit.textui.TestRunner.run(JspStateManagerImplTest.class);
36      }
37  
38      public JspStateManagerImplTest(String name) {
39          super(name);
40      }
41  
42      /**
43       * New test to address an issue uncovered through TCK testing.
44       */
45      public void testWriteAndRestoreState() throws Exception
46      {
47          // additional setup not provided automatically by the myfaces mock stuff
48          facesContext.setResponseWriter(new MockResponseWriter(new BufferedWriter(new CharArrayWriter()), null, null));
49  
50          UIViewRoot viewRoot = facesContext.getViewRoot();
51          viewRoot.setViewId("/root");
52          StateManager stateManager = new JspStateManagerImpl();
53  
54          UIOutput output = new UIOutput();
55          output.setValue("foo");
56          output.setId("foo");
57  
58          stateManager.writeState(facesContext, stateManager.saveSerializedView(facesContext));
59  
60          UIViewRoot restoredViewRoot = stateManager.restoreView(facesContext, "/root", RenderKitFactory.HTML_BASIC_RENDER_KIT);
61          assertNotNull("restored view root should not be null", restoredViewRoot);
62      }
63  
64      public void testSaveInSessionWithoutSerialize() throws Exception
65      {
66          // create a fake viewRoot
67          UIViewRoot root = new UIViewRoot();
68          root.getAttributes().put("key", "value");
69  
70          // simulate server-side-state-saving without serialization
71          Object state = root.saveState(facesContext);
72  
73          // restore view
74          UIViewRoot root1 = new UIViewRoot();
75          root1.restoreState(facesContext, state);
76  
77          // restore view .. next request
78          UIViewRoot root2 = new UIViewRoot();
79          root2.restoreState(facesContext, state);
80  
81          // chaange attribute in root1
82          root1.getAttributes().put("key", "borken");
83  
84          // other values should not have been changed
85          assertEquals("value", root2.getAttributes().get("key"));
86          assertEquals("value", root.getAttributes().get("key"));
87      }
88  }