Search in sources :

Example 46 with Location

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

the class RouterTest method testResolverBeforeSetRoute.

@Test
public void testResolverBeforeSetRoute() {
    Router router = new Router();
    AtomicReference<String> usedHandler = new AtomicReference<>();
    router.reconfigure(configuration -> {
        configuration.setResolver(resolveEvent -> Optional.of(handlerEvent -> {
            usedHandler.set("resolver");
            return HttpServletResponse.SC_OK;
        }));
        configuration.setRoute("*", e -> {
            usedHandler.set("route");
            return HttpServletResponse.SC_OK;
        });
    });
    router.navigate(new RouterTestUI(), new Location(""), NavigationTrigger.PROGRAMMATIC);
    Assert.assertEquals("resolver", usedHandler.get());
}
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) Router(com.vaadin.flow.router.legacy.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 47 with Location

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

the class RouterTest method testNavigateToEmptyLocation_triggersErrorView.

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

Example 48 with Location

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

the class RouterTest method testResolveError.

@Test
public void testResolveError() throws ServletException {
    UI ui = new RouterTestUI();
    VaadinRequest request = Mockito.mock(VaadinRequest.class);
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    ServletConfig servletConfig = new MockServletConfig();
    VaadinServlet servlet = new VaadinServlet();
    servlet.init(servletConfig);
    VaadinService service = servlet.getService();
    service.setCurrentInstances(request, response);
    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"));
    // 404 code should be sent ONLY on initial request
    Mockito.verifyZeroInteractions(response);
    // to verify that the setup has been correct and the mocks work,
    // test the case where 404 should be sent
    router.initializeUI(ui, request);
    ArgumentCaptor<Integer> statusCodeCaptor = ArgumentCaptor.forClass(Integer.class);
    Mockito.verify(response).setStatus(statusCodeCaptor.capture());
    Assert.assertEquals(Integer.valueOf(HttpServletResponse.SC_NOT_FOUND), statusCodeCaptor.getValue());
}
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) ServletConfig(javax.servlet.ServletConfig) MockServletConfig(com.vaadin.flow.server.MockServletConfig) VaadinServlet(com.vaadin.flow.server.VaadinServlet) MockServletConfig(com.vaadin.flow.server.MockServletConfig) Router(com.vaadin.flow.router.legacy.Router) VaadinResponse(com.vaadin.flow.server.VaadinResponse) UI(com.vaadin.flow.component.UI) VaadinService(com.vaadin.flow.server.VaadinService) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 49 with Location

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

the class RouterTest method testStatusCodeUpdates.

@Test
public void testStatusCodeUpdates() {
    RouterTestUI ui = new RouterTestUI();
    Router router = (Router) ui.getRouterInterface().get();
    router.reconfigure(c -> {
        c.setRoute("*", e -> 123);
    });
    VaadinResponse response = Mockito.mock(VaadinResponse.class);
    try {
        CurrentInstance.set(VaadinResponse.class, response);
        VaadinRequest request = requestWithPathInfo(null);
        router.initializeUI(ui, request);
        // Response status should be set when initializing
        Mockito.verify(response).setStatus(123);
        router.navigate(ui, new Location("foo"), NavigationTrigger.PROGRAMMATIC);
        // Non-init navigation shouldn't set any status code
        Mockito.verifyNoMoreInteractions(response);
    } finally {
        CurrentInstance.clearAll();
    }
}
Also used : VaadinResponse(com.vaadin.flow.server.VaadinResponse) Router(com.vaadin.flow.router.legacy.Router) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 50 with Location

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

the class ViewRendererTest method routeParametersInEvent.

@Test
public void routeParametersInEvent() {
    router.reconfigure(c -> c.setRoute("foo/{name}/*", TestView.class));
    router.navigate(ui, new Location("foo/bar/baz/"), NavigationTrigger.PROGRAMMATIC);
    TestView testView = (TestView) ui.getInternals().getActiveViewChain().get(0);
    Assert.assertEquals("bar", testView.namePlaceholderValue);
    Assert.assertEquals("baz/", testView.wildcardValue);
}
Also used : 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