ServletDeployer implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); Collection<DeploymentConfiguration> servletConfigurations = getServletConfigurations( context); boolean enableServlets = true; boolean productionMode = false; for (DeploymentConfiguration configuration : servletConfigurations) { enableServlets = enableServlets && !configuration.disableAutomaticServletRegistration(); productionMode = productionMode || configuration.isProductionMode(); } VaadinServletCreation servletCreation = enableServlets ? createAppServlet(context) : null; logServletCreation(servletCreation, context, productionMode); }  @Override void contextInitialized(ServletContextEvent sce); static void logAppStartupToConsole(ServletContext servletContext,
            boolean servletAutomaticallyCreated); @Override void contextDestroyed(ServletContextEvent sce);  }
@Test public void hasRoutes_automaticallyRegisterTwoServletsWhenNoServletsPresent() throws Exception { deployer.contextInitialized(getContextEvent(true, false)); assertMappingsCount(1, 1); assertMappingIsRegistered(ServletDeployer.class.getName(), "/*"); }
@Test public void hasWebComponents_automaticallyRegisterOneServletsWhenNoServletsPresent() throws Exception { deployer.contextInitialized(getContextEvent(false, true)); assertMappingsCount(1, 1); assertMappingIsRegistered(ServletDeployer.class.getName(), "/*"); }
@Test public void doNotRegisterAnythingIfRegistrationIsDisabled() throws Exception { deployer.contextInitialized(getContextEvent(true, true, getServletRegistration("testServlet", TestServlet.class, singletonList("/test/*"), singletonMap( InitParameters.DISABLE_AUTOMATIC_SERVLET_REGISTRATION, "true")))); assertMappingsCount(0, 0); }
@Test public void noServlets_noRoutes_noWebComponents_servletsAreNotRegistered() throws Exception { deployer.contextInitialized(getContextEvent(false, false)); assertMappingsCount(0, 0); }
@Test public void servletIsNotRegisteredWhenAnotherHasTheSamePathMapping_mainServlet() throws Exception { dynamicMockCheck = registration -> EasyMock .expect(registration.setInitParameters(Collections.emptyMap())) .andReturn(null).once(); deployer.contextInitialized(getContextEvent(true, true, getServletRegistration("test", TestServlet.class, singletonList("/*"), Collections.emptyMap()))); assertMappingsCount(0, 0); }
@Test public void servletIsNotRegisteredWhenAnotherHasTheSamePathMapping_frontendServlet() throws Exception { deployer.contextInitialized(getContextEvent(true, true, getServletRegistration("test", TestServlet.class, singletonList("/frontend/*"), Collections.emptyMap()))); assertMappingsCount(1, 1); assertMappingIsRegistered(ServletDeployer.class.getName(), "/*"); }