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