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.validation;
20  
21  import javax.faces.application.StateManager;
22  import javax.faces.component.UIInput;
23  import javax.faces.validator.BeanValidator;
24  import javax.faces.validator.Validator;
25  import org.apache.myfaces.mc.test.core.AbstractMyFacesCDIRequestTestCase;
26  import org.apache.myfaces.shared.config.MyfacesConfig;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   * This test is the same as FlowMyFacesRequestTestCase with the diference that
32   * in this case CDI is enabled and the other alternative is used.
33   */
34  public class BeanValidationCDIRequestTestCase extends AbstractMyFacesCDIRequestTestCase
35  {
36  
37      @Override
38      protected boolean isScanAnnotations()
39      {
40          return true;
41      }
42  
43      @Override
44      protected void setUpWebConfigParams() throws Exception
45      {
46          super.setUpWebConfigParams();
47          servletContext.addInitParameter("org.apache.myfaces.annotation.SCAN_PACKAGES",
48                  "org.apache.myfaces.view.facelets.tag.jsf.core.validation");
49          servletContext.addInitParameter(StateManager.STATE_SAVING_METHOD_PARAM_NAME, StateManager.STATE_SAVING_METHOD_CLIENT);
50          servletContext.addInitParameter("javax.faces.PARTIAL_STATE_SAVING", "true");
51          servletContext.addInitParameter(MyfacesConfig.INIT_PARAM_REFRESH_TRANSIENT_BUILD_ON_PSS, "auto");
52          servletContext.addInitParameter("javax.faces.CLIENT_WINDOW_MODE", "url");
53          servletContext.addInitParameter("org.apache.myfaces.validator.BEAN_BEFORE_JSF_VALIDATION", "true");
54      }
55      
56      @Test
57      public void testBeanValidation_1() throws Exception
58      {
59          startViewRequest("/testBeanValidation_1.xhtml");
60          
61          processLifecycleExecute();
62          processLifecycleRender();
63          client.inputText("mainForm:username", "someusr");
64          
65          client.submit("mainForm:submit");
66          
67          processLifecycleExecute();
68          
69          UIInput username = (UIInput) facesContext.getViewRoot().findComponent("mainForm:username");
70          Assert.assertNotNull(username);
71          Validator[] array = username.getValidators();
72          Assert.assertTrue(array[0] instanceof BeanValidator);
73      }
74      
75      @Test
76      public void testBeanValidation_2() throws Exception
77      {
78          startViewRequest("/testBeanValidation_2.xhtml");
79          
80          processLifecycleExecute();
81          processLifecycleRender();
82          client.inputText("mainForm:username", "someusr");
83          
84          client.submit("mainForm:submit");
85          
86          processLifecycleExecute();
87          
88          UIInput username = (UIInput) facesContext.getViewRoot().findComponent("mainForm:username");
89          Assert.assertNotNull(username);
90          Validator[] array = username.getValidators();
91          Assert.assertTrue(array[0] instanceof BeanValidator);
92      }
93  }