001/* 002 * Copyright 2023 Web-Legacy 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.apache.tiles.request.jakarta.servlet; 017 018import static org.easymock.EasyMock.createMock; 019import static org.easymock.EasyMock.expect; 020import static org.easymock.EasyMock.expectLastCall; 021import static org.easymock.EasyMock.replay; 022import static org.easymock.EasyMock.verify; 023import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; 024import static org.junit.jupiter.api.Assertions.assertEquals; 025import static org.junit.jupiter.api.Assertions.assertInstanceOf; 026import static org.junit.jupiter.api.Assertions.assertThrowsExactly; 027import static org.junit.jupiter.api.Assertions.assertTrue; 028 029import java.io.IOException; 030import java.io.PrintWriter; 031import java.util.Locale; 032import java.util.Map; 033 034import org.apache.tiles.request.ApplicationContext; 035import org.apache.tiles.request.collection.HeaderValuesMap; 036import org.apache.tiles.request.collection.ReadOnlyEnumerationMap; 037import org.apache.tiles.request.collection.ScopeMap; 038import org.apache.tiles.request.jakarta.servlet.extractor.HeaderExtractor; 039import org.junit.jupiter.api.BeforeEach; 040import org.junit.jupiter.api.Test; 041 042import jakarta.servlet.RequestDispatcher; 043import jakarta.servlet.ServletException; 044import jakarta.servlet.ServletOutputStream; 045import jakarta.servlet.http.HttpServletRequest; 046import jakarta.servlet.http.HttpServletResponse; 047 048/** 049 * Tests {@link ServletRequest}. 050 * 051 * <p>Copied from Apache tiles-request-servlet 1.0.7 and adapted for 052 * Jakarta EE 9.</p> 053 */ 054public class ServletRequestTest { 055 056 /** 057 * Empty default constructor 058 */ 059 public ServletRequestTest() { 060 } 061 062 /** 063 * The application context. 064 */ 065 private ApplicationContext applicationContext; 066 067 /** 068 * The request. 069 */ 070 private HttpServletRequest request; 071 072 /** 073 * The response. 074 */ 075 private HttpServletResponse response; 076 077 /** 078 * The request to test. 079 */ 080 private ServletRequest req; 081 082 /** 083 * Sets up the test. 084 */ 085 @BeforeEach 086 public void setUp() { 087 applicationContext = createMock(ApplicationContext.class); 088 request = createMock(HttpServletRequest.class); 089 response = createMock(HttpServletResponse.class); 090 req = new ServletRequest(applicationContext, request, response); 091 } 092 093 /** 094 * Test method for {@link ServletRequest#doForward(String)}. 095 */ 096 @Test 097 public void testDoForward() { 098 assertDoesNotThrow(() -> { 099 RequestDispatcher rd = createMock(RequestDispatcher.class); 100 101 expect(response.isCommitted()).andReturn(false); 102 expect(request.getRequestDispatcher("/my/path")).andReturn(rd); 103 rd.forward(request, response); 104 105 replay(applicationContext, request, response, rd); 106 req.doForward("/my/path"); 107 verify(applicationContext, request, response, rd); 108 }); 109 } 110 111 /** 112 * Test method for {@link ServletRequest#doForward(String)}. 113 */ 114 @Test 115 public void testDoForwardNoDispatcher() { 116 assertThrowsExactly(IOException.class, () -> { 117 expect(response.isCommitted()).andReturn(false); 118 expect(request.getRequestDispatcher("/my/path")).andReturn(null); 119 120 replay(applicationContext, request, response); 121 try { 122 req.doForward("/my/path"); 123 } finally { 124 verify(applicationContext, request, response); 125 } 126 }); 127 } 128 129 /** 130 * Test method for {@link ServletRequest#doForward(String)}. 131 */ 132 @Test 133 public void testDoForwardServletException() { 134 assertThrowsExactly(IOException.class, () -> { 135 RequestDispatcher rd = createMock(RequestDispatcher.class); 136 137 expect(response.isCommitted()).andReturn(false); 138 expect(request.getRequestDispatcher("/my/path")).andReturn(rd); 139 rd.forward(request, response); 140 expectLastCall().andThrow(new ServletException()); 141 142 replay(applicationContext, request, response, rd); 143 try { 144 req.doForward("/my/path"); 145 } finally { 146 verify(applicationContext, request, response, rd); 147 } 148 }); 149 } 150 151 /** 152 * Test method for {@link ServletRequest#doForward(String)}. 153 */ 154 @Test 155 public void testDoForwardInclude() { 156 assertDoesNotThrow(() -> { 157 RequestDispatcher rd = createMock(RequestDispatcher.class); 158 159 expect(response.isCommitted()).andReturn(true); 160 expect(request.getRequestDispatcher("/my/path")).andReturn(rd); 161 rd.include(request, response); 162 163 replay(applicationContext, request, response, rd); 164 req.doForward("/my/path"); 165 verify(applicationContext, request, response, rd); 166 }); 167 } 168 169 /** 170 * Test method for {@link ServletRequest#doInclude(String)}. 171 */ 172 @Test 173 public void testDoInclude() { 174 assertDoesNotThrow(() -> { 175 RequestDispatcher rd = createMock(RequestDispatcher.class); 176 177 expect(request.getRequestDispatcher("/my/path")).andReturn(rd); 178 rd.include(request, response); 179 180 replay(applicationContext, request, response, rd); 181 req.doInclude("/my/path"); 182 verify(applicationContext, request, response, rd); 183 }); 184 } 185 186 /** 187 * Test method for {@link ServletRequest#doInclude(String)}. 188 */ 189 @Test 190 public void testDoIncludeNoDispatcher() { 191 assertThrowsExactly(IOException.class, () -> { 192 expect(request.getRequestDispatcher("/my/path")).andReturn(null); 193 194 replay(applicationContext, request, response); 195 try { 196 req.doInclude("/my/path"); 197 } finally { 198 verify(applicationContext, request, response); 199 } 200 }); 201 } 202 203 /** 204 * Test method for {@link ServletRequest#doInclude(String)}. 205 */ 206 @Test 207 public void testDoIncludeServletException() { 208 assertThrowsExactly(IOException.class, () -> { 209 RequestDispatcher rd = createMock(RequestDispatcher.class); 210 211 expect(request.getRequestDispatcher("/my/path")).andReturn(rd); 212 rd.include(request, response); 213 expectLastCall().andThrow(new ServletException()); 214 215 replay(applicationContext, request, response, rd); 216 try { 217 req.doInclude("/my/path"); 218 } finally { 219 verify(applicationContext, request, response, rd); 220 } 221 }); 222 } 223 224 /** 225 * Test method for {@link ServletRequest#getHeader()}. 226 */ 227 @Test 228 public void testGetHeader() { 229 assertInstanceOf(ReadOnlyEnumerationMap.class, req.getHeader()); 230 } 231 232 /** 233 * Test method for {@link ServletRequest#getHeader()}. 234 */ 235 @Test 236 public void testGetResponseHeaders() { 237 assertInstanceOf(HeaderExtractor.class, req.getResponseHeaders() ); 238 } 239 240 /** 241 * Test method for {@link ServletRequest#getHeaderValues()}. 242 */ 243 @Test 244 public void testGetHeaderValues() { 245 assertInstanceOf(HeaderValuesMap.class, req.getHeaderValues()); 246 } 247 248 /** 249 * Test method for {@link ServletRequest#getParam()}. 250 */ 251 @Test 252 public void testGetParam() { 253 assertInstanceOf(ReadOnlyEnumerationMap.class, req.getParam()); 254 } 255 256 /** 257 * Test method for {@link ServletRequest#getParamValues()}. 258 */ 259 @Test 260 public void testGetParamValues() { 261 Map<String, String[]> paramMap = createMock(Map.class); 262 263 expect(request.getParameterMap()).andReturn(paramMap); 264 265 replay(applicationContext, request, response, paramMap); 266 assertEquals(paramMap, req.getParamValues()); 267 verify(applicationContext, request, response, paramMap); 268 } 269 270 /** 271 * Test method for {@link ServletRequest#getRequestScope()}. 272 */ 273 @Test 274 public void testGetRequestScope() { 275 assertInstanceOf(ScopeMap.class, req.getRequestScope()); 276 } 277 278 /** 279 * Test method for {@link ServletRequest#getSessionScope()}. 280 */ 281 @Test 282 public void testGetSessionScope() { 283 assertInstanceOf(ScopeMap.class, req.getSessionScope()); 284 } 285 286 /** 287 * Test method for {@link ServletRequest#getOutputStream()}. 288 */ 289 @Test 290 public void testGetOutputStream() { 291 assertDoesNotThrow(() -> { 292 ServletOutputStream os = createMock(ServletOutputStream.class); 293 294 expect(response.getOutputStream()).andReturn(os); 295 296 replay(applicationContext, request, response, os); 297 assertEquals(req.getOutputStream(), os); 298 verify(applicationContext, request, response, os); 299 }); 300 } 301 302 /** 303 * Test method for {@link ServletRequest#getWriter()}. 304 */ 305 @Test 306 public void testGetWriter() { 307 assertDoesNotThrow(() -> { 308 PrintWriter os = createMock(PrintWriter.class); 309 310 expect(response.getWriter()).andReturn(os); 311 312 replay(applicationContext, request, response, os); 313 assertEquals(req.getWriter(), os); 314 verify(applicationContext, request, response, os); 315 }); 316 } 317 318 /** 319 * Test method for {@link ServletRequest#getPrintWriter()}. 320 */ 321 @Test 322 public void testGetPrintWriter() { 323 assertDoesNotThrow(() -> { 324 PrintWriter os = createMock(PrintWriter.class); 325 326 expect(response.getWriter()).andReturn(os); 327 328 replay(applicationContext, request, response, os); 329 assertEquals(req.getPrintWriter(), os); 330 verify(applicationContext, request, response, os); 331 }); 332 } 333 334 /** 335 * Test method for {@link ServletRequest#isResponseCommitted()}. 336 */ 337 @Test 338 public void testIsResponseCommitted() { 339 expect(response.isCommitted()).andReturn(true); 340 341 replay(applicationContext, request, response); 342 assertTrue(req.isResponseCommitted()); 343 verify(applicationContext, request, response); 344 } 345 346 /** 347 * Test method for {@link ServletRequest#setContentType(String)}. 348 */ 349 @Test 350 public void testSetContentType() { 351 response.setContentType("text/html"); 352 353 replay(applicationContext, request, response); 354 req.setContentType("text/html"); 355 verify(applicationContext, request, response); 356 } 357 358 /** 359 * Test method for {@link ServletRequest#getRequestLocale()}. 360 */ 361 @Test 362 public void testGetRequestLocale() { 363 Locale locale = Locale.ITALY; 364 365 expect(request.getLocale()).andReturn(locale); 366 367 replay(applicationContext, request, response); 368 assertEquals(locale, req.getRequestLocale()); 369 verify(applicationContext, request, response); 370 } 371 372 /** 373 * Test method for {@link ServletRequest#getRequest()}. 374 */ 375 @Test 376 public void testGetRequest() { 377 replay(applicationContext, request, response); 378 assertEquals(request, req.getRequest()); 379 verify(applicationContext, request, response); 380 } 381 382 /** 383 * Test method for {@link ServletRequest#getResponse()}. 384 */ 385 @Test 386 public void testGetResponse() { 387 replay(applicationContext, request, response); 388 assertEquals(response, req.getResponse()); 389 verify(applicationContext, request, response); 390 } 391 392 /** 393 * Test method for {@link ServletRequest#isUserInRole(String)}. 394 */ 395 @Test 396 public void testIsUserInRole() { 397 expect(request.isUserInRole("myrole")).andReturn(true); 398 399 replay(applicationContext, request, response); 400 assertTrue(req.isUserInRole("myrole")); 401 verify(applicationContext, request, response); 402 } 403}