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 javax.el.ELContext;
22  import javax.el.ValueExpression;
23  import javax.faces.el.ValueBinding;
24  
25  import org.apache.commons.beanutils.PropertyUtils;
26  import org.apache.shale.test.mock.MockFacesContext12;
27  import static org.easymock.EasyMock.*;
28  import org.easymock.classextension.EasyMock;
29  import org.easymock.classextension.IMocksControl;
30  import static org.testng.Assert.*;
31  import org.testng.annotations.AfterMethod;
32  import org.testng.annotations.BeforeMethod;
33  import org.testng.annotations.BeforeTest;
34  import org.testng.annotations.Test;
35  
36  /**
37   * @author Mathias Broekelmann (latest modification by $Author: skitching $)
38   * @version $Revision: 676298 $ $Date: 2008-07-13 05:31:48 -0500 (Sun, 13 Jul 2008) $
39   */
40  public abstract class AbstractUIComponentPropertyTest<T extends Object>
41  {
42      private final String _property;
43      private final T _defaultValue;
44      private final T[] _testValues;
45  
46      private IMocksControl _mocksControl;
47      private MockFacesContext12 _facesContext;
48      private ValueBinding _valueBinding;
49      private ValueExpression _valueExpression;
50      private ELContext _elContext;
51      private UIComponent _component;
52  
53      public AbstractUIComponentPropertyTest(String property, T defaultValue, T... testValues)
54      {
55          _property = property;
56          _defaultValue = defaultValue;
57          _testValues = testValues;
58      }
59  
60      @BeforeMethod
61      protected void setUp() throws Exception
62      {
63          _mocksControl = EasyMock.createControl();
64          _facesContext = new MockFacesContext12();
65          _elContext = _mocksControl.createMock(ELContext.class);
66          _facesContext.setELContext(_elContext);
67          _valueBinding = _mocksControl.createMock(ValueBinding.class);
68          _valueExpression = _mocksControl.createMock(ValueExpression.class);
69          _component = createComponent();
70      }
71      
72      protected IMocksControl getMocksControl()
73      {
74          return _mocksControl;
75      }
76  
77      protected abstract UIComponent createComponent();
78  
79      @Test
80      public void testDefaultValue() throws Exception
81      {
82          assertEquals(_defaultValue, PropertyUtils.getProperty(_component, _property));
83      }
84  
85      @Test
86      public void testExplicitValue() throws Exception
87      {
88          for (T testValue : _testValues)
89          {
90              PropertyUtils.setProperty(_component, _property, testValue);
91              assertEquals(testValue, PropertyUtils.getProperty(_component, _property));
92          }
93      }
94  
95      @Test
96      public void testExpressionWithLiteralTextValue() throws Exception
97      {
98          for (T testValue : _testValues)
99          {
100             expect(_valueExpression.isLiteralText()).andReturn(true);
101             expect(_valueExpression.getValue(eq(_facesContext.getELContext()))).andReturn(testValue);
102             _mocksControl.replay();
103             _component.setValueExpression(_property, _valueExpression);
104             assertEquals(testValue, PropertyUtils.getProperty(_component, _property));
105             _mocksControl.reset();
106         }
107     }
108 
109     @Test
110     public void testExpressionValue() throws Exception
111     {
112         for (T testValue : _testValues)
113         {
114             expect(_valueExpression.isLiteralText()).andReturn(false);
115             _mocksControl.replay();
116             _component.setValueExpression(_property, _valueExpression);
117             _mocksControl.reset();
118             expect(_valueExpression.getValue(eq(_facesContext.getELContext()))).andReturn(testValue);
119             _mocksControl.replay();
120             assertEquals(testValue, PropertyUtils.getProperty(_component, _property));
121             _mocksControl.reset();
122         }
123     }
124 }