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.extensions.validator.test.trinidad.initializer;
20  
21  import org.apache.myfaces.extensions.validator.PropertyValidationModuleStartupListener;
22  import org.apache.myfaces.extensions.validator.core.DefaultExtValCoreConfiguration;
23  import org.apache.myfaces.extensions.validator.core.ExtValCoreConfiguration;
24  import org.apache.myfaces.extensions.validator.test.trinidad.AbstractTrinidadSupportTestCase;
25  import org.apache.myfaces.trinidad.component.core.CoreForm;
26  import org.apache.myfaces.trinidad.component.core.input.CoreInputHidden;
27  import org.apache.myfaces.trinidad.component.core.input.CoreInputText;
28  import org.apache.myfaces.trinidad.component.core.output.CoreOutputLabel;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  /**
33   */
34  public class TrinidadComponentInitializerTest extends AbstractTrinidadSupportTestCase
35  {
36      private CoreInputText inputComponent;
37  
38      private CoreInputHidden inputHidden;
39  
40      private CoreOutputLabel outputLabel;
41  
42      private DataBean bean;
43  
44      @Override
45      protected void invokeStartupListeners()
46      {
47          // We use the property validation module, so initialize it to have the strategies.
48          new PropertyValidationModuleStartupListener()
49          {
50              private static final long serialVersionUID = 423076920926752646L;
51  
52              @Override
53              protected void init()
54              {
55                  super.initModuleConfig();
56                  super.init();
57              }
58          }.init();
59          super.invokeStartupListeners();
60      }
61  
62  
63      @Override
64      public void setUpTestCase()
65      {
66  
67  
68          super.setUpTestCase();
69  
70          CoreForm form = new CoreForm();
71          facesContext.getViewRoot().getChildren().add(form);
72  
73          outputLabel = new CoreOutputLabel();
74          //outputLabel.setParent(form);
75          // For Trinidad we shouldn't set the parent our selves, it gives errors when running from SureFire.
76          form.getChildren().add(outputLabel);
77  
78          inputComponent = new CoreInputText();
79          inputComponent.setId("input");
80          form.getChildren().add(inputComponent);
81  
82          inputHidden = new CoreInputHidden();
83          inputHidden.setId("hidden");
84          form.getChildren().add(inputHidden);
85  
86  
87          bean = new DataBean();
88          facesContext.getExternalContext().getRequestMap().put("testBean", bean);
89  
90      }
91  
92      @Test
93      public void testInitializerStrong() throws Exception
94      {
95          createValueBinding(inputComponent, "value", "#{testBean.property1}");
96          outputLabel.setFor("input");
97          outputLabel.encodeAll(facesContext);
98          Assert.assertTrue(outputLabel.isShowRequired());
99  
100         inputComponent.encodeAll(facesContext);
101         Assert.assertTrue(inputComponent.isRequired());
102     }
103 
104     @Test
105     public void testInitializerRequiredIf() throws Exception
106     {
107         bean.setProperty1("X");
108         createValueBinding(inputComponent, "value", "#{testBean.property2}");
109         inputComponent.encodeAll(facesContext);
110         // although at rendering the property1 has a value, it doesn't make it required.
111         // Because at postback, the field could be empty and thus property2 is not required anymore.
112         Assert.assertFalse(inputComponent.isRequired());
113     }
114 
115     @Test
116     public void testInitializerWeak() throws Exception
117     {
118         createValueBinding(inputComponent, "value", "#{testBean.property3}");
119         inputComponent.encodeAll(facesContext);
120         Assert.assertTrue(inputComponent.isRequired());
121     }
122 
123     @Test
124     public void testInitializerNone() throws Exception
125     {
126         createValueBinding(inputComponent, "value", "#{testBean.property4}");
127         inputComponent.encodeAll(facesContext);
128         Assert.assertFalse(inputComponent.isRequired());
129     }
130 
131     @Test
132     public void testInitializerReadOnly() throws Exception
133     {
134         createValueBinding(inputComponent, "value", "#{testBean.property1}");
135         inputComponent.setReadOnly(true);
136         inputComponent.encodeAll(facesContext);
137         Assert.assertFalse(inputComponent.isRequired());
138     }
139 
140     @Test
141     public void testInitializerDisabled() throws Exception
142     {
143         createValueBinding(inputComponent, "value", "#{testBean.property1}");
144         inputComponent.setDisabled(true);
145         inputComponent.encodeAll(facesContext);
146         Assert.assertFalse(inputComponent.isRequired());
147     }
148 
149     @Test
150     public void testInitializerInputHidden() throws Exception
151     {
152         createValueBinding(inputHidden, "value", "#{testBean.property1}");
153         inputComponent.encodeAll(facesContext);
154         Assert.assertFalse(inputComponent.isRequired());
155     }
156 
157     @Test
158     public void testInitializerNotActivated() throws Exception
159     {
160 
161         // Here we override it again so that we are back in the defult state of not handling the initialization.
162         DefaultExtValCoreConfiguration.overruleActivateRequiredInitialization(false, true);
163         createValueBinding(inputComponent, "value", "#{testBean.property1}");
164         inputComponent.encodeAll(facesContext);
165         Assert.assertFalse(inputComponent.isRequired());
166     }
167 
168     @Test
169     public void testInitializerNoValidationOfEmptyFields() throws Exception
170     {
171 
172         // Use temporary a version of the config that specifies that empty fields shouldn't be validated.
173         ExtValCoreConfiguration currentModuleConfig = ExtValCoreConfiguration.get();
174         ExtValCoreConfiguration.use(new DefaultExtValCoreConfiguration()
175         {
176             @Override
177             public boolean validateEmptyFields()
178             {
179                 return false;
180             }
181         }, true);
182 
183         createValueBinding(inputComponent, "value", "#{testBean.property1}");
184         inputComponent.encodeAll(facesContext);
185         Assert.assertFalse(inputComponent.isRequired());
186 
187         // set old initialization back for other test cases.
188         ExtValCoreConfiguration.use(currentModuleConfig, true);
189 
190 
191     }
192 
193 }