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.tag.jsf.core.reset;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.component.UIInput;
23  import javax.faces.component.UIViewRoot;
24  import junit.framework.Assert;
25  import org.apache.myfaces.mc.test.core.AbstractMyFacesRequestTestCase;
26  import org.junit.Test;
27  
28  /**
29   *
30   * @author lu4242
31   */
32  public class ResetValuesTestCase extends AbstractMyFacesRequestTestCase
33  {
34      
35      @Override
36      protected boolean isScanAnnotations()
37      {
38          return true;
39      }
40      
41      @Override
42      protected void setUpWebConfigParams() throws Exception
43      {
44          super.setUpWebConfigParams();
45          servletContext.addInitParameter("org.apache.myfaces.annotation.SCAN_PACKAGES","org.apache.myfaces.view.facelets.tag.jsf.core.reset");
46      }
47      
48      /*
49      @Test
50      public void testResetValuesActionListenerHandler2() throws Exception
51      {
52          setupRequest("/resetValuesActionListener_2.xhtml");
53  
54          processLifecycleExecute();
55          
56          ResetValuesBean bean = facesContext.getApplication().evaluateExpressionGet(facesContext, 
57              "#{bean}", ResetValuesBean.class);
58          
59          bean.setField1("Hello");
60          bean.setField2(1);
61          
62          executeBuildViewCycle(facesContext);
63                  
64          UIComponent submitButton = facesContext.getViewRoot().findComponent("mainForm:submit");
65          
66          UIInput field1 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field1");
67          UIInput field2 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field2");
68          
69          executeViewHandlerRender(facesContext);
70          executeAfterRender(facesContext);
71          
72          // The lenght validator for field1 force a minimum of
73          // 4 digits, but the entered text has only 3 charactes.
74          // In the ajax request, that force a validation error, so
75          // the submitted values are not updated into the model
76          // but if the submitted value is set, it will render "xxx"
77          // and "2". Reset values clear the submitted values and
78          // let what's in the model.
79          client.inputText(field1, "xxx");
80          client.inputText(field2, "2");
81          
82          client.ajax(submitButton, "action", 
83              submitButton.getClientId(facesContext) +" "+
84              field1.getClientId(facesContext) + " "+ 
85              field2.getClientId(facesContext), 
86              field1.getClientId(facesContext) + " "+ 
87              field2.getClientId(facesContext), true, true);
88          
89          processLifecycleExecute();
90          processRender();
91  
92          UIComponent submitButton_2 = facesContext.getViewRoot().findComponent("mainForm:submit");
93          UIInput field1_2 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field1");
94          UIInput field2_2 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field2");
95          
96          Assert.assertEquals("Hello", field1_2.getValue());
97          Assert.assertEquals(1, field2_2.getValue());
98          Assert.assertNull(field1_2.getSubmittedValue());
99          Assert.assertNull(field2_2.getSubmittedValue());
100         
101         Assert.assertEquals("Hello", bean.getField1());
102         Assert.assertEquals(Integer.valueOf(1), bean.getField2());
103 
104         //Now let's try the normal way with no resetValues
105         client.inputText(field1_2, "xxx");
106         client.inputText(field2_2, "2");
107         
108         client.ajax(submitButton_2, "action", 
109             submitButton_2.getClientId(facesContext) +" "+
110             field1_2.getClientId(facesContext) + " "+ 
111             field2_2.getClientId(facesContext), 
112             field1_2.getClientId(facesContext) + " "+ 
113             field2_2.getClientId(facesContext), true, false);
114         
115         processLifecycleExecute();
116         processRender();
117 
118         UIComponent submitButton_3 = facesContext.getViewRoot().findComponent("mainForm:submit");
119         UIInput field1_3 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field1");
120         UIInput field2_3 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field2");
121         
122         // The values in the model are kept but the submitted values are there
123         // and the renderer takes them.
124         Assert.assertEquals("Hello", field1_3.getValue());
125         // the second field doesn't have validation error!, but the local value
126         // is set with 2, but the model still is 1 because update model phase
127         // was not executed.
128         Assert.assertEquals(2, field2_3.getValue());
129         Assert.assertTrue(field2_3.isLocalValueSet());
130         Assert.assertEquals("xxx", field1_3.getSubmittedValue());
131         Assert.assertNull(field2_3.getSubmittedValue());
132         
133         Assert.assertEquals("Hello", bean.getField1());
134         Assert.assertEquals(Integer.valueOf(1), bean.getField2());
135         
136         // Now let's try a valid one, in this case the model is updated
137         client.inputText(field1_3, "xxxx");
138         client.inputText(field2_3, "3");
139         
140         client.ajax(submitButton_3, "action", 
141             submitButton_3.getClientId(facesContext) +" "+
142             field1_3.getClientId(facesContext) + " "+ 
143             field2_3.getClientId(facesContext), 
144             field1_3.getClientId(facesContext) + " "+ 
145             field2_3.getClientId(facesContext), true, true);
146         
147         processLifecycleExecute();
148         processRender();
149 
150         //UIComponent submitButton_4 = facesContext.getViewRoot().findComponent("mainForm:submit");
151         UIInput field1_4 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field1");
152         UIInput field2_4 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field2");
153         
154         Assert.assertEquals("xxxx", field1_4.getValue());
155         Assert.assertEquals(3, field2_4.getValue());
156         Assert.assertNull(field1_4.getSubmittedValue());
157         Assert.assertNull(field2_4.getSubmittedValue());
158         
159         Assert.assertEquals("xxxx", bean.getField1());
160         Assert.assertEquals(Integer.valueOf(3), bean.getField2());
161     }*/
162 
163     @Test
164     public void testResetValuesActionListenerHandler3() throws Exception
165     {
166         startViewRequest("/resetValuesActionListener_3.xhtml");
167 
168         processLifecycleExecute();
169         
170         ResetValuesBean bean = facesContext.getApplication().evaluateExpressionGet(facesContext, 
171             "#{bean}", ResetValuesBean.class);
172         
173         bean.setField1("Hello");
174         bean.setField2(1);
175         
176         executeBuildViewCycle(facesContext);
177                 
178         UIComponent submitButton = facesContext.getViewRoot().findComponent("mainForm:submit:button");
179         
180         UIInput field1 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field1");
181         UIInput field2 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field2");
182         
183         executeViewHandlerRender(facesContext);
184         executeAfterRender(facesContext);
185         
186         client.inputText(field1, "xxx");
187         client.inputText(field2, "2");
188         
189         client.submit(submitButton);
190         
191         processLifecycleExecute();
192         renderResponse();
193 
194         submitButton = facesContext.getViewRoot().findComponent("mainForm:submit:button");
195         field1 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field1");
196         field2 = (UIInput) facesContext.getViewRoot().findComponent("mainForm:field2");
197 
198         Assert.assertEquals("Hello", field1.getValue());
199         Assert.assertEquals(1, field2.getValue());
200 
201     }
202 }