Search in sources :

Example 1 with ApplicationRouteRegistry

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;
}
Also used : ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry)

Example 2 with ApplicationRouteRegistry

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);
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) NotFoundException(com.vaadin.flow.router.NotFoundException) ErrorParameter(com.vaadin.flow.router.ErrorParameter) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) BeforeEnterEvent(com.vaadin.flow.router.BeforeEnterEvent) Component(com.vaadin.flow.component.Component) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Test(org.junit.Test)

Example 3 with ApplicationRouteRegistry

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());
}
Also used : BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) JavaScriptBootstrapContext(com.vaadin.flow.server.communication.JavaScriptBootstrapHandler.JavaScriptBootstrapContext) Push(com.vaadin.flow.component.page.Push) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Test(org.junit.Test)

Example 4 with ApplicationRouteRegistry

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);
}
Also used : NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) ErrorNavigationEvent(com.vaadin.flow.router.ErrorNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockUI(com.vaadin.tests.util.MockUI) UI(com.vaadin.flow.component.UI) NavigationState(com.vaadin.flow.router.NavigationState) JsonObject(elemental.json.JsonObject) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 5 with ApplicationRouteRegistry

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"));
}
Also used : MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) Component(com.vaadin.flow.component.Component) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) Route(com.vaadin.flow.router.Route) Test(org.junit.Test)

Aggregations

ApplicationRouteRegistry (com.vaadin.flow.server.startup.ApplicationRouteRegistry)11 Test (org.junit.Test)9 Component (com.vaadin.flow.component.Component)6 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)4 NotFoundException (com.vaadin.flow.router.NotFoundException)3 Route (com.vaadin.flow.router.Route)3 Push (com.vaadin.flow.component.page.Push)2 HasErrorParameter (com.vaadin.flow.router.HasErrorParameter)2 BootstrapContext (com.vaadin.flow.server.BootstrapHandler.BootstrapContext)2 MockVaadinContext (com.vaadin.flow.server.MockVaadinContext)2 VaadinServletContext (com.vaadin.flow.server.VaadinServletContext)2 JavaScriptBootstrapContext (com.vaadin.flow.server.communication.JavaScriptBootstrapHandler.JavaScriptBootstrapContext)2 UI (com.vaadin.flow.component.UI)1 BeforeEnterEvent (com.vaadin.flow.router.BeforeEnterEvent)1 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)1 ErrorParameter (com.vaadin.flow.router.ErrorParameter)1 Location (com.vaadin.flow.router.Location)1 NavigationEvent (com.vaadin.flow.router.NavigationEvent)1 NavigationState (com.vaadin.flow.router.NavigationState)1 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)1