1 15 package org.apache.tapestry.web; 16 17 import java.io.CharArrayWriter ; 18 import java.io.IOException ; 19 import java.io.PrintWriter ; 20 21 import javax.servlet.ServletOutputStream ; 22 import javax.servlet.http.HttpServletResponse ; 23 24 import org.apache.hivemind.ApplicationRuntimeException; 25 import org.apache.hivemind.test.HiveMindTestCase; 26 import org.apache.tapestry.util.ContentType; 27 import org.easymock.MockControl; 28 29 35 public class TestServletWebResponse extends HiveMindTestCase 36 { 37 private static class MockServletOutputStream extends ServletOutputStream 38 { 39 public void write(int b) throws IOException 40 { 41 } 42 } 43 44 public void testGetOutputStream() throws Exception 45 { 46 MockControl control = newControl(HttpServletResponse .class); 47 HttpServletResponse response = (HttpServletResponse ) control.getMock(); 48 49 ServletOutputStream stream = new MockServletOutputStream(); 50 51 response.setContentType("foo/bar"); 52 response.getOutputStream(); 53 control.setReturnValue(stream); 54 55 replayControls(); 56 57 ServletWebResponse swr = new ServletWebResponse(response); 58 59 assertSame(stream, swr.getOutputStream(new ContentType("foo/bar"))); 60 61 verifyControls(); 62 } 63 64 public void testGetOutputStreamFailure() throws Exception 65 { 66 MockControl control = newControl(HttpServletResponse .class); 67 HttpServletResponse response = (HttpServletResponse ) control.getMock(); 68 69 Throwable t = new IOException ("Simulated failure."); 70 71 response.setContentType("foo/bar"); 72 response.getOutputStream(); 73 control.setThrowable(t); 74 75 replayControls(); 76 77 ServletWebResponse swr = new ServletWebResponse(response); 78 79 try 80 { 81 swr.getOutputStream(new ContentType("foo/bar")); 82 unreachable(); 83 } 84 catch (ApplicationRuntimeException ex) 85 { 86 assertEquals( 87 "Error opening response stream for content type foo/bar: Simulated failure.", 88 ex.getMessage()); 89 assertSame(t, ex.getRootCause()); 90 } 91 } 92 93 public void testGetPrintWriter() throws Exception 94 { 95 PrintWriter writer = new PrintWriter (new CharArrayWriter ()); 96 97 MockControl control = newControl(HttpServletResponse .class); 98 HttpServletResponse response = (HttpServletResponse ) control.getMock(); 99 100 response.setContentType("foo/bar"); 101 response.getWriter(); 102 control.setReturnValue(writer); 103 104 replayControls(); 105 106 ServletWebResponse swr = new ServletWebResponse(response); 107 108 assertSame(writer, swr.getPrintWriter(new ContentType("foo/bar"))); 109 110 verifyControls(); 111 } 112 113 public void testGetSecondPrintWriter() throws Exception 114 { 115 PrintWriter writer1 = new PrintWriter (new CharArrayWriter ()); 116 PrintWriter writer2 = new PrintWriter (new CharArrayWriter ()); 117 118 MockControl control = newControl(HttpServletResponse .class); 119 HttpServletResponse response = (HttpServletResponse ) control.getMock(); 120 121 response.setContentType("foo/bar"); 122 response.getWriter(); 123 control.setReturnValue(writer1); 124 125 replayControls(); 126 127 ServletWebResponse swr = new ServletWebResponse(response); 128 129 assertSame(writer1, swr.getPrintWriter(new ContentType("foo/bar"))); 130 131 verifyControls(); 132 133 response.reset(); 134 response.setContentType("zip/zap"); 135 response.getWriter(); 136 control.setReturnValue(writer2); 137 138 replayControls(); 139 140 assertSame(writer2, swr.getPrintWriter(new ContentType("zip/zap"))); 141 142 verifyControls(); 143 } 144 145 public void testGetPrintWriterFailure() throws Exception 146 { 147 MockControl control = newControl(HttpServletResponse .class); 148 HttpServletResponse response = (HttpServletResponse ) control.getMock(); 149 150 Throwable t = new IOException ("Simulated failure."); 151 152 response.setContentType("foo/bar"); 153 response.getWriter(); 154 control.setThrowable(t); 155 156 replayControls(); 157 158 ServletWebResponse swr = new ServletWebResponse(response); 159 160 try 161 { 162 swr.getPrintWriter(new ContentType("foo/bar")); 163 unreachable(); 164 } 165 catch (ApplicationRuntimeException ex) 166 { 167 assertEquals( 168 "Error opening response writer for content type foo/bar: Simulated failure.", 169 ex.getMessage()); 170 assertSame(t, ex.getRootCause()); 171 } 172 } 173 174 public void testReset() 175 { 176 MockControl control = newControl(HttpServletResponse .class); 177 HttpServletResponse response = (HttpServletResponse ) control.getMock(); 178 179 response.reset(); 180 181 replayControls(); 182 183 ServletWebResponse swr = new ServletWebResponse(response); 184 185 swr.reset(); 186 187 verifyControls(); 188 } 189 190 } | Popular Tags |