001/* 002 * Copyright 2002-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.mock.web; 018 019import java.io.IOException; 020import java.io.Reader; 021import java.io.StringReader; 022import java.io.Writer; 023 024import javax.servlet.http.HttpServletResponse; 025import javax.servlet.jsp.JspWriter; 026import javax.servlet.jsp.tagext.BodyContent; 027 028import org.springframework.lang.Nullable; 029 030/** 031 * Mock implementation of the {@link javax.servlet.jsp.tagext.BodyContent} class. 032 * Only necessary for testing applications when testing custom JSP tags. 033 * 034 * @author Juergen Hoeller 035 * @since 2.5 036 */ 037public class MockBodyContent extends BodyContent { 038 039 private final String content; 040 041 042 /** 043 * Create a MockBodyContent for the given response. 044 * @param content the body content to expose 045 * @param response the servlet response to wrap 046 */ 047 public MockBodyContent(String content, HttpServletResponse response) { 048 this(content, response, null); 049 } 050 051 /** 052 * Create a MockBodyContent for the given response. 053 * @param content the body content to expose 054 * @param targetWriter the target Writer to wrap 055 */ 056 public MockBodyContent(String content, Writer targetWriter) { 057 this(content, null, targetWriter); 058 } 059 060 /** 061 * Create a MockBodyContent for the given response. 062 * @param content the body content to expose 063 * @param response the servlet response to wrap 064 * @param targetWriter the target Writer to wrap 065 */ 066 public MockBodyContent(String content, @Nullable HttpServletResponse response, @Nullable Writer targetWriter) { 067 super(adaptJspWriter(targetWriter, response)); 068 this.content = content; 069 } 070 071 private static JspWriter adaptJspWriter(@Nullable Writer targetWriter, @Nullable HttpServletResponse response) { 072 if (targetWriter instanceof JspWriter) { 073 return (JspWriter) targetWriter; 074 } 075 else { 076 return new MockJspWriter(response, targetWriter); 077 } 078 } 079 080 081 @Override 082 public Reader getReader() { 083 return new StringReader(this.content); 084 } 085 086 @Override 087 public String getString() { 088 return this.content; 089 } 090 091 @Override 092 public void writeOut(Writer writer) throws IOException { 093 writer.write(this.content); 094 } 095 096 097 //--------------------------------------------------------------------- 098 // Delegating implementations of JspWriter's abstract methods 099 //--------------------------------------------------------------------- 100 101 @Override 102 public void clear() throws IOException { 103 getEnclosingWriter().clear(); 104 } 105 106 @Override 107 public void clearBuffer() throws IOException { 108 getEnclosingWriter().clearBuffer(); 109 } 110 111 @Override 112 public void close() throws IOException { 113 getEnclosingWriter().close(); 114 } 115 116 @Override 117 public int getRemaining() { 118 return getEnclosingWriter().getRemaining(); 119 } 120 121 @Override 122 public void newLine() throws IOException { 123 getEnclosingWriter().println(); 124 } 125 126 @Override 127 public void write(char[] value, int offset, int length) throws IOException { 128 getEnclosingWriter().write(value, offset, length); 129 } 130 131 @Override 132 public void print(boolean value) throws IOException { 133 getEnclosingWriter().print(value); 134 } 135 136 @Override 137 public void print(char value) throws IOException { 138 getEnclosingWriter().print(value); 139 } 140 141 @Override 142 public void print(char[] value) throws IOException { 143 getEnclosingWriter().print(value); 144 } 145 146 @Override 147 public void print(double value) throws IOException { 148 getEnclosingWriter().print(value); 149 } 150 151 @Override 152 public void print(float value) throws IOException { 153 getEnclosingWriter().print(value); 154 } 155 156 @Override 157 public void print(int value) throws IOException { 158 getEnclosingWriter().print(value); 159 } 160 161 @Override 162 public void print(long value) throws IOException { 163 getEnclosingWriter().print(value); 164 } 165 166 @Override 167 public void print(Object value) throws IOException { 168 getEnclosingWriter().print(value); 169 } 170 171 @Override 172 public void print(String value) throws IOException { 173 getEnclosingWriter().print(value); 174 } 175 176 @Override 177 public void println() throws IOException { 178 getEnclosingWriter().println(); 179 } 180 181 @Override 182 public void println(boolean value) throws IOException { 183 getEnclosingWriter().println(value); 184 } 185 186 @Override 187 public void println(char value) throws IOException { 188 getEnclosingWriter().println(value); 189 } 190 191 @Override 192 public void println(char[] value) throws IOException { 193 getEnclosingWriter().println(value); 194 } 195 196 @Override 197 public void println(double value) throws IOException { 198 getEnclosingWriter().println(value); 199 } 200 201 @Override 202 public void println(float value) throws IOException { 203 getEnclosingWriter().println(value); 204 } 205 206 @Override 207 public void println(int value) throws IOException { 208 getEnclosingWriter().println(value); 209 } 210 211 @Override 212 public void println(long value) throws IOException { 213 getEnclosingWriter().println(value); 214 } 215 216 @Override 217 public void println(Object value) throws IOException { 218 getEnclosingWriter().println(value); 219 } 220 221 @Override 222 public void println(String value) throws IOException { 223 getEnclosingWriter().println(value); 224 } 225 226}