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 javax.faces.component;
20  
21  import static org.easymock.EasyMock.*;
22  
23  import javax.el.ValueExpression;
24  import javax.faces.application.FacesMessage;
25  import javax.faces.convert.Converter;
26  import javax.faces.convert.ConverterException;
27  import javax.faces.validator.Validator;
28  import javax.faces.validator.ValidatorException;
29  
30  import junit.framework.Test;
31  
32  import org.apache.shale.test.base.AbstractJsfTestCase;
33  import org.apache.shale.test.el.MockValueExpression;
34  
35  public class UIInputTest extends AbstractJsfTestCase{
36      
37      private Converter mockConverter;
38      private Validator mockValidator;
39      private UIInput input;
40  
41      public UIInputTest(String name) {
42          super(name);
43      }
44  
45      protected void setUp() throws Exception {
46          super.setUp();
47          input = new UIInput();
48          input.setId("testId");
49          mockConverter = createMock(Converter.class);
50          mockValidator = createMock(Validator.class);
51      }
52      
53      protected void tearDown() throws Exception {
54          super.tearDown();
55          input = null;
56          mockConverter = null;
57          mockValidator = null;
58      }
59  
60      public void testWhenSpecifiedConverterMessageIsUsedInCaseConverterExceptionOccurs() {
61          input.setConverterMessage("Cannot convert");
62          
63          input.setConverter(mockConverter);
64          expect(mockConverter.getAsObject(facesContext, input, "xxx")).andThrow(new ConverterException());
65          replay(mockConverter);
66          
67          input.getConvertedValue(facesContext, "xxx");
68          verify(mockConverter);
69          
70          assertFalse(input.isValid());
71          assertNotNull(facesContext.getMessages("testId"));
72          
73          FacesMessage message = (FacesMessage) facesContext.getMessages("testId").next();
74          assertEquals(message.getDetail(), "Cannot convert");
75          assertEquals(message.getSummary(), "Cannot convert");
76      }
77      
78      public void testWhenSpecifiedValidatorMessageIsUsedInCaseValidatorExceptionOccurs() {
79          input.setValidatorMessage("Cannot validate");
80          
81          input.addValidator(mockValidator);
82          mockValidator.validate(facesContext, input, "xxx");
83          expectLastCall().andThrow(new ValidatorException(new FacesMessage()));
84          replay(mockValidator);
85          
86          input.validateValue(facesContext, "xxx");
87          verify(mockValidator);
88          
89          assertFalse(input.isValid());
90          assertNotNull(facesContext.getMessages("testId"));
91          
92          FacesMessage message = (FacesMessage) facesContext.getMessages("testId").next();
93          assertEquals(message.getDetail(), "Cannot validate");
94          assertEquals(message.getSummary(), "Cannot validate");
95      }
96      
97      public void testUpdateModelSetsTheLocalValueToModelValue() {
98          input.setValue("testValue");
99          
100         ValueExpression expression = new MockValueExpression("#{requestScope.id}",String.class);
101         input.setValueExpression("value", expression);
102         
103         input.updateModel(facesContext);
104         
105         String updatedValue = expression.getValue(facesContext.getELContext()).toString();
106         assertEquals("testValue", updatedValue);
107     }
108 }