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.remoting;
19  
20  import java.util.Locale;
21  import javax.faces.component.UIOutput;
22  import javax.faces.component.UIViewRoot;
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  import org.apache.shale.remoting.faces.ResponseFactory;
26  import org.apache.shale.remoting.impl.MappingImpl;
27  import org.apache.shale.remoting.impl.MappingsImpl;
28  import org.apache.shale.test.base.AbstractJsfTestCase;
29  import org.apache.shale.test.mock.MockPrintWriter;
30  
31  /***
32   * <p>Unit tests for <code>org.apache.shale.remoting.XhtmlHelper</code>.</p>
33   */
34  public class XhtmlHelperTestCase extends AbstractJsfTestCase {
35      
36      // ------------------------------------------------------------ Constructors
37  
38  
39      // Construct a new instance of this test case.
40      public XhtmlHelperTestCase(String name) {
41          super(name);
42      }
43  
44  
45      // ----------------------------------------------------------- Setup Methods
46  
47  
48      // Set up instance variables for this test case.
49      protected void setUp() throws Exception {
50  
51          super.setUp();
52          helper = new XhtmlHelper();
53          facesContext.setViewRoot(new UIViewRoot());
54          mappings = new MappingsImpl();
55          mappings.setExtension(".jsp");
56          mappings.setPatterns(new String[] { "*.faces" });
57          mapping = new MappingImpl();
58          mapping.setMappings(mappings);
59          mapping.setMechanism(Mechanism.CLASS_RESOURCE);
60          mappings.addMapping(mapping);
61          facesContext.getExternalContext().getApplicationMap().
62                  put(Constants.MAPPINGS_ATTR, mappings);
63          facesContext.getViewRoot().setLocale(Locale.getDefault());
64          request.setPathElements("/webapp", "/foo", null, null);
65  
66      }
67  
68  
69      // Return the tests included in this test case.
70      public static Test suite() {
71  
72          return (new TestSuite(XhtmlHelperTestCase.class));
73  
74      }
75  
76  
77      // Tear down instance variables for this test case.
78      protected void tearDown() throws Exception {
79  
80          helper = null;
81          mapping = null;
82          mappings = null;
83          super.tearDown();
84  
85      }
86  
87  
88      // ------------------------------------------------------ Instance Variables
89  
90  
91      // The helper instance to be tested
92      private XhtmlHelper helper = null;
93  
94  
95      // The mapping instance to be tested
96      private MappingImpl mapping = null;
97  
98  
99      // The mappings instance to be tested
100     private MappingsImpl mappings = null;
101 
102 
103     // -------------------------------------------------------- Static Variables
104 
105 
106     // Prefix to the rendered JavaScript test string
107     private static final String JAVASCRIPT_CONTENT =
108       "<script type=\"text/javascript\" src=\"/webapp/dynamic/foo/bar.js.faces\"/>";
109 
110 
111     // Prefix to the rendered Stylesheet test string
112     private static final String STYLESHEET_CONTENT =
113       "<link type=\"text/css\" rel=\"stylesheet\" href=\"/webapp/faces/foo/bar.css.dyn\"/>";
114 
115 
116     // ------------------------------------------------------------ Test Methods
117 
118 
119     // Test linking to a JavaScript resource
120     public void testLinkJavascript() throws Exception {
121 
122         // Use extension mapping for FacesServlet and prefix mapping for the resource
123         mappings.setPatterns(new String[] { "*.faces" });
124         mapping.setPattern("/dynamic/*");
125 
126         // Perform the link request and check the results
127         facesContext.setResponseWriter
128           ((new ResponseFactory()).getResponseWriter(facesContext, "text/javascript"));
129         helper.linkJavascript(facesContext, new UIOutput(),
130                               facesContext.getResponseWriter(),
131                               Mechanism.CLASS_RESOURCE, "/foo/bar.js");
132 
133         // Evaluate the results
134         assertEquals("text/javascript", response.getContentType());
135         MockPrintWriter writer = (MockPrintWriter) response.getWriter();
136         char content[] = writer.content();
137         assertNotNull(content);
138         assertTrue(content.length > JAVASCRIPT_CONTENT.length());
139         for (int i = 0; i < JAVASCRIPT_CONTENT.length(); i++) {
140             assertEquals("Character at position " + i, JAVASCRIPT_CONTENT.charAt(i), content[i]);
141         }
142 
143 
144     }
145 
146     // Test a pristine instance
147     public void testPristine() {
148 
149         ;
150 
151     }
152 
153 
154     // Test linking to a stylesheet resource
155     public void testLinkStylesheet() throws Exception {
156 
157         // Use prefix mapping for FacesServlet and extension mapping for the resource
158         mappings.setPatterns(new String[] { "/faces/*" });
159         mapping.setPattern("*.dyn");
160 
161         // Perform the link request and check the results
162         facesContext.setResponseWriter
163           ((new ResponseFactory()).getResponseWriter(facesContext, "text/css"));
164         helper.linkStylesheet(facesContext, new UIOutput(),
165                               facesContext.getResponseWriter(),
166                               Mechanism.CLASS_RESOURCE, "/foo/bar.css");
167 
168         // Evaluate the results
169         assertEquals("text/css", response.getContentType());
170         MockPrintWriter writer = (MockPrintWriter) response.getWriter();
171         char content[] = writer.content();
172         assertNotNull(content);
173         assertTrue(content.length > STYLESHEET_CONTENT.length());
174         for (int i = 0; i < STYLESHEET_CONTENT.length(); i++) {
175             assertEquals("Character at position " + i, STYLESHEET_CONTENT.charAt(i), content[i]);
176         }
177 
178     }
179 
180 
181 }