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.test.mock;
19
20 import java.io.ByteArrayOutputStream;
21 import javax.servlet.ServletOutputStream;
22
23 /***
24 * <p>Mock implementation of <code>ServletOutputStream</code>.</p>
25 *
26 * $Id: MockServletOutputStream.java 464373 2006-10-16 04:21:54Z rahul $
27 */
28
29 public class MockServletOutputStream extends ServletOutputStream {
30
31
32 // ------------------------------------------------------------ Constructors
33
34
35 /***
36 * <p>Return a default instance.</p>
37 *
38 * @param stream The stream we will use to buffer output
39 */
40 public MockServletOutputStream(ByteArrayOutputStream stream) {
41 this.baos = stream;
42 }
43
44
45 // ----------------------------------------------------- Mock Object Methods
46
47
48 /***
49 * <p>Return the content that has been written to this output stream.</p>
50 */
51 public byte[] content() {
52 return baos.toByteArray();
53 }
54
55
56 /***
57 * <p>Reset this output stream so that it appears no content has been
58 * written.</p>
59 */
60 public void reset() {
61 baos.reset();
62 }
63
64
65 /***
66 * <p>Return the number of bytes that have been written to this output stream.</p>
67 */
68 public int size() {
69 return baos.size();
70 }
71
72
73
74 // ------------------------------------------------------ Instance Variables
75
76
77 /***
78 * <p>The internal buffer we use to capture output.</p>
79 */
80 private ByteArrayOutputStream baos = null;
81
82
83 // --------------------------------------------- ServletOutputStream Methods
84
85
86 /***
87 * <p>Write the specified content to our internal cache.</p>
88 *
89 * @param content Content to be written
90 */
91 public void write(int content) {
92 baos.write(content);
93 }
94
95
96 }