001: package com.mockrunner.mock.web;
002:
003: import java.io.IOException;
004: import java.io.PrintWriter;
005: import java.io.StringWriter;
006: import java.io.Writer;
007:
008: import javax.servlet.jsp.JspWriter;
009:
010: import com.mockrunner.base.NestedApplicationException;
011:
012: /**
013: * Mock implementation of <code>JspWriter</code>.
014: * Collects the output data. If you provide a <code>Writer</code>
015: * in the constructor, the output data is written to this
016: * provided <code>Writer</code>. The method {@link #getOutputAsString}
017: * returns an empty string in this case. Otherwise it returns the
018: * collected data.
019: */
020: public class MockJspWriter extends JspWriter {
021: private PrintWriter printWriter;
022: private Writer writer;
023: private boolean providedWriter;
024:
025: public MockJspWriter() {
026: super (0, true);
027: this .writer = new StringWriter();
028: initWriter();
029: providedWriter = false;
030: }
031:
032: public MockJspWriter(Writer writer) {
033: super (0, true);
034: this .writer = writer;
035: initWriter();
036: providedWriter = true;
037: }
038:
039: /**
040: * Returns the output or an empty string, if
041: * an output <code>Writer</code> was provided
042: * in the constructor.
043: * @return the output or an empty string
044: */
045: public String getOutputAsString() {
046: try {
047: flush();
048: if (!providedWriter) {
049: return ((StringWriter) writer).toString();
050: }
051: return "";
052: } catch (IOException exc) {
053: throw new NestedApplicationException(exc);
054: }
055: }
056:
057: /**
058: * Delegates to {@link #getOutputAsString}
059: */
060: public String toString() {
061: return getOutputAsString();
062: }
063:
064: /**
065: * Clears the output. This method throws an <code>IOException</code>,
066: * if a <code>Writer</code> was provided according to spec.
067: */
068: public void clear() throws IOException {
069: if (!providedWriter) {
070: this .writer = new StringWriter();
071: initWriter();
072: } else {
073: throw new IOException("Illegal call if writer is provided.");
074: }
075: }
076:
077: /**
078: * Clears the output. This method does nothing,
079: * if a <code>Writer</code> was provided according to spec.
080: */
081: public void clearBuffer() throws IOException {
082: if (!providedWriter) {
083: this .writer = new StringWriter();
084: initWriter();
085: }
086: }
087:
088: public void close() throws IOException {
089: flush();
090: printWriter.close();
091: }
092:
093: public int getRemaining() {
094: return 0;
095: }
096:
097: public void flush() throws IOException {
098: printWriter.flush();
099: }
100:
101: public void newLine() throws IOException {
102: print(System.getProperty("line.separator"));
103: }
104:
105: public void print(boolean value) throws IOException {
106: printWriter.print(value);
107: }
108:
109: public void print(char value) throws IOException {
110: printWriter.print(value);
111: }
112:
113: public void print(char[] value) throws IOException {
114: printWriter.print(value);
115: }
116:
117: public void print(double value) throws IOException {
118: printWriter.print(value);
119: }
120:
121: public void print(float value) throws IOException {
122: printWriter.print(value);
123: }
124:
125: public void print(int value) throws IOException {
126: printWriter.print(value);
127: }
128:
129: public void print(long value) throws IOException {
130: printWriter.print(value);
131: }
132:
133: public void print(Object value) throws IOException {
134: printWriter.print(value);
135: }
136:
137: public void print(String value) throws IOException {
138: printWriter.print(value);
139: }
140:
141: public void println() throws IOException {
142: printWriter.println();
143: }
144:
145: public void println(boolean value) throws IOException {
146: printWriter.println(value);
147: }
148:
149: public void println(char value) throws IOException {
150: printWriter.println(value);
151: }
152:
153: public void println(char[] value) throws IOException {
154: printWriter.println(value);
155: }
156:
157: public void println(double value) throws IOException {
158: printWriter.println(value);
159: }
160:
161: public void println(float value) throws IOException {
162: printWriter.println(value);
163: }
164:
165: public void println(int value) throws IOException {
166: printWriter.println(value);
167: }
168:
169: public void println(long value) throws IOException {
170: printWriter.println(value);
171: }
172:
173: public void println(Object value) throws IOException {
174: printWriter.println(value);
175: }
176:
177: public void println(String value) throws IOException {
178: printWriter.println(value);
179: }
180:
181: public void write(char[] cbuf, int off, int len) throws IOException {
182: printWriter.write(cbuf, off, len);
183: }
184:
185: private void initWriter() {
186: printWriter = new PrintWriter(writer);
187: }
188: }
|