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.core;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.myfaces.trinidad.component.UIComponentTestCase;
30  
31  /**
32   * Unit tests for CoreForm.
33   *
34   */
35  public class CoreFormTest extends UIComponentTestCase
36  {
37    /**
38     * Creates a new CoreFormTest.
39     *
40     * @param testName  the unit test name
41     */
42    public CoreFormTest(
43      String testName)
44    {
45      super(testName);
46    }
47    
48    @Override
49    protected void setUp() throws Exception
50    {
51      super.setUp();
52    }
53    
54    @Override
55    protected void tearDown() throws Exception
56    {
57      super.tearDown();
58    }
59    
60    public static Test suite()
61    {
62      return new TestSuite(CoreFormTest.class);
63    }
64  
65    /**
66     * Tests the initial values for the component attributes.
67     */
68    public void testInitialAttributeValues()
69    {
70      CoreForm form = new CoreForm();
71      assertEquals(false, form.isUsesUpload());
72      assertEquals(false, form.isTransient());
73      assertEquals(false, form.isSubmitted());
74      assertEquals(true, form.isRendered());
75    }
76  
77    /**
78     * Tests that the constants stored on the component class definition
79     * are identical to the instances retrieved dynamically from the component
80     * storage type.
81     */
82    public void testTypeKeyInstances()
83    {
84      CoreForm form = new CoreForm();
85      assertSame(form.getFacesBean().getType(),
86                 CoreForm.TYPE);
87      assertSame(form.getFacesBean().getType().findKey("usesUpload"),
88                 CoreForm.USES_UPLOAD_KEY);
89    }
90  
91    /**
92     * Tests the transparency of component attributes.
93     *
94     * @todo remaining attributes
95     */
96    public void testAttributeTransparency()
97    {
98      CoreForm component = new CoreForm();
99  
100     doTestAttributeTransparency(component, "usesUpload",
101                                 Boolean.TRUE, Boolean.FALSE);
102     // remaining attributes here
103   }
104 
105   /**
106    * Tests the apply-request-values lifecycle phase.
107    */
108   public void testApplyRequestValues()
109   {
110     TestForm testForm = new TestForm(true);
111     doTestApplyRequestValues(testForm);
112 
113     testForm = new TestForm(true);
114     testForm.setRendered(false);
115     doTestApplyRequestValues(testForm);
116 
117     testForm = new TestForm(false);
118     doTestApplyRequestValues(testForm);
119 
120     testForm = new TestForm(false);
121     testForm.setRendered(false);
122     doTestApplyRequestValues(testForm);
123   }
124 
125   /**
126    * Tests the process-validations lifecycle phase.
127    */
128   public void testProcessValidations()
129   {
130     CoreForm form;
131     form = new CoreForm();
132     form.setSubmitted(false);
133     doTestProcessValidations(form);
134 
135     form = new CoreForm();
136     form.setSubmitted(true);
137     doTestProcessValidations(form);
138   }
139 
140   /**
141    * Tests the update-model-values lifecycle phase.
142    */
143   public void testUpdateModelValues()
144   {
145     CoreForm form;
146     form = new CoreForm();
147     form.setSubmitted(false);
148     doTestUpdateModelValues(form);
149 
150     form = new CoreForm();
151     form.setSubmitted(true);
152     doTestUpdateModelValues(form);
153   }
154 
155 
156   /**
157    * Tests the invoke-application lifecycle phase.
158    */
159   public void testInvokeApplication()
160   {
161     CoreForm component = new CoreForm();
162 
163     doTestInvokeApplication(component, null);
164   }
165 
166   /**
167    * Tests the render-response lifecycle phase.
168    *
169    * @throws IOException  when test fails
170    */
171   public void testRenderResponse() throws IOException
172   {
173     doTestRenderResponse(new CoreForm());
174   }
175 
176   // Subclass that will hardcode whether the form is getting
177   // submitted
178   static private class TestForm extends CoreForm
179   {
180     public TestForm(boolean willBeSubmitted)
181     {
182       _willBeSubmitted = willBeSubmitted;
183     }
184 
185     public boolean getWillBeSubmitted()
186     {
187       return _willBeSubmitted;
188     }
189 
190     @Override
191     public void decode(FacesContext context)
192     {
193       super.decode(context);
194       setSubmitted(getWillBeSubmitted());
195     }
196 
197     private final boolean _willBeSubmitted;
198   }
199 
200   @Override
201   protected boolean willChildrenBeProcessed(UIComponent component)
202   {
203     if (!component.isRendered())
204       return false;
205 
206     if (component instanceof TestForm)
207       return ((TestForm) component).getWillBeSubmitted();
208     else
209       return ((CoreForm) component).isSubmitted();
210   }
211 }
212