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.application.contracts;
20  
21  import java.util.List;
22  import java.util.Set;
23  import javax.faces.application.ProjectStage;
24  import javax.faces.application.StateManager;
25  import javax.faces.component.UICommand;
26  
27  
28  import org.apache.myfaces.config.RuntimeConfig;
29  
30  import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
31  import org.apache.myfaces.shared.config.MyfacesConfig;
32  import org.apache.myfaces.test.mock.MockPrintWriter;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  public class SingleContractMyFacesRequestTestCase 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.application.contracts");
50          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
51          servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
52          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS, "auto");
53          servletContext.addInitParameter("javax.faces.CONFIG_FILES", "/no-contract-faces-config.xml");
54          servletContext.addInitParameter(ProjectStage.PROJECT_STAGE_PARAM_NAME, ProjectStage.Production.toString());
55      }
56      
57      @Test
58      public void testDefaultConfiguration() throws Exception
59      {
60          startViewRequest("/index.xhtml");
61          RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(externalContext);
62          
63          Set<String> allContracts = runtimeConfig.getResourceLibraryContracts();
64          Set<String> externalContextContracts = runtimeConfig.getExternalContextResourceLibraryContracts();
65          Set<String> classloaderContracts = runtimeConfig.getClassLoaderResourceLibraryContracts();
66  
67          Assert.assertTrue(allContracts.contains("yellow"));
68          Assert.assertTrue(allContracts.contains("blue"));
69          Assert.assertTrue(allContracts.contains("red"));
70  
71          Assert.assertTrue(classloaderContracts.contains("yellow"));
72          Assert.assertTrue(classloaderContracts.contains("blue"));
73          Assert.assertTrue(externalContextContracts.contains("red"));
74          
75          processLifecycleExecute();
76          executeBuildViewCycle(facesContext);
77          
78          List<String> contractsList = facesContext.getResourceLibraryContracts();
79          Assert.assertTrue(contractsList == null || contractsList.isEmpty());
80          
81          executeViewHandlerRender(facesContext);
82          executeAfterRender(facesContext);
83          
84          endRequest();
85      }
86  
87      @Test
88      public void testView1() throws Exception
89      {
90          startViewRequest("/view_1.xhtml");
91          
92          processLifecycleExecute();
93          executeBuildViewCycle(facesContext);
94          
95          List<String> contractsList = facesContext.getResourceLibraryContracts();
96          Assert.assertTrue(contractsList.contains("yellow"));
97          
98          executeViewHandlerRender(facesContext);
99          executeAfterRender(facesContext);
100         
101         UICommand submitButton = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
102         client.submit(submitButton);
103         
104         processLifecycleExecute();
105         
106         List<String> contractsList2 = facesContext.getResourceLibraryContracts();
107         Assert.assertTrue(contractsList2.contains("yellow"));
108     }
109     
110     @Test
111     public void testView2() throws Exception
112     {
113         startViewRequest("/view_2.xhtml");
114         processLifecycleExecute();
115         executeBuildViewCycle(facesContext);
116         
117         List<String> contractsList = facesContext.getResourceLibraryContracts();
118         Assert.assertTrue(contractsList.contains("blue"));
119         
120         executeViewHandlerRender(facesContext);
121         executeAfterRender(facesContext);
122         
123         UICommand submitButton = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
124         client.submit(submitButton);
125         
126         processLifecycleExecute();
127         
128         List<String> contractsList2 = facesContext.getResourceLibraryContracts();
129         Assert.assertTrue(contractsList2.contains("blue"));
130     }
131 
132     @Test
133     public void testView3() throws Exception
134     {
135         startViewRequest("/view_3.xhtml");
136         processLifecycleExecute();
137         executeBuildViewCycle(facesContext);
138         
139         List<String> contractsList = facesContext.getResourceLibraryContracts();
140         Assert.assertTrue(contractsList.contains("red"));
141         
142         executeViewHandlerRender(facesContext);
143         executeAfterRender(facesContext);
144         
145         UICommand submitButton = (UICommand) facesContext.getViewRoot().findComponent("mainForm:submit");
146         client.submit(submitButton);
147         
148         processLifecycleExecute();
149         
150         List<String> contractsList2 = facesContext.getResourceLibraryContracts();
151         Assert.assertTrue(contractsList2.contains("red"));
152     }
153     
154     @Test
155     public void testView1_3() throws Exception
156     {
157         startViewRequest("/view_1.xhtml");
158         processLifecycleExecute();
159         executeBuildViewCycle(facesContext);
160         
161         List<String> contractsList = facesContext.getResourceLibraryContracts();
162         Assert.assertTrue(contractsList.contains("yellow"));
163         
164         executeViewHandlerRender(facesContext);
165         executeAfterRender(facesContext);
166         
167         MockPrintWriter writer1 = (MockPrintWriter) response.getWriter();        
168         String content1 = new String(writer1.content());
169         Assert.assertTrue(content1.contains("header_yellow"));
170         
171         endRequest();
172         
173         startViewRequest("/view_3.xhtml");
174         processLifecycleExecute();
175         executeBuildViewCycle(facesContext);
176         
177         contractsList = facesContext.getResourceLibraryContracts();
178         Assert.assertTrue(contractsList.contains("red"));
179         
180         executeViewHandlerRender(facesContext);
181 
182         MockPrintWriter writer2 = (MockPrintWriter) response.getWriter();
183         String content2 = new String(writer2.content());
184         Assert.assertTrue(content2.contains("header_red"));
185         
186         executeAfterRender(facesContext);
187         
188         endRequest();
189     }    
190 }