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.faces.component.UIComponent;
22  import javax.faces.component.UIViewRoot;
23  import javax.faces.context.FacesContext;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  import junit.textui.TestRunner;
28  
29  import org.apache.myfaces.trinidad.component.UIComponentTestCase;
30  import org.jmock.Mock;
31  
32  
33  /**
34   * Unit tests for UIXSwitcher.
35   *
36   */
37  public class UIXSwitcherTest extends UIComponentTestCase
38  {
39    /**
40     * Creates a new UIXSwitcherTest.
41     *
42     * @param testName  the unit test name
43     */
44    public UIXSwitcherTest(
45      String testName)
46    {
47      super(testName);
48    }
49    
50    @Override
51    protected void setUp() throws Exception
52    {
53      super.setUp();
54    }
55    
56    @Override
57    protected void tearDown() throws Exception
58    {
59      super.tearDown();
60    }
61    
62    public static Test suite()
63    {
64      return new TestSuite(UIXSwitcherTest.class);
65    }
66  
67    /**
68     * Tests the initial values for the component attributes.
69     */
70    public void testInitialAttributeValues()
71    {
72      UIXSwitcher switcher = new UIXSwitcher();
73  
74      assertNull(switcher.getFacetName());
75      assertNull(switcher.getDefaultFacet());
76    }
77  
78    /**
79     * Tests the transparency of the component attribute by comparing
80     * bean accessor and mutator methods with attribute map accessor
81     * and mutator methods.
82     */
83    public void testAttributeTransparency()
84    {
85      UIXSwitcher component = new UIXSwitcher();
86  
87      doTestAttributeTransparency(component, "defaultFacet",
88                                  "foo", "notFoo");
89      doTestAttributeTransparency(component, "facetName",
90                                  "bar", "notBar");
91    }
92  
93    /**
94     * Tests the apply-request-values lifecycle phase.
95     */
96    public void testApplyRequestValues()
97    {
98      UIXSwitcher switcher = new UIXSwitcher();
99      switcher.setFacetName("foo");
100     doTestApplyRequestValues(switcher);
101 
102     switcher = new UIXSwitcher();
103     switcher.setFacetName("foo");
104     switcher.setRendered(false);
105     doTestApplyRequestValues(switcher);
106   }
107 
108   @SuppressWarnings("unchecked")
109   @Override
110   protected void doTestApplyRequestValues(
111     FacesContext context,
112     UIViewRoot   root,
113     UIComponent  component)
114   {
115 
116     Mock mockUIComponent = createMockUIComponent();
117     UIComponent fooChild = (UIComponent) mockUIComponent.proxy();
118     
119     // JavaServer Faces 1.0 Specification, section 2.2.2
120     // During the apply-request-values phase,
121     // only the processDecodes lifecycle method may be called.
122     if (component.isRendered()){
123       mockUIComponent.stubs().method("processDecodes").with(eq(facesContext));
124     }
125 
126     // These children will never get called
127     Mock mockBarChild = createMockUIComponent();
128     UIComponent barChild = (UIComponent) mockBarChild.proxy();
129     
130     Mock mockOrdinaryChild = createMockUIComponent();
131     UIComponent ordinaryChild = (UIComponent) mockOrdinaryChild.proxy();
132 
133     // construct the UIComponent tree and
134     // execute the apply-request-values lifecycle phase
135     root.getChildren().add(component);
136     component.getFacets().put("foo", fooChild);
137     component.getFacets().put("bar", barChild);
138     component.getChildren().add(ordinaryChild);
139     root.processDecodes(context);
140 
141     mockUIComponent.verify();
142     mockBarChild.verify();
143     mockOrdinaryChild.verify();
144   }
145 
146 
147   /**
148    * Tests the process-validations lifecycle phase.
149    */
150   public void testProcessValidations()
151   {
152     UIXSwitcher switcher = new UIXSwitcher();
153     switcher.setFacetName("foo");
154     doTestProcessValidations(switcher);
155   }
156 
157   @SuppressWarnings("unchecked")
158   @Override
159   protected void doTestProcessValidations(
160     FacesContext context,
161     UIViewRoot   root,
162     UIComponent  component)
163   {
164     
165     Mock mockUIComponent = createMockUIComponent();//mock(UIComponent.class);
166     UIComponent fooChild = (UIComponent) mockUIComponent.proxy();
167     
168     // JavaServer Faces 1.0 Specification, section 2.2.2
169     // During the apply-request-values phase,
170     // only the processDecodes lifecycle method may be called.
171     mockUIComponent.expects(once()).method("processValidators");
172 
173     // These children will never get called
174     Mock mockBarChild = createMockUIComponent();
175     UIComponent barChild = (UIComponent) mockBarChild.proxy();
176     
177     Mock mockOrdinaryChild = createMockUIComponent();
178     UIComponent ordinaryChild = (UIComponent) mockOrdinaryChild.proxy(); 
179 
180     // construct the UIComponent tree and
181     // execute the apply-request-values lifecycle phase
182     root.getChildren().add(component);
183     component.getFacets().put("foo", fooChild);
184     component.getFacets().put("bar", barChild);
185     component.getChildren().add(ordinaryChild);
186     root.processValidators(context);
187 
188     mockUIComponent.verify();
189     mockBarChild.verify();
190     mockOrdinaryChild.verify();
191   }
192 
193 
194 
195   /**
196    * Tests the updatem-model-values lifecycle phase.
197    */
198   public void testUpdateModelValues()
199   {
200     UIXSwitcher switcher = new UIXSwitcher();
201     switcher.setFacetName("foo");
202     doTestUpdateModelValues(switcher);
203   }
204 
205 
206   @SuppressWarnings("unchecked")
207   @Override
208   protected void doTestUpdateModelValues(
209     FacesContext context,
210     UIViewRoot   root,
211     UIComponent  component)
212   {
213     Mock mockUIComponent = createMockUIComponent();
214     UIComponent fooChild = (UIComponent) mockUIComponent.proxy();
215     // JavaServer Faces 1.0 Specification, section 2.2.2
216     // During the apply-request-values phase,
217     // only the processDecodes lifecycle method may be called.
218     mockUIComponent.expects(once()).method("processUpdates");
219 
220     // These children will never get called
221     Mock mockBarChild = createMockUIComponent();
222     UIComponent barChild = (UIComponent) mockBarChild.proxy();
223     
224     Mock mockOrdinaryChild = createMockUIComponent();
225     UIComponent ordinaryChild = (UIComponent) mockOrdinaryChild.proxy();
226 
227     // construct the UIComponent tree and
228     // execute the apply-request-values lifecycle phase
229     root.getChildren().add(component);
230     component.getFacets().put("foo", fooChild);
231     component.getFacets().put("bar", barChild);
232     component.getChildren().add(ordinaryChild);
233     root.processUpdates(context);
234 
235     mockUIComponent.verify();
236     mockBarChild.verify();
237     mockOrdinaryChild.verify();
238   }
239 
240   public static void main(String[] args)
241   {
242     TestRunner.run(UIXSwitcherTest.class);
243   }
244 
245   @Override
246   protected boolean isRendererUsed()
247   {
248     return false;
249   }
250 }