Search in sources :

Example 16 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class RouterConfigurationTest method testRoutesCopied.

@Test
public void testRoutesCopied() throws Exception {
    RouterConfiguration original = createConfiguration();
    original.setRoute("foo/bar", createNoopHandler());
    RouterConfiguration copy = new RouterConfiguration(original, false);
    original.removeRoute("foo/bar");
    Assert.assertNotNull("Updating the original should not affect the copy", copy.resolveRoute(new Location("foo/bar")));
}
Also used : RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 17 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class RouterTest method testChangeLocation.

@Test
public void testChangeLocation() {
    UI ui = new RouterTestUI();
    Router router = new Router();
    TestResolver resolver = new TestResolver();
    router.reconfigure(c -> c.setResolver(resolver));
    VaadinRequest request = requestWithPathInfo(null);
    router.initializeUI(ui, request);
    Assert.assertEquals(Arrays.asList(""), resolver.resolvedLocation.get().getSegments());
    resolver.resolvedLocation.set(null);
    resolver.handledEvent.set(null);
    ui.getPage().getHistory().getHistoryStateChangeHandler().onHistoryStateChange(new HistoryStateChangeEvent(ui.getPage().getHistory(), null, new Location("foo"), NavigationTrigger.HISTORY));
    Assert.assertEquals(Arrays.asList("foo"), resolver.resolvedLocation.get().getSegments());
}
Also used : HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 18 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class RouterTest method testResolverError_noCurrentResponse.

@Test
public void testResolverError_noCurrentResponse() {
    UI ui = new RouterTestUI();
    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"));
}
Also used : Arrays(java.util.Arrays) ServletException(javax.servlet.ServletException) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) ErrorView(com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentCaptor(org.mockito.ArgumentCaptor) After(org.junit.After) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) TestView(com.vaadin.flow.router.legacy.ViewRendererTest.TestView) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) ServletConfig(javax.servlet.ServletConfig) NavigationHandler(com.vaadin.flow.router.NavigationHandler) VaadinResponse(com.vaadin.flow.server.VaadinResponse) VaadinServlet(com.vaadin.flow.server.VaadinServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) RouterInterface(com.vaadin.flow.router.RouterInterface) Resolver(com.vaadin.flow.router.legacy.Resolver) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) NotThreadSafe(net.jcip.annotations.NotThreadSafe) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) HistoryStateChangeEvent(com.vaadin.flow.component.page.History.HistoryStateChangeEvent) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinService(com.vaadin.flow.server.VaadinService) Optional(java.util.Optional) Assert(org.junit.Assert) NavigationEvent(com.vaadin.flow.router.NavigationEvent) RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Router(com.vaadin.flow.router.legacy.Router) UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 19 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class RouterTest method testNavigateToEmptyLocation_triggersDefaultErrorView.

@Test
public void testNavigateToEmptyLocation_triggersDefaultErrorView() {
    UI ui = new RouterTestUI();
    Router router = new Router();
    router.reconfigure(c -> {
    });
    router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals(new DefaultErrorView().getText(), ui.getElement().getTextRecursively());
}
Also used : DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 20 with Location

use of com.vaadin.flow.router.Location in project flow by vaadin.

the class ViewRenderer method extractRoutePlaceholders.

private static Map<String, String> extractRoutePlaceholders(Location destination, RouteLocation routeDefinition) {
    assert destination != null;
    assert routeDefinition != null;
    Map<String, String> routePlaceholders = new HashMap<>();
    routeDefinition.visitSegments(new RouteSegmentVisitor() {

        Optional<Location> maybeCurrentDestination = Optional.of(destination);

        private String getNextDestinationSegment() {
            Location currentDestination = maybeCurrentDestination.get();
            /*
                 * This might return an empty optional, but in that case there
                 * shouldn't be any more calls to any accept method, as long as
                 * the current destination actually matches the route
                 * definition.
                 */
            maybeCurrentDestination = currentDestination.getSubLocation();
            return currentDestination.getFirstSegment();
        }

        @Override
        public void acceptWildcard() {
            // This is always the last step, so not need to advance the
            // destination
            routePlaceholders.put("*", maybeCurrentDestination.get().getPath());
        }

        @Override
        public void acceptSegment(String segmentName) {
            // Just advance the current destination
            getNextDestinationSegment();
        }

        @Override
        public void acceptPlaceholder(String placeholderName) {
            String placeholderValue = getNextDestinationSegment();
            assert !routePlaceholders.containsKey(placeholderName);
            routePlaceholders.put(placeholderName, placeholderValue);
        }
    });
    return routePlaceholders;
}
Also used : HashMap(java.util.HashMap) RouteSegmentVisitor(com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor) Location(com.vaadin.flow.router.Location)

Aggregations

Location (com.vaadin.flow.router.Location)51 Test (org.junit.Test)40 UI (com.vaadin.flow.component.UI)15 Router (com.vaadin.flow.router.legacy.Router)15 NavigationEvent (com.vaadin.flow.router.NavigationEvent)10 NavigationHandler (com.vaadin.flow.router.NavigationHandler)8 RouterConfiguration (com.vaadin.flow.router.legacy.RouterConfiguration)7 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)6 TestView (com.vaadin.flow.router.legacy.ViewRendererTest.TestView)6 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)5 QueryParameters (com.vaadin.flow.router.QueryParameters)5 DefaultErrorView (com.vaadin.flow.router.legacy.DefaultErrorView)5 VaadinRequest (com.vaadin.flow.server.VaadinRequest)5 VaadinResponse (com.vaadin.flow.server.VaadinResponse)5 Optional (java.util.Optional)4 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 RouterInterface (com.vaadin.flow.router.RouterInterface)3 ImmutableRouterConfiguration (com.vaadin.flow.router.legacy.ImmutableRouterConfiguration)3 Resolver (com.vaadin.flow.router.legacy.Resolver)3 RouteSegmentVisitor (com.vaadin.flow.router.legacy.RouteLocation.RouteSegmentVisitor)3