Search in sources :

Example 6 with ApplicationRouteRegistry

use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.

the class RouteUtilTest method deannotatedRouteClass_updateRouteRegistry_routeIsRemovedFromRegistry.

@Test
public void deannotatedRouteClass_updateRouteRegistry_routeIsRemovedFromRegistry() {
    // given
    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.singleton(A.class), Collections.emptySet());
    // 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) Test(org.junit.Test)

Example 7 with ApplicationRouteRegistry

use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.

the class RouteUtilTest method newRouteAnnotatedClass_updateRouteRegistry_routeIsAddedToRegistry.

@Test
public void newRouteAnnotatedClass_updateRouteRegistry_routeIsAddedToRegistry() {
    // given
    @Route("a")
    class A extends Component {
    }
    MockVaadinServletService service = new MockVaadinServletService() {

        @Override
        public VaadinContext getContext() {
            return new MockVaadinContext();
        }
    };
    ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(service.getContext());
    // when
    RouteUtil.updateRouteRegistry(registry, Collections.singleton(A.class), Collections.emptySet(), Collections.emptySet());
    // then
    Assert.assertTrue(registry.getConfiguration().hasRoute("a"));
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) 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)

Example 8 with ApplicationRouteRegistry

use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.

the class CustomRouteRegistry method getInstance.

public static CustomRouteRegistry getInstance(VaadinContext context) {
    assert context != null;
    RegistryWrapper attribute;
    synchronized (context) {
        attribute = context.getAttribute(RegistryWrapper.class);
        if (attribute == null) {
            final CustomRouteRegistry registry = new CustomRouteRegistry(context);
            attribute = new RegistryWrapper(registry);
            context.setAttribute(attribute);
            // Get collected application routes
            final ApplicationRouteRegistry instance = ApplicationRouteRegistry.getInstance(context);
            instance.getRegisteredRoutes().forEach(routeData -> {
                registry.setRoute(routeData.getTemplate(), routeData.getNavigationTarget(), routeData.getParentLayouts());
            });
            // Add only NotFoundException and Exception ignoring other error
            // views collected
            Map<Class<? extends Exception>, Class<? extends Component>> map = new HashMap<>();
            map.put(NotFoundException.class, CustomNotFoundView.class);
            registry.configure(configuration -> map.forEach(configuration::setErrorRoute));
        }
    }
    return attribute.getRegistry();
}
Also used : HashMap(java.util.HashMap) Component(com.vaadin.flow.component.Component) ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) NotFoundException(com.vaadin.flow.router.NotFoundException)

Example 9 with ApplicationRouteRegistry

use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.

the class RouteUtilTest method renamedRouteAnnotatedClass_updateRouteRegistry_routeIsUpdatedInRegistry.

@Test
public void renamedRouteAnnotatedClass_updateRouteRegistry_routeIsUpdatedInRegistry() {
    // given
    @Route("aa")
    class A extends Component {
    }
    MockVaadinServletService service = new MockVaadinServletService() {

        @Override
        public VaadinContext getContext() {
            return new MockVaadinContext();
        }
    };
    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.singleton(A.class), Collections.emptySet());
    // then
    Assert.assertFalse(registry.getConfiguration().hasRoute("a"));
    Assert.assertTrue(registry.getConfiguration().hasRoute("aa"));
}
Also used : MockVaadinContext(com.vaadin.flow.server.MockVaadinContext) 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)

Example 10 with ApplicationRouteRegistry

use of com.vaadin.flow.server.startup.ApplicationRouteRegistry in project flow by vaadin.

the class BootstrapContextTest method getPushAnnotation_routeTargetIsAbsent_pushFromTheErrorNavigationTargetIsUsed.

@Test
public void getPushAnnotation_routeTargetIsAbsent_pushFromTheErrorNavigationTargetIsUsed() {
    Mockito.when(request.getParameter(ApplicationConstants.REQUEST_LOCATION_PARAMETER)).thenReturn("bar");
    ApplicationRouteRegistry registry = ApplicationRouteRegistry.getInstance(ui.getSession().getService().getContext());
    registry.setErrorNavigationTargets(Collections.singleton(CustomRouteNotFound.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.AUTOMATIC, pushAnnotation.value());
    Assert.assertEquals(Transport.WEBSOCKET, 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)

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