Search in sources :

Example 16 with VaadinService

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

the class AbstractSinglePropertyFieldTest method getValue_wrapExistingElement_elementHasProperty_valueIsThePropertyValue.

@Test
public void getValue_wrapExistingElement_elementHasProperty_valueIsThePropertyValue() {
    Element element = new Element("tag");
    element.setProperty("property", "foo");
    UI ui = new UI();
    UI.setCurrent(ui);
    VaadinSession session = Mockito.mock(VaadinSession.class);
    ui.getInternals().setSession(session);
    VaadinService service = Mockito.mock(VaadinService.class);
    Mockito.when(session.getService()).thenReturn(service);
    Instantiator instantiator = Mockito.mock(Instantiator.class);
    Mockito.when(service.getInstantiator()).thenReturn(instantiator);
    Mockito.when(instantiator.createComponent(StringField.class)).thenAnswer(a -> new StringField());
    StringField field = Component.from(element, StringField.class);
    Assert.assertEquals("foo", field.getValue());
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) Element(com.vaadin.flow.dom.Element) VaadinService(com.vaadin.flow.server.VaadinService) Instantiator(com.vaadin.flow.di.Instantiator) Test(org.junit.Test)

Example 17 with VaadinService

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

the class CompositeTest method setup.

@Before
public void setup() {
    compositeWithComponent = new CompositeWithComponent() {

        @Override
        public String toString() {
            return "Composite";
        }
    };
    layoutWithSingleComponentComposite = new TestLayout() {

        @Override
        public String toString() {
            return "Layout";
        }
    };
    layoutWithSingleComponentComposite.addComponent(compositeWithComponent);
    ((TracksAttachDetach) componentInsideLayoutInsideComposite).track();
    compositeWithComponent.track();
    layoutInsideComposite.track();
    layoutWithSingleComponentComposite.track();
    Assert.assertNull(VaadinService.getCurrent());
    VaadinService service = Mockito.mock(VaadinService.class);
    DeploymentConfiguration configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(configuration.isProductionMode()).thenReturn(true);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(configuration);
    VaadinService.setCurrent(service);
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) TestLayout(com.vaadin.flow.component.CompositeNestedTest.TestLayout) TracksAttachDetach(com.vaadin.flow.component.ComponentTest.TracksAttachDetach) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration) Before(org.junit.Before)

Example 18 with VaadinService

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

the class FeatureFlagsTest method mockResourcesLocation.

private void mockResourcesLocation() {
    VaadinService service = Mockito.mock(VaadinService.class);
    VaadinService.setCurrent(service);
    VaadinContext vaadinContext = Mockito.mock(VaadinContext.class);
    Mockito.when(service.getContext()).thenReturn(vaadinContext);
    ApplicationConfiguration applicationConfiguration = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(vaadinContext.getAttribute(Mockito.eq(ApplicationConfiguration.class), Mockito.any())).thenReturn(applicationConfiguration);
    Mockito.when(applicationConfiguration.getJavaResourceFolder()).thenReturn(propertiesDir);
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinService(com.vaadin.flow.server.VaadinService) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration)

Example 19 with VaadinService

use of com.vaadin.flow.server.VaadinService 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 20 with VaadinService

use of com.vaadin.flow.server.VaadinService 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)

Aggregations

VaadinService (com.vaadin.flow.server.VaadinService)86 Test (org.junit.Test)39 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)19 VaadinSession (com.vaadin.flow.server.VaadinSession)18 Properties (java.util.Properties)15 VaadinContext (com.vaadin.flow.server.VaadinContext)12 Before (org.junit.Before)11 SpringInstantiatorTest (com.vaadin.flow.spring.instantiator.SpringInstantiatorTest)10 UI (com.vaadin.flow.component.UI)9 ApplicationConfiguration (com.vaadin.flow.server.startup.ApplicationConfiguration)8 Lookup (com.vaadin.flow.di.Lookup)7 VaadinResponse (com.vaadin.flow.server.VaadinResponse)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 ServletContext (javax.servlet.ServletContext)6 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)5 DefaultInstantiator (com.vaadin.flow.di.DefaultInstantiator)4 Instantiator (com.vaadin.flow.di.Instantiator)4 BrowserLiveReload (com.vaadin.flow.internal.BrowserLiveReload)4 DefaultDeploymentConfiguration (com.vaadin.flow.server.DefaultDeploymentConfiguration)4 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4