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));
}
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);
}
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);
}
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;
}
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());
}
Aggregations