Search in sources :

Example 1 with VaadinService

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

the class VaadinUIScopeTest method mockUI.

private UI mockUI() {
    VaadinSession session = mockSession();
    RouterInterface routerIface = mock(RouterInterface.class);
    VaadinService service = session.getService();
    when(service.getRouter()).thenReturn(routerIface);
    ImmutableRouterConfiguration config = mock(ImmutableRouterConfiguration.class);
    when(routerIface.getConfiguration()).thenReturn(config);
    when(config.isConfigured()).thenReturn(false);
    UI ui = new UI();
    ui.getInternals().setSession(session);
    ui.doInit(null, 1);
    UI.setCurrent(ui);
    // prevent UI from being GCed.
    this.ui = ui;
    return ui;
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) SpringVaadinSession(com.vaadin.flow.spring.SpringVaadinSession) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) RouterInterface(com.vaadin.flow.router.RouterInterface)

Example 2 with VaadinService

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

the class VaadinServiceTest method createService.

private static VaadinService createService() {
    ServletConfig servletConfig = new MockServletConfig();
    VaadinServlet servlet = new VaadinServlet();
    try {
        servlet.init(servletConfig);
    } catch (ServletException e) {
        throw new RuntimeException(e);
    }
    VaadinService service = servlet.getService();
    return service;
}
Also used : ServletException(javax.servlet.ServletException) ServletConfig(javax.servlet.ServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) VaadinService(com.vaadin.flow.server.VaadinService)

Example 3 with VaadinService

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

the class InternalServerError method reportException.

private void reportException(Exception exception, String path, String exceptionText) {
    getElement().appendChild(Element.createText(exceptionText));
    VaadinService vaadinService = VaadinService.getCurrent();
    // Check that we have a vaadinService as else we will fail on a NPE and
    // the stacktrace we actually got will disappear and getting a NPE is
    // confusing.
    boolean productionMode = vaadinService != null && vaadinService.getDeploymentConfiguration().isProductionMode();
    if (!productionMode) {
        checkLogBinding();
        printStacktrace(exception);
    }
    getLogger().error("There was an exception while trying to navigate to '{}'", path, exception);
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService)

Example 4 with VaadinService

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

the class UIInternals method setSession.

/**
 * Sets the session to which the related UI is assigned.
 * <p>
 * This method is for internal use by the framework. To explicitly close a
 * UI, see {@link UI#close()}.
 *
 * @param session
 *            the session to set
 *
 * @throws IllegalStateException
 *             if the session has already been set
 *
 * @see #getSession()
 */
public void setSession(VaadinSession session) {
    if (session == null && this.session == null) {
        throw new IllegalStateException("Session should never be set to null when UI.session is already null");
    } else if (session != null && this.session != null) {
        throw new IllegalStateException("Session has already been set. Old session: " + getSessionDetails(this.session) + ". New session: " + getSessionDetails(session) + ".");
    } else {
        if (session == null) {
            try {
                ComponentUtil.onComponentDetach(ui);
            } catch (Exception e) {
                getLogger().warn("Error while detaching UI from session", e);
            }
            // Disable push when the UI is detached. Otherwise the
            // push connection and possibly VaadinSession will live on.
            ui.getPushConfiguration().setPushMode(PushMode.DISABLED);
            setPushConnection(null);
        }
        this.session = session;
    }
    if (session != null) {
        VaadinService service = getSession().getService();
        if (service != null) {
            // Allow null service to simplify testing mocks
            RouterInterface serviceRouter = service.getRouter();
            if (serviceRouter != null && serviceRouter.getConfiguration().isConfigured()) {
                router = serviceRouter;
            }
        }
        ComponentUtil.onComponentAttach(ui, true);
    }
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) RouterInterface(com.vaadin.flow.router.RouterInterface)

Example 5 with VaadinService

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

the class PolymerTemplateTest method createService.

@Override
public VaadinService createService() {
    configuration = Mockito.mock(DeploymentConfiguration.class);
    Mockito.when(configuration.isProductionMode()).thenReturn(false);
    VaadinService service = Mockito.mock(VaadinService.class);
    Mockito.when(service.getDeploymentConfiguration()).thenReturn(configuration);
    Instantiator instantiator = Mockito.mock(Instantiator.class);
    TemplateParser.TemplateParserFactory factory = Mockito.mock(TemplateParser.TemplateParserFactory.class);
    Mockito.when(instantiator.getOrCreate(TemplateParserFactory.class)).thenReturn(factory);
    Mockito.when(factory.createParser()).thenReturn(NpmTemplateParser.getInstance());
    Mockito.when(service.getInstantiator()).thenReturn(instantiator);
    return service;
}
Also used : TemplateParserFactory(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateParserFactory) VaadinService(com.vaadin.flow.server.VaadinService) Instantiator(com.vaadin.flow.di.Instantiator) DefaultInstantiator(com.vaadin.flow.di.DefaultInstantiator) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

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