1 15 package org.apache.tapestry.test.mock; 16 17 import java.io.FileInputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 22 import javax.servlet.RequestDispatcher ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.ServletRequest ; 25 import javax.servlet.ServletResponse ; 26 27 33 public class MockRequestDispatcher implements RequestDispatcher 34 { 35 private String _resourcePath; 36 37 public MockRequestDispatcher(String resourcePath) 38 { 39 _resourcePath = resourcePath; 40 } 41 42 public void forward(ServletRequest request, ServletResponse response) 43 throws ServletException , IOException 44 { 45 if (_resourcePath.endsWith("/FAIL_SERVLET")) 46 throw new ServletException ("Test-directed ServletException from RequestDispatcher forward()."); 47 48 50 51 InputStream in = new FileInputStream (_resourcePath); 52 53 response.setContentType("test/html"); 54 55 OutputStream 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 request, ServletResponse response) 75 throws ServletException , IOException 76 { 77 throw new ServletException ("MockRequestDispatcher.include() not supported."); 78 } 79 80 } 81 | Popular Tags |