Search in sources :

Example 41 with Location

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

the class RouterConfigurationTest method defaultErrorHandlerStatusCode.

@Test
public void defaultErrorHandlerStatusCode() {
    Router router = new Router();
    int statusCode = router.getConfiguration().getErrorHandler().handle(new NavigationEvent(router, new Location(""), new UI(), NavigationTrigger.PROGRAMMATIC));
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, statusCode);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 42 with Location

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

the class RouterConfigurationTest method assertRoutePriority.

private static void assertRoutePriority(String location, String strongerRoute, String weakerRoute) {
    // First verify that the test makes sense
    assertMatches(location, strongerRoute);
    assertMatches(location, weakerRoute);
    NavigationHandler weakerHandler = createNoopHandler();
    NavigationHandler strongerHandler = createNoopHandler();
    RouterConfiguration configuration = createConfiguration();
    configuration.setRoute(strongerRoute, strongerHandler);
    configuration.setRoute(weakerRoute, weakerHandler);
    Optional<NavigationHandler> handler = configuration.resolveRoute(new Location(location));
    Assert.assertSame(handler.get(), strongerHandler);
    // Do the same again with setRoute run in the opposite order
    configuration = createConfiguration();
    configuration.setRoute(weakerRoute, weakerHandler);
    configuration.setRoute(strongerRoute, strongerHandler);
    handler = configuration.resolveRoute(new Location(location));
    Assert.assertSame(handler.get(), strongerHandler);
}
Also used : RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) NavigationHandler(com.vaadin.flow.router.NavigationHandler) Location(com.vaadin.flow.router.Location)

Example 43 with Location

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

the class RouterTest method testNavigateWithToggledSlash.

@Test
public void testNavigateWithToggledSlash() {
    UI ui = new RouterTestUI();
    Router router = new Router();
    router.reconfigure(c -> {
        c.setRoute("foo/{name}", TestView.class);
        c.setRoute("bar/*", TestView.class);
    });
    router.navigate(ui, new Location("foo/bar/"), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals("foo/bar", ui.getInternals().getActiveViewLocation().getPath());
    router.navigate(ui, new Location("bar"), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals("bar/", ui.getInternals().getActiveViewLocation().getPath());
}
Also used : UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 44 with Location

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

the class RouterTest method testSetRouteIfNoResolverHandler.

@Test
public void testSetRouteIfNoResolverHandler() {
    Router router = new Router();
    AtomicReference<String> usedHandler = new AtomicReference<>();
    router.reconfigure(configuration -> {
        configuration.setResolver(resolveEvent -> Optional.empty());
        configuration.setRoute("*", e -> {
            usedHandler.set("route");
            return HttpServletResponse.SC_OK;
        });
    });
    router.navigate(new RouterTestUI(), new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals("route", usedHandler.get());
}
Also used : Router(com.vaadin.flow.router.legacy.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 45 with Location

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

the class RouterTest method testResolve.

@Test
public void testResolve() {
    UI ui = new RouterTestUI();
    Router router = new Router();
    TestResolver resolver = new TestResolver();
    router.reconfigure(c -> c.setResolver(resolver));
    Assert.assertNull(resolver.resolvedLocation.get());
    Assert.assertNull(resolver.handledEvent.get());
    Location testLocation = new Location("");
    router.navigate(ui, testLocation, NavigationTrigger.PROGRAMMATIC);
    Assert.assertSame(testLocation, resolver.resolvedLocation.get());
    Assert.assertSame(testLocation, resolver.handledEvent.get().getLocation());
    Assert.assertSame(ui, resolver.handledEvent.get().getUI());
}
Also used : UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

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