1 15 package org.apache.tapestry.services.impl; 16 17 import java.io.UnsupportedEncodingException ; 18 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpServletResponse ; 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 33 public class TestSetupRequestEncoding extends HiveMindTestCase 34 { 35 private HttpServletRequest newRequest(String encoding) 36 { 37 MockControl control = newControl(HttpServletRequest .class); 38 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 39 40 request.getCharacterEncoding(); 41 control.setReturnValue(encoding); 42 43 return request; 44 } 45 46 private HttpServletResponse newResponse() 47 { 48 return (HttpServletResponse ) newMock(HttpServletResponse .class); 49 } 50 51 private ServletRequestServicer newServicer() 52 { 53 return (ServletRequestServicer) newMock(ServletRequestServicer.class); 54 } 55 56 public void testEncodingNotNull() throws Exception 57 { 58 HttpServletRequest request = newRequest("utf-8"); 59 HttpServletResponse 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 75 { 76 HttpServletRequest request = newRequest(null); 77 HttpServletResponse 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 95 { 96 MockControl control = newControl(HttpServletRequest .class); 97 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 98 99 HttpServletResponse response = newResponse(); 100 ServletRequestServicer servicer = newServicer(); 101 102 Throwable t = new UnsupportedEncodingException ("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 132 { 133 MockControl control = newControl(HttpServletRequest .class); 134 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 135 136 HttpServletResponse response = newResponse(); 137 ServletRequestServicer servicer = newServicer(); 138 139 Throwable t = new NoSuchMethodError (); 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 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 170 { 171 MockControl control = newControl(HttpServletRequest .class); 172 HttpServletRequest request = (HttpServletRequest ) control.getMock(); 173 174 HttpServletResponse response = newResponse(); 175 ServletRequestServicer servicer = newServicer(); 176 177 Throwable t = new AbstractMethodError (); 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 198 servicer.service(request, response); 199 200 replayControls(); 201 202 sre.service(request, response, servicer); 203 204 verifyControls(); 205 } 206 } | Popular Tags |