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 { 022 private PrintWriter printWriter; 023 private Writer writer; 024 private boolean providedWriter; 025 026 public MockJspWriter() 027 { 028 super(0, true); 029 this.writer = new StringWriter(); 030 initWriter(); 031 providedWriter = false; 032 } 033 034 public MockJspWriter(Writer writer) 035 { 036 super(0, true); 037 this.writer = writer; 038 initWriter(); 039 providedWriter = true; 040 } 041 042 /** 043 * Returns the output or an empty string, if 044 * an output <code>Writer</code> was provided 045 * in the constructor. 046 * @return the output or an empty string 047 */ 048 public String getOutputAsString() 049 { 050 try 051 { 052 flush(); 053 if(!providedWriter) 054 { 055 return ((StringWriter)writer).toString(); 056 } 057 return ""; 058 } 059 catch(IOException exc) 060 { 061 throw new NestedApplicationException(exc); 062 } 063 } 064 065 /** 066 * Delegates to {@link #getOutputAsString} 067 */ 068 public String toString() 069 { 070 return getOutputAsString(); 071 } 072 073 /** 074 * Clears the output. This method throws an <code>IOException</code>, 075 * if a <code>Writer</code> was provided according to spec. 076 */ 077 public void clear() throws IOException 078 { 079 if(!providedWriter) 080 { 081 this.writer = new StringWriter(); 082 initWriter(); 083 } 084 else 085 { 086 throw new IOException("Illegal call if writer is provided."); 087 } 088 } 089 090 /** 091 * Clears the output. This method does nothing, 092 * if a <code>Writer</code> was provided according to spec. 093 */ 094 public void clearBuffer() throws IOException 095 { 096 if(!providedWriter) 097 { 098 this.writer = new StringWriter(); 099 initWriter(); 100 } 101 } 102 103 public void close() throws IOException 104 { 105 flush(); 106 printWriter.close(); 107 } 108 109 public int getRemaining() 110 { 111 return 0; 112 } 113 114 public void flush() throws IOException 115 { 116 printWriter.flush(); 117 } 118 119 public void newLine() throws IOException 120 { 121 print(System.getProperty("line.separator")); 122 } 123 124 public void print(boolean value) throws IOException 125 { 126 printWriter.print(value); 127 } 128 129 public void print(char value) throws IOException 130 { 131 printWriter.print(value); 132 } 133 134 public void print(char[] value) throws IOException 135 { 136 printWriter.print(value); 137 } 138 139 public void print(double value) throws IOException 140 { 141 printWriter.print(value); 142 } 143 144 public void print(float value) throws IOException 145 { 146 printWriter.print(value); 147 } 148 149 public void print(int value) throws IOException 150 { 151 printWriter.print(value); 152 } 153 154 public void print(long value) throws IOException 155 { 156 printWriter.print(value); 157 } 158 159 public void print(Object value) throws IOException 160 { 161 printWriter.print(value); 162 } 163 164 public void print(String value) throws IOException 165 { 166 printWriter.print(value); 167 } 168 169 public void println() throws IOException 170 { 171 printWriter.println(); 172 } 173 174 public void println(boolean value) throws IOException 175 { 176 printWriter.println(value); 177 } 178 179 public void println(char value) throws IOException 180 { 181 printWriter.println(value); 182 } 183 184 public void println(char[] value) throws IOException 185 { 186 printWriter.println(value); 187 } 188 189 public void println(double value) throws IOException 190 { 191 printWriter.println(value); 192 } 193 194 public void println(float value) throws IOException 195 { 196 printWriter.println(value); 197 } 198 199 public void println(int value) throws IOException 200 { 201 printWriter.println(value); 202 } 203 204 public void println(long value) throws IOException 205 { 206 printWriter.println(value); 207 } 208 209 public void println(Object value) throws IOException 210 { 211 printWriter.println(value); 212 } 213 214 public void println(String value) throws IOException 215 { 216 printWriter.println(value); 217 } 218 219 public void write(char[] cbuf, int off, int len) throws IOException 220 { 221 printWriter.write(cbuf, off, len); 222 } 223 224 private void initWriter() 225 { 226 printWriter = new PrintWriter(writer); 227 } 228 }