Search in sources :

Example 1 with Resolver

use of com.vaadin.flow.router.legacy.Resolver in project flow by vaadin.

the class RouterTest method testReconfigureThreadSafety.

@Test
public void testReconfigureThreadSafety() throws InterruptedException {
    Router router = new Router();
    Resolver newResolver = e -> null;
    CountDownLatch configUpdated = new CountDownLatch(1);
    CountDownLatch configVerified = new CountDownLatch(1);
    Thread updaterThread = new Thread() {

        @Override
        public void run() {
            router.reconfigure(config -> {
                config.setResolver(newResolver);
                // Signal that config has been updated
                configUpdated.countDown();
                // configuration is not yet in effect
                try {
                    configVerified.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    };
    updaterThread.start();
    // Wait until updater thread has updated the config
    configUpdated.await();
    Assert.assertNotSame("Update should not yet be visible", newResolver, router.getConfiguration().getResolver());
    // Allow the update thread to exit the configure method
    configVerified.countDown();
    // Wait for updater thread to finish
    updaterThread.join();
    Assert.assertSame("Update should now be visible", newResolver, router.getConfiguration().getResolver());
}
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) Resolver(com.vaadin.flow.router.legacy.Resolver) Router(com.vaadin.flow.router.legacy.Router) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 2 with Resolver

use of com.vaadin.flow.router.legacy.Resolver in project flow by vaadin.

the class RouterTest method testLeakedConfigurationImmutable.

@Test
public void testLeakedConfigurationImmutable() {
    Router router = new Router();
    AtomicReference<RouterConfiguration> configurationLeak = new AtomicReference<>();
    router.reconfigure(configurationLeak::set);
    Resolver newResolver = e -> null;
    configurationLeak.get().setResolver(newResolver);
    Assert.assertNotSame(newResolver, router.getConfiguration().getResolver());
}
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) Resolver(com.vaadin.flow.router.legacy.Resolver) Router(com.vaadin.flow.router.legacy.Router) AtomicReference(java.util.concurrent.atomic.AtomicReference) ImmutableRouterConfiguration(com.vaadin.flow.router.legacy.ImmutableRouterConfiguration) RouterConfiguration(com.vaadin.flow.router.legacy.RouterConfiguration) Test(org.junit.Test)

Example 3 with Resolver

use of com.vaadin.flow.router.legacy.Resolver 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)

Aggregations

UI (com.vaadin.flow.component.UI)3 HistoryStateChangeEvent (com.vaadin.flow.component.page.History.HistoryStateChangeEvent)3 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)3 Location (com.vaadin.flow.router.Location)3 NavigationEvent (com.vaadin.flow.router.NavigationEvent)3 NavigationHandler (com.vaadin.flow.router.NavigationHandler)3 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)3 RouterInterface (com.vaadin.flow.router.RouterInterface)3 DefaultErrorView (com.vaadin.flow.router.legacy.DefaultErrorView)3 ImmutableRouterConfiguration (com.vaadin.flow.router.legacy.ImmutableRouterConfiguration)3 Resolver (com.vaadin.flow.router.legacy.Resolver)3 Router (com.vaadin.flow.router.legacy.Router)3 RouterConfiguration (com.vaadin.flow.router.legacy.RouterConfiguration)3 ErrorView (com.vaadin.flow.router.legacy.ViewRendererTest.ErrorView)3 TestView (com.vaadin.flow.router.legacy.ViewRendererTest.TestView)3 MockServletConfig (com.vaadin.flow.server.MockServletConfig)3 VaadinRequest (com.vaadin.flow.server.VaadinRequest)3 VaadinResponse (com.vaadin.flow.server.VaadinResponse)3 VaadinService (com.vaadin.flow.server.VaadinService)3 VaadinServlet (com.vaadin.flow.server.VaadinServlet)3