KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > web > TestServletWebResponse


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.web;
16
17 import java.io.CharArrayWriter JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20
21 import javax.servlet.ServletOutputStream JavaDoc;
22 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
30  * Tests for {@link org.apache.tapestry.web.ServletWebResponse}.
31  *
32  * @author Howard M. Lewis Ship
33  * @since 4.0
34  */

35 public class TestServletWebResponse extends HiveMindTestCase
36 {
37     private static class MockServletOutputStream extends ServletOutputStream JavaDoc
38     {
39         public void write(int b) throws IOException JavaDoc
40         {
41         }
42     }
43
44     public void testGetOutputStream() throws Exception JavaDoc
45     {
46         MockControl control = newControl(HttpServletResponse JavaDoc.class);
47         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) control.getMock();
48
49         ServletOutputStream JavaDoc 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 JavaDoc
65     {
66         MockControl control = newControl(HttpServletResponse JavaDoc.class);
67         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) control.getMock();
68
69         Throwable JavaDoc t = new IOException JavaDoc("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 JavaDoc
94     {
95         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(new CharArrayWriter JavaDoc());
96
97         MockControl control = newControl(HttpServletResponse JavaDoc.class);
98         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) 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 JavaDoc
114     {
115         PrintWriter JavaDoc writer1 = new PrintWriter JavaDoc(new CharArrayWriter JavaDoc());
116         PrintWriter JavaDoc writer2 = new PrintWriter JavaDoc(new CharArrayWriter JavaDoc());
117
118         MockControl control = newControl(HttpServletResponse JavaDoc.class);
119         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) 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 JavaDoc
146     {
147         MockControl control = newControl(HttpServletResponse JavaDoc.class);
148         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) control.getMock();
149
150         Throwable JavaDoc t = new IOException JavaDoc("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 JavaDoc.class);
177         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) 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