2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.clay.config;
19  
20  import java.io.StringWriter;
21  import java.util.Iterator;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIForm;
25  import javax.faces.component.UISelectItem;
26  import javax.faces.component.UISelectItems;
27  import javax.faces.component.html.HtmlCommandButton;
28  import javax.faces.component.html.HtmlInputText;
29  import javax.faces.component.html.HtmlInputTextarea;
30  import javax.faces.component.html.HtmlOutputLabel;
31  import javax.faces.component.html.HtmlOutputLink;
32  import javax.faces.component.html.HtmlSelectBooleanCheckbox;
33  import javax.faces.component.html.HtmlSelectManyMenu;
34  import javax.faces.component.html.HtmlSelectOneMenu;
35  import javax.faces.component.html.HtmlSelectOneRadio;
36  import javax.faces.context.ResponseWriter;
37  import javax.faces.el.MethodBinding;
38  import javax.faces.el.ValueBinding;
39  
40  import junit.framework.Test;
41  import junit.framework.TestSuite;
42  
43  import org.apache.shale.clay.component.Clay;
44  
45  public class ImplicitMappingTestCase extends AbstractTestCaseConfig {
46  
47      // Construct a new instance of this test case.
48      public ImplicitMappingTestCase(String name) {
49          super(name);
50      }
51  
52      // Return the tests included in this test case.
53      public static Test suite() {
54          return (new TestSuite(ImplicitMappingTestCase.class));
55      }
56  
57      private Clay clay = null;
58  
59      protected void setUp() throws Exception {
60          super.setUp();
61  
62          // done by the startup context listener
63          loadConfigFiles(null, null);
64  
65          clay = (Clay) application
66                  .createComponent("org.apache.shale.clay.component.Clay");
67          clay.setId("test");
68          clay.setJsfid("/org/apache/shale/clay/config/implicit.html");
69          clay.setManagedBeanName("test");
70  
71          // builds a buffer to write the page to
72          StringWriter writer = new StringWriter();
73          // create a buffered response writer
74          ResponseWriter buffResponsewriter = facesContext.getRenderKit()
75                  .createResponseWriter(writer, null,
76                          response.getCharacterEncoding());
77          // push buffered writer to the faces context
78          facesContext.setResponseWriter(buffResponsewriter);
79          // start a document
80          buffResponsewriter.startDocument();
81  
82          // build subtree
83          clay.encodeBegin(facesContext);
84      }
85  
86      public void testForm() throws Exception {
87  
88          // form
89          UIComponent widget = findComponent(clay, "logonForm");
90          assertNotNull("UIForm", widget);
91          assertTrue("UIForm", widget instanceof UIForm);
92      }
93  
94      public void testLabel() {
95          // label
96          UIComponent widget = findComponent(clay, "usernameLabel");
97          assertNotNull("HtmlOutputLabel", widget);
98          assertTrue("HtmlOutputLabel", widget instanceof HtmlOutputLabel);
99          assertEquals("for", "username", widget.getAttributes().get("for"));
100 
101     }
102 
103     public void testInputText() {
104         // input text
105         UIComponent widget = findComponent(clay, "username");
106         assertNotNull("HtmlInputText", widget);
107         assertTrue("HtmlInputText", widget instanceof HtmlInputText);
108         assertEquals("size", "16", widget.getAttributes().get("size")
109                 .toString());
110 
111         ValueBinding vb = widget.getValueBinding("value");
112         assertNotNull("ValueBinding", vb);
113         String exp = vb.getExpressionString();
114         assertNotNull("Expression String", exp);
115         assertEquals("value", "#{test.username}", exp);
116     }
117 
118     public void testSubmit() {
119         // submit
120         UIComponent widget = findComponent(clay, "submit");
121         assertNotNull("HtmlCommandButton", widget);
122         assertTrue("HtmlCommandButton", widget instanceof HtmlCommandButton);
123         assertEquals("value", "submit", widget.getAttributes().get("value")
124                 .toString());
125 
126         MethodBinding mb = ((HtmlCommandButton) widget).getAction();
127         assertNotNull("MethodBinding", mb);
128         String exp = mb.getExpressionString();
129         assertNotNull("Expression String", exp);
130         assertEquals("action", "#{test.save}", exp);
131 
132     }
133 
134     public void testAnchor() {
135         UIComponent widget = findComponent(clay, "shaleSite");
136         assertNotNull("HtmlOutputLink", widget);
137         assertTrue("HtmlOutputLink", widget instanceof HtmlOutputLink);
138         assertEquals("value", "http://shale.apache.org/", widget
139                 .getAttributes().get("value"));
140         assertEquals("target", "_blank", widget.getAttributes().get("target"));
141 
142     }
143 
144     public void testCheckBox() {
145         UIComponent widget = findComponent(clay, "checkbox");
146         assertNotNull("HtmlSelectBooleanCheckbox", widget);
147         assertTrue("HtmlSelectBooleanCheckbox",
148                 widget instanceof HtmlSelectBooleanCheckbox);
149 
150         ValueBinding vb = widget.getValueBinding("value");
151         assertNotNull("ValueBinding", vb);
152         String exp = vb.getExpressionString();
153         assertNotNull("Expression String", exp);
154         assertEquals("value", "#{test.shaleRocks}", exp);
155 
156     }
157 
158     public void testRadio() {
159         UIComponent widget = findComponent(clay, "radio");
160         assertNotNull("HtmlSelectOneRadio", widget);
161         assertTrue("HtmlSelectOneRadio", widget instanceof HtmlSelectOneRadio);
162 
163         ValueBinding vb = widget.getValueBinding("value");
164         assertNotNull("ValueBinding", vb);
165         String exp = vb.getExpressionString();
166         assertNotNull("Expression String", exp);
167         assertEquals("value", "#{test.rockType}", exp);
168 
169     }
170 
171     public void testSelectOne() {
172         UIComponent widget = findComponent(clay, "states");
173         assertNotNull("HtmlSelectOneMenu", widget);
174         assertTrue("HtmlSelectOneMenu", widget instanceof HtmlSelectOneMenu);
175 
176         ValueBinding vb = widget.getValueBinding("value");
177         assertNotNull("ValueBinding", vb);
178         String exp = vb.getExpressionString();
179         assertNotNull("Expression String", exp);
180         assertEquals("value", "#{test.state}", exp);
181 
182     }
183 
184     public void testOption() {
185         UIComponent widget = findComponent(clay, "alabama");
186         assertNotNull("UISelectItem", widget);
187         assertTrue("UISelectItem", widget instanceof UISelectItem);
188         assertEquals("itemLabel", "Alabama", widget.getAttributes().get(
189                 "itemLabel"));
190         assertEquals("itemValue", "AL", widget.getAttributes().get("itemValue"));
191     }
192 
193     public void testSelectMany() {
194         UIComponent widget = findComponent(clay, "multiStates");
195         assertNotNull("HtmlSelectManyMenu", widget);
196         assertTrue("HtmlSelectManyMenu", widget instanceof HtmlSelectManyMenu);
197 
198         ValueBinding vb = widget.getValueBinding("value");
199         assertNotNull("ValueBinding", vb);
200         String exp = vb.getExpressionString();
201         assertNotNull("Expression String", exp);
202         assertEquals("value", "#{test.state}", exp);
203 
204     }
205 
206     public void testOptions() {
207         UIComponent widget = findComponent(clay, "stateOptions");
208         assertNotNull("UISelectItems", widget);
209         assertTrue("UISelectItems", widget instanceof UISelectItems);
210 
211         ValueBinding vb = widget.getValueBinding("value");
212         assertNotNull("ValueBinding", vb);
213         String exp = vb.getExpressionString();
214         assertNotNull("Expression String", exp);
215         assertEquals("value", "#{test.states}", exp);
216 
217     }
218 
219     public void testTextArea() {
220         UIComponent widget = findComponent(clay, "textArea");
221         assertNotNull("HtmlInputTextarea", widget);
222         assertTrue("HtmlInputTextarea", widget instanceof HtmlInputTextarea);
223 
224         ValueBinding vb = widget.getValueBinding("value");
225         assertNotNull("ValueBinding", vb);
226         String exp = vb.getExpressionString();
227         assertNotNull("Expression String", exp);
228         assertEquals("value", "#{test.textarea}", exp);
229 
230         assertEquals("rows", "10", widget.getAttributes().get("rows")
231                 .toString());
232 
233     }
234 
235     private UIComponent findComponent(UIComponent parent, String id) {
236         if (parent == null) {
237             return null;
238         }
239 
240         if (parent.getId() != null && parent.getId().equals(id)) {
241             return parent;
242         } else {
243             Iterator ci = parent.getChildren().iterator();
244             while (ci.hasNext()) {
245                 UIComponent child = (UIComponent) ci.next();
246                 UIComponent target = findComponent(child, id);
247                 if (target != null) {
248                     return target;
249                 }
250             }
251         }
252 
253         return null;
254     }
255 
256 }