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.utils;
19  
20  import java.io.File;
21  import java.util.Map;
22  import java.util.TreeMap;
23  
24  import javax.faces.component.UIComponent;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.shale.clay.config.beans.AttributeBean;
30  import org.apache.shale.clay.config.beans.ComponentBean;
31  import org.apache.shale.test.base.AbstractViewControllerTestCase;
32  
33  public class ClayAmalgamTestCase extends AbstractViewControllerTestCase {
34  
35      private ClayAmalgam clayAmalgam = null;
36          
37      protected void setUp() throws Exception {
38          super.setUp();
39          
40          // Configure document root for tests
41          String documentRoot = System.getProperty("documentRoot");
42          if (documentRoot == null) {
43          	documentRoot = System.getProperty("user.dir") + "//target//test-classes";            
44          }
45          servletContext.setDocumentRoot(new File(documentRoot));
46  
47          clayAmalgam = new ClayAmalgam();
48          application.addComponent("org.apache.shale.clay.component.Clay", "org.apache.shale.clay.component.Clay"); 
49      }
50   
51      // Construct a new instance of this test case.
52      public ClayAmalgamTestCase(String name) {
53          super(name);
54      }
55  
56      // Return the tests included in this test case.
57      public static Test suite() {
58          return (new TestSuite(ClayAmalgamTestCase.class));
59      }
60  
61      //test the import file into a outputText component
62      public void testClayImport() {
63         //test excapeXml=true
64         Map requestParams = new TreeMap();
65         //context root import
66         requestParams.put("url", "/org/apache/shale/clay/utils/hello.html");
67         externalContext.setRequestParameterMap(requestParams);
68         
69         ComponentBean displayElement = new ComponentBean();
70         displayElement.setId("clayImport");
71         displayElement.setComponentType("org.apache.shale.clay.component.Clay");
72         displayElement.setJsfid("clayImport");
73          
74         UIComponent component = application.createComponent("org.apache.shale.clay.component.Clay");     
75         component.setId("clayImport");
76         component.getAttributes().put("url","#{param.url}");
77         component.getAttributes().put("escapeXml", Boolean.TRUE.toString());
78         
79         clayAmalgam.clayImport(facesContext, component, displayElement);
80         
81         assertEquals("jsfid", "outputText", displayElement.getJsfid());
82         assertEquals("componentType", "javax.faces.HtmlOutputText", displayElement.getComponentType());
83         
84         AttributeBean attr = displayElement.getAttribute("escape");
85         assertNotNull("escape", attr);
86         assertEquals("escape", Boolean.FALSE.toString(), attr.getValue());
87         
88         attr = displayElement.getAttribute("isTransient");
89         assertNotNull("isTransient", attr);
90         assertEquals("isTransient", Boolean.TRUE.toString(), attr.getValue());
91         
92         attr = displayElement.getAttribute("value");
93         assertNotNull("value", attr);
94         assertEquals("value", "<html><head></head><body bgcolor=\"blue\">Hello World</body></html>", attr.getValue());
95  
96         //test excapeXml=false
97         component.getAttributes().put("escapeXml", Boolean.FALSE.toString());
98         displayElement = new ComponentBean();
99         displayElement.setId("clayImport");
100        displayElement.setComponentType("org.apache.shale.clay.component.Clay");
101        displayElement.setJsfid("clayImport");
102       
103        clayAmalgam.clayImport(facesContext, component, displayElement);
104        
105        assertEquals("jsfid", "outputText", displayElement.getJsfid());
106        assertEquals("componentType", "javax.faces.HtmlOutputText", displayElement.getComponentType());
107        
108        attr = displayElement.getAttribute("escape");
109        assertNotNull("escape", attr);
110        assertEquals("escape", Boolean.FALSE.toString(), attr.getValue());
111        
112        attr = displayElement.getAttribute("isTransient");
113        assertNotNull("isTransient", attr);
114        assertEquals("isTransient", Boolean.TRUE.toString(), attr.getValue());
115        
116        attr = displayElement.getAttribute("value");
117        assertNotNull("value", attr);
118        assertEquals("value", "<html><head></head><body bgcolor=\"blue\">Hello World</body></html>", attr.getValue());
119 
120        
121        //test classpath file import
122        requestParams = new TreeMap();
123        requestParams.put("url", "classpath*:/org/apache/shale/clay/utils/hello.html");
124        externalContext.setRequestParameterMap(requestParams);
125        
126        displayElement = new ComponentBean();
127        displayElement.setId("clayImport");
128        displayElement.setComponentType("org.apache.shale.clay.component.Clay");
129        displayElement.setJsfid("clayImport");
130         
131        component = application.createComponent("org.apache.shale.clay.component.Clay");     
132        component.setId("clayImport");
133        component.getAttributes().put("url","#{param.url}");
134        component.getAttributes().put("escapeXml", Boolean.TRUE.toString());
135        
136        clayAmalgam.clayImport(facesContext, component, displayElement);
137        attr = displayElement.getAttribute("value");
138        assertNotNull("value", attr);
139        assertEquals("value", "", attr.getValue());
140 
141        
142        //test no file found
143        requestParams = new TreeMap();
144        requestParams.put("url", "/org/apache/shale/clay/utils/notfound.html");
145        externalContext.setRequestParameterMap(requestParams);
146        
147        displayElement = new ComponentBean();
148        displayElement.setId("clayImport");
149        displayElement.setComponentType("org.apache.shale.clay.component.Clay");
150        displayElement.setJsfid("clayImport");
151         
152        component = application.createComponent("org.apache.shale.clay.component.Clay");     
153        component.setId("clayImport");
154        component.getAttributes().put("url","#{param.url}");
155        component.getAttributes().put("escapeXml", Boolean.TRUE.toString());
156        
157        clayAmalgam.clayImport(facesContext, component, displayElement);
158        attr = displayElement.getAttribute("value");
159        assertNotNull("value", attr);
160        assertEquals("value", "", attr.getValue());
161 
162        
163     }
164     
165     //convert an escaped value into a un-escaped outputText
166     public void testClayOut() {
167         ComponentBean displayElement = new ComponentBean();
168         displayElement.setId("clayOut");
169         displayElement.setComponentType("org.apache.shale.clay.component.Clay");
170         displayElement.setJsfid("clayOut");
171 
172         UIComponent component = application.createComponent("org.apache.shale.clay.component.Clay");     
173         component.setId("clayOut");
174         component.getAttributes().put("value","&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body bgcolor=\"blue\"&gt;Hello World&lt;/body&gt;&lt;/html&gt;");
175         component.getAttributes().put("escapeXml", Boolean.FALSE.toString());
176         
177    
178         clayAmalgam.clayOut(facesContext, component, displayElement);
179         
180         assertEquals("jsfid", "outputText", displayElement.getJsfid());
181         assertEquals("componentType", "javax.faces.HtmlOutputText", displayElement.getComponentType());
182         
183         AttributeBean attr = displayElement.getAttribute("escape");
184         assertNotNull("escape", attr);
185         assertEquals("escape", Boolean.FALSE.toString(), attr.getValue());
186         
187         attr = displayElement.getAttribute("isTransient");
188         assertNotNull("isTransient", attr);
189         assertEquals("isTransient", Boolean.TRUE.toString(), attr.getValue());
190         
191         attr = displayElement.getAttribute("value");
192         assertNotNull("value", attr);
193         assertEquals("value", "<html><head></head><body bgcolor=\"blue\">Hello World</body></html>", attr.getValue());
194           
195     }
196     
197 }