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.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      // ------------------------------------------------------------ Constructors
36  
37  
38      // Construct a new instance of this test case.
39      public MethodBindingProcessorTestCase(String name) {
40          super(name);
41      }
42  
43  
44      // ----------------------------------------------------------- Setup Methods
45  
46  
47      // Set up instance variables for this test case.
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      // Return the tests included in this test case.
60      public static Test suite() {
61  
62          return (new TestSuite(MethodBindingProcessorTestCase.class));
63  
64      }
65  
66  
67      // Tear down instance variables for this test case.
68      protected void tearDown() throws Exception {
69  
70          servletContext.removeAttribute("business");
71          processor = null;
72          super.tearDown();
73  
74      }
75  
76  
77      // -------------------------------------------------------- Static Variables
78  
79  
80      // Resource identifiers that should be rejected by the default
81      // include/exclude rules
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      // ------------------------------------------------------ Instance Variables
95  
96  
97      // The Processor instance to be tested
98      private MethodBindingProcessor processor = null;
99  
100 
101     // ------------------------------------------------------------ Test Methods
102 
103 
104     // Test attempt to execute a resource id on the implicitly rejected list
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     // Test attempt to execute an expression with an invalid bean name
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             ; // Expected result
129         }
130 
131     }
132 
133 
134     // Test attempt to execute an expression with an invalid method name
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             ; // Expected result
142         }
143 
144     }
145 
146 
147     // Test mapping of resource identifiers to expressions
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     // Test attempt to execute an expression for an excluded pattern
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     // Test attempt to execute an expression for an included pattern
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     // Test attempt to execute an expression for a mixed exclude/include case
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     // Test a pristine instance of the Processor to be tested
194     public void testPristine() {
195 
196         assertNotNull(processor);
197         assertNotNull(servletContext.getAttribute("business"));
198         assertNull(servletContext.getAttribute("invalid"));
199 
200     }
201 
202 
203     // Test output sent directly to the servlet response ServletOutputStream
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     // Test output sent directly to the servlet response PrintWriter
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     // Test output sent indirectly to the servlet or portlet response stream
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     // Test output sent indirectly to the servlet or portlet response writer
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     // --------------------------------------------------------- Support Methods
274 
275 
276 
277 }