Search in sources :

Example 1 with StaticFileHandlerFactory

use of com.vaadin.flow.server.StaticFileHandlerFactory in project flow by vaadin.

the class SpringInstantiatorTest method getService.

public static VaadinService getService(ApplicationContext context, Properties configProperties, boolean rootMapping) throws ServletException {
    SpringServlet servlet = new SpringServlet(context, rootMapping) {

        @Override
        protected DeploymentConfiguration createDeploymentConfiguration(Properties initParameters) {
            if (configProperties != null) {
                configProperties.putAll(initParameters);
                return super.createDeploymentConfiguration(configProperties);
            }
            return super.createDeploymentConfiguration(initParameters);
        }
    };
    ServletConfig config = Mockito.mock(ServletConfig.class);
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getClassLoader()).thenReturn(servlet.getClass().getClassLoader());
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(appConfig.getContext()).thenReturn(new VaadinServletContext(servletContext));
    Mockito.when(servletContext.getAttribute(ApplicationConfiguration.class.getName())).thenReturn(appConfig);
    Lookup lookup = Mockito.mock(Lookup.class);
    ResourceProvider provider = Mockito.mock(ResourceProvider.class);
    Mockito.when(lookup.lookup(ResourceProvider.class)).thenReturn(provider);
    StaticFileHandlerFactory staticFileHandlerFactory = vaadinService -> new StaticFileServer((VaadinServletService) vaadinService);
    Mockito.when(lookup.lookup(StaticFileHandlerFactory.class)).thenReturn(staticFileHandlerFactory);
    Mockito.when(servletContext.getAttribute(Lookup.class.getName())).thenReturn(lookup);
    Mockito.when(config.getServletContext()).thenReturn(servletContext);
    Mockito.when(config.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(servletContext.getInitParameterNames()).thenReturn(Collections.emptyEnumeration());
    servlet.init(config);
    return servlet.getService();
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) BeanInstantiationException(org.springframework.beans.BeanInstantiationException) ServletException(javax.servlet.ServletException) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) Div(com.vaadin.flow.component.html.Div) StaticFileServer(com.vaadin.flow.server.StaticFileServer) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Scope(org.springframework.context.annotation.Scope) Locale(java.util.Locale) Lookup(com.vaadin.flow.di.Lookup) SpringServlet(com.vaadin.flow.spring.SpringServlet) SpringRunner(org.springframework.test.context.junit4.SpringRunner) I18NProvider(com.vaadin.flow.i18n.I18NProvider) ServiceInitEvent(com.vaadin.flow.server.ServiceInitEvent) Instantiator(com.vaadin.flow.di.Instantiator) ServletConfig(javax.servlet.ServletConfig) Properties(java.util.Properties) SpringInstantiator(com.vaadin.flow.spring.SpringInstantiator) Set(java.util.Set) Import(org.springframework.context.annotation.Import) Test(org.junit.Test) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) ComponentScan(org.springframework.context.annotation.ComponentScan) VaadinServiceInitListener(com.vaadin.flow.server.VaadinServiceInitListener) Configuration(org.springframework.context.annotation.Configuration) Mockito(org.mockito.Mockito) List(java.util.List) Component(org.springframework.stereotype.Component) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) VaadinService(com.vaadin.flow.server.VaadinService) ServletContext(javax.servlet.ServletContext) Assert(org.junit.Assert) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) StaticFileServer(com.vaadin.flow.server.StaticFileServer) ResourceProvider(com.vaadin.flow.di.ResourceProvider) ServletConfig(javax.servlet.ServletConfig) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) SpringServlet(com.vaadin.flow.spring.SpringServlet)

Example 2 with StaticFileHandlerFactory

use of com.vaadin.flow.server.StaticFileHandlerFactory in project flow by vaadin.

the class LookupInitializerTest method initialize_noStaticFileHandlerFactory_defaultStaticFileHandlerFactoryCreated.

@Test
public void initialize_noStaticFileHandlerFactory_defaultStaticFileHandlerFactoryCreated() throws ServletException {
    AtomicReference<Lookup> capture = new AtomicReference<>();
    initializer.initialize(null, new HashMap<>(), capture::set);
    Lookup lookup = capture.get();
    StaticFileHandlerFactory factory = lookup.lookup(StaticFileHandlerFactory.class);
    VaadinService service = Mockito.mock(VaadinService.class);
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(configuration);
    ClassLoader loader = Mockito.mock(ClassLoader.class);
    Mockito.when(service.getClassLoader()).thenReturn(loader);
    StaticFileHandler handler = factory.createHandler(service);
    Assert.assertNotNull(handler);
    Assert.assertEquals(StaticFileServer.class, handler.getClass());
}
Also used : StaticFileHandler(com.vaadin.flow.server.StaticFileHandler) StaticFileHandlerFactory(com.vaadin.flow.server.StaticFileHandlerFactory) VaadinService(com.vaadin.flow.server.VaadinService) AtomicReference(java.util.concurrent.atomic.AtomicReference) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Test(org.junit.Test)

Aggregations

DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)2 StaticFileHandlerFactory (com.vaadin.flow.server.StaticFileHandlerFactory)2 VaadinService (com.vaadin.flow.server.VaadinService)2 Test (org.junit.Test)2 Div (com.vaadin.flow.component.html.Div)1 Instantiator (com.vaadin.flow.di.Instantiator)1 Lookup (com.vaadin.flow.di.Lookup)1 ResourceProvider (com.vaadin.flow.di.ResourceProvider)1 I18NProvider (com.vaadin.flow.i18n.I18NProvider)1 ServiceInitEvent (com.vaadin.flow.server.ServiceInitEvent)1 StaticFileHandler (com.vaadin.flow.server.StaticFileHandler)1 StaticFileServer (com.vaadin.flow.server.StaticFileServer)1 VaadinServiceInitListener (com.vaadin.flow.server.VaadinServiceInitListener)1 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)1 VaadinServletService (com.vaadin.flow.server.VaadinServletService)1 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)1 SpringInstantiator (com.vaadin.flow.spring.SpringInstantiator)1 SpringServlet (com.vaadin.flow.spring.SpringServlet)1 Collections (java.util.Collections)1 List (java.util.List)1