1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets;
20
21 import javax.faces.application.StateManager;
22 import javax.faces.component.UIViewRoot;
23 import javax.faces.render.RenderKitFactory;
24 import javax.faces.render.ResponseStateManager;
25
26 import org.apache.myfaces.application.StateManagerImpl;
27 import org.apache.myfaces.renderkit.html.HtmlResponseStateManager;
28 import org.apache.myfaces.shared.util.StateUtils;
29 import org.apache.myfaces.shared_impl.util.serial.DefaultSerialFactory;
30 import org.apache.myfaces.test.mock.MockFacesContext20;
31 import org.apache.myfaces.test.mock.MockRenderKit;
32 import org.junit.Test;
33 import org.testng.Assert;
34
35 public class StateManagerWithFaceletsTest extends FaceletMultipleRequestsTestCase
36 {
37
38 @Override
39 protected void setUpApplication() throws Exception
40 {
41 super.setUpApplication();
42
43 application.setStateManager(new StateManagerImpl());
44 }
45
46 @Override
47 protected void setUpServletContextAndSession() throws Exception
48 {
49 super.setUpServletContextAndSession();
50
51 servletContext.addInitParameter(StateManager.PARTIAL_STATE_SAVING_PARAM_NAME, "true");
52 servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
53 }
54
55 @Test
56 public void testWriteAndRestoreState() throws Exception
57 {
58 String viewStateParam = null;
59 try
60 {
61 setupRequest();
62
63 UIViewRoot root = facesContext.getViewRoot();
64 root.setViewId("/simpleTree.xhtml");
65 vdl.buildView(facesContext, root, "/simpleTree.xhtml");
66
67 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
68
69 viewStateParam = application.getStateManager().getViewState(facesContext);
70
71 }
72 finally
73 {
74 tearDownRequest();
75 }
76
77 try
78 {
79 setupRequest();
80
81 request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
82 ((MockFacesContext20)facesContext).setPostback(true);
83
84 UIViewRoot restoredViewRoot = application.getStateManager().restoreView(facesContext, "/simpleTree.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
85
86 Assert.assertNotNull(restoredViewRoot);
87 }
88 finally
89 {
90 tearDownRequest();
91 }
92 }
93
94 @Test
95 public void testWriteAndRestoreStateWithMyFacesRSM() throws Exception
96 {
97 String viewStateParam = null;
98
99 ((MockRenderKit)renderKit).setResponseStateManager(new HtmlResponseStateManager());
100 StateUtils.initSecret(servletContext);
101 servletContext.setAttribute(StateUtils.SERIAL_FACTORY, new DefaultSerialFactory());
102
103 try
104 {
105 setupRequest();
106
107 UIViewRoot root = facesContext.getViewRoot();
108 root.setViewId("/simpleTree.xhtml");
109 vdl.buildView(facesContext, root, "/simpleTree.xhtml");
110
111 application.getStateManager().writeState(facesContext, application.getStateManager().saveView(facesContext));
112
113 viewStateParam = application.getStateManager().getViewState(facesContext);
114
115 }
116 finally
117 {
118 tearDownRequest();
119 }
120
121 try
122 {
123 setupRequest();
124
125 request.addParameter(ResponseStateManager.VIEW_STATE_PARAM, viewStateParam);
126 ((MockFacesContext20)facesContext).setPostback(true);
127
128 UIViewRoot restoredViewRoot = application.getStateManager().restoreView(facesContext, "/simpleTree.xhtml", RenderKitFactory.HTML_BASIC_RENDER_KIT);
129
130 Assert.assertNotNull(restoredViewRoot);
131 }
132 finally
133 {
134 tearDownRequest();
135 }
136 }
137
138 }