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.composite;
20  
21  import javax.el.ExpressionFactory;
22  import javax.faces.component.UIComponent;
23  import javax.faces.component.UIInput;
24  import javax.faces.component.UINamingContainer;
25  import javax.faces.component.UIViewRoot;
26  
27  import org.apache.myfaces.config.RuntimeConfig;
28  import org.apache.myfaces.test.mock.MockExternalContext;
29  import org.apache.myfaces.view.facelets.FaceletTestCase;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  public class CompositeComponentValidatorTestCase extends FaceletTestCase
34  {
35      @Override
36      protected void setupComponents() throws Exception
37      {
38          super.setupComponents();
39          application.addComponent(CompositeTestComponent.class.getName(), 
40                  CompositeTestComponent.class.getName());
41          application.addComponent(SimpleComponent.class.getName(), SimpleComponent.class.getName());
42      }
43  
44      @Override
45      protected void setUpServletObjects() throws Exception
46      {
47          super.setUpServletObjects();
48          servletContext.addInitParameter("javax.faces.FACELETS_LIBRARIES", "/test-facelet.taglib.xml");
49      }
50      
51      @Override
52      protected ExpressionFactory createExpressionFactory()
53      {
54          return new org.apache.el.ExpressionFactoryImpl();
55      }
56  
57      @Test
58      public void testSimpleValidatorTarget() throws Exception
59      {
60          MockAttributeBean bean = new MockAttributeBean();
61          
62          facesContext.getExternalContext().getRequestMap().put("bean",
63                  bean);
64          
65          UIViewRoot root = facesContext.getViewRoot();
66          vdl.buildView(facesContext, root, "testSimpleAttributeValidatorTarget.xhtml");
67  
68          
69          UIComponent panelGroup1 = root.findComponent("testGroup1");
70          Assert.assertNotNull(panelGroup1);
71          UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
72          Assert.assertNotNull(compositeComponent1);
73          UIInput input1 = (UIInput) compositeComponent1.findComponent("testComponent");
74          Assert.assertNotNull(input1);
75          Assert.assertNotNull(input1.getValidators());
76          Assert.assertEquals(1, input1.getValidators().length);
77          
78          bean.setValidator1Called(false);
79          input1.getValidators()[0].validate(facesContext, input1, "x");
80          Assert.assertTrue(bean.isValidator1Called());
81      }
82  
83      @Test
84      public void testCompositeValidatorTarget() throws Exception
85      {
86          MockAttributeBean bean = new MockAttributeBean();
87          
88          facesContext.getExternalContext().getRequestMap().put("bean",
89                  bean);
90          
91          UIViewRoot root = facesContext.getViewRoot();
92          vdl.buildView(facesContext, root, "testCompositeAttributeValidatorTarget.xhtml");
93  
94          UIComponent panelGroup1 = root.findComponent("testGroup1");
95          Assert.assertNotNull(panelGroup1);
96          UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
97          Assert.assertNotNull(compositeComponent1);
98          
99          UINamingContainer compositeComponent1inner = (UINamingContainer) compositeComponent1.findComponent("simpleAttributeMethodExpressionTarget");
100         Assert.assertNotNull(compositeComponent1inner);
101         UIInput input1 = (UIInput) compositeComponent1inner.findComponent("testComponent");
102         Assert.assertNotNull(input1);
103         Assert.assertNotNull(input1.getValidators());
104         Assert.assertEquals(1, input1.getValidators().length);
105         
106         bean.setValidator1Called(false);
107         input1.getValidators()[0].validate(facesContext, input1, "x");
108         Assert.assertTrue(bean.isValidator1Called());
109 
110     }
111     
112     @Test
113     public void testSimpleValidatorTarget2() throws Exception
114     {
115         MockAttributeBean bean = new MockAttributeBean();
116         
117         facesContext.getExternalContext().getRequestMap().put("bean",
118                 bean);
119         
120         UIViewRoot root = facesContext.getViewRoot();
121         vdl.buildView(facesContext, root, "testSimpleAttributeValidatorTarget2.xhtml");
122 
123         
124         UIComponent panelGroup1 = root.findComponent("testGroup1");
125         Assert.assertNotNull(panelGroup1);
126         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
127         Assert.assertNotNull(compositeComponent1);
128         UIInput input1 = (UIInput) compositeComponent1.findComponent("testComponent");
129         Assert.assertNotNull(input1);
130         Assert.assertNotNull(input1.getValidators());
131         Assert.assertEquals(1, input1.getValidators().length);
132         
133         bean.setValidator1Called(false);
134         input1.getValidators()[0].validate(facesContext, input1, "x");
135         Assert.assertTrue(bean.isValidator1Called());
136         
137         UIInput input1n = (UIInput) compositeComponent1.findComponent("testComponentNoTarget");
138         
139         Assert.assertNotNull(input1n);
140         Assert.assertNotNull(input1n.getValidators());
141         Assert.assertEquals(1, input1n.getValidators().length);
142         
143         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
144         bean.setValidator1Called(false);
145         input1n.getValidators()[0].validate(facesContext, input1n, "x");
146         Assert.assertTrue(bean.isValidator1Called());
147         compositeComponent1.popComponentFromEL(facesContext);
148         
149     }
150 
151     @Test
152     public void testCompositeValidatorTarget2() throws Exception
153     {
154         MockAttributeBean bean = new MockAttributeBean();
155         
156         facesContext.getExternalContext().getRequestMap().put("bean",
157                 bean);
158         
159         UIViewRoot root = facesContext.getViewRoot();
160         vdl.buildView(facesContext, root, "testCompositeAttributeValidatorTarget2.xhtml");
161 
162         UIComponent panelGroup1 = root.findComponent("testGroup1");
163         Assert.assertNotNull(panelGroup1);
164         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
165         Assert.assertNotNull(compositeComponent1);
166         
167         UINamingContainer compositeComponent1target = (UINamingContainer) compositeComponent1.findComponent("simpleAttributeMethodExpressionTarget");
168         Assert.assertNotNull(compositeComponent1target);
169         UIInput input1target = (UIInput) compositeComponent1target.findComponent("testComponent");
170         Assert.assertNotNull(input1target);
171         Assert.assertNotNull(input1target.getValidators());
172         Assert.assertEquals(1, input1target.getValidators().length);
173         
174         bean.setValidator1Called(false);
175         input1target.getValidators()[0].validate(facesContext, input1target, "x");
176         Assert.assertTrue(bean.isValidator1Called());
177 
178         UIInput input1notarget = (UIInput) compositeComponent1target.findComponent("testComponentNoTarget");
179         Assert.assertNotNull(input1notarget);
180         Assert.assertNotNull(input1notarget.getValidators());
181         Assert.assertEquals(1, input1notarget.getValidators().length);
182         
183         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
184         compositeComponent1target.pushComponentToEL(facesContext, compositeComponent1target);
185         bean.setValidator1Called(false);
186         input1notarget.getValidators()[0].validate(facesContext, input1notarget, "x");
187         Assert.assertTrue(bean.isValidator1Called());
188         compositeComponent1target.popComponentFromEL(facesContext);
189         compositeComponent1.popComponentFromEL(facesContext);
190         
191         UINamingContainer compositeComponent1notarget = (UINamingContainer) compositeComponent1.findComponent("simpleAttributeMethodExpressionNoTarget");
192         Assert.assertNotNull(compositeComponent1notarget);
193         UIInput inputnotarget1target = (UIInput) compositeComponent1notarget.findComponent("testComponent");
194         Assert.assertNotNull(inputnotarget1target);
195         Assert.assertNotNull(inputnotarget1target.getValidators());
196         Assert.assertEquals(1, inputnotarget1target.getValidators().length);
197         
198         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
199         compositeComponent1notarget.pushComponentToEL(facesContext, compositeComponent1notarget);
200         bean.setValidator1Called(false);
201         inputnotarget1target.getValidators()[0].validate(facesContext, inputnotarget1target, "x");
202         Assert.assertTrue(bean.isValidator1Called());
203         compositeComponent1notarget.popComponentFromEL(facesContext);
204         compositeComponent1.popComponentFromEL(facesContext);
205         
206         UIInput inputnotarget1notarget = (UIInput) compositeComponent1notarget.findComponent("testComponentNoTarget");
207         Assert.assertNotNull(inputnotarget1notarget);
208         Assert.assertNotNull(inputnotarget1notarget.getValidators());
209         Assert.assertEquals(1, inputnotarget1notarget.getValidators().length);
210         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
211         compositeComponent1notarget.pushComponentToEL(facesContext, compositeComponent1notarget);
212         bean.setValidator1Called(false);
213         inputnotarget1notarget.getValidators()[0].validate(facesContext, inputnotarget1notarget, "x");
214         Assert.assertTrue(bean.isValidator1Called());
215         compositeComponent1notarget.popComponentFromEL(facesContext);
216         compositeComponent1.popComponentFromEL(facesContext);
217         
218     }
219     
220     @Test
221     public void testSimpleValidatorNoTarget() throws Exception
222     {
223         MockAttributeBean bean = new MockAttributeBean();
224         
225         facesContext.getExternalContext().getRequestMap().put("bean",
226                 bean);
227         
228         UIViewRoot root = facesContext.getViewRoot();
229         vdl.buildView(facesContext, root, "testSimpleAttributeValidatorNoTarget.xhtml");
230 
231         UIComponent panelGroup1 = root.findComponent("testGroup1");
232         Assert.assertNotNull(panelGroup1);
233         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
234         Assert.assertNotNull(compositeComponent1);
235         UIInput input1 = (UIInput) compositeComponent1.findComponent("testComponentNoTarget");
236         Assert.assertNotNull(input1);
237         Assert.assertNotNull(input1.getValidators());
238         Assert.assertEquals(1, input1.getValidators().length);
239         
240         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
241         input1.pushComponentToEL(facesContext,  input1);
242         bean.setValidator1Called(false);
243         input1.getValidators()[0].validate(facesContext, input1, "x");
244         Assert.assertTrue(bean.isValidator1Called());
245         input1.popComponentFromEL(facesContext);
246         compositeComponent1.popComponentFromEL(facesContext);
247 
248     }
249     
250     @Test
251     public void testCompositeValidatorNoTarget() throws Exception
252     {
253         MockAttributeBean bean = new MockAttributeBean();
254         
255         facesContext.getExternalContext().getRequestMap().put("bean",
256                 bean);
257         
258         UIViewRoot root = facesContext.getViewRoot();
259         vdl.buildView(facesContext, root, "testCompositeAttributeValidatorNoTarget.xhtml");
260 
261         UIComponent panelGroup1 = root.findComponent("testGroup1");
262         Assert.assertNotNull(panelGroup1);
263         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
264         Assert.assertNotNull(compositeComponent1);
265 
266         UINamingContainer compositeComponent1inner = (UINamingContainer) compositeComponent1.findComponent("simpleAttributeActionMethodExpressionNoTarget");
267         Assert.assertNotNull(compositeComponent1inner);
268         UIInput testComponentNoTarget1 = (UIInput) compositeComponent1inner.findComponent("testComponentNoTarget");
269         Assert.assertNotNull(testComponentNoTarget1);
270         Assert.assertNotNull(testComponentNoTarget1.getValidators());
271         Assert.assertEquals(1, testComponentNoTarget1.getValidators().length);
272 
273         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
274         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1inner);
275         bean.setValidator1Called(false);
276         testComponentNoTarget1.getValidators()[0].validate(facesContext, testComponentNoTarget1, "x");
277         Assert.assertTrue(bean.isValidator1Called());
278         compositeComponent1inner.popComponentFromEL(facesContext);
279         compositeComponent1.popComponentFromEL(facesContext);
280 
281     }
282 
283     @Test
284     public void testCompositeValidatorNoTarget2() throws Exception
285     {
286         MockAttributeBean bean = new MockAttributeBean();
287         
288         facesContext.getExternalContext().getRequestMap().put("bean",
289                 bean);
290         
291         UIViewRoot root = facesContext.getViewRoot();
292         vdl.buildView(facesContext, root, "testCompositeAttributeValidatorNoTarget2.xhtml");
293         
294         UIComponent panelGroup1 = root.findComponent("testGroup1");
295         Assert.assertNotNull(panelGroup1);
296         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
297         Assert.assertNotNull(compositeComponent1);
298         
299         UINamingContainer compositeComponent1n1 = (UINamingContainer) compositeComponent1.findComponent("compositeAttributeMethodExpressionNoTarget");
300         Assert.assertNotNull(compositeComponent1n1);
301         UINamingContainer compositeComponent1n2 = (UINamingContainer) compositeComponent1n1.findComponent("simpleAttributeMethodExpressionNoTarget");
302         Assert.assertNotNull(compositeComponent1n2);
303         UIInput input1 = (UIInput) compositeComponent1n2.findComponent("testComponentNoTarget");
304         Assert.assertNotNull(input1);
305         Assert.assertNotNull(input1.getValidators());
306         Assert.assertEquals(1, input1.getValidators().length);
307         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
308         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1n2);
309         bean.setValidator1Called(false);
310         input1.getValidators()[0].validate(facesContext, input1, "x");
311         Assert.assertTrue(bean.isValidator1Called());
312         compositeComponent1n2.popComponentFromEL(facesContext);
313         compositeComponent1.popComponentFromEL(facesContext);
314     }
315 }