KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > mocks > servlet > MockServletResponse


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 package org.exoplatform.test.mocks.servlet;
7 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import javax.servlet.ServletOutputStream JavaDoc;
10 import java.io.ByteArrayOutputStream JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.* ;
14
15 /**
16  * Created by The eXo Platform SARL
17  * Author : Mestrallet Benjamin
18  * benjmestrallet@users.sourceforge.net
19  * Date: Jul 29, 2003
20  * Time: 6:54:45 PM
21  */

22 public class MockServletResponse extends HttpServletResponseWrapper JavaDoc {
23
24   private PrintWriter JavaDoc tmpWriter;
25   private ByteArrayOutputStream JavaDoc output;
26   private ByteArrayServletOutputStream servletOutput;
27   private Locale locale_ ;
28
29   public MockServletResponse(HttpServletResponse JavaDoc httpServletResponse) {
30     super(httpServletResponse);
31     output = new ByteArrayOutputStream JavaDoc();
32     tmpWriter = new PrintWriter JavaDoc(output);
33     servletOutput = new ByteArrayServletOutputStream(output);
34   }
35
36   public void finalize() throws Throwable JavaDoc {
37     super.finalize();
38     servletOutput.close();
39     output.close();
40     tmpWriter.close();
41   }
42
43   public String JavaDoc getPortletContent() {
44     String JavaDoc s = output.toString();
45     reset();
46     return s;
47   }
48   
49   /*
50   public PrintWriter getWriter() throws IOException {
51     //return servletResponse.getWriter();
52     return tmpWriter;
53   }
54
55   public ServletOutputStream getOutputStream() throws IOException {
56     return servletOutput;
57   }
58   */

59
60   public byte[] toByteArray() {
61     return output.toByteArray();
62   }
63   
64   public String JavaDoc getOutputContent() {
65     return new String JavaDoc(output.toByteArray());
66   }
67
68   public void flushBuffer() throws IOException JavaDoc {
69     tmpWriter.flush();
70     servletOutput.flush();
71   }
72
73   public void reset() {
74     output.reset();
75   }
76
77   public void close() throws IOException JavaDoc {
78     tmpWriter.close();
79   }
80
81   private static class ByteArrayServletOutputStream extends ServletOutputStream JavaDoc {
82     ByteArrayOutputStream JavaDoc baos;
83
84     public ByteArrayServletOutputStream(ByteArrayOutputStream JavaDoc baos) {
85       this.baos = baos;
86     }
87
88     public void write(int i) throws IOException JavaDoc {
89       baos.write(i);
90     }
91   }
92   
93   public Locale getLocale() { return locale_ ; }
94   public void setLocale(java.util.Locale JavaDoc loc) { locale_ = loc ; }
95   
96 }
97
Popular Tags