/** * Helper method for determining (and then caching) the {@link ServletRuntime} implementation appropriate for * the current Servlet runtime environment. If the current Servlet runtime environment supports the Servlet 3 API * (i.e. async requests) then a Servlet-3-async-request-capable implementation will be returned, otherwise a * Servlet-2-blocking-requests-only implementation will be returned. The first time this method is called the * result will be cached, and the cached value returned for subsequent calls. * * @param request The concrete {@link ServletRequest} implementation use to determine the Servlet runtime * environment. * * @return The {@link ServletRuntime} implementation appropriate for the current Servlet runtime environment. */ protected ServletRuntime getServletRuntime(ServletRequest request) { // It's ok that this isn't synchronized - this logic is idempotent and if it's executed a few extra times // due to concurrent requests when the service first starts up it won't hurt anything. if (servletRuntime == null) { servletRuntime = ServletRuntime.determineServletRuntime(request.getClass(), ASYNC_LISTENER_CLASSNAME); } return servletRuntime; }
@Test public void getServletRuntime_returns_value_of_ServletRuntime_determineServletRuntime_method_and_caches_result() { // given Class<? extends ServletRuntime> expectedServletRuntimeClass = ServletRuntime.determineServletRuntime(requestMock.getClass(), ASYNC_LISTENER_CLASSNAME).getClass(); RequestTracingFilter filter = getBasicFilter(); assertThat(filter.servletRuntime).isNull(); // when ServletRuntime result = filter.getServletRuntime(requestMock); // then assertThat(result.getClass()).isEqualTo(expectedServletRuntimeClass); assertThat(filter.servletRuntime).isSameAs(result); }
@DataProvider(value = { "true | true | true", "true | false | false", "false | true | false", "false | false | false" }, splitBy = "\\|") @Test public void determineServletRuntime_returns_ServletRuntime_based_on_arguments( boolean classHasServlet3Method, boolean useAsyncListenerClassThatExists, boolean expectServlet3Runtime ) { // given Class<?> servletRequestClass = (classHasServlet3Method) ? GoodFakeServletRequest.class : BadFakeServletRequest.class; String asyncListenerClassname = (useAsyncListenerClassThatExists) ? AsyncListener.class.getName() : "does.not.exist.AsyncListener" + UUID.randomUUID().toString(); // when ServletRuntime result = ServletRuntime.determineServletRuntime(servletRequestClass, asyncListenerClassname); // then if (expectServlet3Runtime) { assertThat(result).isInstanceOf(Servlet3Runtime.class); } else { assertThat(result).isInstanceOf(Servlet2Runtime.class); } }