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  package org.apache.shale.clay.config;
18  
19  import java.io.StringWriter;
20  import java.util.Iterator;
21  
22  import javax.faces.component.UIComponent;
23  import javax.faces.component.html.HtmlOutputLabel;
24  import javax.faces.context.ResponseWriter;
25  import javax.faces.convert.BooleanConverter;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.shale.clay.component.Clay;
31  
32  public class ConverterTestCase extends AbstractTestCaseConfig {
33  
34      // Construct a new instance of this test case.
35      public ConverterTestCase(String name) {
36          super(name);
37      }
38  
39      // Return the tests included in this test case.
40      public static Test suite() {
41          return (new TestSuite(ConverterTestCase.class));
42      }
43  
44      private Clay clay = null;
45  
46      protected void setUp() throws Exception {
47          super.setUp();
48  
49          loadConfigFiles("/org/apache/shale/clay/config/converter-config.xml",
50                  null);
51  
52          // register one of the standard converters
53          facesContext.getApplication().addConverter("javax.faces.Boolean",
54                  "javax.faces.convert.BooleanConverter");
55  
56          clay = (Clay) application
57                  .createComponent("org.apache.shale.clay.component.Clay");
58          clay.setId("test");
59          clay.setJsfid("/org/apache/shale/clay/config/converter.html");
60          clay.setManagedBeanName("test");
61  
62          // builds a buffer to write the page to
63          StringWriter writer = new StringWriter();
64          // create a buffered response writer
65          ResponseWriter buffResponsewriter = facesContext.getRenderKit()
66                  .createResponseWriter(writer, null,
67                          response.getCharacterEncoding());
68          // push buffered writer to the faces context
69          facesContext.setResponseWriter(buffResponsewriter);
70          // start a document
71          buffResponsewriter.startDocument();
72  
73          // build subtree
74          clay.encodeBegin(facesContext);
75  
76      }
77  
78      public void testConverterComponentType() {
79          HtmlOutputLabel label = (HtmlOutputLabel) findComponent(clay, "testlabel1");
80          assertNotNull(label);
81          
82          assertNotNull(label.getConverter());
83          assertTrue(label.getConverter() instanceof BooleanConverter);
84      }
85  
86      public void testConverterIdOverride() {
87          HtmlOutputLabel label = (HtmlOutputLabel) findComponent(clay, "testlabel2");
88          assertNotNull(label);
89          
90          assertNotNull(label.getConverter());
91          assertTrue(label.getConverter() instanceof BooleanConverter);
92   
93      }
94      
95      private UIComponent findComponent(UIComponent parent, String id) {
96          if (parent == null) {
97              return null;
98          }
99  
100         if (parent.getId() != null && parent.getId().equals(id)) {
101             return parent;
102         } else {
103             Iterator ci = parent.getChildren().iterator();
104             while (ci.hasNext()) {
105                 UIComponent child = (UIComponent) ci.next();
106                 UIComponent target = findComponent(child, id);
107                 if (target != null) {
108                     return target;
109                 }
110             }
111         }
112 
113         return null;
114     }
115 
116 
117 
118 }