KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > mock > MockRequestDispatcher


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.test.mock;
16
17 import java.io.FileInputStream JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 import javax.servlet.RequestDispatcher JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.ServletRequest JavaDoc;
25 import javax.servlet.ServletResponse JavaDoc;
26
27 /**
28  * Used to enable mock testing of internal request forwarding.
29  *
30  * @author Howard Lewis Ship
31  * @since 4.0
32  */

33 public class MockRequestDispatcher implements RequestDispatcher JavaDoc
34 {
35     private String JavaDoc _resourcePath;
36     
37     public MockRequestDispatcher(String JavaDoc resourcePath)
38     {
39         _resourcePath = resourcePath;
40     }
41
42     public void forward(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
43         throws ServletException JavaDoc, IOException JavaDoc
44     {
45         if (_resourcePath.endsWith("/FAIL_SERVLET"))
46             throw new ServletException JavaDoc("Test-directed ServletException from RequestDispatcher forward().");
47         
48         // For testing purposes, assume we only forward to static HTML files.
49

50         
51         InputStream JavaDoc in = new FileInputStream JavaDoc(_resourcePath);
52
53         response.setContentType("test/html");
54         
55         OutputStream JavaDoc out = response.getOutputStream();
56
57         
58         byte[] buffer = new byte[1000];
59         
60         while (true)
61         {
62             int length = in.read(buffer);
63             
64             if (length < 0)
65                 break;
66                 
67             out.write(buffer, 0, length);
68         }
69         
70         in.close();
71         out.close();
72     }
73
74     public void include(ServletRequest JavaDoc request, ServletResponse JavaDoc response)
75         throws ServletException JavaDoc, IOException JavaDoc
76     {
77         throw new ServletException JavaDoc("MockRequestDispatcher.include() not supported.");
78     }
79
80 }
81
Popular Tags