KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > TestSetupRequestEncoding


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.services.impl;
16
17 import java.io.UnsupportedEncodingException JavaDoc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.apache.hivemind.ApplicationRuntimeException;
23 import org.apache.hivemind.test.HiveMindTestCase;
24 import org.apache.tapestry.services.ServletRequestServicer;
25 import org.easymock.MockControl;
26
27 /**
28  * Tests for {@link org.apache.tapestry.services.impl.SetupRequestEncoding}.
29  *
30  * @author Howard M. Lewis Ship
31  * @since 4.0
32  */

33 public class TestSetupRequestEncoding extends HiveMindTestCase
34 {
35     private HttpServletRequest JavaDoc newRequest(String JavaDoc encoding)
36     {
37         MockControl control = newControl(HttpServletRequest JavaDoc.class);
38         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
39
40         request.getCharacterEncoding();
41         control.setReturnValue(encoding);
42
43         return request;
44     }
45
46     private HttpServletResponse JavaDoc newResponse()
47     {
48         return (HttpServletResponse JavaDoc) newMock(HttpServletResponse JavaDoc.class);
49     }
50
51     private ServletRequestServicer newServicer()
52     {
53         return (ServletRequestServicer) newMock(ServletRequestServicer.class);
54     }
55
56     public void testEncodingNotNull() throws Exception JavaDoc
57     {
58         HttpServletRequest JavaDoc request = newRequest("utf-8");
59         HttpServletResponse JavaDoc response = newResponse();
60         ServletRequestServicer servicer = newServicer();
61
62         servicer.service(request, response);
63
64         replayControls();
65
66         SetupRequestEncoding sre = new SetupRequestEncoding();
67         sre.setOutputEncoding("output-encoding");
68
69         sre.service(request, response, servicer);
70
71         verifyControls();
72     }
73
74     public void testEncodingNull() throws Exception JavaDoc
75     {
76         HttpServletRequest JavaDoc request = newRequest(null);
77         HttpServletResponse JavaDoc response = newResponse();
78         ServletRequestServicer servicer = newServicer();
79
80         request.setCharacterEncoding("output-encoding");
81
82         servicer.service(request, response);
83
84         replayControls();
85
86         SetupRequestEncoding sre = new SetupRequestEncoding();
87         sre.setOutputEncoding("output-encoding");
88
89         sre.service(request, response, servicer);
90
91         verifyControls();
92     }
93
94     public void testUnsupportedEncoding() throws Exception JavaDoc
95     {
96         MockControl control = newControl(HttpServletRequest JavaDoc.class);
97         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
98
99         HttpServletResponse JavaDoc response = newResponse();
100         ServletRequestServicer servicer = newServicer();
101
102         Throwable JavaDoc t = new UnsupportedEncodingException JavaDoc("Bad encoding.");
103
104         request.getCharacterEncoding();
105         control.setReturnValue(null);
106
107         request.setCharacterEncoding("output-encoding");
108         control.setThrowable(t);
109
110         replayControls();
111
112         SetupRequestEncoding sre = new SetupRequestEncoding();
113         sre.setOutputEncoding("output-encoding");
114
115         try
116         {
117             sre.service(request, response, servicer);
118             unreachable();
119         }
120         catch (ApplicationRuntimeException ex)
121         {
122             assertEquals(
123                     "Unable to set request character encoding to 'output-encoding': Bad encoding.",
124                     ex.getMessage());
125             assertSame(t, ex.getRootCause());
126         }
127
128         verifyControls();
129     }
130
131     public void testNoSuchMethodError() throws Exception JavaDoc
132     {
133         MockControl control = newControl(HttpServletRequest JavaDoc.class);
134         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
135
136         HttpServletResponse JavaDoc response = newResponse();
137         ServletRequestServicer servicer = newServicer();
138
139         Throwable JavaDoc t = new NoSuchMethodError JavaDoc();
140
141         request.getCharacterEncoding();
142         control.setReturnValue(null);
143
144         request.setCharacterEncoding("output-encoding");
145         control.setThrowable(t);
146
147         servicer.service(request, response);
148
149         replayControls();
150
151         SetupRequestEncoding sre = new SetupRequestEncoding();
152         sre.setOutputEncoding("output-encoding");
153
154         sre.service(request, response, servicer);
155
156         verifyControls();
157
158         // Check that, after such an error, we don't even try to do it again.
159

160         servicer.service(request, response);
161
162         replayControls();
163
164         sre.service(request, response, servicer);
165
166         verifyControls();
167     }
168     
169     public void testAbstractMethodError() throws Exception JavaDoc
170     {
171         MockControl control = newControl(HttpServletRequest JavaDoc.class);
172         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) control.getMock();
173
174         HttpServletResponse JavaDoc response = newResponse();
175         ServletRequestServicer servicer = newServicer();
176
177         Throwable JavaDoc t = new AbstractMethodError JavaDoc();
178
179         request.getCharacterEncoding();
180         control.setReturnValue(null);
181
182         request.setCharacterEncoding("output-encoding");
183         control.setThrowable(t);
184
185         servicer.service(request, response);
186
187         replayControls();
188
189         SetupRequestEncoding sre = new SetupRequestEncoding();
190         sre.setOutputEncoding("output-encoding");
191
192         sre.service(request, response, servicer);
193
194         verifyControls();
195
196         // Check that, after such an error, we don't even try to do it again.
197

198         servicer.service(request, response);
199
200         replayControls();
201
202         sre.service(request, response, servicer);
203
204         verifyControls();
205     }
206 }
Popular Tags