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 java.io.IOException;
22  
23  import javax.el.ValueExpression;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.el.CompositeComponentExpressionHolder;
27  
28  import org.apache.myfaces.el.unified.resolver.CompositeComponentELResolver;
29  import org.apache.myfaces.view.facelets.FaceletTestCase;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  /**
34   * Tests for using BeanValidation in conjunction with Composite Components.
35   * 
36   * @author Jakob Korherr (latest modification by $Author$)
37   * @version $Revision$ $Date$
38   */
39  public class CompositeComponentBeanValidationTest extends FaceletTestCase
40  {
41  
42      /**
43       * Tests the case that a composite component includes an editableValueHolder
44       * attribute which points to a property that is validated via BeanValidation.
45       * In this case the BeanValidator would get the ValueExpression #{cc.attrs.input}
46       * which he does not need. He needs the actual ValueExpression, thus he has
47       * to get it from the composite component. To accomplish this, he uses the
48       * CompositeComponentExpressionHolder interface.
49       * 
50       * @throws IOException
51       */
52      @Test
53      public void testCompositeComponentExpressionHolder() throws IOException
54      {
55          UIViewRoot root = facesContext.getViewRoot();
56          vdl.buildView(facesContext, root, "testSimpleEditableValueHolder.xhtml");
57          
58          UIComponent form = root.findComponent("testForm1");
59          UIComponent compositeComponent = form.getChildren().get(0);
60  
61          // "resolve" #{cc.attrs}
62          CompositeComponentELResolver resolver = new CompositeComponentELResolver();
63          Object attrs = resolver.getValue(facesContext.getELContext(), compositeComponent, "attrs");
64          
65          // the resolved value has to be a CompositeComponentExpressionHolder
66          Assert.assertTrue(attrs instanceof CompositeComponentExpressionHolder);
67          
68          // get the actual ValueExpression which is needed by the BeanValidator
69          ValueExpression valueExpression 
70                  = ((CompositeComponentExpressionHolder) attrs).getExpression("input");
71          
72          // the expression String from the VE has to be #{myBean.input}
73          Assert.assertTrue("#{myBean.input}".equals(valueExpression.getExpressionString()));
74      }
75      
76  }