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.trinidad.component;
20  
21  import javax.el.ValueExpression;
22  import javax.faces.el.ValueBinding;
23  
24  import org.jmock.Mock;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  import junit.textui.TestRunner;
29  
30  /**
31   * Unit tests for UIXInput
32   *
33   */
34  public class UIXInputTest extends UIComponentTestCase
35  {
36    /**
37     * @param testName  the unit test name
38     */
39    public UIXInputTest(
40      String testName)
41    {
42      super(testName);
43    }
44    
45    @Override
46    protected void setUp() throws Exception
47    {
48      super.setUp();
49    }
50    
51    @Override
52    protected void tearDown() throws Exception
53    {
54      super.tearDown();
55    }
56    
57    public static Test suite()
58    {
59      return new TestSuite(UIXInputTest.class);
60    }
61  
62    /**
63     * Tests the initial values for the component attributes.
64     */
65    public void testInitialAttributeValues()
66    {
67      UIXInput input = new UIXInput();
68      assertFalse(input.isLocalValueSet());
69      assertNull(input.getSubmittedValue());
70      assertNull(input.getLocalValue());
71      assertNull(input.getValue());
72    }
73  
74  
75    public void testValueExpressions()
76    {
77      Mock mockExpression = mock(ValueExpression.class);
78      ValueExpression expr = (ValueExpression) mockExpression.proxy();
79      mockExpression.expects(atLeastOnce()).method("getValue").will(returnValue("socks"));
80      mockExpression.expects(atLeastOnce()).method("isLiteralText").will(returnValue(Boolean.TRUE));
81  
82      UIXInput input = new UIXInput();
83      input.setValueExpression("value", expr);
84      assertEquals("socks", input.getValue());
85      input.setValue("shoes");
86      assertEquals("shoes", input.getValue());
87      input.setValueExpression("value", null);
88      assertEquals("shoes", input.getValue());    
89    }
90  
91    /**
92     * Tests the transparency of the component attribute by comparing
93     * bean accessor and mutator methods with attribute map accessor
94     * and mutator methods.
95     */
96    public void testAttributeTransparency()
97    {
98      UIXInput input = new UIXInput();
99      doTestAttributeTransparency(input, "localValueSet", Boolean.TRUE, Boolean.FALSE);
100     doTestAttributeTransparency(input, "value", "foo", "fum");
101     doTestAttributeTransparency(input, "submittedValue", "bar", "baz");
102   }
103 
104   /**
105    * Tests to make sure the setValue(), isLocalValueSet() contract is
106    * preserved.
107    */
108   public void testLocalValue()
109   {
110     UIXInput input = new UIXInput();
111     assertFalse(input.isLocalValueSet());
112     assertNull(input.getLocalValue());
113     assertNull(input.getValue());
114     
115     // setup a valueBinding
116     Mock mockBinding = mock(ValueBinding.class);
117     ValueBinding binding = (ValueBinding) mockBinding.proxy();
118     mockBinding.expects(atLeastOnce()).method("getValue").will(returnValue(Boolean.TRUE));//  setupGetValue(Boolean.TRUE);
119     
120     input.setValueBinding("value", binding);
121     assertFalse(input.isLocalValueSet());
122     assertNull(input.getLocalValue());
123     assertEquals(Boolean.TRUE, input.getValue());
124     
125     // now set a local value
126     input.setValue("foo");
127     assertTrue(input.isLocalValueSet());
128     assertEquals("foo", input.getLocalValue());
129     assertEquals("foo", input.getValue());
130   }
131 
132   public static void main(String[] args)
133   {
134     TestRunner.run(UIXInputTest.class);
135   }
136 
137 }