001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.services.impl;
016    
017    import static org.easymock.EasyMock.expect;
018    import static org.easymock.EasyMock.expectLastCall;
019    
020    import java.io.UnsupportedEncodingException;
021    
022    import org.apache.hivemind.ApplicationRuntimeException;
023    import org.apache.tapestry.BaseComponentTestCase;
024    import org.apache.tapestry.services.ServletRequestServicer;
025    import org.testng.annotations.Test;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    /**
031     * Tests for {@link org.apache.tapestry.services.impl.SetupRequestEncoding}.
032     * 
033     * @author Howard M. Lewis Ship
034     * @since 4.0
035     */
036    @Test
037    public class TestSetupRequestEncoding extends BaseComponentTestCase
038    {
039        private HttpServletRequest newRequest(String encoding)
040        {
041            HttpServletRequest request = newMock(HttpServletRequest.class);
042    
043            expect(request.getCharacterEncoding()).andReturn(encoding);
044    
045            return request;
046        }
047    
048        private HttpServletResponse newResponse()
049        {
050            return newMock(HttpServletResponse.class);
051        }
052    
053        private ServletRequestServicer newServicer()
054        {
055            return newMock(ServletRequestServicer.class);
056        }
057    
058        public void testEncodingNotNull() throws Exception
059        {
060            HttpServletRequest request = newRequest("utf-8");
061            HttpServletResponse response = newResponse();
062            ServletRequestServicer servicer = newServicer();
063    
064            servicer.service(request, response);
065    
066            replay();
067    
068            SetupRequestEncoding sre = new SetupRequestEncoding();
069            sre.setOutputEncoding("output-encoding");
070    
071            sre.service(request, response, servicer);
072    
073            verify();
074        }
075    
076        public void testEncodingNull() throws Exception
077        {
078            HttpServletRequest request = newRequest(null);
079            HttpServletResponse response = newResponse();
080            ServletRequestServicer servicer = newServicer();
081    
082            request.setCharacterEncoding("output-encoding");
083    
084            servicer.service(request, response);
085    
086            replay();
087    
088            SetupRequestEncoding sre = new SetupRequestEncoding();
089            sre.setOutputEncoding("output-encoding");
090    
091            sre.service(request, response, servicer);
092    
093            verify();
094        }
095    
096        public void testUnsupportedEncoding() throws Exception
097        {
098            HttpServletRequest request = newMock(HttpServletRequest.class);
099    
100            HttpServletResponse response = newResponse();
101            ServletRequestServicer servicer = newServicer();
102    
103            Throwable t = new UnsupportedEncodingException("Bad encoding.");
104    
105            expect(request.getCharacterEncoding()).andReturn(null);
106    
107            request.setCharacterEncoding("output-encoding");
108            expectLastCall().andThrow(t);
109    
110            replay();
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            verify();
129        }
130    
131        public void testNoSuchMethodError() throws Exception
132        {
133            HttpServletRequest request = newMock(HttpServletRequest.class);
134    
135            HttpServletResponse response = newResponse();
136            ServletRequestServicer servicer = newServicer();
137    
138            Throwable t = new NoSuchMethodError();
139    
140            expect(request.getCharacterEncoding()).andReturn(null);
141    
142            request.setCharacterEncoding("output-encoding");
143            expectLastCall().andThrow(t);
144    
145            servicer.service(request, response);
146    
147            replay();
148    
149            SetupRequestEncoding sre = new SetupRequestEncoding();
150            sre.setOutputEncoding("output-encoding");
151    
152            sre.service(request, response, servicer);
153    
154            verify();
155    
156            // Check that, after such an error, we don't even try to do it again.
157    
158            servicer.service(request, response);
159    
160            replay();
161    
162            sre.service(request, response, servicer);
163    
164            verify();
165        }
166        
167        public void testAbstractMethodError() throws Exception
168        {
169            HttpServletRequest request = newMock(HttpServletRequest.class);
170    
171            HttpServletResponse response = newResponse();
172            ServletRequestServicer servicer = newServicer();
173    
174            Throwable t = new AbstractMethodError();
175    
176            expect(request.getCharacterEncoding()).andReturn(null);
177    
178            request.setCharacterEncoding("output-encoding");
179            expectLastCall().andThrow(t);
180    
181            servicer.service(request, response);
182    
183            replay();
184    
185            SetupRequestEncoding sre = new SetupRequestEncoding();
186            sre.setOutputEncoding("output-encoding");
187    
188            sre.service(request, response, servicer);
189    
190            verify();
191    
192            // Check that, after such an error, we don't even try to do it again.
193    
194            servicer.service(request, response);
195    
196            replay();
197    
198            sre.service(request, response, servicer);
199    
200            verify();
201        }    
202    }