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.view.facelets.stateless;
20  
21  import javax.faces.application.StateManager;
22  import javax.faces.component.UICommand;
23  import javax.faces.component.UIComponent;
24  import javax.faces.render.ResponseStateManager; 
25  
26  import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
27  import org.apache.myfaces.shared.config.MyfacesConfig;
28  import org.junit.Assert;
29  import org.junit.Ignore;
30  import org.junit.Test;
31  
32  /**
33   *
34   * @author lu4242
35   */
36  public class StatelessTest extends AbstractMyFacesRequestTestCase
37  {
38  
39      @Override
40      protected boolean isScanAnnotations()
41      {
42          return true;
43      }
44  
45      @Override
46      protected void setUpWebConfigParams() throws Exception
47      {
48          super.setUpWebConfigParams();
49          servletContext.addInitParameter("org.apache.myfaces.annotation.SCAN_PACKAGES","org.apache.myfaces.view.facelets.stateless");
50          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_SERVER);
51          servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
52          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS, "auto");
53          servletContext.addInitParameter("org.apache.myfaces.STRICT_JSF_2_REFRESH_TARGET_AJAX", "true");
54      }
55      
56      /**
57       * Verify that a view with a template that has transient set can be restored
58       * 
59       * @throws Exception
60       */
61      @Test
62      public void restoreStatelessTemplateView() throws Exception
63      {
64          startViewRequest("/stateless.xhtml");
65          processLifecycleExecuteAndRender();
66  
67          Assert.assertTrue(facesContext.getViewRoot().isTransient());
68  
69          // set the view state param so this context is treated as a postback
70          client.getParameters().put(ResponseStateManager.VIEW_STATE_PARAM, "stateless");
71          UIComponent formButton = facesContext.getViewRoot().findComponent("smt");
72          client.submit(formButton);
73  
74          try {
75              // this will cause an exception without the fix in MYFACES-4267
76              restoreView();
77          } catch (Exception e) {
78              Assert.fail("caught an exception trying to restore a stateless view: " + e.getMessage());
79              endRequest();
80              return;
81          }
82  
83          Assert.assertNotNull(facesContext.getViewRoot());
84  
85          // render the response and make sure the view contains the expected text
86          renderResponse();
87          String text = getRenderedContent(facesContext);
88  
89          Assert.assertTrue(text.contains("success"));
90  
91          endRequest();
92      }
93  }