1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
75
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
111
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
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
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
188 ExtValCoreConfiguration.use(currentModuleConfig, true);
189
190
191 }
192
193 }