2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38
39
40 public XhtmlHelperTestCase(String name) {
41 super(name);
42 }
43
44
45
46
47
48
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
70 public static Test suite() {
71
72 return (new TestSuite(XhtmlHelperTestCase.class));
73
74 }
75
76
77
78 protected void tearDown() throws Exception {
79
80 helper = null;
81 mapping = null;
82 mappings = null;
83 super.tearDown();
84
85 }
86
87
88
89
90
91
92 private XhtmlHelper helper = null;
93
94
95
96 private MappingImpl mapping = null;
97
98
99
100 private MappingsImpl mappings = null;
101
102
103
104
105
106
107 private static final String JAVASCRIPT_CONTENT =
108 "<script type=\"text/javascript\" src=\"/webapp/dynamic/foo/bar.js.faces\"/>";
109
110
111
112 private static final String STYLESHEET_CONTENT =
113 "<link type=\"text/css\" rel=\"stylesheet\" href=\"/webapp/faces/foo/bar.css.dyn\"/>";
114
115
116
117
118
119
120 public void testLinkJavascript() throws Exception {
121
122
123 mappings.setPatterns(new String[] { "*.faces" });
124 mapping.setPattern("/dynamic/*");
125
126
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
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
147 public void testPristine() {
148
149 ;
150
151 }
152
153
154
155 public void testLinkStylesheet() throws Exception {
156
157
158 mappings.setPatterns(new String[] { "/faces/*" });
159 mapping.setPattern("*.dyn");
160
161
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
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 }