Search in sources :

Example 46 with VaadinService

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

the class VaadinServiceTest method currentInstancesAfterPendingAccessTasks.

@Test
public void currentInstancesAfterPendingAccessTasks() {
    VaadinService service = createService();
    MockVaadinSession session = new MockVaadinSession(service);
    session.lock();
    service.accessSession(session, () -> {
        CurrentInstance.set(String.class, "Set in task");
    });
    CurrentInstance.set(String.class, "Original value");
    service.runPendingAccessTasks(session);
    Assert.assertEquals("Original CurrentInstance should be set after the task has been run", "Original value", CurrentInstance.get(String.class));
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 47 with VaadinService

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

the class VaadinServiceTest method testFireSessionDestroy.

@Test
public void testFireSessionDestroy() throws ServletException {
    VaadinService service = createService();
    TestSessionDestroyListener listener = new TestSessionDestroyListener();
    service.addSessionDestroyListener(listener);
    MockVaadinSession vaadinSession = new MockVaadinSession(service);
    service.fireSessionDestroy(vaadinSession);
    Assert.assertEquals("'fireSessionDestroy' method doesn't call 'close' for the session", 1, vaadinSession.getCloseCount());
    vaadinSession.valueUnbound(EasyMock.createMock(HttpSessionBindingEvent.class));
    Assert.assertEquals("'fireSessionDestroy' method may not call 'close' " + "method for closing session", 1, vaadinSession.getCloseCount());
    Assert.assertEquals("SessionDestroyListeners not called exactly once", 1, listener.callCount);
}
Also used : HttpSessionBindingEvent(javax.servlet.http.HttpSessionBindingEvent) VaadinService(com.vaadin.flow.server.VaadinService) Test(org.junit.Test)

Example 48 with VaadinService

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

the class RouterLinkTest method testFailForWrongImplementation.

@Test
public void testFailForWrongImplementation() throws InvalidRouteConfigurationException {
    registry.setNavigationTargets(Stream.of(FaultySetup.class).collect(Collectors.toSet()));
    com.vaadin.flow.router.Router router = new com.vaadin.flow.router.Router(registry);
    VaadinService service = Mockito.mock(VaadinService.class);
    Mockito.when(service.getRouter()).thenReturn(router);
    CurrentInstance.set(VaadinService.class, service);
    expectedEx.expect(RuntimeException.class);
    expectedEx.expectMessage("Only navigation targets for old Router should implement 'View'. Remove 'implements View' from '" + FaultySetup.class.getName() + "'");
    RouterLink faulty = new RouterLink("Faulty", FaultySetup.class);
}
Also used : VaadinService(com.vaadin.flow.server.VaadinService) Router(com.vaadin.flow.router.legacy.Router) Test(org.junit.Test)

Example 49 with VaadinService

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

the class RouterLinkTest method createUI.

private RouterTestUI createUI() {
    RouterTestUI ui = new RouterTestUI();
    VaadinService service = VaadinService.getCurrent();
    Mockito.when(service.getRouter()).thenReturn(ui.getRouterInterface().get());
    return ui;
}
Also used : RouterTestUI(com.vaadin.flow.router.legacy.RouterTest.RouterTestUI) VaadinService(com.vaadin.flow.server.VaadinService)

Example 50 with VaadinService

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

the class RouterTest method testResolveError.

@Test
public void testResolveError() throws ServletException {
    UI ui = new RouterTestUI();
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    ServletConfig servletConfig = new MockServletConfig();
    VaadinServlet servlet = new VaadinServlet();
    servlet.init(servletConfig);
    VaadinService service = servlet.getService();
    service.setCurrentInstances(request, response);
    Router router = new Router();
    router.reconfigure(c -> c.setResolver(event -> Optional.empty()));
    router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertTrue(ui.getElement().getTextRecursively().contains("404"));
    // 404 code should be sent ONLY on initial request
    Mockito.verifyZeroInteractions(response);
    // to verify that the setup has been correct and the mocks work,
    // test the case where 404 should be sent
    router.initializeUI(ui, request);
    ArgumentCaptor<Integer> statusCodeCaptor = ArgumentCaptor.forClass(Integer.class);
    Mockito.verify(response).setStatus(statusCodeCaptor.capture());
    Assert.assertEquals(Integer.valueOf(HttpServletResponse.SC_NOT_FOUND), statusCodeCaptor.getValue());
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) ErrorView(com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentCaptor(org.mockito.ArgumentCaptor) After(org.junit.After) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) ServletConfig(javax.servlet.ServletConfig) NavigationHandler(com.vaadin.flow.router.NavigationHandler) VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) RouterInterface(com.vaadin.flow.router.RouterInterface) Resolver(com.vaadin.flow.router.legacy.Resolver) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) NotThreadSafe(net.jcip.annotations.NotThreadSafe) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Assert(org.junit.Assert) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Router(com.vaadin.flow.router.legacy.Router) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) MockServletConfig(com.vaadin.flow.server.MockServletConfig) Router(com.vaadin.flow.router.legacy.Router) VaadinResponse(com.vaadin.flow.server.VaadinResponse) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) 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