use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.
the class VaadinServletContextInitializer method createCompositeListener.
private CompositeServletContextListener createCompositeListener(VaadinServletContext context) {
CompositeServletContextListener compositeListener = new CompositeServletContextListener();
compositeListener.addListener(new LookupInitializerListener());
compositeListener.addListener(new VaadinAppShellContextListener());
ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(context);
// has done its job already, skip the custom routes search
if (registry.getRegisteredRoutes().isEmpty()) {
/*
* Don't rely on RouteRegistry.isInitialized() negative return value
* here because it's not known whether RouteRegistryInitializer has
* been executed already or not (the order is undefined). Postpone
* this to the end of context initialization cycle. At this point
* RouteRegistry is either initialized or it's not initialized
* because an RouteRegistryInitializer has not been executed (end
* never will).
*/
compositeListener.addListener(new RouteServletContextListener());
}
compositeListener.addListener(new ErrorParameterServletContextListener());
compositeListener.addListener(new AnnotationValidatorServletContextListener());
compositeListener.addListener(new DevModeServletContextListener());
// initialized
if (!WebComponentConfigurationRegistry.getInstance(context).hasConfigurations()) {
compositeListener.addListener(new WebComponentServletContextListener());
}
return compositeListener;
}
use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.
the class VaadinServletContextInitializerTest method errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewImplementingHasErrorParameter_customRouteNotFoundViewIsRegistered.
@Test
public void errorParameterServletContextListenerEvent_hasCustomRouteNotFoundViewImplementingHasErrorParameter_customRouteNotFoundViewIsRegistered() throws Exception {
// given
initDefaultMocks();
VaadinServletContextInitializer initializer = getStubbedVaadinServletContextInitializer();
Runnable when = initRouteNotFoundMocksAndGetContextInitializedMockCall(initializer);
class TestErrorView extends Component implements HasErrorParameter<NotFoundException> {
@Override
public int setErrorParameter(BeforeEnterEvent event, ErrorParameter<NotFoundException> parameter) {
return 0;
}
}
Mockito.doAnswer(invocation -> Stream.of(TestErrorView.class)).when(initializer).findBySuperType(Mockito.anyCollection(), Mockito.eq(HasErrorParameter.class));
// when
when.run();
// then
ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(new VaadinServletContext(servletContext));
final Class<? extends Component> navigationTarget = registry.getErrorNavigationTarget(new NotFoundException()).get().getNavigationTarget();
Assert.assertEquals(TestErrorView.class, navigationTarget);
}
use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.
the class BootstrapContextTest method getPushAnnotation_routeTargetIsAbsent_pushIsDefinedOnParentLayout_pushFromTheErrorNavigationTargetParentLayoutIsUsed.
@Test
public void getPushAnnotation_routeTargetIsAbsent_pushIsDefinedOnParentLayout_pushFromTheErrorNavigationTargetParentLayoutIsUsed() {
Mockito.when(request.getParameter(ApplicationConstants.REQUEST_LOCATION_PARAMETER)).thenReturn("bar");
ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(ui.getSession().getService().getContext());
registry.setErrorNavigationTargets(Collections.singleton(AnotherCustomRouteNotFound.class));
BootstrapContext context = new BootstrapContext(request, Mockito.mock(VaadinResponse.class), session, ui, request -> "");
Optional<Push> push = context.getPageConfigurationAnnotation(Push.class);
Assert.assertTrue(push.isPresent());
Push pushAnnotation = push.get();
Assert.assertEquals(PushMode.MANUAL, pushAnnotation.value());
Assert.assertEquals(Transport.LONG_POLLING, pushAnnotation.transport());
}
use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.
the class ErrorStateRendererTest method handle_openNPEView_infiniteReroute_noStackOverflow_throws.
@Test(expected = ExceptionsTrace.class)
public void handle_openNPEView_infiniteReroute_noStackOverflow_throws() {
UI ui = configureMocks();
NavigationState state = new NavigationStateBuilder(ui.getInternals().getRouter()).withTarget(InfiniteLoopNPEView.class).build();
NavigationStateRenderer renderer = new NavigationStateRenderer(state);
RouteConfiguration.forRegistry(ui.getInternals().getRouter().getRegistry()).setAnnotatedRoute(InfiniteLoopNPEView.class);
((ApplicationRouteRegistry) ui.getInternals().getRouter().getRegistry()).setErrorNavigationTargets(Collections.singleton(InfiniteLoopErrorTarget.class));
NavigationEvent event = new NavigationEvent(ui.getInternals().getRouter(), new Location("npe"), ui, NavigationTrigger.CLIENT_SIDE);
// event should route to ErrorTarget whose layout forwards to NPEView
// which reroute to ErrorTarget and this is an infinite loop
renderer.handle(event);
JsonObject routerLinkState = Json.createObject();
routerLinkState.put("href", "router_link");
routerLinkState.put("scrollPositionX", 0d);
routerLinkState.put("scrollPositionY", 0d);
event = new NavigationEvent(ui.getInternals().getRouter(), new Location("npe"), ui, NavigationTrigger.ROUTER_LINK, routerLinkState, false);
// event should route to ErrorTarget whose layout forwards to NPEView
// which reroute to ErrorTarget and this is an infinite loop
renderer.handle(event);
}
use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.
the class RouteUtilTest method deletedRouteAnnotatedClass_updateRouteRegistry_routeIsRemovedFromRegistry.
@Test
public void deletedRouteAnnotatedClass_updateRouteRegistry_routeIsRemovedFromRegistry() {
// given
@Route("a")
class A extends Component {
}
MockVaadinServletService service = new MockVaadinServletService();
ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(service.getContext());
registry.setRoute("a", A.class, Collections.emptyList());
Assert.assertTrue(registry.getConfiguration().hasRoute("a"));
// when
RouteUtil.updateRouteRegistry(registry, Collections.emptySet(), Collections.emptySet(), Collections.singleton(A.class));
// then
Assert.assertFalse(registry.getConfiguration().hasRoute("a"));
}
Aggregations