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  import java.util.Locale;
22  
23  import javax.faces.FactoryFinder;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.ResponseWriter;
26  import javax.faces.render.RenderKitFactory;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.shale.clay.component.Clay;
32  import org.apache.shale.test.mock.MockRenderKit;
33  
34  public class AssignViewRootTestCase extends AbstractTestCaseConfig {
35  
36      // Construct a new instance of this test case.
37      public AssignViewRootTestCase(String name) {
38          super(name);
39      }
40  
41      // Return the tests included in this test case.
42      public static Test suite() {
43          return (new TestSuite(AssignViewRootTestCase.class));
44      }
45  
46      private Clay clay = null;
47  
48      protected void setUp() throws Exception {
49          super.setUp();
50  
51          loadConfigFiles(null, null);
52  
53      }
54  
55      public void testAssign1() throws Exception {        
56  
57          RenderKitFactory renderKitFactory = (RenderKitFactory)
58          FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
59          renderKit = new MockRenderKit();
60          renderKitFactory.addRenderKit("MY_KIT1", renderKit);
61  
62          buildSubtree("/org/apache/shale/clay/config/viewroot1.html");
63          
64          String renderKitId = facesContext.getViewRoot().getRenderKitId();
65          assertEquals("renderKitId", "MY_KIT1", renderKitId);
66          
67          Locale locale = facesContext.getViewRoot().getLocale();
68          assertEquals("locale", "ja_JP", locale.toString());
69  
70          UIComponent input = findComponent(clay, "input1");
71          assertNotNull("child 1 assigned", input);
72  
73          input = findComponent(clay, "input2");
74          assertNotNull("child 2 assigned", input);
75  
76      }
77  
78      public void testAssign2() throws Exception {        
79    
80          RenderKitFactory renderKitFactory = (RenderKitFactory)
81          FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
82          renderKit = new MockRenderKit();
83          renderKitFactory.addRenderKit("MY_KIT2", renderKit);
84  
85          buildSubtree("/org/apache/shale/clay/config/viewroot2.html");
86          
87          String renderKitId = facesContext.getViewRoot().getRenderKitId();
88          assertEquals("renderKitId", "MY_KIT2", renderKitId);
89          
90          Locale locale = facesContext.getViewRoot().getLocale();
91          assertEquals("locale", "pt_PT", locale.toString());
92  
93          UIComponent input = findComponent(clay, "input1");
94          assertNotNull("child 1 assigned", input);
95  
96          input = findComponent(clay, "input2");
97          assertNotNull("child 2 assigned", input);
98  
99      }
100 
101     public void testAssign3() throws Exception {        
102 
103         RenderKitFactory renderKitFactory = (RenderKitFactory)
104         FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
105         renderKit = new MockRenderKit();
106         renderKitFactory.addRenderKit("MY_KIT3", renderKit);
107 
108         buildSubtree("/org/apache/shale/clay/config/viewroot3.html");
109         
110         String renderKitId = facesContext.getViewRoot().getRenderKitId();
111         assertEquals("renderKitId", "MY_KIT3", renderKitId);
112         
113         Locale locale = facesContext.getViewRoot().getLocale();
114         assertEquals("locale", "de", locale.toString());
115 
116         UIComponent input = findComponent(clay, "input1");
117         assertNotNull("child 1 assigned", input);
118 
119         input = findComponent(clay, "input2");
120         assertNotNull("child 2 assigned", input);
121 
122     }
123 
124     private void buildSubtree(String jsfid) throws Exception {
125         clay = (Clay) application.createComponent("org.apache.shale.clay.component.Clay");
126         clay.setId("test");
127         clay.setJsfid(jsfid);
128         clay.setManagedBeanName("test");
129         facesContext.getViewRoot().getChildren().add(0, clay);
130 
131         //      builds a buffer to write the page to
132         StringWriter writer = new StringWriter();
133         //      create a buffered response writer
134         ResponseWriter buffResponsewriter = facesContext.getRenderKit()
135             .createResponseWriter(writer, null,
136             response.getCharacterEncoding());
137         //      push buffered writer to the faces context
138         facesContext.setResponseWriter(buffResponsewriter);
139         //      start a document
140         buffResponsewriter.startDocument();
141 
142         //      build subtree
143         clay.encodeBegin(facesContext);
144 
145     }
146     
147     private UIComponent findComponent(UIComponent parent, String id) {
148         if (parent == null) {
149             return null;
150         }
151 
152         if (parent.getId() != null && parent.getId().equals(id)) {
153             return parent;
154         } else {
155             Iterator ci = parent.getChildren().iterator();
156             while (ci.hasNext()) {
157                 UIComponent child = (UIComponent) ci.next();
158                 UIComponent target = findComponent(child, id);
159                 if (target != null) {
160                     return target;
161                 }
162             }
163         }
164 
165         return null;
166     }
167 
168 
169 
170 }