Search in sources :

Example 16 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration 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 17 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class AbstractUIScopedTest method mockUI.

protected UI mockUI() {
    VaadinSession session = mockSession();
    Router router = mock(Router.class);
    VaadinService service = session.getService();
    when(service.getRouter()).thenReturn(router);
    Properties initParameters = new Properties();
    ApplicationConfiguration appConfig = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    when(service.getMainDivId(Mockito.any(), Mockito.any())).thenReturn(" - ");
    final Map<String, Object> attributeMap = new HashMap<>();
    ServletContext servletContext = Mockito.mock(ServletContext.class);
    Mockito.when(servletContext.getAttribute(Mockito.anyString())).then(invocationOnMock -> attributeMap.get(invocationOnMock.getArguments()[0].toString()));
    Mockito.doAnswer(invocationOnMock -> attributeMap.put(invocationOnMock.getArguments()[0].toString(), invocationOnMock.getArguments()[1])).when(servletContext).setAttribute(Mockito.anyString(), Mockito.any());
    VaadinServletContext context = new VaadinServletContext(servletContext);
    Mockito.when(service.getContext()).thenReturn(context);
    Lookup lookup = Mockito.mock(Lookup.class);
    Mockito.when(context.getAttribute(Lookup.class)).thenReturn(lookup);
    Mockito.when(appConfig.getContext()).thenReturn(context);
    DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration(appConfig, getClass(), initParameters);
    when(service.getDeploymentConfiguration()).thenReturn(config);
    ui = new UI();
    ui.getInternals().setSession(session);
    ui.doInit(null, 1);
    UI.setCurrent(ui);
    return ui;
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) VaadinSession(com.vaadin.flow.server.VaadinSession) HashMap(java.util.HashMap) Router(com.vaadin.flow.router.Router) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) ServletContext(javax.servlet.ServletContext) Lookup(com.vaadin.flow.di.Lookup) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration)

Example 18 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class SpringApplicationConfigurationFactoryTest method doCreate_vaadinApplicationConfigurationHasSpringPropertiesPrefixedByVaadin.

@Test
public void doCreate_vaadinApplicationConfigurationHasSpringPropertiesPrefixedByVaadin() {
    String prefix = "foo_";
    SpringServlet.PROPERTY_NAMES.stream().forEach(name -> Mockito.when(env.getProperty("vaadin." + name)).thenReturn(prefix + name));
    Map<String, String> props = new HashMap<>();
    ApplicationConfiguration config = factory.doCreate(context, null, props);
    for (String prop : SpringServlet.PROPERTY_NAMES) {
        Assert.assertEquals("'" + prop + "' property is not available via " + ApplicationConfiguration.class, prefix + prop, config.getStringProperty(prop, null));
        Assert.assertEquals("'" + prop + "' property is not set in the properties map passed to the " + ApplicationConfiguration.class.getSimpleName() + " CTOR", prefix + prop, props.get(prop));
    }
    Assert.assertEquals(SpringServlet.PROPERTY_NAMES.size(), props.size());
}
Also used : HashMap(java.util.HashMap) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

Example 19 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class DefaultDeploymentConfigurationTest method useV14Bootstrap_v14ModeIsSetViaParentOnly_v14ModeIsTakenFromParent.

@Test
public void useV14Bootstrap_v14ModeIsSetViaParentOnly_v14ModeIsTakenFromParent() {
    ApplicationConfiguration appConfig = setupAppConfig();
    Mockito.when(appConfig.useV14Bootstrap()).thenReturn(true);
    // Note: application configuration doesn't contain production mode
    // parameter !
    Assert.assertNull(appConfig.getStringProperty(InitParameters.SERVLET_PARAMETER_USE_V14_BOOTSTRAP, null));
    DefaultDeploymentConfiguration config = createDeploymentConfig(appConfig, new Properties());
    Assert.assertTrue(config.useV14Bootstrap());
    Assert.assertTrue(config.getProperties().isEmpty());
}
Also used : Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

Example 20 with ApplicationConfiguration

use of com.vaadin.flow.server.startup.ApplicationConfiguration in project flow by vaadin.

the class DefaultDeploymentConfigurationTest method testGetSystemProperty.

@Test
public void testGetSystemProperty() {
    String value = "value";
    String prop = "prop";
    System.setProperty(DefaultDeploymentConfigurationTest.class.getPackage().getName() + '.' + prop, value);
    Properties initParameters = new Properties();
    ApplicationConfiguration appConfig = setupAppConfig();
    Mockito.when(appConfig.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration(appConfig, DefaultDeploymentConfigurationTest.class, initParameters);
    assertEquals(value, config.getSystemProperty(prop));
}
Also used : Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

Aggregations

ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)61 Properties (java.util.Properties)39 Test (org.junit.Test)39 VaadinContext (com.vaadin.flow.server.VaadinContext)14 Lookup (com.vaadin.flow.di.Lookup)9 VaadinService (com.vaadin.flow.server.VaadinService)7 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)6 ServletContext (javax.servlet.ServletContext)5 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)4 VaadinServletService (com.vaadin.flow.server.VaadinServletService)4 VaadinSession (com.vaadin.flow.server.VaadinSession)4 File (java.io.File)4 Before (org.junit.Before)4 UI (com.vaadin.flow.component.UI)3 ResourceProvider (com.vaadin.flow.di.ResourceProvider)3 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)3 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2