use of com.vaadin.flow.router.RouterInterface 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;
}
use of com.vaadin.flow.router.RouterInterface in project flow by vaadin.
the class UidlWriterTest method initializeUIForDependenciesTest.
private UI initializeUIForDependenciesTest(UI ui) {
ServletContext context = Mockito.mock(ServletContext.class);
VaadinServletService service = new VaadinServletService(new VaadinServlet() {
@Override
public ServletContext getServletContext() {
return context;
}
}, new MockDeploymentConfiguration()) {
RouterInterface router = new com.vaadin.flow.router.legacy.Router();
@Override
public RouterInterface getRouter() {
return router;
}
@Override
public Iterable<DependencyFilter> getDependencyFilters() {
return Collections.emptyList();
}
};
service.getRouter().reconfigure(conf -> {
conf.setRoute("", BaseClass.class);
conf.setParentView(BaseClass.class, ParentClass.class);
conf.setParentView(ParentClass.class, SuperParentClass.class);
});
MockVaadinSession session = new MockVaadinSession(service);
session.lock();
ui.getInternals().setSession(session);
when(service.getResourceAsStream(anyString())).thenAnswer(invocation -> new ByteArrayInputStream(((String) invocation.getArguments()[0]).getBytes()));
HttpServletRequest servletRequestMock = mock(HttpServletRequest.class);
VaadinServletRequest vaadinRequestMock = mock(VaadinServletRequest.class);
when(vaadinRequestMock.getHttpServletRequest()).thenReturn(servletRequestMock);
service.setCurrentInstances(vaadinRequestMock, mock(VaadinResponse.class));
ui.doInit(vaadinRequestMock, 1);
factory = mock(VaadinUriResolverFactory.class);
VaadinUriResolver vaadinUriResolver = Mockito.mock(VaadinUriResolver.class);
Mockito.when(factory.getUriResolver(any())).thenReturn(vaadinUriResolver);
Mockito.when(vaadinUriResolver.resolveVaadinUri(anyString())).thenAnswer(invocation -> invocation.getArguments()[0]);
doAnswer(invocation -> invocation.getArguments()[1]).when(factory).toServletContextPath(any(), anyString());
session.setAttribute(VaadinUriResolverFactory.class, factory);
VaadinSession.setCurrent(session);
return ui;
}
use of com.vaadin.flow.router.RouterInterface in project flow by vaadin.
the class InternalRedirectHandler method handle.
@Override
public int handle(NavigationEvent event) {
UI ui = event.getUI();
RouterInterface router = event.getSource();
ui.getPage().getHistory().replaceState(null, target);
return router.navigate(ui, target, event.getTrigger());
}
use of com.vaadin.flow.router.RouterInterface 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);
}
}
Aggregations