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.impl;
19
20 import javax.faces.el.EvaluationException;
21 import javax.faces.el.MethodNotFoundException;
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24 import org.apache.shale.remoting.Constants;
25 import org.apache.shale.test.base.AbstractJsfTestCase;
26 import org.apache.shale.test.mock.MockPrintWriter;
27 import org.apache.shale.test.mock.MockServletOutputStream;
28
29 /***
30 * <p>Test case for <code>org.apache.shale.remoting.impl.MethodBindingProcessor</code>.</p>
31 */
32 public class MethodBindingProcessorTestCase extends AbstractJsfTestCase {
33
34
35
36
37
38
39 public MethodBindingProcessorTestCase(String name) {
40 super(name);
41 }
42
43
44
45
46
47
48 protected void setUp() throws Exception {
49
50 super.setUp();
51 processor = new MethodBindingProcessor();
52 processor.setExcludes(Constants.DYNAMIC_RESOURCES_EXCLUDES_DEFAULT);
53 processor.setIncludes(Constants.DYNAMIC_RESOURCES_INCLUDES_DEFAULT);
54 servletContext.setAttribute("business", new MethodBindingProcessorBusinessObject());
55
56 }
57
58
59
60 public static Test suite() {
61
62 return (new TestSuite(MethodBindingProcessorTestCase.class));
63
64 }
65
66
67
68 protected void tearDown() throws Exception {
69
70 servletContext.removeAttribute("business");
71 processor = null;
72 super.tearDown();
73
74 }
75
76
77
78
79
80
81
82 private static final String[] DEFAULT_EXCLUDES =
83 { "/applicationScope/clear",
84 "/requestScope/clear",
85 "/response/flushBuffer",
86 "/response/getOutputStream",
87 "/response/getWriter",
88 "/response/reset",
89 "/response/resetBuffer",
90 "/sessionScope/clear",
91 };
92
93
94
95
96
97
98 private MethodBindingProcessor processor = null;
99
100
101
102
103
104
105 public void testImplicitExclude() throws Exception {
106
107 for (int i = 0; i < DEFAULT_EXCLUDES.length; i++) {
108 try {
109 processor.process(facesContext, DEFAULT_EXCLUDES[i]);
110 assertEquals("Should return 404 for '" + DEFAULT_EXCLUDES[i] + "'",
111 404, response.getStatus());
112 } catch (Exception e) {
113 fail("Should have returned 404 for '" + DEFAULT_EXCLUDES[i] +
114 "' instead of exception " + e.getMessage());
115 }
116 }
117
118 }
119
120
121
122 public void testInvalidBean() throws Exception {
123
124 try {
125 processor.process(facesContext, "/invalid/directStream");
126 fail("Should have thrown EvaluationException");
127 } catch (EvaluationException e) {
128 ;
129 }
130
131 }
132
133
134
135 public void testInvalidMethod() throws Exception {
136
137 try {
138 processor.process(facesContext, "/business/invalidMethod");
139 fail("Should have thrown MethodNotFoundException");
140 } catch (MethodNotFoundException e) {
141 ;
142 }
143
144 }
145
146
147
148 public void testMapping() {
149
150 assertEquals("#{business.directStream}",
151 processor.mapResourceId(facesContext, "/business/directStream").getExpressionString());
152 assertEquals("#{business.directWriter}",
153 processor.mapResourceId(facesContext, "/business/directWriter").getExpressionString());
154 assertEquals("#{business.indirectStream}",
155 processor.mapResourceId(facesContext, "/business/indirectStream").getExpressionString());
156 assertEquals("#{business.indirectWriter}",
157 processor.mapResourceId(facesContext, "/business/indirectWriter").getExpressionString());
158
159 }
160
161
162
163 public void testPatternExcluded() throws Exception {
164
165 processor.setExcludes("/business/*");
166 processor.process(facesContext, "/business/directWriter");
167 assertEquals(404, response.getStatus());
168
169 }
170
171
172
173 public void testPatternIncluded() throws Exception {
174
175 processor.setIncludes("/business/*");
176 processor.process(facesContext, "/business/directWriter");
177 assertEquals(200, response.getStatus());
178
179 }
180
181
182
183 public void testPatternMixed() throws Exception {
184
185 processor.setExcludes("/bar/*");
186 processor.setIncludes("/business/*");
187 processor.process(facesContext, "/business/directWriter");
188 assertEquals(200, response.getStatus());
189
190 }
191
192
193
194 public void testPristine() {
195
196 assertNotNull(processor);
197 assertNotNull(servletContext.getAttribute("business"));
198 assertNull(servletContext.getAttribute("invalid"));
199
200 }
201
202
203
204 public void testDirectStream() throws Exception {
205
206 processor.process(facesContext, "/business/directStream");
207 assertEquals("application/x-binary", response.getContentType());
208 MockServletOutputStream stream =
209 (MockServletOutputStream) response.getOutputStream();
210 assertNotNull(stream);
211 assertEquals(10, stream.size());
212 byte content[] = stream.content();
213 for (int i = 0; i < 10; i++) {
214 assertEquals("Byte at position " + i, (byte) i, content[i]);
215 }
216 assertTrue(facesContext.getResponseComplete());
217
218 }
219
220
221
222 public void testDirectWriter() throws Exception {
223
224 processor.process(facesContext, "/business/directWriter");
225 assertEquals("text/x-plain", response.getContentType());
226 MockPrintWriter writer = (MockPrintWriter) response.getWriter();
227 assertNotNull(writer);
228 assertEquals(10, writer.size());
229 char content[] = writer.content();
230 for (int i = 0; i < 10; i++) {
231 assertEquals("Character at position " + i, (char) ('a' + i), content[i]);
232 }
233 assertTrue(facesContext.getResponseComplete());
234
235 }
236
237
238
239 public void testIndirectStream() throws Exception {
240
241 processor.process(facesContext, "/business/indirectStream");
242 assertEquals("application/x-binary", response.getContentType());
243 MockServletOutputStream stream =
244 (MockServletOutputStream) response.getOutputStream();
245 assertNotNull(stream);
246 assertEquals(10, stream.size());
247 byte content[] = stream.content();
248 for (int i = 0; i < 10; i++) {
249 assertEquals("Byte at position " + i, (byte) i, content[i]);
250 }
251 assertTrue(facesContext.getResponseComplete());
252
253 }
254
255
256
257 public void testIndirectWriter() throws Exception {
258
259 processor.process(facesContext, "/business/indirectWriter");
260 assertEquals("text/x-plain", response.getContentType());
261 MockPrintWriter writer = (MockPrintWriter) response.getWriter();
262 assertNotNull(writer);
263 assertEquals(10, writer.size());
264 char content[] = writer.content();
265 for (int i = 0; i < 10; i++) {
266 assertEquals("Character at position " + i, (char) ('a' + i), content[i]);
267 }
268 assertTrue(facesContext.getResponseComplete());
269
270 }
271
272
273
274
275
276
277 }