Search in sources :

Example 26 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, boolean isRootCauseAvailable) {
    if (isRootCauseAvailable) {
        getElement().appendChild(ElementFactory.createHeading3(exceptionText));
    } else {
        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 27 with VaadinService

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

the class JavaScriptBootstrapUITest method navigate_firsClientSideRoutingThrows_navigationInProgressIsReset_secondClientSideRoutingWorks.

@Test
public void navigate_firsClientSideRoutingThrows_navigationInProgressIsReset_secondClientSideRoutingWorks() {
    VaadinSession session = Mockito.mock(VaadinSession.class);
    VaadinService service = Mockito.mock(VaadinService.class);
    Mockito.when(session.getService()).thenReturn(service);
    Router router = Mockito.mock(Router.class);
    Mockito.when(service.getRouter()).thenReturn(router);
    Mockito.doThrow(RuntimeException.class).when(router).resolveNavigationTarget(Mockito.any());
    JavaScriptBootstrapUI ui = new JavaScriptBootstrapUI();
    ui.getInternals().setSession(session);
    try {
        ui.navigate("foo", QueryParameters.empty());
    } catch (RuntimeException expected) {
        router = Mockito.mock(Router.class);
        Mockito.when(service.getRouter()).thenReturn(router);
        Mockito.when(router.resolveNavigationTarget(Mockito.any())).thenReturn(Optional.empty());
        ui.navigate("foo", QueryParameters.empty());
        Mockito.verify(router).resolveNavigationTarget(Mockito.any());
        return;
    }
    // self control: code inside catch should be invoked
    Assert.fail();
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) Router(com.vaadin.flow.router.Router) Test(org.junit.Test)

Example 28 with VaadinService

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

the class WebComponentBootstrapHandlerTest method mockRequest.

private VaadinRequest mockRequest(boolean hasConfig) {
    VaadinContext context = Mockito.mock(VaadinContext.class);
    VaadinService service = Mockito.mock(VaadinService.class);
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    Mockito.when(request.getService()).thenReturn(service);
    Mockito.when(service.getContext()).thenReturn(context);
    WebComponentConfigurationRegistry registry = Mockito.mock(WebComponentConfigurationRegistry.class);
    Mockito.when(context.getAttribute(Mockito.eq(WebComponentConfigurationRegistry.class), Mockito.any())).thenReturn(registry);
    Mockito.when(registry.hasConfigurations()).thenReturn(hasConfig);
    Mockito.when(request.getPathInfo()).thenReturn("/web-component/web-component-ui.js");
    return request;
}
Also used : VaadinContext(com.vaadin.flow.server.VaadinContext) WebComponentConfigurationRegistry(com.vaadin.flow.server.webcomponent.WebComponentConfigurationRegistry) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest)

Example 29 with VaadinService

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

the class UidlRequestHandlerTest method writeSessionExpired_whenUINotFound.

@Test
public void writeSessionExpired_whenUINotFound() throws IOException {
    VaadinService service = mock(VaadinService.class);
    VaadinSession session = mock(VaadinSession.class);
    when(session.getService()).thenReturn(service);
    when(service.findUI(request)).thenReturn(null);
    boolean result = handler.synchronizedHandleRequest(session, request, response);
    Assert.assertTrue("Result should be true", result);
    String responseContent = CommunicationUtil.getStringWhenWriteString(outputStream);
    // response shouldn't contain async
    Assert.assertEquals("Invalid response", "for(;;);[{\"meta\":{\"sessionExpired\":true}}]", responseContent);
}
Also used : VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) Test(org.junit.Test)

Example 30 with VaadinService

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

the class UidlRequestHandlerTest method writeSessionExpired.

@Test
public void writeSessionExpired() throws Exception {
    ApplicationConfiguration config = Mockito.mock(ApplicationConfiguration.class);
    Mockito.when(config.getPropertyNames()).thenReturn(Collections.emptyEnumeration());
    Mockito.when(config.getBuildFolder()).thenReturn(".");
    VaadinContext context = new MockVaadinContext();
    Mockito.when(config.getContext()).thenReturn(context);
    VaadinService service = new VaadinServletService(null, new DefaultDeploymentConfiguration(config, getClass(), new Properties()));
    when(request.getService()).thenReturn(service);
    when(request.getParameter(ApplicationConstants.REQUEST_TYPE_PARAMETER)).thenReturn(RequestType.UIDL.getIdentifier());
    boolean result = handler.handleSessionExpired(request, response);
    Assert.assertTrue("Result should be true", result);
    String responseContent = CommunicationUtil.getStringWhenWriteBytesOffsetLength(outputStream);
    // response shouldn't contain async
    Assert.assertEquals("Invalid response", "for(;;);[{\"meta\":{\"sessionExpired\":true}}]", responseContent);
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) VaadinContext(com.vaadin.flow.server.VaadinContext) VaadinService(com.vaadin.flow.server.VaadinService) VaadinServletService(com.vaadin.flow.server.VaadinServletService) DefaultDeploymentConfiguration(com.vaadin.flow.server.DefaultDeploymentConfiguration) Properties(java.util.Properties) ApplicationConfiguration(com.vaadin.flow.server.startup.ApplicationConfiguration) Test(org.junit.Test)

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