ServletContainerImpl implements ServletContainer { @Override public Servlet getServletForClass(final Class<? extends HttpServlet> servletClass, final ServletConfig servletConfig) throws ServletInitializationException, ServletException { if (servlets.containsKey(servletClass)) { servletStats.get(servletClass).setLastRequestedAt(dateProvider.now()); return servlets.get(servletClass); } return initializeServlet(servletClass, servletConfig); } ServletContainerImpl(final DateProvider dateProvider,
                                final long servletTimeToLiveInMs,
                                final long monitoringIntervalInMs); @Override Servlet getServletForClass(final Class<? extends HttpServlet> servletClass, final ServletConfig servletConfig); @Override Filter getFilterForClass(final Class<? extends Filter> filterClass, final FilterConfig filterConfig); @Override void shutdown(); Map<Class<? extends HttpServlet>, ServletStats> getServletStats();  }
@Test public void shouldInitializeServlet() throws Exception { SampleServlet servlet = (SampleServlet) servletContainer.getServletForClass(SampleServlet.class, servletConfig); assertThat(servlet, is(not(nullValue()))); assertThat(servlet.getInitializedCounter(), is(equalTo(1))); assertThat(servlet.getServletConfig(), is(equalTo(servletConfig))); }
@Test(expected = ServletInitializationException.class) public void shouldThrowExceptionWhenInitializingInvalidServlet() throws Exception { servletContainer.getServletForClass(InvalidServletWithPrivateConstructor.class, servletConfig); }
@Test public void shouldDestroyOutdatedServlet() throws Exception { servletContainer = new ServletContainerImpl(new DateProvider(), 50, 50); SampleServlet servlet = (SampleServlet) servletContainer.getServletForClass(SampleServlet.class, servletConfig); assertThat(servlet, is(not(nullValue()))); Thread.sleep(200); SampleServlet servlet2 = (SampleServlet) servletContainer.getServletForClass(SampleServlet.class, servletConfig); assertThat(servlet, is(not(nullValue()))); assertThat(servlet, is(not(sameInstance(servlet2)))); }