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.util.Iterator;
20  
21  import javax.servlet.ServletContextEvent;
22  import javax.servlet.ServletContextListener;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.commons.chain.web.ChainListener;
28  import org.apache.shale.clay.config.beans.AttributeBean;
29  import org.apache.shale.clay.config.beans.ComponentBean;
30  
31  // tests registering builders in an alternate namespace
32  public class ConfigAltNsTestCase extends AbstractTestCaseConfig {
33  
34      // Construct a new instance of this test case.
35      public ConfigAltNsTestCase(String name) {
36          super(name);
37      }
38  
39      // Return the tests included in this test case.
40      public static Test suite() {
41  
42          return (new TestSuite(ConfigAltNsTestCase.class));
43  
44      }
45      
46      protected void setUp() throws Exception {
47          super.setUp();
48          
49      }
50      
51      public void testLoadBuilderNamespace() throws Exception {
52      	
53      	//contains a widget in the xmlns:acme="http://www.acme.org/widgets" namespace
54          loadConfigFiles("/org/apache/shale/clay/config/altns-config.xml", null);
55          
56          // simulate invoking the common chains context listener
57          servletContext.addInitParameter("org.apache.commons.chain.CONFIG_WEB_RESOURCE", "/org/apache/shale/clay/config/chain-config.xml");        
58          ServletContextListener chainsListener = new ChainListener();
59          ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);
60          chainsListener.contextInitialized(servletContextEvent);
61          
62          // find the config bean definition in the commons XML config file
63          ComponentBean configBean = standardConfigBean.getElement("acme:city");
64          assertNotNull("acme:city exists", configBean);
65          assertEquals("id", "city", configBean.getId());
66          
67          AttributeBean size = configBean.getAttribute("size");
68          assertNotNull("size", size);
69          assertEquals("size", "20", size.getValue());
70          
71          AttributeBean value = configBean.getAttribute("value");
72          assertNotNull("value", value);
73          assertEquals("value", "#{@managed-bean-name.city}", value.getValue());
74          
75          AttributeBean maxlength = configBean.getAttribute("maxlength");
76          assertNotNull("maxlength", maxlength);
77          assertEquals("maxlength", "30", maxlength.getValue());
78          
79          AttributeBean required = configBean.getAttribute("required");
80          assertNotNull("required", required);
81          assertEquals("required", "true", required.getValue());
82         
83  
84          // load a template that points/extends the acme:city widget
85          ComponentBean templateRoot = htmlTemplateConfigBean.getElement("/org/apache/shale/clay/config/altns.html");
86          assertNotNull("/org/apache/shale/clay/config/altns.html",  templateRoot);
87          
88          // config bean after extend by the html template
89          ComponentBean cityConfigBean = findForId(configBean.getId(), templateRoot);
90          assertNotNull("city from html template", cityConfigBean);
91          
92          // inherit from acme:city
93          assertEquals("id", "city", cityConfigBean.getId());
94          
95          // html override
96          size = cityConfigBean.getAttribute("size");
97          assertNotNull("size", size);
98          assertEquals("size", "10", size.getValue());
99          
100         // inherit from acme:city
101         value = cityConfigBean.getAttribute("value");
102         assertNotNull("value", value);
103         assertEquals("value", "#{@managed-bean-name.city}", value.getValue());
104         
105         // html override
106         maxlength = cityConfigBean.getAttribute("maxlength");
107         assertNotNull("maxlength", maxlength);
108         assertEquals("maxlength", "100", maxlength.getValue());
109         
110         // html override
111         required = cityConfigBean.getAttribute("required");
112         assertNotNull("required", required);
113         assertEquals("required", "false", required.getValue());
114         
115         // just because we can
116         chainsListener.contextDestroyed(servletContextEvent);
117     }
118     
119     private ComponentBean findForId(String id, ComponentBean root) {
120         if (root.getId() != null && root.getId().equals(id)) {
121            return root;
122         }
123         Iterator ci = root.getChildren().iterator();
124         while (ci.hasNext()) {
125            ComponentBean child = (ComponentBean) ci.next();
126            ComponentBean target = findForId(id, child);
127            if (target != null) {
128               return target;
129            }
130         }
131         
132         return null;
133     }
134 
135 }